mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
Schedule-expansion never minted today's intake rows for medications
with `daysOfWeek: null` until the reminder worker entered the RED
phase at the end of the dose window. The database stores `null` to
mean "every day" (the `MedicationSchedule.daysOfWeek` column carries
that annotation in `prisma/schema.prisma`), so daily meds spent the
whole morning with zero scheduled rows. Endpoint
`/api/medications/intake?scope=today` returned `[]` even when two
active daily meds existed; iOS Dashboard tile + Erfassen sheet both
fell to "Heute nichts geplant" / "Keine offene Einnahme" end-to-end.
Adds `expandTodayIntakes` to `src/lib/medication-schedule.ts` — a
pure projection that re-uses the existing `parseScheduleRecurrence`
(empty array == every day) and respects explicit weekday filters
when present. The `scope=today` route projects active schedules
through the helper, then idempotently backfills any missing rows
into `MedicationIntakeEvent` so the iOS POST update path keeps the
existing intakeId contract.
Tests:
- `medication-schedule.test.ts` gains eight cases covering
`daysOfWeek: null`, `""`, `"0,1,2,3,4,5,6"`, explicit-weekday
excludes-today / includes-today, the operator's Ramipril +
Metformin fixture, DST-correct `scheduledFor` anchoring at
`windowStart` in Europe/Berlin, and multi-week cadence skip.
- `intake/route.test.ts` gains two cases: backfill mints the
missing row + does-not-double-mint when the row already exists.
Full vitest suite stays green (4672 passed / 1 skipped).
381 lines
13 KiB
TypeScript
381 lines
13 KiB
TypeScript
/**
|
|
* Top-level medication-intake aggregator + writer.
|
|
*
|
|
* GET /api/medications/intake?scope=today
|
|
* → array of today's intake events for the user (across medications).
|
|
*
|
|
* GET /api/medications/intake?scope=compliance&days=N
|
|
* → per-day { date, scheduled, taken } for the last N days.
|
|
*
|
|
* POST /api/medications/intake
|
|
* Body: { intakeId, status: "taken" | "skipped" | "snoozed", takenAt?, snoozedUntil? }
|
|
* Updates the named MedicationIntakeEvent and returns the updated row.
|
|
*/
|
|
import { NextRequest } from "next/server";
|
|
import { z } from "zod/v4";
|
|
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
|
import {
|
|
apiError,
|
|
apiSuccess,
|
|
getClientIp,
|
|
safeJson,
|
|
} from "@/lib/api-response";
|
|
import { annotate } from "@/lib/logging/context";
|
|
import { prisma } from "@/lib/db";
|
|
import { auditLog } from "@/lib/auth/audit";
|
|
import { userDayKey, DEFAULT_TIMEZONE } from "@/lib/tz/resolver";
|
|
import { cached, caches, type ServerCache } from "@/lib/cache/server-cache";
|
|
import { invalidateUserMedications } from "@/lib/cache/invalidate";
|
|
import {
|
|
readMedicationCompliance,
|
|
hasMedicationComplianceCoverage,
|
|
recomputeMedicationComplianceForEvent,
|
|
enqueueUserMedicationComplianceBackfill,
|
|
type ComplianceBucket as RollupComplianceBucket,
|
|
} from "@/lib/medications/compliance-rollups";
|
|
import { expandTodayIntakes } from "@/lib/medication-schedule";
|
|
|
|
const querySchema = z.object({
|
|
scope: z.enum(["today", "compliance"]),
|
|
days: z.coerce.number().int().min(1).max(365).optional().default(30),
|
|
});
|
|
|
|
const updateSchema = z.object({
|
|
intakeId: z.string().min(1),
|
|
status: z.enum(["taken", "skipped", "snoozed"]),
|
|
takenAt: z.iso
|
|
.datetime({ offset: true })
|
|
.transform((s) => new Date(s))
|
|
.optional(),
|
|
snoozedUntil: z.iso
|
|
.datetime({ offset: true })
|
|
.transform((s) => new Date(s))
|
|
.optional(),
|
|
});
|
|
|
|
function startOfDayInTz(date: Date, tz: string): Date {
|
|
const parts = new Intl.DateTimeFormat("en-US", {
|
|
timeZone: tz,
|
|
year: "numeric",
|
|
month: "2-digit",
|
|
day: "2-digit",
|
|
}).formatToParts(date);
|
|
const y = parts.find((p) => p.type === "year")?.value ?? "1970";
|
|
const m = parts.find((p) => p.type === "month")?.value ?? "01";
|
|
const d = parts.find((p) => p.type === "day")?.value ?? "01";
|
|
return new Date(`${y}-${m}-${d}T00:00:00.000Z`);
|
|
}
|
|
|
|
export const GET = apiHandler(async (request: NextRequest) => {
|
|
const { user } = await requireAuth();
|
|
|
|
const parsed = querySchema.safeParse(
|
|
Object.fromEntries(request.nextUrl.searchParams),
|
|
);
|
|
if (!parsed.success) {
|
|
return apiError(parsed.error.issues[0].message, 422);
|
|
}
|
|
|
|
const { scope, days } = parsed.data;
|
|
|
|
// v1.4.25 W7b — anchor "today" and per-day compliance buckets to the
|
|
// user's display timezone so a 23:30 reading in Pacific/Auckland lands
|
|
// in today's bucket rather than yesterday's Berlin one.
|
|
const userTz = user.timezone ?? DEFAULT_TIMEZONE;
|
|
|
|
if (scope === "today") {
|
|
const todayStart = startOfDayInTz(new Date(), userTz);
|
|
const todayEnd = new Date(todayStart.getTime() + 86_400_000);
|
|
|
|
// v1.4.39 W-SERVER-FIX — project pending intake rows for every
|
|
// active schedule whose window opens today, then idempotently
|
|
// backfill any missing rows. Pre-fix the endpoint returned `[]`
|
|
// for daily meds (`schedule.daysOfWeek = null` in the DB) until
|
|
// the reminder worker entered the RED phase at the end of the
|
|
// dose window — leaving the iOS Dashboard tile + the "Erfassen"
|
|
// sheet empty for the whole morning. The projection re-uses the
|
|
// canonical `parseScheduleRecurrence` (null/empty = every day)
|
|
// so the daysOfWeek semantics stay aligned across every reader.
|
|
const activeMedications = await prisma.medication.findMany({
|
|
where: { userId: user.id, active: true },
|
|
select: {
|
|
id: true,
|
|
schedules: {
|
|
select: {
|
|
id: true,
|
|
medicationId: true,
|
|
windowStart: true,
|
|
windowEnd: true,
|
|
daysOfWeek: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
const projected = expandTodayIntakes(
|
|
activeMedications.flatMap((m) => m.schedules),
|
|
new Date(),
|
|
userTz,
|
|
);
|
|
|
|
const existing = await prisma.medicationIntakeEvent.findMany({
|
|
where: {
|
|
userId: user.id,
|
|
scheduledFor: { gte: todayStart, lt: todayEnd },
|
|
},
|
|
select: { medicationId: true, scheduledFor: true },
|
|
});
|
|
const existingKey = new Set(
|
|
existing.map((e) => `${e.medicationId}|${e.scheduledFor.toISOString()}`),
|
|
);
|
|
const missing = projected.filter(
|
|
(p) =>
|
|
!existingKey.has(`${p.medicationId}|${p.scheduledFor.toISOString()}`),
|
|
);
|
|
if (missing.length > 0) {
|
|
await prisma.medicationIntakeEvent.createMany({
|
|
data: missing.map((m) => ({
|
|
userId: user.id,
|
|
medicationId: m.medicationId,
|
|
scheduledFor: m.scheduledFor,
|
|
takenAt: null,
|
|
skipped: false,
|
|
// `REMINDER` is the same source the reminder worker uses to
|
|
// mint RED-phase rows — semantically "the server projected
|
|
// this slot before the user logged anything". The IntakeSource
|
|
// enum has no separate `SCHEDULER` literal; reusing REMINDER
|
|
// keeps the doctor-report + analytics filters byte-stable.
|
|
source: "REMINDER",
|
|
})),
|
|
});
|
|
}
|
|
|
|
const events = await prisma.medicationIntakeEvent.findMany({
|
|
where: {
|
|
userId: user.id,
|
|
scheduledFor: { gte: todayStart, lt: todayEnd },
|
|
},
|
|
orderBy: { scheduledFor: "asc" },
|
|
include: { medication: { select: { id: true, snoozedUntil: true } } },
|
|
});
|
|
|
|
annotate({
|
|
action: { name: "medications.intake.today" },
|
|
meta: {
|
|
count: events.length,
|
|
projected: projected.length,
|
|
backfilled: missing.length,
|
|
},
|
|
});
|
|
|
|
return apiSuccess(
|
|
events.map((e) => ({
|
|
id: e.id,
|
|
medicationId: e.medicationId,
|
|
scheduledAt: e.scheduledFor.toISOString(),
|
|
takenAt: e.takenAt?.toISOString() ?? null,
|
|
status: e.skipped
|
|
? "skipped"
|
|
: e.takenAt
|
|
? "taken"
|
|
: e.medication.snoozedUntil &&
|
|
e.medication.snoozedUntil > new Date()
|
|
? "snoozed"
|
|
: "pending",
|
|
snoozedUntil: e.medication.snoozedUntil?.toISOString() ?? null,
|
|
})),
|
|
);
|
|
}
|
|
|
|
// v1.4.34 IW-G — compliance: per-day scheduled vs taken for the last
|
|
// N days. Cached at a 15-minute TTL because daily compliance buckets
|
|
// are slow-moving (intake events trickle in, yesterday's row doesn't
|
|
// move). Cache key carries the userTz so a user who changes timezone
|
|
// doesn't read another tz's bucketing.
|
|
//
|
|
// v1.4.39 W-MED — read path now consumes the persistent
|
|
// `medication_compliance_rollups` tier. A coverage probe falls back
|
|
// to the legacy live aggregator when the user has intake events but
|
|
// zero rollup rows; the fallback fires a boot-backfill enqueue in the
|
|
// background so the next request hits the rollup tier.
|
|
const result = await cached(
|
|
caches.medicationsIntake as ServerCache<ComplianceBucket[]>,
|
|
`${user.id}|compliance|${days}|${userTz}`,
|
|
() => readComplianceBucketsWithFallback(user.id, days, userTz),
|
|
annotate,
|
|
);
|
|
|
|
annotate({
|
|
action: { name: "medications.intake.compliance" },
|
|
meta: { days, count: result.length },
|
|
});
|
|
|
|
return apiSuccess(result);
|
|
});
|
|
|
|
/**
|
|
* v1.4.39 W-MED — read from the persistent rollup tier when covered,
|
|
* fall through to the legacy live aggregator on coverage miss. The
|
|
* fallback fires the boot backfill in the background so subsequent
|
|
* requests hit the rollup path; the current request still returns a
|
|
* correct response built from the raw events.
|
|
*/
|
|
async function readComplianceBucketsWithFallback(
|
|
userId: string,
|
|
days: number,
|
|
userTz: string,
|
|
): Promise<ComplianceBucket[]> {
|
|
const covered = await hasMedicationComplianceCoverage(userId, days, userTz);
|
|
if (covered) {
|
|
const buckets = await readMedicationCompliance(userId, days, userTz);
|
|
annotate({
|
|
meta: {
|
|
medication_compliance_path: "rollup",
|
|
medication_compliance_days: days,
|
|
},
|
|
});
|
|
return buckets satisfies RollupComplianceBucket[];
|
|
}
|
|
|
|
// Coverage miss — the legacy aggregator stays correct over an empty
|
|
// rollup window. Fire a user-scoped backfill in the background so
|
|
// the next request lands on the rollup tier; the current request
|
|
// still returns the live-derived buckets.
|
|
//
|
|
// QA F-SEC-M-01 (v1.4.39): pre-fix this fired the cluster-wide
|
|
// `enqueueBootTimeMedicationComplianceBackfill` on every coverage-
|
|
// miss request, opening a soft-DoS amplifier (every authenticated
|
|
// user could drive a multi-tenant `LEFT JOIN` scan on each hit).
|
|
// The user-scoped helper enqueues exactly the caller's account.
|
|
void enqueueUserMedicationComplianceBackfill(userId);
|
|
annotate({
|
|
meta: {
|
|
medication_compliance_path: "live-fallback",
|
|
medication_compliance_days: days,
|
|
},
|
|
});
|
|
return buildComplianceBuckets(userId, days, userTz);
|
|
}
|
|
|
|
interface ComplianceBucket {
|
|
date: string;
|
|
scheduled: number;
|
|
taken: number;
|
|
}
|
|
|
|
/**
|
|
* v1.4.34 IW-G — pulled out of the GET handler so `cached()` can wrap
|
|
* the per-user compliance aggregation. Pure function over the user's
|
|
* intake events + the requested window; deterministic given a fixed
|
|
* "now" wall-clock (we anchor on `Date.now()` once at call time so a
|
|
* 15-minute cached row stays internally consistent).
|
|
*/
|
|
async function buildComplianceBuckets(
|
|
userId: string,
|
|
days: number,
|
|
userTz: string,
|
|
): Promise<ComplianceBucket[]> {
|
|
const nowMs = Date.now();
|
|
const start = new Date(nowMs - days * 86_400_000);
|
|
const events = await prisma.medicationIntakeEvent.findMany({
|
|
where: { userId, scheduledFor: { gte: start } },
|
|
select: { scheduledFor: true, takenAt: true, skipped: true },
|
|
});
|
|
|
|
const buckets = new Map<string, { scheduled: number; taken: number }>();
|
|
for (let i = 0; i < days; i++) {
|
|
const d = new Date(nowMs - i * 86_400_000);
|
|
buckets.set(userDayKey(d, userTz), { scheduled: 0, taken: 0 });
|
|
}
|
|
for (const e of events) {
|
|
const key = userDayKey(e.scheduledFor, userTz);
|
|
const bucket = buckets.get(key);
|
|
if (!bucket) continue;
|
|
bucket.scheduled += 1;
|
|
if (e.takenAt && !e.skipped) bucket.taken += 1;
|
|
}
|
|
|
|
return [...buckets.entries()]
|
|
.map(([date, v]) => ({ date, scheduled: v.scheduled, taken: v.taken }))
|
|
.sort((a, b) => a.date.localeCompare(b.date));
|
|
}
|
|
|
|
export const POST = apiHandler(async (request: NextRequest) => {
|
|
const { user } = await requireAuth();
|
|
|
|
const { data: body, error } = await safeJson(request);
|
|
if (error) return error;
|
|
|
|
const parsed = updateSchema.safeParse(body);
|
|
if (!parsed.success) {
|
|
return apiError(parsed.error.issues[0].message, 422);
|
|
}
|
|
|
|
const { intakeId, status, takenAt, snoozedUntil } = parsed.data;
|
|
|
|
const existing = await prisma.medicationIntakeEvent.findUnique({
|
|
where: { id: intakeId },
|
|
});
|
|
if (!existing || existing.userId !== user.id) {
|
|
return apiError("Intake event not found", 404);
|
|
}
|
|
|
|
const userTzForHook = user.timezone ?? DEFAULT_TIMEZONE;
|
|
|
|
let updated;
|
|
if (status === "taken") {
|
|
[updated] = await prisma.$transaction([
|
|
prisma.medicationIntakeEvent.update({
|
|
where: { id: intakeId },
|
|
data: { takenAt: takenAt ?? new Date(), skipped: false },
|
|
}),
|
|
prisma.medication.update({
|
|
where: { id: existing.medicationId },
|
|
data: { snoozedUntil: null },
|
|
}),
|
|
]);
|
|
} else if (status === "skipped") {
|
|
updated = await prisma.medicationIntakeEvent.update({
|
|
where: { id: intakeId },
|
|
data: { takenAt: null, skipped: true },
|
|
});
|
|
} else {
|
|
// snoozed: snoozedUntil lives on the Medication row.
|
|
const until = snoozedUntil ?? new Date(Date.now() + 30 * 60_000); // default +30min
|
|
await prisma.medication.update({
|
|
where: { id: existing.medicationId },
|
|
data: { snoozedUntil: until },
|
|
});
|
|
updated = await prisma.medicationIntakeEvent.findUnique({
|
|
where: { id: intakeId },
|
|
});
|
|
}
|
|
|
|
await auditLog("medications.intake.update", {
|
|
userId: user.id,
|
|
ipAddress: getClientIp(request),
|
|
details: { intakeId, status },
|
|
});
|
|
|
|
annotate({
|
|
action: { name: "medications.intake.update" },
|
|
meta: { intakeId, status },
|
|
});
|
|
|
|
// v1.4.34 IW-G — bust the medications + compliance + achievement
|
|
// caches for this user so the next read reflects the dose change.
|
|
invalidateUserMedications(user.id);
|
|
|
|
// v1.4.39 W-MED — refresh the persistent compliance rollup row for
|
|
// the affected day so the next read after the cache miss returns
|
|
// the up-to-date `(scheduled, taken, skipped)` tuple. Best-effort:
|
|
// a populator failure annotates ops without blocking the response.
|
|
await recomputeMedicationComplianceForEvent({
|
|
userId: user.id,
|
|
medicationId: existing.medicationId,
|
|
scheduledFor: existing.scheduledFor,
|
|
tz: userTzForHook,
|
|
});
|
|
|
|
return apiSuccess(updated);
|
|
});
|