diff --git a/src/lib/insights/derived/adherence-storyline.ts b/src/lib/insights/derived/adherence-storyline.ts index 11e4c4b1d..893ff68f0 100644 --- a/src/lib/insights/derived/adherence-storyline.ts +++ b/src/lib/insights/derived/adherence-storyline.ts @@ -34,6 +34,7 @@ import { MED_TARGET_MAP, type MedTargetClass, } from "@/lib/medications/med-target-map"; +import { TRACKED_INTAKE_WHERE } from "@/lib/medications/intake-tracking"; const MS_PER_DAY = 24 * 60 * 60 * 1000; const MAD_TO_SIGMA = 1.4826; @@ -164,6 +165,9 @@ export async function buildAdherenceStoryline( where: { userId, active: true, + // v1.39.1 (#1033) — the adherence figure below counts tracked + // medications only, so a record-only one cannot be its subject. + ...TRACKED_INTAKE_WHERE, OR: [{ endsOn: null }, { endsOn: { gte: now } }], }, select: { name: true, treatmentClass: true }, diff --git a/tests/integration/medication-intake-tracking.test.ts b/tests/integration/medication-intake-tracking.test.ts index e2567c7f2..a4e46b58a 100644 --- a/tests/integration/medication-intake-tracking.test.ts +++ b/tests/integration/medication-intake-tracking.test.ts @@ -196,7 +196,9 @@ describe("intake tracking off — nothing is due", () => { const block = await buildMedsTodayBlock(prisma, userId, TZ, new Date()); expect(block.activeCount).toBe(1); - expect(block.dueCandidates.map((c) => c.medicationId)).toEqual([tracked]); + expect((block.dueCandidates ?? []).map((c) => c.medicationId)).toEqual([ + tracked, + ]); const intakeRoute = await import("@/app/api/medications/intake/route"); const today = await call>( @@ -350,7 +352,9 @@ describe("switching intake tracking back on", () => { // Today's 08:00 lies before the switch: not projected, not reminded. const block = await buildMedsTodayBlock(prisma, userId, TZ, new Date()); expect(block.scheduledToday).toBe(0); - expect(block.dueCandidates.every((c) => c.overdue === false)).toBe(true); + expect((block.dueCandidates ?? []).every((c) => c.overdue === false)).toBe( + true, + ); await handleReminderCheck([]); expect(dispatchedFor(medicationId)).toBe(0);