Files
HealthLog/src/app/api/settings/reminder-thresholds/route.ts
T
Marc-André Bombeck cac9a32756 feat(medications): low-stock notifications with a runway threshold
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.
2026-06-12 21:04:09 +02:00

33 lines
1.3 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.
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 });
});