mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 16:12:11 +00:00
fix(illness): glucose safety-floor push honours the user's display unit
The hypoglycemia/hyperglycemia escalation push hardcoded mg/dL in all six
locale strings and interpolated the raw mg/dL reading regardless of the
user's glucose display preference. A mmol/L user hitting the confirmed-hypo
alarm read "70 mg/dL (65 mg/dL)" — the wrong unit at the single most
safety-critical moment.
Resolve User.glucoseUnit in the safety-floor write hook and convert both the
reading and the floor threshold before handing them to the notification
copy. The bundle strings now take {threshold} {unit} / {value} {unit}
instead of a baked-in "70 mg/dL" / "54 mg/dL".
This commit is contained in:
1 parent
a08e4378a0
commit
0518c39050
9 files changed
+97
-44
No files matched your search
@@ -81,7 +81,17 @@ describe("notifySafetyFloor", () => {
|
||||
const opts = dispatchMock.mock.calls[0][0];
|
||||
expect(opts.messageKey).toBe("safety.floor.glucoseLowSevereEmergency");
|
||||
expect(opts.titleKey).toBe("safety.floor.glucoseLowSevereTitle");
|
||||
expect(opts.params).toEqual({ value: 48 });
|
||||
expect(opts.params).toEqual({ value: 48, unit: "mg/dL", threshold: 54 });
|
||||
});
|
||||
|
||||
it("converts the glucose value + threshold to the caller's display unit", async () => {
|
||||
await notifySafetyFloor({
|
||||
userId: "u1",
|
||||
decision: glucoseLowSymptomatic,
|
||||
glucoseUnit: "mmol/L",
|
||||
});
|
||||
const opts = dispatchMock.mock.calls[0][0];
|
||||
expect(opts.params).toEqual({ value: 2.7, unit: "mmol/L", threshold: 3 });
|
||||
});
|
||||
|
||||
it("de-dupes by reason when a recent ledger row exists", async () => {
|
||||
|
||||
@@ -19,6 +19,7 @@ import type { MeasurementType } from "@/generated/prisma/client";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { getEvent } from "@/lib/logging/context";
|
||||
import { isModuleEnabled } from "@/lib/modules/gate";
|
||||
import { resolveGlucoseUnit } from "@/lib/glucose";
|
||||
import { FEVER_RED_FLAG_C, SPO2_RED_FLAG_PCT } from "@/lib/clinical-floors";
|
||||
import {
|
||||
evaluateBloodPressure,
|
||||
@@ -194,7 +195,17 @@ async function checkGlucose(
|
||||
mgdl: r.value,
|
||||
}));
|
||||
const decision = evaluateGlucose({ candidate, recent, symptomCoupled });
|
||||
await notifySafetyFloor({ userId, decision });
|
||||
if (!decision) return;
|
||||
|
||||
// Resolve the user's display unit only when there's actually a breach to
|
||||
// report — the escalation push must speak the user's own unit (mmol/L
|
||||
// users get "3.9 mmol/L", not a raw mg/dL figure mid-hypo).
|
||||
const profile = await prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: { glucoseUnit: true },
|
||||
});
|
||||
const glucoseUnit = resolveGlucoseUnit(profile?.glucoseUnit ?? null);
|
||||
await notifySafetyFloor({ userId, decision, glucoseUnit });
|
||||
}
|
||||
|
||||
/* ── sustained fever / low-SpO2 (multi-day calendar runs) ─────────────── */
|
||||
|
||||
@@ -20,9 +20,12 @@
|
||||
import { prisma } from "@/lib/db";
|
||||
import { getEvent } from "@/lib/logging/context";
|
||||
import { dispatchLocalisedNotification } from "@/lib/notifications/dispatch-localised";
|
||||
import type {
|
||||
SafetyFloorDecision,
|
||||
SafetyFloorReason,
|
||||
import { convertGlucose, type GlucoseUnit } from "@/lib/glucose";
|
||||
import {
|
||||
GLUCOSE_HYPO,
|
||||
GLUCOSE_HYPO_SEVERE,
|
||||
type SafetyFloorDecision,
|
||||
type SafetyFloorReason,
|
||||
} from "@/lib/illness/safety-floors";
|
||||
|
||||
/** One escalation per (user, reason) per this window. */
|
||||
@@ -55,9 +58,20 @@ function copyKeysFor(decision: SafetyFloorDecision): {
|
||||
};
|
||||
}
|
||||
|
||||
/** Body params — the re-confirmed reading value(s) echoed into the copy. */
|
||||
/**
|
||||
* Body params — the re-confirmed reading value(s) echoed into the copy.
|
||||
*
|
||||
* Glucose is stored canonically in mg/dL (`decision.value`), but the escalation
|
||||
* push must speak the user's own display unit — a mmol/L user reading "70
|
||||
* mg/dL" mid-hypo is the wrong unit at the single most safety-critical moment.
|
||||
* `glucoseUnit` is resolved by the caller (`safety-floor-check.ts`) from
|
||||
* `User.glucoseUnit`; every glucose copy key takes `{value} {unit}` and the
|
||||
* hypo variants also take `{threshold} {unit}` for the "below X" clause —
|
||||
* the literal "70 mg/dL" / "54 mg/dL" text no longer lives in the bundle.
|
||||
*/
|
||||
function paramsFor(
|
||||
decision: SafetyFloorDecision,
|
||||
glucoseUnit: GlucoseUnit,
|
||||
): Record<string, string | number> {
|
||||
if (decision.kind === "BLOOD_PRESSURE") {
|
||||
return {
|
||||
@@ -65,17 +79,35 @@ function paramsFor(
|
||||
diastolic: decision.diastolic ?? 0,
|
||||
};
|
||||
}
|
||||
return { value: decision.value };
|
||||
const params: Record<string, string | number> = {
|
||||
value: convertGlucose(decision.value, glucoseUnit),
|
||||
unit: glucoseUnit,
|
||||
};
|
||||
if (
|
||||
decision.reason === "glucose_hypo" ||
|
||||
decision.reason === "glucose_hypo_severe"
|
||||
) {
|
||||
const thresholdMgdl =
|
||||
decision.reason === "glucose_hypo_severe"
|
||||
? GLUCOSE_HYPO_SEVERE
|
||||
: GLUCOSE_HYPO;
|
||||
params.threshold = convertGlucose(thresholdMgdl, glucoseUnit);
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Emit an urgent escalation push for a confirmed safety-floor breach, at most
|
||||
* once per (user, reason) per `DEDUPE_WINDOW_MS`. No-op when `decision` is
|
||||
* null. Never throws.
|
||||
*
|
||||
* `glucoseUnit` is only consulted for `GLUCOSE` decisions; defaults to
|
||||
* canonical mg/dL when the caller omits it (BP-only callers never need it).
|
||||
*/
|
||||
export async function notifySafetyFloor(input: {
|
||||
userId: string;
|
||||
decision: SafetyFloorDecision | null;
|
||||
glucoseUnit?: GlucoseUnit;
|
||||
}): Promise<void> {
|
||||
const { userId, decision } = input;
|
||||
if (!decision) return;
|
||||
@@ -107,7 +139,7 @@ export async function notifySafetyFloor(input: {
|
||||
userId,
|
||||
titleKey,
|
||||
messageKey,
|
||||
params: paramsFor(decision),
|
||||
params: paramsFor(decision, input.glucoseUnit ?? "mg/dL"),
|
||||
eventType: "SYSTEM_ALERT",
|
||||
urgent: true,
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user