Files
tenant-registry/internal/store/postgres_company_profile.go
T
sharangandClaude Opus 5.5 174a2f1e01
ci / shared (push) Successful in 14s
ci / test (push) Successful in 25m47s
ci / image (push) Successful in 37s
feat(company-profile): Unternehmensprofil per tenant, registry as the authority (#85)
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>
2026-10-20 10:30:09 +00:00

154 lines
4.9 KiB
Go

package store
import (
"context"
"encoding/json"
"errors"
"time"
"github.com/jackc/pgx/v5"
)
const profileSelect = `
SELECT tenant_id::text, data, schema_version, revision, status, updated_by, updated_at, submitted_by, submitted_at
FROM company_profiles `
func scanProfile(row pgx.Row) (*CompanyProfile, error) {
var p CompanyProfile
var data []byte
if err := row.Scan(&p.TenantID, &data, &p.SchemaVersion, &p.Revision, &p.Status, &p.UpdatedBy, &p.UpdatedAt, &p.SubmittedBy, &p.SubmittedAt); err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrNotFound
}
return nil, err
}
p.Data = json.RawMessage(data)
return &p, nil
}
func (p *Postgres) GetCompanyProfile(ctx context.Context, tenantID string) (*CompanyProfile, error) {
pr, err := scanProfile(p.pool.QueryRow(ctx, profileSelect+`WHERE tenant_id = $1::uuid`, tenantID))
if isInvalidUUID(err) {
return nil, ErrNotFound
}
return pr, err
}
// currentProfile locks the tenant's profile row for the transaction; nil
// when there is none yet.
func currentProfile(ctx context.Context, tx pgx.Tx, tenantID string) (*CompanyProfile, error) {
cur, err := scanProfile(tx.QueryRow(ctx, profileSelect+`WHERE tenant_id = $1::uuid FOR UPDATE`, tenantID))
switch {
case errors.Is(err, ErrNotFound):
return nil, nil
case isInvalidUUID(err):
return nil, ErrNotFound
}
return cur, err
}
func (p *Postgres) SaveCompanyProfile(ctx context.Context, w CompanyProfileWrite) (*CompanyProfile, bool, error) {
if !json.Valid(w.Data) || w.SchemaVersion == "" {
return nil, false, ErrInvalidInput
}
tx, err := p.pool.Begin(ctx)
if err != nil {
return nil, false, err
}
defer func() { _ = tx.Rollback(ctx) }()
cur, err := currentProfile(ctx, tx, w.TenantID)
if err != nil {
return nil, false, err
}
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) {
return cur, false, nil
}
next := current + 1
if cur == nil {
_, err = tx.Exec(ctx, `
INSERT INTO company_profiles (tenant_id, data, schema_version, revision, updated_by, updated_at)
VALUES ($1::uuid, $2::jsonb, $3, $4, $5, $6)`,
w.TenantID, string(w.Data), w.SchemaVersion, next, w.By, w.At.UTC())
} else {
_, err = tx.Exec(ctx, `
UPDATE company_profiles SET data = $2::jsonb, schema_version = $3, revision = $4, updated_by = $5, updated_at = $6
WHERE tenant_id = $1::uuid`,
w.TenantID, string(w.Data), w.SchemaVersion, next, w.By, w.At.UTC())
}
switch {
case err == nil:
case isFKViolation(err):
return nil, false, ErrNotFound
case isUniqueViolation(err):
// A concurrent first save won: report its revision.
return nil, false, &RevisionConflict{Current: 1}
case isCheckViolation(err):
return nil, false, ErrInvalidInput
default:
return nil, false, err
}
if err := insertProfileRevision(ctx, tx, w.TenantID, next, w.Source, w.By, w.At); err != nil {
return nil, false, err
}
if err := tx.Commit(ctx); err != nil {
return nil, false, err
}
saved, err := p.GetCompanyProfile(ctx, w.TenantID)
return saved, true, err
}
func (p *Postgres) SubmitCompanyProfile(ctx context.Context, tenantID string, expectedRevision int, by string, at time.Time) (*CompanyProfile, error) {
tx, err := p.pool.Begin(ctx)
if err != nil {
return nil, err
}
defer func() { _ = tx.Rollback(ctx) }()
cur, err := currentProfile(ctx, tx, tenantID)
if err != nil {
return nil, err
}
if cur == nil {
var exists bool
if err := tx.QueryRow(ctx, `SELECT EXISTS (SELECT 1 FROM tenants WHERE id = $1::uuid)`, tenantID).Scan(&exists); err != nil {
return nil, err
}
if !exists {
return nil, ErrNotFound
}
return nil, &RevisionConflict{Current: 0}
}
if expectedRevision != cur.Revision {
return nil, &RevisionConflict{Current: cur.Revision}
}
next := cur.Revision + 1
if _, err := tx.Exec(ctx, `
UPDATE company_profiles SET revision = $2, status = 'submitted', updated_by = $3, updated_at = $4, submitted_by = $3, submitted_at = $4
WHERE tenant_id = $1::uuid`, tenantID, next, by, at.UTC()); err != nil {
return nil, err
}
if err := insertProfileRevision(ctx, tx, tenantID, next, ProfileSourceSubmit, by, at); err != nil {
return nil, err
}
if err := tx.Commit(ctx); err != nil {
return nil, err
}
return p.GetCompanyProfile(ctx, tenantID)
}
// insertProfileRevision copies the row just written into the history.
func insertProfileRevision(ctx context.Context, tx pgx.Tx, tenantID string, revision int, source, by string, at time.Time) error {
_, err := tx.Exec(ctx, `
INSERT INTO company_profile_revisions (tenant_id, revision, data, schema_version, status, source, changed_by, changed_at)
SELECT tenant_id, revision, data, schema_version, status, $3, $4, $5
FROM company_profiles WHERE tenant_id = $1::uuid AND revision = $2`,
tenantID, revision, source, by, at.UTC())
return err
}