feat(labs): record qualitative lab results

Add first-class qualitative lab findings (positive / negative / borderline
serology) alongside the existing numeric path. A LabResult is now EITHER a
numeric reading (value + unit, optional reference bounds) OR a qualitative one
(valueText, e.g. "negativ"), never both, never neither.

- schema: make `value` nullable, add `value_text`; migration 0181 is additive
  (DROP NOT NULL + ADD COLUMN), every existing numeric row is untouched.
- validation: numeric-XOR-qualitative refine on create + update; unit/bounds
  apply to the numeric path only; valueText capped at 120 chars. The single
  resource PUT rejects a cross-type edit.
- serialise: carry valueText through both DTOs; a qualitative row reports the
  neutral `unknown` range verdict (no number to classify).
- FHIR: a qualitative result emits valueCodeableConcept — SNOMED qualifier
  value for confidently-known terms (Negative 260385009, Positive 10828004,
  Not detected 260415000, Detected 260373001), text-only for borderline /
  grenzwertig and any unrecognised term. The raw text always rides .text.
- UI: a numeric/qualitative toggle in the lab form; list, history, detail,
  chart, sparkline, and the doctor-report PDF all render qualitative readings
  as their text and never plot or divide on a null value.
- i18n: new labs.form keys across all six locales.
- openapi: response carries a nullable value + valueText; regenerated.
This commit is contained in:
Marc-André Bombeck committed 2026-06-20 11:17:43 +02:00
1 parent e631396af3
commit a5b3715a46
30 files changed
+819 -123

No files matched your search

@@ -122,6 +122,72 @@ describe("createLabResultSchema", () => {
});
expect(r.success).toBe(false);
});
// ── qualitative readings (v1.18.9) ────────────────────────────────
it("accepts a qualitative reading (valueText, no numeric value/unit)", () => {
const r = createLabResultSchema.safeParse({
analyte: "Hepatitis Bs-Antigen",
valueText: "negativ",
takenAt: RECENT,
});
expect(r.success).toBe(true);
});
it("accepts a qualitative reading against a catalog biomarker", () => {
const r = createLabResultSchema.safeParse({
biomarkerId: "bm_123",
valueText: "positiv",
takenAt: RECENT,
});
expect(r.success).toBe(true);
});
it("rejects a reading carrying BOTH value and valueText", () => {
const r = createLabResultSchema.safeParse({
analyte: "LDL",
value: 110,
unit: "mg/dL",
valueText: "negativ",
takenAt: RECENT,
});
expect(r.success).toBe(false);
});
it("rejects a reading carrying NEITHER value nor valueText", () => {
const r = createLabResultSchema.safeParse({
analyte: "LDL",
unit: "mg/dL",
takenAt: RECENT,
});
expect(r.success).toBe(false);
});
it("rejects an empty qualitative valueText", () => {
const r = createLabResultSchema.safeParse({
analyte: "Serology",
valueText: " ",
takenAt: RECENT,
});
expect(r.success).toBe(false);
});
it("rejects an over-long valueText (> 120 chars)", () => {
const r = createLabResultSchema.safeParse({
analyte: "Serology",
valueText: "x".repeat(121),
takenAt: RECENT,
});
expect(r.success).toBe(false);
});
it("does not require a unit for a qualitative reading", () => {
const r = createLabResultSchema.safeParse({
analyte: "Serology",
valueText: "negativ",
takenAt: RECENT,
});
expect(r.success).toBe(true);
});
});
describe("updateLabResultSchema", () => {
@@ -139,6 +205,19 @@ describe("updateLabResultSchema", () => {
expect(r.success).toBe(true);
});
it("accepts a partial valueText-only edit (v1.18.9)", () => {
const r = updateLabResultSchema.safeParse({ valueText: "positiv" });
expect(r.success).toBe(true);
});
it("rejects an edit carrying both value and valueText", () => {
const r = updateLabResultSchema.safeParse({
value: 5.6,
valueText: "negativ",
});
expect(r.success).toBe(false);
});
it("still enforces the range order when both bounds are supplied", () => {
const r = updateLabResultSchema.safeParse({
referenceLow: 10,
+43 -6
View File
@@ -72,12 +72,22 @@ const referenceHigh = z.number().finite().optional();
*/
const biomarkerId = z.string().trim().min(1).max(64).optional();
/**
* v1.18.9 — a qualitative reading ("negativ" / "positiv" / "grenzwertig" /
* "nicht nachweisbar"). Trimmed, non-empty, sensibly capped. Mutually
* exclusive with the numeric `value` (see the create/update refinements).
*/
const valueText = requiredText(120).optional();
export const createLabResultSchema = z
.object({
biomarkerId,
panel: optionalPanel,
analyte: requiredText(120).optional(),
value: z.number().finite(),
// v1.18.9 — `value` is no longer required: a reading is EITHER numeric
// (`value`) OR qualitative (`valueText`), never both, never neither.
value: z.number().finite().optional(),
valueText,
unit: requiredText(40).optional(),
referenceLow,
referenceHigh,
@@ -89,14 +99,31 @@ export const createLabResultSchema = z
message: "Either biomarkerId or analyte is required",
path: ["analyte"],
})
// Without a catalog link the unit is required (the catalog otherwise
// supplies it server-side).
.refine((d) => d.biomarkerId !== undefined || d.unit !== undefined, {
message: "unit is required when no biomarkerId is given",
path: ["unit"],
// Numeric XOR qualitative: exactly one of `value` / `valueText` is set.
.refine((d) => (d.value !== undefined) !== (d.valueText !== undefined), {
message:
"Provide exactly one of value (numeric) or valueText (qualitative)",
path: ["value"],
})
// A numeric reading without a catalog link still needs a unit (the catalog
// otherwise supplies it server-side). A qualitative reading needs no unit —
// its result is the text, and a unit/range is meaningless for it.
.refine(
(d) =>
d.valueText !== undefined ||
d.biomarkerId !== undefined ||
d.unit !== undefined,
{
message:
"unit is required for a numeric reading when no biomarkerId is given",
path: ["unit"],
},
)
// Reference bounds apply to the numeric path only; skip them entirely for a
// qualitative reading.
.refine(
(d) =>
d.valueText !== undefined ||
d.referenceLow === undefined ||
d.referenceHigh === undefined ||
d.referenceLow <= d.referenceHigh,
@@ -118,12 +145,22 @@ export const updateLabResultSchema = z
panel: optionalPanel.or(z.null()),
analyte: requiredText(120).optional(),
value: z.number().finite().optional(),
// v1.18.9 — edit the qualitative result. A row stays single-typed: a PUT
// must not set BOTH `value` and `valueText` (the refine below). Switching a
// row's type (numeric ↔ qualitative) is intentionally not supported here —
// delete and re-add — so an omitted key leaves the row's existing type.
valueText: requiredText(120).optional(),
unit: requiredText(40).optional(),
referenceLow: referenceLow.or(z.null()),
referenceHigh: referenceHigh.or(z.null()),
takenAt: takenAtField.optional(),
note: optionalNote.or(z.null()),
})
// A single edit never carries both a numeric and a qualitative value.
.refine((d) => d.value === undefined || d.valueText === undefined, {
message: "Provide value (numeric) or valueText (qualitative), not both",
path: ["value"],
})
.refine(
(d) =>
d.referenceLow === undefined ||