Migration 0021 (company_profiles + company_profile_revisions), GET/PUT /v1/tenants/{id}/company-profile, POST …/submit with optimistic revisions (409 + current revision; unchanged saves are no new revision), staff import via /v1/admin/tenants/{id}/company-profile (named staff member only), outline validation of the form document, audit without answers, OpenAPI.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
86 lines
2.3 KiB
Go
86 lines
2.3 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"time"
|
|
)
|
|
|
|
func (m *Memory) GetCompanyProfile(_ context.Context, tenantID string) (*CompanyProfile, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
p, ok := m.profiles[tenantID]
|
|
if !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
cp := *p
|
|
return &cp, nil
|
|
}
|
|
|
|
func (m *Memory) SaveCompanyProfile(_ context.Context, w CompanyProfileWrite) (*CompanyProfile, bool, error) {
|
|
if !json.Valid(w.Data) || w.SchemaVersion == "" {
|
|
return nil, false, ErrInvalidInput
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if _, ok := m.tenants[w.TenantID]; !ok {
|
|
return nil, false, ErrNotFound
|
|
}
|
|
cur := m.profiles[w.TenantID]
|
|
current := 0
|
|
if cur != nil {
|
|
current = cur.Revision
|
|
}
|
|
if w.ExpectedRevision != current {
|
|
return nil, false, &RevisionConflict{Current: current}
|
|
}
|
|
if cur != nil && cur.SchemaVersion == w.SchemaVersion && sameJSON(cur.Data, w.Data) {
|
|
cp := *cur
|
|
return &cp, false, nil
|
|
}
|
|
next := CompanyProfile{TenantID: w.TenantID, Status: ProfileDraft}
|
|
if cur != nil {
|
|
next = *cur
|
|
}
|
|
next.Data = append(json.RawMessage(nil), w.Data...)
|
|
next.SchemaVersion = w.SchemaVersion
|
|
next.Revision = current + 1
|
|
next.UpdatedBy, next.UpdatedAt = w.By, w.At.UTC()
|
|
m.profiles[w.TenantID] = &next
|
|
m.profRevs[w.TenantID] = append(m.profRevs[w.TenantID], next)
|
|
cp := next
|
|
return &cp, true, nil
|
|
}
|
|
|
|
func (m *Memory) SubmitCompanyProfile(_ context.Context, tenantID string, expectedRevision int, by string, at time.Time) (*CompanyProfile, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
cur, ok := m.profiles[tenantID]
|
|
if !ok {
|
|
if _, known := m.tenants[tenantID]; !known {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, &RevisionConflict{Current: 0}
|
|
}
|
|
if expectedRevision != cur.Revision {
|
|
return nil, &RevisionConflict{Current: cur.Revision}
|
|
}
|
|
next := *cur
|
|
t := at.UTC()
|
|
next.Revision++
|
|
next.Status = ProfileSubmitted
|
|
next.UpdatedBy, next.UpdatedAt = by, t
|
|
next.SubmittedBy, next.SubmittedAt = by, &t
|
|
m.profiles[tenantID] = &next
|
|
m.profRevs[tenantID] = append(m.profRevs[tenantID], next)
|
|
cp := next
|
|
return &cp, nil
|
|
}
|
|
|
|
// CompanyProfileRevisions returns every stored revision, oldest first (tests).
|
|
func (m *Memory) CompanyProfileRevisions(tenantID string) []CompanyProfile {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
return append([]CompanyProfile(nil), m.profRevs[tenantID]...)
|
|
}
|