Files
HealthLog/src/lib/analytics/compliance.ts
T
Marc-André Bombeck d978c2b662 refactor(compliance): split the god-module into focused units behind a barrel
The medication-compliance engine lived in one 2,099-line file with 38
exports feeding eight surfaces (Coach prompt, BP-status gate, dashboard
pillar, per-medication tile, insight targets, doctor report, …). A single
edit intended for one surface could silently shift all of them.

Split along the existing internal seams into `analytics/compliance/`:
parsing, types, adapters, dose-status, occurrences, windows, cycle,
ledger, display. `analytics/compliance.ts` is now a thin barrel that
re-exports every public symbol, so no consumer import path changes. Pure
move — no logic edits; the tally, window ladder, ledger and cycle math
are byte-for-byte the code they were. Tests move alongside under
`compliance/__tests__/` and keep exercising the public barrel.
2026-07-18 02:09:33 +02:00

49 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* Medication compliance calculations.
*
* v1.5.0 — `calculateCompliance` is now a cadence-aware adapter on top
* of `complianceChips` / `buildCadenceTimeline`. Prior to this release
* the helper computed `totalExpected = schedules.length * days`, which
* silently ignored `MedicationSchedule.daysOfWeek` and `intervalWeeks`.
* A weekly Ozempic schedule (Mondays only) reported ~13% adherence
* instead of 100%; a weekday-only 3×/day metformin reported 73%
* instead of 100%. The wire shape (`{ totalExpected, taken, skipped,
* missed, rate, streak }`) is unchanged so every consumer (Health
* Score, AI Coach prompt context, /api/medications/[id]/compliance,
* BP-status compliance gate, insight targets, the per-medication
* tile) keeps reading the same fields. Only the math underneath the
* fields was wrong — that's what got fixed. Closes #214.
*
* `classifyIntakeTiming` is unchanged and still owns the early /
* on_time / late / very_late punctuality bucket logic used by the
* daily compliance heatmap on `/api/medications/[id]/compliance`.
*
* ---
*
* This file is a thin barrel. The implementation lives in focused modules
* under `./compliance/` — split along the original internal seams so the
* most-changed business path is editable one concern at a time:
*
* - `compliance/parsing.ts` HH:mm parsing + punctuality classification
* - `compliance/types.ts` shared shapes, selects, context builders
* - `compliance/adapters.ts` canonical-engine adapters (internal)
* - `compliance/dose-status.ts` per-dose window model + cadence family
* - `compliance/occurrences.ts` expected-slot expansion + counting
* - `compliance/windows.ts` the compliance-window ladder + selection
* - `compliance/cycle.ts` open-cycle (current dose) descriptor
* - `compliance/ledger.ts` dose-history ledger tally + per-day rates
* - `compliance/display.ts` two-row display, calculateCompliance, bundle
*
* Every symbol the eight compliance call sites import keeps resolving from
* `@/lib/analytics/compliance` — no consumer import path changed.
*/
export * from "./compliance/parsing";
export * from "./compliance/types";
export * from "./compliance/dose-status";
export * from "./compliance/occurrences";
export * from "./compliance/windows";
export * from "./compliance/cycle";
export * from "./compliance/ledger";
export * from "./compliance/display";