mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 16:12:11 +00:00
A daily job projects each tracked medication's remaining supply against its schedules and notifies through the normal channel cascade when the runway falls below the user's threshold (default seven days, 1-60 or off, set beside the reminder thresholds). One notification per crossing: a stamp on the medication row re-arms on refill or threshold change, and a failed dispatch leaves it clear for the next day's retry. The GLP-1 card's fixed four-dose flag stays as the locked iOS read contract; the engine covers it like any other medication.
33 lines
1.3 KiB
TypeScript
33 lines
1.3 KiB
TypeScript
import { apiSuccess } from "@/lib/api-response";
|
||
import { getReminderThresholds } from "@/lib/app-settings";
|
||
import { apiHandler, requireAuth } from "@/lib/api-handler";
|
||
import { annotate } from "@/lib/logging/context";
|
||
import { prisma } from "@/lib/db";
|
||
import { resolveLowStockRunwayDays } from "@/lib/validations/notification-prefs";
|
||
|
||
export const dynamic = "force-dynamic";
|
||
|
||
export const GET = apiHandler(async () => {
|
||
const { user } = await requireAuth();
|
||
|
||
const thresholds = await getReminderThresholds();
|
||
|
||
// v1.16.11 — the low-stock runway threshold rides along so every
|
||
// threshold consumer reads one endpoint. Unlike `lateMinutes` /
|
||
// `missedMinutes` (operator-level singleton) this one is PER-USER:
|
||
// it lives in `notificationPrefs.medication.lowStockRunwayDays`
|
||
// (1–60 days, `null` = alert off, default 7) and is written through
|
||
// the established `PATCH /api/auth/me/notification-prefs` path.
|
||
const row = await prisma.user.findUnique({
|
||
where: { id: user.id },
|
||
select: { notificationPrefs: true },
|
||
});
|
||
const lowStockRunwayDays = resolveLowStockRunwayDays(
|
||
row?.notificationPrefs ?? null,
|
||
);
|
||
|
||
annotate({ action: { name: "settings.reminder-thresholds.get" } });
|
||
|
||
return apiSuccess({ ...thresholds, lowStockRunwayDays });
|
||
});
|