fix(fhir): code the mean glucose only when it is a continuous-stream mean

97507-8 is the mean of interstitial glucose in mass/volume. A spot-reading
mean is neither, so it now goes out text-only in the account's unit. A
continuous-stream mean keeps the code and is sent in mg/dL, the unit it is
computed in, whatever unit the account displays.
This commit is contained in:
Marc-André Bombeck committed 2026-09-26 13:55:41 +02:00
1 parent 0ac5377cc2
commit 0a58edc87a
2 files changed
+72 -10

No files matched your search

@@ -131,6 +131,55 @@ describe("clinical glucose-panel FHIR Observations", () => {
expect(tir!.valueQuantity?.value).toBeLessThanOrEqual(100);
});
it("codes a continuous-stream mean as 97507-8 in mg/dL, even for a mmol/L account", () => {
const glucoseClinical = computeGlucoseClinicalMetrics(denseReadings(), {
now: FIXED_NOW,
windowDays: 90,
});
expect(glucoseClinical.isSpotEstimate).toBe(false);
const bundle = buildFhirDocumentBundle(
makeData({ glucoseClinical, glucoseUnit: "mmol/L" }),
{ insuranceNumber: null },
FIXED_NOW,
);
const mean = observationsOf(bundle).find(
(o) => o.code.text === "Mean glucose",
);
expect(codeOf(mean!)).toBe(GLUCOSE_MEAN_LOINC);
expect(mean?.valueQuantity?.code).toBe("mg/dL");
expect(mean?.valueQuantity?.value).toBe(
Math.round(glucoseClinical.meanMgdl!),
);
});
it("sends a spot-reading mean text-only, in the account's unit", () => {
// Four readings a day: fingerstick cadence, not a continuous stream.
const sparse = Array.from({ length: 120 }, (_, i) => ({
measuredAt: new Date(FIXED_NOW.getTime() - i * 6 * 3_600_000),
mgdl: 100 + (i % 5) * 10,
}));
const glucoseClinical = computeGlucoseClinicalMetrics(sparse, {
now: FIXED_NOW,
windowDays: 90,
});
expect(glucoseClinical.isSpotEstimate).toBe(true);
expect(glucoseClinical.meanMgdl).not.toBeNull();
const bundle = buildFhirDocumentBundle(
makeData({ glucoseClinical, glucoseUnit: "mmol/L" }),
{ insuranceNumber: null },
FIXED_NOW,
);
const mean = observationsOf(bundle).find(
(o) => o.code.text === "Mean glucose",
);
expect(mean).toBeDefined();
expect(mean?.code.coding).toBeUndefined();
expect(mean?.valueQuantity?.unit).toBe("mmol/L");
expect(observationsOf(bundle).map(codeOf)).not.toContain(
GLUCOSE_MEAN_LOINC,
);
});
it("emits NO clinical glucose Observation when the panel has no readings (module off / no data)", () => {
// Default makeData() has an empty (zero-reading) panel.
const bundle = buildFhirDocumentBundle(
+23 -10
View File
@@ -143,16 +143,29 @@ export function glucoseClinicalObservations(
});
}
if (clinical.meanMgdl !== null) {
push({
loinc: {
code: GLUCOSE_MEAN_LOINC,
display:
"Average glucose [Mass/volume] in Interstitial fluid during Reporting Period",
},
text: "Mean glucose",
value: convertGlucose(clinical.meanMgdl, glucoseUnit),
unit: glucoseUnit,
});
// 97507-8 is the mean of interstitial glucose in mass/volume. Only a
// continuous stream (not a spot estimate, by reading density) is
// interstitial, so only that mean carries the code, and it goes out in
// mg/dL: the canonical unit the mean is computed in, so no conversion is
// involved. A spot-reading mean stays text-only in the display unit.
if (clinical.isSpotEstimate) {
push({
text: "Mean glucose",
value: convertGlucose(clinical.meanMgdl, glucoseUnit),
unit: glucoseUnit,
});
} else {
push({
loinc: {
code: GLUCOSE_MEAN_LOINC,
display:
"Average glucose [Mass/volume] in Interstitial fluid during Reporting Period",
},
text: "Mean glucose",
value: convertGlucose(clinical.meanMgdl, "mg/dL"),
unit: "mg/dL",
});
}
}
if (clinical.gmi !== null) {
push({