138 lines
3.8 KiB
TypeScript
138 lines
3.8 KiB
TypeScript
import type {
|
|
SheetIndex,
|
|
SheetIndexSource,
|
|
} from '../anatomical-sheet/sheet-index'
|
|
import type { Annotation, LocalizedText } from '../anatomical-sheet/schema'
|
|
import type { SheetSnapshot } from '../archive/snapshot'
|
|
import {
|
|
ReportSchema,
|
|
type Finding,
|
|
type Observation,
|
|
type Report,
|
|
type ReportZone,
|
|
type SheetRef,
|
|
} from './v3'
|
|
|
|
/**
|
|
* Moves an imported report onto the sheet the application has loaded
|
|
* (STEP6 §3.5). A report written on another revision of the sheet keeps
|
|
* every datum: a zone or finding the loaded sheet no longer holds is flagged
|
|
* `unresolved`, its labels taken from the archived snapshot when the report
|
|
* carries none, and the report now references the loaded sheet. Nothing
|
|
* happens when the report already sits on the loaded sheet.
|
|
*/
|
|
|
|
/** An index whose source names its sheet (a loaded sheet or a snapshot). */
|
|
export type RebaseIndex = SheetIndex<SheetIndexSource & { ref: SheetRef }>
|
|
|
|
export interface RebaseOutcome {
|
|
report: Report
|
|
/** Zones and findings newly flagged by this rebase. */
|
|
unresolvedZones: number
|
|
unresolvedFindings: number
|
|
rebased: boolean
|
|
}
|
|
|
|
interface Counters {
|
|
zones: number
|
|
findings: number
|
|
}
|
|
|
|
/** The name the archived snapshot gives an annotation (a trigger: its view's). */
|
|
function archivedName(
|
|
archived: SheetSnapshot | null,
|
|
annotationId: string,
|
|
): LocalizedText | undefined {
|
|
if (!archived) return undefined
|
|
const annotation: Annotation | undefined = archived.document.annotations.find(
|
|
(a) => a.id === annotationId,
|
|
)
|
|
if (!annotation) return undefined
|
|
if (annotation.kind !== 'detail_trigger') return annotation.name
|
|
const view = archived.document.views.find(
|
|
(v) => v.id === annotation.opens_view_id,
|
|
)
|
|
return annotation.name ?? view?.name
|
|
}
|
|
|
|
function flagZone(
|
|
zone: ReportZone,
|
|
archived: SheetSnapshot | null,
|
|
): ReportZone {
|
|
const out: ReportZone = { ...zone, unresolved: true }
|
|
const name = archivedName(archived, zone.annotationId)
|
|
if (name) {
|
|
out.labelFrSnapshot ??= name.fr
|
|
out.labelEnSnapshot ??= name.en
|
|
}
|
|
return out
|
|
}
|
|
|
|
function rebaseZone(
|
|
zone: ReportZone,
|
|
index: RebaseIndex,
|
|
archived: SheetSnapshot | null,
|
|
counters: Counters,
|
|
): ReportZone {
|
|
if (zone.unresolved) return { ...zone }
|
|
const known =
|
|
index.annotation(zone.annotationId) !== undefined &&
|
|
index.view(zone.viewId) !== undefined
|
|
if (known) return { ...zone }
|
|
counters.zones += 1
|
|
return flagZone(zone, archived)
|
|
}
|
|
|
|
function rebaseFinding(
|
|
finding: Finding,
|
|
index: RebaseIndex,
|
|
counters: Counters,
|
|
): Finding {
|
|
if (finding.unresolved) return { ...finding }
|
|
if (index.nodeAt(finding.findingZoneId, finding.path)) return { ...finding }
|
|
counters.findings += 1
|
|
return { ...finding, unresolved: true }
|
|
}
|
|
|
|
function rebaseObservation(
|
|
observation: Observation,
|
|
index: RebaseIndex,
|
|
archived: SheetSnapshot | null,
|
|
counters: Counters,
|
|
): Observation {
|
|
return {
|
|
...observation,
|
|
zones: observation.zones.map((z) =>
|
|
rebaseZone(z, index, archived, counters),
|
|
),
|
|
findings: observation.findings.map((f) =>
|
|
rebaseFinding(f, index, counters),
|
|
),
|
|
}
|
|
}
|
|
|
|
export function rebaseReportOnSheet(
|
|
report: Report,
|
|
index: RebaseIndex,
|
|
archived: SheetSnapshot | null,
|
|
): RebaseOutcome {
|
|
if (report.sheet.documentSha256 === index.sheet.ref.documentSha256) {
|
|
return { report, unresolvedZones: 0, unresolvedFindings: 0, rebased: false }
|
|
}
|
|
const counters: Counters = { zones: 0, findings: 0 }
|
|
const { sheetId, versionId, documentSha256 } = index.sheet.ref
|
|
const rebased: Report = {
|
|
...report,
|
|
sheet: { sheetId, versionId, documentSha256 },
|
|
observations: report.observations.map((o) =>
|
|
rebaseObservation(o, index, archived, counters),
|
|
),
|
|
}
|
|
return {
|
|
report: ReportSchema.parse(rebased),
|
|
unresolvedZones: counters.zones,
|
|
unresolvedFindings: counters.findings,
|
|
rebased: true,
|
|
}
|
|
}
|