From 0a58edc87a2615d3724c22a4571283de16d83151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Sat, 26 Sep 2026 13:55:41 +0200 Subject: [PATCH] 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. --- .../glucose-clinical-observations.test.ts | 49 +++++++++++++++++++ src/lib/fhir/resources/glucose.ts | 33 +++++++++---- 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/src/lib/fhir/__tests__/glucose-clinical-observations.test.ts b/src/lib/fhir/__tests__/glucose-clinical-observations.test.ts index 1274631e9..8562be554 100644 --- a/src/lib/fhir/__tests__/glucose-clinical-observations.test.ts +++ b/src/lib/fhir/__tests__/glucose-clinical-observations.test.ts @@ -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( diff --git a/src/lib/fhir/resources/glucose.ts b/src/lib/fhir/resources/glucose.ts index 0ec140729..5f5c3aaa2 100644 --- a/src/lib/fhir/resources/glucose.ts +++ b/src/lib/fhir/resources/glucose.ts @@ -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({