Files
tenant-registry/internal/store/document_store.go
T
Sharang ParnerkarandClaude Opus 5.5 251cddf3d3
ci / shared (pull_request) Failing after 4s
ci / test (pull_request) Successful in 25m54s
ci / image (pull_request) Skipped
feat(store): Dokumentenablage — three areas, digest-checked serving, staff bundle 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:07:57 +02:00

89 lines
3.2 KiB
Go

package store
import "time"
// Dokumentenablage (0022, workspace ticket #171): a tenant's own documents in
// three areas, each with its original and, once the Factory has delivered it,
// a prepared version (LANE_B_UNIFIED_V5 chunk file, images, cookie-banner
// capture). Everything is addressed by SHA-256 and checked when it is served.
const (
AreaLegalWeb = "legal_web" // Rechtliche Website-Dokumente
AreaProductPublic = "product_public" // Produktdokumente (öffentlich)
AreaInternal = "internal" // Interne Dokumente
ProcessingNone = "none"
ProcessingPending = "pending"
ProcessingReady = "ready"
ProcessingFailed = "failed"
)
// LegalKinds are the folders of the legal website documents, in display
// order, with the codes the workspace and the Factory use.
var LegalKinds = []string{"impressum", "dse", "cookie", "agb", "widerruf", "barrierefreiheit", "entsorgung", "vsbg"}
// ValidArea reports whether area is one of the three store areas.
func ValidArea(area string) bool {
return area == AreaLegalWeb || area == AreaProductPublic || area == AreaInternal
}
// ValidLegalKind reports whether kind is one of the legal folders.
func ValidLegalKind(kind string) bool {
for _, k := range LegalKinds {
if k == kind {
return true
}
}
return false
}
// validStoreFields: a legal kind exactly for legal documents, a known area,
// a known status.
func validStoreFields(d Document) bool {
if d.Area == "" {
return d.LegalKind == "" && d.ExternalID == ""
}
if !ValidArea(d.Area) || (d.Area == AreaLegalWeb) != (d.LegalKind != "") {
return false
}
if d.LegalKind != "" && !ValidLegalKind(d.LegalKind) {
return false
}
switch d.ProcessingStatus {
case "", ProcessingNone, ProcessingPending, ProcessingReady, ProcessingFailed:
return len(d.ExternalID) <= 120
}
return false
}
// Preparation is the prepared version of a document as delivered. The chunk
// part and the consent part are delivered independently; either may be empty.
type Preparation struct {
DocumentID string `json:"document_id"`
Format string `json:"format,omitempty"`
Revision int `json:"revision,omitempty"`
Pin string `json:"pin,omitempty"`
ChunkObjectKey string `json:"-"`
ChunkSHA256 string `json:"chunk_sha256,omitempty"`
ChunkBytes int64 `json:"chunk_bytes,omitempty"`
ChunkCount int `json:"chunk_count"`
ConsentObjectKey string `json:"-"`
ConsentSHA256 string `json:"consent_sha256,omitempty"`
AssetDigests []string `json:"asset_digests"`
DeliveredBy string `json:"delivered_by,omitempty"`
DeliveredAt time.Time `json:"delivered_at"`
}
// HasChunks and HasConsent say which parts are there.
func (p Preparation) HasChunks() bool { return p.ChunkSHA256 != "" }
func (p Preparation) HasConsent() bool { return p.ConsentSHA256 != "" }
// StoreAsset is one image of a tenant's prepared documents.
type StoreAsset struct {
TenantID string `json:"tenant_id"`
SHA256 string `json:"sha256"`
ObjectKey string `json:"-"`
ContentType string `json:"content_type"`
SizeBytes int64 `json:"size_bytes"`
CreatedAt time.Time `json:"created_at"`
}