mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-15 23:12:04 +00:00
fix(medications): fan out the today-tile projector over every time-of-day
The today-window projector minted a single pending intake row per schedule, anchored at `windowStart`, dropping a schedule's first-class `timesOfDay`. A twice-daily medication stored as one schedule row with `timesOfDay = ["07:00","19:00"]` therefore surfaced only its morning dose in the today tile and the "Erfassen" sheet, and the event-count compliance rollup recorded one expected dose per day instead of two — a perfectly adherent user read 50% the moment the second dose was logged, and deleting then re-adding one dose left the rate stuck at 50%. Fan the projection out over every `timesOfDay` entry, mirroring the reminder worker's per-slot mint and dedup against the `(userId, medicationId, scheduledFor, source)` unique index. Absent first-class `timesOfDay` the projector still emits one slot at `windowStart`, byte-stable against the worker's legacy single-window row. Adds regression coverage across the projector (dashboard summary), the legacy compliance walker, and the cadence expansion pinning the single-row twice-daily case at the correct two-dose-per-day shape.
This commit is contained in:
1 parent
869f13612a
commit
fcb517df87
4 files changed
+206
-6
No files matched your search
@@ -623,6 +623,68 @@ describe("GET /api/dashboard/summary", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it("projects one row per timesOfDay for a twice-daily schedule", async () => {
|
||||
// A single MedicationSchedule row carrying two first-class
|
||||
// `timesOfDay` is two distinct dose slots per day. Pre-fix the
|
||||
// projector minted only the `windowStart` slot, so the second daily
|
||||
// dose never appeared in the today-tile and the event-count
|
||||
// compliance rollup read half the expected doses (a 2×/day med
|
||||
// showed 50%). Assert the backfill mints both slots.
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(new Date("2026-05-21T03:00:00.000Z"));
|
||||
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
|
||||
vi.mocked(prisma.medication.findMany).mockResolvedValue([
|
||||
{
|
||||
id: "med-twice-daily",
|
||||
startsOn: null,
|
||||
endsOn: null,
|
||||
oneShot: false,
|
||||
createdAt: new Date("2026-01-01T00:00:00Z"),
|
||||
schedules: [
|
||||
{
|
||||
id: "sched-bid",
|
||||
medicationId: "med-twice-daily",
|
||||
windowStart: "07:00",
|
||||
windowEnd: "07:30",
|
||||
daysOfWeek: null,
|
||||
timesOfDay: ["07:00", "19:00"],
|
||||
reminderGraceMinutes: null,
|
||||
rrule: null,
|
||||
rollingIntervalDays: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
] as never);
|
||||
vi.mocked(prisma.medicationIntakeEvent.findMany).mockImplementation(((
|
||||
args: unknown,
|
||||
) => {
|
||||
const a = args as { where: { OR?: unknown }; select?: { id?: boolean } };
|
||||
if (a.where.OR) return Promise.resolve([]) as never;
|
||||
if (!a.select?.id) return Promise.resolve([]) as never;
|
||||
return Promise.resolve([]) as never;
|
||||
}) as never);
|
||||
vi.mocked(prisma.medicationIntakeEvent.createMany).mockResolvedValue({
|
||||
count: 2,
|
||||
} as never);
|
||||
|
||||
const res = await callGet(makeReq());
|
||||
expect(res.status).toBe(200);
|
||||
expect(prisma.medicationIntakeEvent.createMany).toHaveBeenCalledTimes(1);
|
||||
const args = vi.mocked(prisma.medicationIntakeEvent.createMany).mock
|
||||
.calls[0][0] as {
|
||||
data: Array<{ medicationId: string; scheduledFor: Date }>;
|
||||
};
|
||||
expect(args.data).toHaveLength(2);
|
||||
const hours = args.data
|
||||
.map((r) => new Date(r.scheduledFor).getUTCHours())
|
||||
.sort((a, b) => a - b);
|
||||
// 07:00 and 19:00 in the default zone (Europe/Berlin, UTC+2 in May)
|
||||
// materialise to 05:00 and 17:00 UTC. The assertion that matters is
|
||||
// two distinct slots, not one.
|
||||
expect(hours).toEqual([5, 17]);
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("is idempotent on a second pass (v1.4.39 W-SERVER-FIX-2)", async () => {
|
||||
// When the existence probe already sees a row for the projected
|
||||
// slot (e.g. the intake route or the reminder worker minted it
|
||||
|
||||
Reference in new issue
Block a user