fix(clinician): print every shared statistic with its unit

The clinician view printed bare numbers for every measurement group and
the per-context glucose rows in raw mg/dL, so a recipient read glucose
without a unit and in the wrong one for an account on mmol/L. The PDF's
measurement table had the same gap for the raw glucose series.

Both artefacts now take the unit and the displayed number from one helper
(doctor-report/stat-display.ts): SI for mass, length and temperature as
before, hours for sleep, and the record owner's unit for glucose.

Refs #1067
This commit is contained in:
Marc-André Bombeck committed 2026-10-01 08:46:39 +02:00
1 parent 1b694ec222
commit d00e106ce1
13 files changed
+207 -41

No files matched your search

@@ -227,6 +227,30 @@ describe("renderDoctorReportPdfBytes", () => {
expect(text).not.toContain("450 h");
});
it("prints the raw glucose row in the owner's unit, like the glucose panel", async () => {
// The measurement table used to take BLOOD_GLUCOSE's canonical unit and
// number, so an mmol/L account read "99.0 mg/dL" two lines above its own
// context rows in mmol/L.
const data = makeData({
glucoseUnit: "mmol/L",
stats: {
...makeData().stats,
BLOOD_GLUCOSE: { avg: 108, min: 90, max: 126, count: 9, latest: 99 },
},
});
const bytes = renderDoctorReportPdfBytes(data, {
timeFormat: "AUTO",
dateFormat: "AUTO",
t: getServerTranslator("en").t,
locale: "en",
now: FIXED_NOW,
});
const text = await extractText(bytes);
expect(text).toContain("5.5 mmol/L");
expect(text).toContain("6.0 mmol/L");
expect(text).not.toContain("mg/dL");
});
it("omits the sleep vitals row when there are no sleep stats", async () => {
const bytes = renderDoctorReportPdfBytes(makeData(), {
timeFormat: "AUTO",
+11 -25
View File
@@ -19,7 +19,11 @@ import {
REPORT_GROUPS,
} from "./report-selection/catalogue";
import { getUnitForType } from "./validations/measurement";
import {
DOCTOR_REPORT_TYPE_UNIT_KEYS,
reportStatUnit,
} from "./doctor-report/stat-display";
import { resolveGlucoseUnit } from "./glucose";
import {
makeFormatters,
type DateFormatPreference,
@@ -175,18 +179,9 @@ export interface DoctorReportRenderOptions {
includeCharts?: boolean;
}
export const DOCTOR_REPORT_TYPE_UNIT_KEYS: Record<string, string | null> = {
WEIGHT: "kg",
BLOOD_PRESSURE_SYS: "mmHg",
BLOOD_PRESSURE_DIA: "mmHg",
PULSE: "bpm",
BODY_FAT: "%",
SLEEP_DURATION: "h",
ACTIVITY_STEPS: null, // translated unit
TOTAL_BODY_WATER: "kg",
BONE_MASS: "kg",
OXYGEN_SATURATION: "%",
};
// The unit map moved beside the clinician view's reader of it; re-exported so
// the enum-coverage test keeps one import.
export { DOCTOR_REPORT_TYPE_UNIT_KEYS };
/**
* The measurement table is driven by the selection, not by a whitelist.
@@ -238,18 +233,9 @@ export function buildDoctorReportPdfDocument(
const footerTz =
userTz && isValidTimezone(userTz) ? userTz : DEFAULT_TIMEZONE;
const unitFor = (type: string): string => {
const staticUnit = DOCTOR_REPORT_TYPE_UNIT_KEYS[type];
if (staticUnit === null && type === "ACTIVITY_STEPS") {
return t("doctorReport.unitSteps");
}
if (staticUnit !== undefined && staticUnit !== null) return staticUnit;
// Every other type falls back to the canonical unit the measurement
// validation layer already records. A type with no recorded unit prints
// none — an absent unit is absent, not a guessed symbol.
const canonical = getUnitForType(type);
return canonical === "unknown" ? "" : canonical;
};
const glucoseUnit = resolveGlucoseUnit(data.glucoseUnit ?? null);
const unitFor = (type: string): string =>
reportStatUnit(type, glucoseUnit, t);
const doc = new jsPDF({ orientation: "portrait", unit: "mm", format: "a4" });
patchPdfTextSanitiser(doc);
@@ -2,6 +2,7 @@ import type { jsPDF } from "jspdf";
import { GLUCOSE_CONTEXT_BUCKETS } from "@/lib/glucose";
import autoTable from "jspdf-autotable";
import { convertGlucose, resolveGlucoseUnit } from "../glucose";
import { reportStatValue } from "../doctor-report/stat-display";
import type { MeasurementType } from "@/generated/prisma/client";
import { MEASUREMENT_TYPE_LABEL_KEYS } from "../measurements/type-label-keys";
import {
@@ -235,12 +236,9 @@ export function buildMeasurementsChartsSection(
const s = data.stats[type];
if (!s) continue;
const unit = unitFor(type);
// SLEEP_DURATION stats are per-night asleep totals in MINUTES (the data
// layer reconstructs them for exactly this row); the vitals unit is
// hours (`h`), so convert to keep value and unit consistent — the same
// hours value the FHIR sleep Observation emits.
const conv =
type === "SLEEP_DURATION" ? (v: number) => v / 60 : (v: number) => v;
// Sleep minutes print as hours and glucose in the owner's unit; the
// clinician view reads the same helper, so the two cannot drift.
const conv = (v: number) => reportStatValue(type, v, glucoseUnit);
groupRows.push([
t(MEASUREMENT_TYPE_LABEL_KEYS[type as MeasurementType] ?? ""),
`${num(conv(s.latest))} ${unit}`.trim(),
+79
View File
@@ -0,0 +1,79 @@
/**
* How a measurement statistic reads in the clinician-facing artefacts: the
* PDF report and the shared clinician view.
*
* Both describe the same record to the same reader, so they take the unit and
* the displayed number from here and nowhere else. Before this module the PDF
* carried its own unit map and the clinician view printed bare numbers, so a
* doctor opening a share link read "latest 5.8 (avg 6.1, range 4.9–7.4)" with
* no unit at all, and for an account on mmol/L the glucose rows of the PDF's
* measurement table still printed raw mg/dL beside a mmol/L glucose panel.
*
* The policy, which `display-transform.ts` states for the metric/imperial
* preference: a document handed to a clinician stays SI for mass, length and
* temperature regardless of the owner's imperial preference. Glucose is the
* exception, because mmol/L versus mg/dL is not an imperial question — it is
* the clinical convention of the country the record lives in — so glucose
* follows the owner's `glucoseUnit` here as it does everywhere a person reads
* it.
*/
import { convertGlucose, type GlucoseUnit } from "@/lib/glucose";
import { getUnitForType } from "@/lib/validations/measurement";
/**
* Report-specific unit symbols, where the report's own reading differs from
* the validation layer's canonical unit. `null` marks a unit that has to be
* translated (steps). Everything absent falls back to `getUnitForType`.
*/
export const DOCTOR_REPORT_TYPE_UNIT_KEYS: Record<string, string | null> = {
WEIGHT: "kg",
BLOOD_PRESSURE_SYS: "mmHg",
BLOOD_PRESSURE_DIA: "mmHg",
PULSE: "bpm",
BODY_FAT: "%",
SLEEP_DURATION: "h",
ACTIVITY_STEPS: null, // translated unit
TOTAL_BODY_WATER: "kg",
BONE_MASS: "kg",
OXYGEN_SATURATION: "%",
};
/**
* The unit symbol a report statistic of `type` is printed with.
*
* `translate` resolves the one translated unit (steps). A type with no
* recorded unit prints none — an absent unit is absent, not a guessed symbol.
*/
export function reportStatUnit(
type: string,
glucoseUnit: GlucoseUnit,
translate: (key: string) => string,
): string {
if (type === "BLOOD_GLUCOSE") return glucoseUnit;
const staticUnit = DOCTOR_REPORT_TYPE_UNIT_KEYS[type];
if (staticUnit === null && type === "ACTIVITY_STEPS") {
return translate("doctorReport.unitSteps");
}
if (staticUnit !== undefined && staticUnit !== null) return staticUnit;
const canonical = getUnitForType(type);
return canonical === "unknown" ? "" : canonical;
}
/**
* The number a report statistic of `type` is printed as, in the unit
* {@link reportStatUnit} names.
*
* SLEEP_DURATION statistics are per-night asleep totals in minutes (the data
* layer reconstructs them for exactly this row) while the unit is hours, so
* they are divided — the same hours value the FHIR sleep Observation emits.
* Glucose is stored in mg/dL and converted to the owner's unit.
*/
export function reportStatValue(
type: string,
value: number,
glucoseUnit: GlucoseUnit,
): number {
if (type === "SLEEP_DURATION") return value / 60;
if (type === "BLOOD_GLUCOSE") return convertGlucose(value, glucoseUnit);
return value;
}