fix(labs): let a marker without a unit be edited

A marker minted from a text result (OCR, CSV import, inbound document)
stores an empty unit: none has been chosen yet, and its first numeric
reading sets it. The editor read that empty unit as a unit still to be
typed and refused to save, ticking "No unit" sent "1", and the update
route refused any unit change on a marker with readings. Every save of
such a marker failed, a plain rename included, because the form always
sent the unit.

- The form opens an empty unit with "No unit" ticked and sends the unit
  only when it changed; the empty unit and the unity count as the same
  "no unit" there, so a rename leaves the stored unit as it is.
- PUT /api/biomarkers/{id} locks the unit only on a real change while
  numeric readings exist (deleted ones included). Another spelling of
  the stored unit is no change, and text results carry no number the
  unit could misstate, so a text-only marker may take any unit.
- The empty unit stays distinct from "1": it still adopts the unit of
  its first number, which "1" must not. No stored value changes.

Refs #1393
This commit is contained in:
Marc-André Bombeck committed 2026-11-15 23:32:18 +01:00
1 parent 78c2601e2d
commit 31253ff615
11 files changed
+289 -28

No files matched your search

+13 -6
View File
@@ -24,6 +24,7 @@ import { serialiseBiomarker } from "@/lib/labs/biomarker-dto";
import { mergedInto } from "@/lib/labs/biomarker-merge";
import { encryptContextToBytes } from "@/lib/labs/biomarker-store";
import { matchAnalyteKey } from "@/lib/labs/analyte-match";
import { sameLabUnit } from "@/lib/labs/unit-normalise";
import { annotate } from "@/lib/logging/context";
import { computeLabTrend } from "@/lib/labs/lab-trend";
@@ -160,14 +161,20 @@ export const PUT = apiHandler(
}
}
// A marker's unit labels every reading linked to it: readers resolve the
// A marker's unit labels every number linked to it: readers resolve the
// unit from the marker, and the stored numbers are in the old unit. A
// change while readings exist would relabel history without converting a
// single value, so it is refused. Tombstoned readings count too, since an
// undo brings them back under the new label.
if (d.unit !== undefined && d.unit !== existing.unit) {
// change while numeric readings exist would relabel history without
// converting a single value, so it is refused. Tombstoned readings count
// too, since an undo brings them back under the new label.
//
// Only a real change is one: another spelling of the same unit relabels
// nothing. And only numbers lock it: a text result ("negative") carries
// no value the unit could misstate. That is what frees a marker minted
// from a text result, whose empty unit nothing has chosen yet (#1393);
// its first number would have set the unit the same way.
if (d.unit !== undefined && !sameLabUnit(d.unit, existing.unit)) {
const readingCount = await prisma.labResult.count({
where: { userId: user.id, biomarkerId: id },
where: { userId: user.id, biomarkerId: id, value: { not: null } },
});
if (readingCount > 0) {
annotate({