Files
tenant-registry/internal/store/memory_documents.go
T
Sharang ParnerkarandClaude Opus 5.5 94da0e0013
ci / shared (pull_request) Successful in 12s
ci / test (pull_request) Successful in 25m54s
ci / image (pull_request) Skipped
feat(store): Dokumentenablage with areas, checked serving, import
Workspace ticket #171 (decided 2026-10-20): the tenant's own documents
live next to BreakPilot's deliveries ("Unterlagen") in the same table and
bucket, in three areas, with the original and the prepared version the
Factory delivers.

- migration 0022: documents get area (legal_web / product_public /
  internal; NULL = Unterlagen), legal_kind (the 8 folders as the workspace
  and Factory name them), processing_status (none/pending/ready/failed),
  external_id (the Factory's tenant_document_id, unique per tenant);
  document_preparations (chunk file and/or cookie-banner capture with
  digest, format, revision, pin, image digests); store_assets (images per
  tenant, content-addressed)
- GET /v1/tenants/{id}/store; POST …/store/documents (customer upload:
  legal folder, product of an internal document; legal and product
  documents wait for the Factory, an internal one only on request);
  GET …/original, …/prepared, …/consent-banner and …/store/assets/{sha}:
  every byte is hashed and compared before anything is sent (fail-closed,
  502 digest_mismatch), HTML originals sandboxed
- POST /v1/admin/tenants/{id}/store/import (named staff only): a .tar.gz in
  the Factory's run layout; unsafe paths and links refused, every file
  checked against its digest, keyed on tenant_document_id so it can run
  again; per-document outcome, never content
- Unterlagen lists stay as they were (area NULL only); deleting a store
  document removes its prepared files; storage gains Get
- OpenAPI for the seven routes; tests on memory and Postgres with
  synthetic data only

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 15:11:52 +02:00

145 lines
3.5 KiB
Go

package store
import (
"context"
"sort"
"time"
)
func (m *Memory) CreateDocument(_ context.Context, d Document) (*Document, error) {
m.mu.Lock()
defer m.mu.Unlock()
if d.ID == "" || d.ObjectKey == "" || d.Title == "" {
return nil, ErrInvalidInput
}
if _, ok := m.tenants[d.TenantID]; !ok {
return nil, ErrNotFound
}
if !validStoreFields(d) {
return nil, ErrInvalidInput
}
for _, x := range m.documents {
if x.ID == d.ID || x.ObjectKey == d.ObjectKey ||
(d.ExternalID != "" && x.TenantID == d.TenantID && x.ExternalID == d.ExternalID) {
return nil, ErrConflict
}
}
now := time.Now().UTC()
if d.Status == "" {
d.Status = DocumentDraft
}
if d.ProcessingStatus == "" {
d.ProcessingStatus = ProcessingNone
}
ids, ok := m.tenantProjectIDs(d.TenantID, d.ProjectIDs)
if !ok {
return nil, ErrInvalidInput
}
d.ProjectIDs = ids
d.CreatedAt, d.UpdatedAt = now, now
row := d
m.documents[d.ID] = &row
return row.copy(), nil
}
// copy returns the document with its own ProjectIDs slice.
func (d *Document) copy() *Document {
out := *d
out.ProjectIDs = append([]string{}, d.ProjectIDs...)
return &out
}
// tenantProjectIDs deduplicates and sorts ids and reports whether every one
// is a project of the tenant. Caller holds the lock.
func (m *Memory) tenantProjectIDs(tenantID string, ids []string) ([]string, bool) {
out := dedupe(ids)
for _, id := range out {
p, ok := m.projects[id]
if !ok || p.TenantID != tenantID {
return nil, false
}
}
sort.Strings(out)
return out, true
}
func (m *Memory) SetDocumentProjects(_ context.Context, docID string, projectIDs []string) (*Document, error) {
m.mu.Lock()
defer m.mu.Unlock()
d, ok := m.documents[docID]
if !ok {
return nil, ErrNotFound
}
ids, ok := m.tenantProjectIDs(d.TenantID, projectIDs)
if !ok {
return nil, ErrInvalidInput
}
d.ProjectIDs, d.UpdatedAt = ids, time.Now().UTC()
return d.copy(), nil
}
func (m *Memory) SetDocumentOwn(_ context.Context, docID string, own bool) (*Document, error) {
m.mu.Lock()
defer m.mu.Unlock()
d, ok := m.documents[docID]
if !ok {
return nil, ErrNotFound
}
d.OwnDocument, d.UpdatedAt = own, time.Now().UTC()
return d.copy(), nil
}
func (m *Memory) GetDocument(_ context.Context, id string) (*Document, error) {
m.mu.RLock()
defer m.mu.RUnlock()
d, ok := m.documents[id]
if !ok {
return nil, ErrNotFound
}
return d.copy(), nil
}
func (m *Memory) ListDocuments(_ context.Context, tenantID string, publishedOnly bool) ([]Document, error) {
m.mu.RLock()
defer m.mu.RUnlock()
out := []Document{}
for _, d := range m.documents {
if d.TenantID != tenantID || d.Area != "" || (publishedOnly && d.Status != DocumentPublished) {
continue
}
out = append(out, *d.copy())
}
sort.Slice(out, func(i, j int) bool {
if out[i].CreatedAt.Equal(out[j].CreatedAt) {
return out[i].ID > out[j].ID
}
return out[i].CreatedAt.After(out[j].CreatedAt)
})
return out, nil
}
func (m *Memory) PublishDocument(_ context.Context, id, by string, at time.Time) (*Document, error) {
m.mu.Lock()
defer m.mu.Unlock()
d, ok := m.documents[id]
if !ok {
return nil, ErrNotFound
}
if d.Status != DocumentPublished {
at = at.UTC()
d.Status, d.PublishedBy, d.PublishedAt, d.UpdatedAt = DocumentPublished, by, &at, at
}
return d.copy(), nil
}
func (m *Memory) DeleteDocument(_ context.Context, id string) error {
m.mu.Lock()
defer m.mu.Unlock()
if _, ok := m.documents[id]; !ok {
return ErrNotFound
}
delete(m.documents, id)
delete(m.preparations, id)
return nil
}