feat(domain): archive contracts — sheet snapshot (lean document, caps, integrity), envelope v1 layered reader, branding and label schemas, report rebase, sheet index moved from the app; bundle carries asset hashes

This commit is contained in:
bchanot committed 2026-10-11 12:30:57 +02:00
1 parent 8b86b926c8
commit cdc0943f35
25 files changed
+2253 -116

No files matched your search

@@ -0,0 +1,73 @@
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { join, resolve } from 'node:path'
import {
AnatomicalSheetDocumentSchema,
ReportMigrationMapSchema,
type ReportMigrationMap,
type SnapshotSource,
} from '../../src'
/**
* The committed equine draft (`pnpm import:equine`) as the application
* bundles it: document, reference, SVG assets and their manifest hashes,
* plus the v2 → v3 migration map. Read from the seed, never copied.
*/
export const DRAFT_DIR = resolve(
fileURLToPath(import.meta.url),
'../../../../../supabase/seed/anatomical-sheets/equine/draft',
)
interface SeedRef {
sheet_id: string
version_id: string | null
document_sha256: string
schema_version: number
species_id: string
}
interface SeedAsset {
file: string
sha256: string
svg_asset_id: string
}
function json<T>(name: string): T {
return JSON.parse(readFileSync(join(DRAFT_DIR, name), 'utf8')) as T
}
export interface DraftSheet extends SnapshotSource {
assetHashes: Readonly<Record<string, string>>
migrationMap: ReportMigrationMap
}
export function loadDraftSheet(): DraftSheet {
const ref = json<SeedRef>('sheet-ref.json')
const manifest = json<SeedAsset[]>('assets.json')
const assets: Record<string, string> = {}
const assetHashes: Record<string, string> = {}
for (const asset of manifest) {
assets[asset.svg_asset_id] = readFileSync(
join(DRAFT_DIR, 'assets', asset.file),
'utf8',
)
assetHashes[asset.svg_asset_id] = asset.sha256
}
return {
status: 'draft',
ref: {
sheetId: ref.sheet_id,
versionId: ref.version_id,
documentSha256: ref.document_sha256,
},
speciesId: ref.species_id,
schemaVersion: ref.schema_version,
document: AnatomicalSheetDocumentSchema.parse(json('document.json')),
assets,
assetHashes,
migrationMap: ReportMigrationMapSchema.parse(
json('report-migration-map.json'),
),
}
}
@@ -0,0 +1,95 @@
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { resolve } from 'node:path'
import {
AnatomicalSheetDocumentSchema,
ReportV2Schema,
type AnatomicalSheetDocument,
type FindingCategory,
type FindingNode,
type ObservationV2,
type ReportV2,
} from '../../src'
/**
* The legacy catalog as the application's archive knew it: every leaf of the
* generated baseline (`legacy-import/document.json`) becomes a Report v2
* finding, grouped in one observation per zone. Same recipe as the app and
* anatomy helpers; migrated through the draft map it yields the unresolved
* items (sphenoid / ssb leaves) a Phase 0 golden was frozen with.
*/
const BASELINE = resolve(
fileURLToPath(import.meta.url),
'../../../../../supabase/seed/anatomical-sheets/equine/legacy-import/document.json',
)
export interface LegacyLeaf {
zoneId: string
path: string[]
name: { fr: string; en: string }
vet: boolean
}
export function loadLegacyBaseline(): AnatomicalSheetDocument {
return AnatomicalSheetDocumentSchema.parse(
JSON.parse(readFileSync(BASELINE, 'utf8')),
)
}
export function legacyLeaves(doc: AnatomicalSheetDocument): LegacyLeaf[] {
const out: LegacyLeaf[] = []
const walk = (
zoneId: string,
nodes: readonly (FindingCategory | FindingNode)[],
prefix: string[],
) => {
for (const node of nodes) {
const path = [...prefix, node.id]
if (node.children.length > 0) walk(zoneId, node.children, path)
else {
const vet =
'requires_vet_referral' in node && node.requires_vet_referral
out.push({ zoneId, path, name: node.name, vet })
}
}
}
for (const zone of doc.finding_zones) walk(zone.id, zone.categories, [])
return out
}
export function leavesAsV2Report(leaves: LegacyLeaf[]): ReportV2 {
const byZone = new Map<string, ObservationV2>()
for (const leaf of leaves) {
let obs = byZone.get(leaf.zoneId)
if (!obs) {
obs = {
id: `obs-${leaf.zoneId}`,
zones: [],
findings: [],
freeTextFindings: [],
comment: '',
}
byZone.set(leaf.zoneId, obs)
}
obs.findings.push({
zoneId: leaf.zoneId,
path: leaf.path,
labelFrSnapshot: leaf.name.fr,
labelEnSnapshot: leaf.name.en || leaf.name.fr,
...(leaf.vet ? { requiresVetReferral: true } : {}),
})
}
return ReportV2Schema.parse({
version: 2,
id: 'all-leaves',
createdAt: '2026-10-10T00:00:00Z',
date: '2026-10-10',
practitioner: { name: 'Synthetic' },
animal: { speciesId: 'equine', name: 'Synthetic' },
owner: { name: 'Synthetic' },
observations: [...byZone.values()],
conclusion: '',
recommendations: '',
})
}