feat(schema): bump report to v2 + observation findings + drop pathology
ObservationSchema now carries a hierarchical Finding (zoneId + path + labelFrSnapshot + labelEnSnapshot + requiresVetReferral?) plus a renamed freeTextFindings list, replacing the flat pathologyIds / freeTextPathologies pair. Snapshot labels keep already-exported reports readable if the catalog is later edited. ReportSchema.version bumped from 1 → 2. PDF import surfaces a distinct, French-language error message on legacy v1 attachments instead of the generic "not an osteo PDF" line, via a new IncompatibleReportVersionError detected before the strict Zod parse runs. Store seed and tests updated for the new shape. The standalone Pathology Zod schema and its test are removed — the new findings tree is the single source of truth for what an observation can carry. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
f80e161d61
commit
d08d773d74
13 files changed
+146
-108
No files matched your search
@@ -26,11 +26,28 @@ export const ObservationZoneSchema = z.object({
|
||||
|
||||
export type ObservationZone = z.infer<typeof ObservationZoneSchema>
|
||||
|
||||
/**
|
||||
* A selected leaf from the species findings tree. The path is anchored at the
|
||||
* zone root (categories array), e.g. ['sphenoid-dysfunctions', 'physiological',
|
||||
* 'flexion']. Labels are snapshotted at selection time so previously exported
|
||||
* reports remain readable if the catalog is later edited.
|
||||
*/
|
||||
export const FindingSchema = z.object({
|
||||
zoneId: z.string().min(1),
|
||||
path: z.array(z.string().min(1)).min(1),
|
||||
labelFrSnapshot: z.string().min(1),
|
||||
labelEnSnapshot: z.string().min(1),
|
||||
requiresVetReferral: z.boolean().optional(),
|
||||
})
|
||||
|
||||
export type Finding = z.infer<typeof FindingSchema>
|
||||
|
||||
export const ObservationSchema = z.object({
|
||||
id: z.string().min(1),
|
||||
zones: z.array(ObservationZoneSchema),
|
||||
pathologyIds: z.array(z.string()),
|
||||
freeTextPathologies: z.array(z.string()),
|
||||
findings: z.array(FindingSchema),
|
||||
/** Free-text additions when the tree does not cover a finding */
|
||||
freeTextFindings: z.array(z.string()),
|
||||
severity: SeveritySchema.optional(),
|
||||
comment: z.string(),
|
||||
})
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
import { z } from 'zod'
|
||||
|
||||
export const PathologyCategorySchema = z.enum([
|
||||
'osseux',
|
||||
'articulaire',
|
||||
'musculaire',
|
||||
'tendino-ligamentaire',
|
||||
'fascial',
|
||||
'visceral',
|
||||
'cranio-sacre',
|
||||
'nerveux',
|
||||
'autre',
|
||||
])
|
||||
|
||||
export type PathologyCategory = z.infer<typeof PathologyCategorySchema>
|
||||
|
||||
export const PathologyRefTypeSchema = z.enum([
|
||||
'manual',
|
||||
'journal',
|
||||
'database',
|
||||
'web',
|
||||
])
|
||||
|
||||
export const PathologyRefSchema = z.object({
|
||||
type: PathologyRefTypeSchema,
|
||||
/** Human-readable citation — no raw URLs in PDF output */
|
||||
citation: z.string().min(1),
|
||||
url: z.string().url().optional(),
|
||||
})
|
||||
|
||||
export type PathologyRef = z.infer<typeof PathologyRefSchema>
|
||||
|
||||
export const PathologySchema = z.object({
|
||||
/** Stable kebab-case identifier */
|
||||
id: z.string().min(1),
|
||||
labelFr: z.string().min(1),
|
||||
description: z.string().min(1),
|
||||
category: PathologyCategorySchema,
|
||||
/** Empty array = applicable to all species */
|
||||
speciesIds: z.array(z.string()),
|
||||
/** Empty array = applicable to all zones */
|
||||
boneIds: z.array(z.string()),
|
||||
/** At least one source required */
|
||||
sources: z.array(PathologyRefSchema).min(1),
|
||||
})
|
||||
|
||||
export type Pathology = z.infer<typeof PathologySchema>
|
||||
@@ -11,8 +11,11 @@ const NextAppointmentSchema = z.discriminatedUnion('kind', [
|
||||
export type NextAppointment = z.infer<typeof NextAppointmentSchema>
|
||||
|
||||
export const ReportSchema = z.object({
|
||||
/** Schema version — always 1 for MVP. Bump on breaking changes. */
|
||||
version: z.literal(1),
|
||||
/**
|
||||
* Schema version. v2 introduced the hierarchical findings tree
|
||||
* (replacing the flat pathology list). Bump on breaking changes.
|
||||
*/
|
||||
version: z.literal(2),
|
||||
id: z.string().min(1),
|
||||
/** ISO timestamp of report creation */
|
||||
createdAt: z.string(),
|
||||
|
||||
+21
-2
@@ -3,11 +3,21 @@ import { ReportSchema, type Report } from '@/domain/schema/report'
|
||||
|
||||
const FILENAME = 'osteo-report.json'
|
||||
|
||||
export class IncompatibleReportVersionError extends Error {
|
||||
constructor(public readonly foundVersion: number) {
|
||||
super(
|
||||
`Ce PDF est au format v${foundVersion}, incompatible avec la version actuelle (v2). ` +
|
||||
`Réexportez le bilan depuis une version récente de l'application.`,
|
||||
)
|
||||
this.name = 'IncompatibleReportVersionError'
|
||||
}
|
||||
}
|
||||
|
||||
export async function attachReportJson(pdfDoc: PDFDocument, report: Report): Promise<void> {
|
||||
const bytes = new TextEncoder().encode(JSON.stringify(report, null, 2))
|
||||
await pdfDoc.attach(bytes, FILENAME, {
|
||||
mimeType: 'application/json',
|
||||
description: 'Osteo Report state v1',
|
||||
description: 'Osteo Report state v2',
|
||||
creationDate: new Date(),
|
||||
modificationDate: new Date(),
|
||||
afRelationship: AFRelationship.Data,
|
||||
@@ -30,7 +40,16 @@ export async function extractReportJson(pdfDoc: PDFDocument): Promise<Report> {
|
||||
|
||||
function parseAttachment(bytes: Uint8Array): Report {
|
||||
const text = new TextDecoder().decode(bytes)
|
||||
const data = JSON.parse(text)
|
||||
const data: unknown = JSON.parse(text)
|
||||
if (
|
||||
data !== null &&
|
||||
typeof data === 'object' &&
|
||||
'version' in data &&
|
||||
typeof (data as { version: unknown }).version === 'number' &&
|
||||
(data as { version: number }).version !== 2
|
||||
) {
|
||||
throw new IncompatibleReportVersionError((data as { version: number }).version)
|
||||
}
|
||||
return ReportSchema.parse(data)
|
||||
}
|
||||
|
||||
|
||||
+12
-4
@@ -1,10 +1,13 @@
|
||||
import { PDFDocument } from 'pdf-lib'
|
||||
import { extractReportJson } from './attachment'
|
||||
import { extractReportJson, IncompatibleReportVersionError } from './attachment'
|
||||
import type { Report } from '@/domain/schema/report'
|
||||
|
||||
export class ImportError extends Error {
|
||||
constructor() {
|
||||
super("Ce PDF n'a pas été généré par Osteo Report (pas de données détectées).")
|
||||
constructor(message?: string) {
|
||||
super(
|
||||
message ??
|
||||
"Ce PDF n'a pas été généré par Osteo Report (pas de données détectées).",
|
||||
)
|
||||
this.name = 'ImportError'
|
||||
}
|
||||
}
|
||||
@@ -18,7 +21,12 @@ export async function importReportFromPdf(pdfBytes: Uint8Array): Promise<Report>
|
||||
}
|
||||
try {
|
||||
return await extractReportJson(pdfDoc)
|
||||
} catch {
|
||||
} catch (err) {
|
||||
// Version mismatch is a distinct failure mode — surface its message
|
||||
// to the user instead of the generic "not an osteo PDF" line.
|
||||
if (err instanceof IncompatibleReportVersionError) {
|
||||
throw new ImportError(err.message)
|
||||
}
|
||||
throw new ImportError()
|
||||
}
|
||||
}
|
||||
@@ -30,8 +30,8 @@ function emptyObservation(): Observation {
|
||||
return {
|
||||
id: generateId(),
|
||||
zones: [],
|
||||
pathologyIds: [],
|
||||
freeTextPathologies: [],
|
||||
findings: [],
|
||||
freeTextFindings: [],
|
||||
comment: '',
|
||||
}
|
||||
}
|
||||
@@ -44,7 +44,7 @@ export const useReportStore = create<ReportStore>((set) => ({
|
||||
set({
|
||||
report: {
|
||||
...draft,
|
||||
version: 1,
|
||||
version: 2,
|
||||
id: generateId(),
|
||||
createdAt: nowIso(),
|
||||
observations:
|
||||
|
||||
Reference in new issue
Block a user