diff --git a/src/lib/export/labs-backup-schema.ts b/src/lib/export/labs-backup-schema.ts index df15b82e6..70aa14023 100644 --- a/src/lib/export/labs-backup-schema.ts +++ b/src/lib/export/labs-backup-schema.ts @@ -44,6 +44,9 @@ export const labResultBackupSchema = z sourceUnit: z.string().nullable().optional(), takenAt: isoDateTime, source: z.string().min(1), + // The identity a reading arrived under from Health Records (a digest). + // Optional: absent on every file written before the column. + externalId: z.string().min(1).max(128).nullable().optional(), biomarkerName: z.string().nullable().optional(), biomarkerId: z.string().nullable().optional(), note: z.string().nullable().optional(), diff --git a/src/lib/export/labs-backup.ts b/src/lib/export/labs-backup.ts index 21e14ba6d..97063e078 100644 --- a/src/lib/export/labs-backup.ts +++ b/src/lib/export/labs-backup.ts @@ -81,6 +81,9 @@ export interface LabResultBackupEntry { sourceUnit?: string | null; takenAt: string; source: string; + /** The identity a Health Records reading arrived under (a digest), so a + * restored account still recognises it when it arrives again. */ + externalId?: string | null; /** Human-readable cross-reference into `biomarkers` below, not an id. */ /** Stable canonical FK; legacy portable payloads use biomarkerName. */ biomarkerId?: string | null; @@ -221,6 +224,7 @@ export async function buildLabsBackupSlice( sourceUnit: r.sourceUnit, takenAt: r.takenAt.toISOString(), source: r.source, + externalId: r.externalId, biomarkerName: r.biomarker?.name ?? null, })); @@ -464,6 +468,7 @@ export async function restoreLabResults(ctx: RestoreContext): Promise { sourceUnit: lab.sourceUnit ?? null, takenAt: new Date(lab.takenAt), source: lab.source, + externalId: lab.externalId ?? null, noteEncrypted: lab.noteEncrypted !== undefined ? lab.noteEncrypted === null diff --git a/tests/integration/__golden__/backup-parity/export-disaster-recovery.json b/tests/integration/__golden__/backup-parity/export-disaster-recovery.json index aa165803e..cd2481656 100644 --- a/tests/integration/__golden__/backup-parity/export-disaster-recovery.json +++ b/tests/integration/__golden__/backup-parity/export-disaster-recovery.json @@ -902,8 +902,8 @@ "id": "", "biomarkerId": "", "noteEncrypted": "", - "createdAt": "2026-05-02T10:58:00.000Z", - "updatedAt": "2026-05-02T10:59:00.000Z", + "createdAt": "2026-05-02T10:59:00.000Z", + "updatedAt": "2026-05-02T11:00:00.000Z", "deletedAt": null, "note": null, "panel": "rt-LabResult-panel", @@ -920,14 +920,15 @@ "sourceUnit": "rt-LabResult-sourceUnit", "takenAt": "2026-06-30T09:00:00.000Z", "source": "rt-LabResult-source", + "externalId": "rt-LabResult-externalId", "biomarkerName": "Ferritin" }, { "id": "", "biomarkerId": null, "noteEncrypted": null, - "createdAt": "2026-05-02T10:58:01.000Z", - "updatedAt": "2026-05-02T10:59:01.000Z", + "createdAt": "2026-05-02T10:59:01.000Z", + "updatedAt": "2026-05-02T11:00:01.000Z", "deletedAt": "", "note": null, "panel": null, @@ -944,6 +945,7 @@ "sourceUnit": null, "takenAt": "2026-05-30T09:00:00.000Z", "source": "MANUAL", + "externalId": null, "biomarkerName": null } ], @@ -3361,13 +3363,14 @@ "sourceUnit": "rt-LabResult-sourceUnit", "takenAt": "2026-06-30T09:00:00.000Z", "source": "rt-LabResult-source", + "externalId": "rt-LabResult-externalId", "biomarkerName": "Ferritin", "biomarkerId": "", "note": null, - "createdAt": "2026-05-02T10:58:00.000Z", + "createdAt": "2026-05-02T10:59:00.000Z", "noteEncrypted": "", "deletedAt": null, - "updatedAt": "2026-05-02T10:59:00.000Z" + "updatedAt": "2026-05-02T11:00:00.000Z" }, { "id": "", @@ -3385,13 +3388,14 @@ "sourceUnit": null, "takenAt": "2026-05-30T09:00:00.000Z", "source": "MANUAL", + "externalId": null, "biomarkerName": null, "biomarkerId": null, "note": null, - "createdAt": "2026-05-02T10:58:01.000Z", + "createdAt": "2026-05-02T10:59:01.000Z", "noteEncrypted": null, "deletedAt": "", - "updatedAt": "2026-05-02T10:59:01.000Z" + "updatedAt": "2026-05-02T11:00:01.000Z" } ], "biomarkers": [ diff --git a/tests/integration/__golden__/backup-parity/export-portable.json b/tests/integration/__golden__/backup-parity/export-portable.json index cdfcdef7f..46beefee4 100644 --- a/tests/integration/__golden__/backup-parity/export-portable.json +++ b/tests/integration/__golden__/backup-parity/export-portable.json @@ -547,6 +547,7 @@ "sourceUnit": "rt-LabResult-sourceUnit", "takenAt": "2026-06-30T09:00:00.000Z", "source": "rt-LabResult-source", + "externalId": "rt-LabResult-externalId", "biomarkerName": "Ferritin" } ], @@ -2216,6 +2217,7 @@ "sourceUnit": "rt-LabResult-sourceUnit", "takenAt": "2026-06-30T09:00:00.000Z", "source": "rt-LabResult-source", + "externalId": "rt-LabResult-externalId", "biomarkerName": "Ferritin", "note": "rt-LabResult-noteEncrypted" } diff --git a/tests/integration/__golden__/backup-parity/restore-disaster-recovery.json b/tests/integration/__golden__/backup-parity/restore-disaster-recovery.json index 1d2735ca8..091d6f1ac 100644 --- a/tests/integration/__golden__/backup-parity/restore-disaster-recovery.json +++ b/tests/integration/__golden__/backup-parity/restore-disaster-recovery.json @@ -962,9 +962,10 @@ "takenAt": "2026-06-30T09:00:00.000Z", "biomarkerId": "", "source": "rt-LabResult-source", + "externalId": "rt-LabResult-externalId", "noteEncrypted": "", - "createdAt": "2026-05-02T10:58:00.000Z", - "updatedAt": "2026-05-02T10:59:00.000Z", + "createdAt": "2026-05-02T10:59:00.000Z", + "updatedAt": "2026-05-02T11:00:00.000Z", "deletedAt": null }, { @@ -985,9 +986,10 @@ "takenAt": "2026-05-30T09:00:00.000Z", "biomarkerId": null, "source": "MANUAL", + "externalId": null, "noteEncrypted": null, - "createdAt": "2026-05-02T10:58:01.000Z", - "updatedAt": "2026-05-02T10:59:01.000Z", + "createdAt": "2026-05-02T10:59:01.000Z", + "updatedAt": "2026-05-02T11:00:01.000Z", "deletedAt": "" } ], @@ -3824,8 +3826,8 @@ "id": "", "biomarkerId": "", "noteEncrypted": "", - "createdAt": "2026-05-02T10:58:00.000Z", - "updatedAt": "2026-05-02T10:59:00.000Z", + "createdAt": "2026-05-02T10:59:00.000Z", + "updatedAt": "2026-05-02T11:00:00.000Z", "deletedAt": null, "note": null, "panel": "rt-LabResult-panel", @@ -3842,14 +3844,15 @@ "sourceUnit": "rt-LabResult-sourceUnit", "takenAt": "2026-06-30T09:00:00.000Z", "source": "rt-LabResult-source", + "externalId": "rt-LabResult-externalId", "biomarkerName": "Ferritin" }, { "id": "", "biomarkerId": null, "noteEncrypted": null, - "createdAt": "2026-05-02T10:58:01.000Z", - "updatedAt": "2026-05-02T10:59:01.000Z", + "createdAt": "2026-05-02T10:59:01.000Z", + "updatedAt": "2026-05-02T11:00:01.000Z", "deletedAt": "", "note": null, "panel": null, @@ -3866,6 +3869,7 @@ "sourceUnit": null, "takenAt": "2026-05-30T09:00:00.000Z", "source": "MANUAL", + "externalId": null, "biomarkerName": null } ], diff --git a/tests/integration/__golden__/backup-parity/restore-portable.json b/tests/integration/__golden__/backup-parity/restore-portable.json index 1c0668faf..a2f53d38a 100644 --- a/tests/integration/__golden__/backup-parity/restore-portable.json +++ b/tests/integration/__golden__/backup-parity/restore-portable.json @@ -976,6 +976,7 @@ "takenAt": "2026-06-30T09:00:00.000Z", "biomarkerId": "", "source": "rt-LabResult-source", + "externalId": "rt-LabResult-externalId", "noteEncrypted": "", "createdAt": "", "updatedAt": "", @@ -2963,6 +2964,7 @@ "sourceUnit": "rt-LabResult-sourceUnit", "takenAt": "2026-06-30T09:00:00.000Z", "source": "rt-LabResult-source", + "externalId": "rt-LabResult-externalId", "biomarkerName": "Ferritin" } ], diff --git a/tests/integration/__golden__/backup-parity/restore-version-1.json b/tests/integration/__golden__/backup-parity/restore-version-1.json index 5ea1c9686..4d40c7bb1 100644 --- a/tests/integration/__golden__/backup-parity/restore-version-1.json +++ b/tests/integration/__golden__/backup-parity/restore-version-1.json @@ -738,6 +738,7 @@ "takenAt": "2026-07-05T08:00:00.000Z", "biomarkerId": "", "source": "MANUAL", + "externalId": null, "noteEncrypted": "", "createdAt": "", "updatedAt": "", @@ -1635,6 +1636,7 @@ "sourceUnit": null, "takenAt": "2026-07-05T08:00:00.000Z", "source": "MANUAL", + "externalId": null, "biomarkerName": "Ferritin" } ], diff --git a/tests/integration/__golden__/backup-parity/seeded-account.json b/tests/integration/__golden__/backup-parity/seeded-account.json index 67b39f9c5..cf5f7c419 100644 --- a/tests/integration/__golden__/backup-parity/seeded-account.json +++ b/tests/integration/__golden__/backup-parity/seeded-account.json @@ -557,9 +557,10 @@ "takenAt": "2026-06-30T09:00:00.000Z", "biomarkerId": "", "source": "rt-LabResult-source", + "externalId": "rt-LabResult-externalId", "noteEncrypted": "", - "createdAt": "2026-05-02T10:58:00.000Z", - "updatedAt": "2026-05-02T10:59:00.000Z", + "createdAt": "2026-05-02T10:59:00.000Z", + "updatedAt": "2026-05-02T11:00:00.000Z", "deletedAt": null }, { @@ -580,9 +581,10 @@ "takenAt": "2026-05-30T09:00:00.000Z", "biomarkerId": null, "source": "MANUAL", + "externalId": null, "noteEncrypted": null, - "createdAt": "2026-05-02T10:58:01.000Z", - "updatedAt": "2026-05-02T10:59:01.000Z", + "createdAt": "2026-05-02T10:59:01.000Z", + "updatedAt": "2026-05-02T11:00:01.000Z", "deletedAt": "" } ],