From 291fdb99886df7be32a02b02beb6117fbc78fbfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Tue, 17 Nov 2026 18:37:44 +0100 Subject: [PATCH] refactor(labs): move the reading duplicate keys into their own module The CSV import owned readingKeys and LAB_TEXT_LIMITS; the FHIR import needs the same rules, so they move to src/lib/labs/reading-keys.ts. --- .../__tests__/record-import-rows.test.ts | 3 +- src/lib/import/labs-csv.ts | 48 +--------------- src/lib/labs/reading-keys.ts | 57 +++++++++++++++++++ 3 files changed, 60 insertions(+), 48 deletions(-) create mode 100644 src/lib/labs/reading-keys.ts diff --git a/src/lib/import/__tests__/record-import-rows.test.ts b/src/lib/import/__tests__/record-import-rows.test.ts index be25ceb3e..1947055db 100644 --- a/src/lib/import/__tests__/record-import-rows.test.ts +++ b/src/lib/import/__tests__/record-import-rows.test.ts @@ -14,7 +14,8 @@ import { medicationsCsvQuerySchema, } from "@/lib/validations/record-import"; -import { normaliseLabRow, readingKeys } from "../labs-csv"; +import { readingKeys } from "@/lib/labs/reading-keys"; +import { normaliseLabRow } from "../labs-csv"; import { medicationCreateBody, normaliseMedicationRow, diff --git a/src/lib/import/labs-csv.ts b/src/lib/import/labs-csv.ts index f4cef611a..f0053e166 100644 --- a/src/lib/import/labs-csv.ts +++ b/src/lib/import/labs-csv.ts @@ -46,6 +46,7 @@ import { labReadingDay, labReadingDaySearchRange, } from "@/lib/labs/reading-day"; +import { LAB_TEXT_LIMITS, readingKeys } from "@/lib/labs/reading-keys"; import { encryptNoteToBytes } from "@/lib/labs/store"; import type { LabsCsvQuery, @@ -60,14 +61,6 @@ const MAX_LISTED_MARKERS = 200; const FUTURE_SLACK_MS = 86_400_000; const EARLIEST = Date.UTC(1900, 0, 1); -export const LAB_TEXT_LIMITS = { - analyte: 200, - valueText: 200, - unit: 80, - panel: 100, - note: 1000, -} as const; - export interface LabCsvRow { line: number; analyte: string; @@ -212,45 +205,6 @@ export function normaliseLabRow( }; } -/** - * The keys a reading is a duplicate under, mirroring the scan commit's - * `isDuplicate`: the marker (or the name, for a reading filed without one), - * the reading's calendar day, and the value as stored, as printed, or as text. - */ -export function readingKeys( - reading: { - biomarkerId: string | null; - analyte: string; - takenAt: Date; - value: number | null; - valueText: string | null; - sourceValue?: number | null; - sourceUnit?: string | null; - unit?: string | null; - }, - tz: string, -): string[] { - const who = reading.biomarkerId - ? `bm:${reading.biomarkerId}` - : `an:${reading.analyte.trim().toLowerCase()}`; - const day = labReadingDay(reading.takenAt, tz); - const keys: string[] = []; - if (reading.value !== null) keys.push(`${who}|${day}|n:${reading.value}`); - if ( - reading.sourceValue !== null && - reading.sourceValue !== undefined && - reading.sourceUnit - ) { - keys.push( - `${who}|${day}|s:${reading.sourceValue}:${reading.sourceUnit.trim().toLowerCase()}`, - ); - } - if (reading.valueText !== null) { - keys.push(`${who}|${day}|t:${reading.valueText.trim().toLowerCase()}`); - } - return keys; -} - interface Planned { row: LabCsvRow; marker: IndexedMarker | null; diff --git a/src/lib/labs/reading-keys.ts b/src/lib/labs/reading-keys.ts new file mode 100644 index 000000000..4ec0f1221 --- /dev/null +++ b/src/lib/labs/reading-keys.ts @@ -0,0 +1,57 @@ +/** + * What makes two lab readings the same reading, and the text ceilings every + * bulk lab write shares. + * + * Used by the CSV import (`src/lib/import/labs-csv.ts`) and the FHIR import + * (`src/lib/labs/fhir-import/write.ts`), so a reading typed by hand, scanned, + * imported from a spreadsheet or arriving from Apple Health is recognised as + * one reading whichever way it came in first. + */ +import { labReadingDay } from "@/lib/labs/reading-day"; + +export const LAB_TEXT_LIMITS = { + analyte: 200, + valueText: 200, + unit: 80, + panel: 100, + note: 1000, +} as const; + +/** + * The keys a reading is a duplicate under, mirroring the scan commit's + * `isDuplicate`: the marker (or the name, for a reading filed without one), + * the reading's calendar day, and the value as stored, as printed, or as text. + */ +export function readingKeys( + reading: { + biomarkerId: string | null; + analyte: string; + takenAt: Date; + value: number | null; + valueText: string | null; + sourceValue?: number | null; + sourceUnit?: string | null; + unit?: string | null; + }, + tz: string, +): string[] { + const who = reading.biomarkerId + ? `bm:${reading.biomarkerId}` + : `an:${reading.analyte.trim().toLowerCase()}`; + const day = labReadingDay(reading.takenAt, tz); + const keys: string[] = []; + if (reading.value !== null) keys.push(`${who}|${day}|n:${reading.value}`); + if ( + reading.sourceValue !== null && + reading.sourceValue !== undefined && + reading.sourceUnit + ) { + keys.push( + `${who}|${day}|s:${reading.sourceValue}:${reading.sourceUnit.trim().toLowerCase()}`, + ); + } + if (reading.valueText !== null) { + keys.push(`${who}|${day}|t:${reading.valueText.trim().toLowerCase()}`); + } + return keys; +}