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.
This commit is contained in:
Marc-André Bombeck committed 2026-11-17 18:37:44 +01:00
1 parent d32036b003
commit 291fdb9988
3 files changed
+60 -48

No files matched your search

@@ -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,
+1 -47
View File
@@ -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;
+57
View File
@@ -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;
}