tenant_contract_documents records a workspace's own filled contract document (for now the AVV): master version, PDF SHA-256, bucket key, digest of the filled-in values and the staff member's Keycloak sub. Rows are insert-only (an UPDATE trigger refuses changes); the newest per tenant and kind is current. Memory and Postgres stores, purge with the tenant. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
func (m *Memory) CreateContractDocument(_ context.Context, d ContractDocument) (*ContractDocument, error) {
|
|
if !validContractDocument(d) {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
if _, ok := m.tenants[d.TenantID]; !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
m.contractDocID++
|
|
d.ID = m.contractDocID
|
|
if d.CreatedAt.IsZero() {
|
|
d.CreatedAt = time.Now()
|
|
}
|
|
d.CreatedAt = d.CreatedAt.UTC()
|
|
m.contractDocs = append(m.contractDocs, d)
|
|
cp := d
|
|
return &cp, nil
|
|
}
|
|
|
|
func (m *Memory) CurrentContractDocument(_ context.Context, tenantID, kind string) (*ContractDocument, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
for i := len(m.contractDocs) - 1; i >= 0; i-- {
|
|
if d := m.contractDocs[i]; d.TenantID == tenantID && d.Kind == kind {
|
|
return &d, nil
|
|
}
|
|
}
|
|
return nil, ErrNotFound
|
|
}
|
|
|
|
func (m *Memory) ListContractDocuments(_ context.Context, tenantID string) ([]ContractDocument, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
out := []ContractDocument{}
|
|
for _, d := range m.contractDocs {
|
|
if d.TenantID == tenantID {
|
|
out = append(out, d)
|
|
}
|
|
}
|
|
return out, nil
|
|
}
|