mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +00:00
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:
1 parent
78c2601e2d
commit
31253ff615
11 files changed
+289
-28
No files matched your search
@@ -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({
|
||||
|
||||
@@ -38,6 +38,7 @@ describe("buildBiomarkerBody", () => {
|
||||
it("sends null for every field an edit emptied", () => {
|
||||
expect(
|
||||
buildBiomarkerBody(empty, {
|
||||
unit: "mmol/L",
|
||||
lowerBound: 10,
|
||||
upperBound: 0,
|
||||
panel: "Lipids",
|
||||
@@ -56,6 +57,7 @@ describe("buildBiomarkerBody", () => {
|
||||
it("leaves a field out that was empty when the edit began", () => {
|
||||
expect(
|
||||
buildBiomarkerBody(empty, {
|
||||
unit: "mmol/L",
|
||||
lowerBound: null,
|
||||
upperBound: null,
|
||||
panel: null,
|
||||
@@ -64,3 +66,51 @@ describe("buildBiomarkerBody", () => {
|
||||
).toEqual({ name: "LDL", unit: "mg/dL" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("buildBiomarkerBody: the unit on an edit", () => {
|
||||
const initial = {
|
||||
lowerBound: null,
|
||||
upperBound: null,
|
||||
panel: null,
|
||||
context: null,
|
||||
};
|
||||
|
||||
it("leaves an unchanged unit out, so a rename never touches it", () => {
|
||||
expect(
|
||||
buildBiomarkerBody(
|
||||
{ ...empty, name: "LDL-C" },
|
||||
{ ...initial, unit: "mg/dL" },
|
||||
),
|
||||
).toEqual({ name: "LDL-C" });
|
||||
});
|
||||
|
||||
it("leaves the unit out when only the whitespace around it moved", () => {
|
||||
expect(
|
||||
buildBiomarkerBody(
|
||||
{ ...empty, unit: " mg/dL " },
|
||||
{ ...initial, unit: "mg/dL" },
|
||||
),
|
||||
).toEqual({ name: "LDL" });
|
||||
});
|
||||
|
||||
it("reads a marker minted from a text result and the ticked box as the same no unit", () => {
|
||||
expect(
|
||||
buildBiomarkerBody(
|
||||
{ ...empty, name: "Urine protein", unit: "1" },
|
||||
{ ...initial, unit: "" },
|
||||
),
|
||||
).toEqual({ name: "Urine protein" });
|
||||
});
|
||||
|
||||
it("sends a unit the edit changed", () => {
|
||||
expect(
|
||||
buildBiomarkerBody({ ...empty, unit: "mg/dL" }, { ...initial, unit: "" }),
|
||||
).toEqual({ name: "LDL", unit: "mg/dL" });
|
||||
expect(
|
||||
buildBiomarkerBody(
|
||||
{ ...empty, unit: "1" },
|
||||
{ ...initial, unit: "mg/dL" },
|
||||
),
|
||||
).toEqual({ name: "LDL", unit: "1" });
|
||||
});
|
||||
});
|
||||
@@ -91,6 +91,32 @@ describe("<BiomarkerForm> unit lock", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("<BiomarkerForm> a marker without a unit", () => {
|
||||
function noneBox(html: string): string {
|
||||
const box = html.match(/<button[^>]*role="checkbox"[^>]*>/)?.[0];
|
||||
if (!box) throw new Error("no checkbox rendered");
|
||||
return box;
|
||||
}
|
||||
|
||||
it("opens a marker minted from a text result with No unit ticked", () => {
|
||||
// A text result ("negative") mints its marker with an empty unit. The
|
||||
// editor read that as a unit still to be typed and refused every save.
|
||||
const html = render(<BiomarkerForm existing={{ ...MARKER, unit: "" }} />);
|
||||
expect(noneBox(html)).toContain('aria-checked="true"');
|
||||
expect(html).not.toMatch(/<input[^>]*id="biomarker-unit"[^>]*required/);
|
||||
});
|
||||
|
||||
it("opens a marker kept in the unity with No unit ticked", () => {
|
||||
const html = render(<BiomarkerForm existing={{ ...MARKER, unit: "1" }} />);
|
||||
expect(noneBox(html)).toContain('aria-checked="true"');
|
||||
});
|
||||
|
||||
it("opens a marker with a real unit with the box clear", () => {
|
||||
const html = render(<BiomarkerForm existing={MARKER} />);
|
||||
expect(noneBox(html)).toContain('aria-checked="false"');
|
||||
});
|
||||
});
|
||||
|
||||
describe("<BiomarkerForm> panel suggestions", () => {
|
||||
it("suggests every catalog panel and the person's own panels", () => {
|
||||
const html = render(<BiomarkerForm />, ["Annual check"]);
|
||||
|
||||
@@ -8,8 +8,17 @@
|
||||
* none is cleared, which an omitted key never did. Only a field that held a
|
||||
* value when the form opened can be emptied, so a context the list could not
|
||||
* decrypt (shown empty) is never wiped by saving another field.
|
||||
*
|
||||
* An edit sends the unit only when it changed. A marker with readings refuses
|
||||
* a unit change, so a unit sent back unchanged could only trip that check, and
|
||||
* did on a marker minted from a text result: its empty unit opens as "no unit",
|
||||
* which the box states as the unity, and a rename then read as a unit change.
|
||||
* The empty unit and the unity are the same "no unit" here; saving leaves the
|
||||
* empty one as it is, so the marker still takes the unit of its first number.
|
||||
*/
|
||||
|
||||
import { DIMENSIONLESS_UNIT, markerHasNoUnit } from "@/lib/labs/dimensionless";
|
||||
|
||||
export interface BiomarkerFormFields {
|
||||
name: string;
|
||||
unit: string;
|
||||
@@ -20,6 +29,7 @@ export interface BiomarkerFormFields {
|
||||
}
|
||||
|
||||
export interface BiomarkerFormInitial {
|
||||
unit: string;
|
||||
lowerBound: number | null;
|
||||
upperBound: number | null;
|
||||
panel: string | null;
|
||||
@@ -28,7 +38,7 @@ export interface BiomarkerFormInitial {
|
||||
|
||||
export type BiomarkerBody = {
|
||||
name: string;
|
||||
unit: string;
|
||||
unit?: string;
|
||||
lowerBound?: number | null;
|
||||
upperBound?: number | null;
|
||||
panel?: string | null;
|
||||
@@ -41,10 +51,9 @@ export function buildBiomarkerBody(
|
||||
): BiomarkerBody {
|
||||
const panel = fields.panel.trim();
|
||||
const context = fields.context.trim();
|
||||
const body: BiomarkerBody = {
|
||||
name: fields.name.trim(),
|
||||
unit: fields.unit.trim(),
|
||||
};
|
||||
const unit = fields.unit.trim();
|
||||
const body: BiomarkerBody = { name: fields.name.trim() };
|
||||
if (!initial || !sameFormUnit(unit, initial.unit)) body.unit = unit;
|
||||
if (fields.lower !== null) body.lowerBound = fields.lower;
|
||||
else if (initial?.lowerBound != null) body.lowerBound = null;
|
||||
if (fields.upper !== null) body.upperBound = fields.upper;
|
||||
@@ -55,3 +64,18 @@ export function buildBiomarkerBody(
|
||||
else if (initial?.context) body.context = null;
|
||||
return body;
|
||||
}
|
||||
|
||||
/**
|
||||
* The unit an edit opens with. A marker minted from a text result has an
|
||||
* empty unit, none chosen yet; it opens as "no unit", like the unity, rather
|
||||
* than as a unit still to be typed.
|
||||
*/
|
||||
export function formUnit(stored: string): string {
|
||||
return markerHasNoUnit(stored) ? DIMENSIONLESS_UNIT : stored;
|
||||
}
|
||||
|
||||
/** Whether the unit the form holds is the one the marker opened with. */
|
||||
function sameFormUnit(unit: string, initial: string): boolean {
|
||||
if (unit === initial.trim()) return true;
|
||||
return markerHasNoUnit(unit) && markerHasNoUnit(initial);
|
||||
}
|
||||
@@ -26,7 +26,7 @@ import {
|
||||
} from "@/lib/labs/biomarker-catalog";
|
||||
import { useTranslations } from "@/lib/i18n/context";
|
||||
|
||||
import { buildBiomarkerBody } from "./biomarker-form-body";
|
||||
import { buildBiomarkerBody, formUnit } from "./biomarker-form-body";
|
||||
import { BiomarkerPanelField } from "./biomarker-panel-field";
|
||||
import { LabUnitInput } from "./lab-unit-input";
|
||||
import type { BiomarkerDto } from "./types";
|
||||
@@ -76,7 +76,7 @@ export function BiomarkerForm({
|
||||
const unitHintId = `${formId}-unit-hint`;
|
||||
|
||||
const [name, setName] = useState(existing?.name ?? "");
|
||||
const [unit, setUnit] = useState(existing?.unit ?? "");
|
||||
const [unit, setUnit] = useState(existing ? formUnit(existing.unit) : "");
|
||||
const [lower, setLower] = useState(
|
||||
existing?.lowerBound != null ? String(existing.lowerBound) : "",
|
||||
);
|
||||
|
||||
@@ -12,6 +12,15 @@
|
||||
* other way round. Only the display differs: nobody wants "3.6 1", so the
|
||||
* helpers below render the unity as nothing beside a value.
|
||||
*
|
||||
* A marker's unit has one more state: empty. A marker minted from a text
|
||||
* result ("negative", "SEE NOTE") has no unit because nothing has chosen one
|
||||
* yet, and the first numeric reading gives it its unit, the unity included
|
||||
* (`adoptFirstNumericUnit` in `./reading-conversion.ts`). Empty is therefore
|
||||
* not a second spelling of `"1"`: the unity refuses a reading in mg/dL, an
|
||||
* empty unit takes it. Both hold no number in a unit, so the editor shows
|
||||
* both as "no unit" (`markerHasNoUnit`), and the unit lock, which guards the
|
||||
* numbers stored under a unit, never fires on either while they hold none.
|
||||
*
|
||||
* Pure and dependency-free.
|
||||
*/
|
||||
|
||||
@@ -23,6 +32,17 @@ export function isDimensionlessUnit(unit: string | null | undefined): boolean {
|
||||
return typeof unit === "string" && unit.trim() === DIMENSIONLESS_UNIT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a marker's unit reads as "no unit" in the editor: the unity, or the
|
||||
* empty unit of a marker no number has given a unit yet.
|
||||
*/
|
||||
export function markerHasNoUnit(unit: string | null | undefined): boolean {
|
||||
return (
|
||||
typeof unit === "string" &&
|
||||
(unit.trim() === "" || unit.trim() === DIMENSIONLESS_UNIT)
|
||||
);
|
||||
}
|
||||
|
||||
/** The unit as it reads beside a value: trimmed, and empty for the unity. */
|
||||
export function labUnitText(unit: string | null | undefined): string {
|
||||
if (typeof unit !== "string") return "";
|
||||
@@ -54,3 +74,6 @@ export function labUnitLabel(
|
||||
/** How the API contract describes a lab unit field. */
|
||||
export const LAB_UNIT_DESCRIPTION =
|
||||
'The unit the value is in, free-form as the report prints it (e.g. `mg/dL`). `"1"`, the UCUM unity, means the value has no unit: a ratio or an index such as INR or cholesterol/HDL. It is an explicit choice and a unit of its own: it matches only `"1"`, so a reading without a unit is refused under a marker kept in a real unit and the other way round. Clients show nothing beside such a value. An empty unit on a numeric value is never read as `"1"` and stays refused.';
|
||||
|
||||
/** How the API contract describes a marker's stored unit. */
|
||||
export const BIOMARKER_UNIT_DESCRIPTION = `${LAB_UNIT_DESCRIPTION} On a marker, an empty unit means none has been chosen yet: the marker was minted from a text result, and its first numeric reading gives it that reading's unit. It is not the unity: it holds no number, so a \`PUT\` may set any unit on it.`;
|
||||
@@ -14,7 +14,7 @@
|
||||
import type { ZodOpenApiObject } from "zod-openapi";
|
||||
import { z } from "zod/v4";
|
||||
|
||||
import { LAB_UNIT_DESCRIPTION } from "@/lib/labs/dimensionless";
|
||||
import { BIOMARKER_UNIT_DESCRIPTION } from "@/lib/labs/dimensionless";
|
||||
import { dateKeySchema } from "@/lib/day/date-key-schema";
|
||||
import {
|
||||
labSeriesQuerySchema,
|
||||
@@ -62,7 +62,7 @@ const biomarkerRow = z
|
||||
.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
unit: z.string().describe(LAB_UNIT_DESCRIPTION),
|
||||
unit: z.string().describe(BIOMARKER_UNIT_DESCRIPTION),
|
||||
lowerBound: z.number().nullable(),
|
||||
upperBound: z.number().nullable(),
|
||||
panel: z.string().nullable(),
|
||||
@@ -318,7 +318,7 @@ export const biomarkerPaths: NonNullable<ZodOpenApiObject["paths"]> = {
|
||||
tags: ["Labs"],
|
||||
summary: "Edit a biomarker",
|
||||
description:
|
||||
"Partial edit; omitted fields are untouched, an explicit null clears `context` / `panel` / a bound. Since v1.43 `aliases` (0..50, each 1..120 characters) replaces the marker's set of other spellings: listed spellings stay or are added, unlisted ones are removed; omitting the field leaves them as they are. Audits as `biomarker.update`. Since v1.40.0 a `unit` different from the stored one is refused with 422 `biomarkers.unit.locked` while any reading is linked to the marker (`meta.readingCount` carries how many): readers label every linked reading with the marker's unit, so a change would relabel stored numbers without converting them. Sending the current unit, or changing it on a marker with no readings, is accepted.",
|
||||
"Partial edit; omitted fields are untouched, an explicit null clears `context` / `panel` / a bound. Since v1.43 `aliases` (0..50, each 1..120 characters) replaces the marker's set of other spellings: listed spellings stay or are added, unlisted ones are removed; omitting the field leaves them as they are. Audits as `biomarker.update`. Since v1.40.0 a `unit` different from the stored one is refused with 422 `biomarkers.unit.locked` while a numeric reading is linked to the marker, a deleted one included (`meta.readingCount` carries how many): readers label every linked number with the marker's unit, so a change would relabel stored numbers without converting them. Sending the current unit or another spelling of it (`mg/dl` for `mg/dL`), or changing it on a marker whose readings are all text or that has none, is accepted. A marker minted from a text result has an empty unit, so any unit may be set on it.",
|
||||
requestParams: { path: z.object({ id: z.string() }) },
|
||||
requestBody: {
|
||||
required: true,
|
||||
|
||||
Reference in new issue
Block a user