fix(medications): the audit findings on the new supply and dose surfaces

- The escalated card row keeps its time anchor: the red badge carries
  the matched window or the overdue-slot value beside it, and the
  inline row's range stays visible on mobile, where it was the only
  clock on the card.
- Take-all-due collects what the cards offer: a dose escalated past its
  catch-up band joins the set with exactly the slot the card's own
  button would record; as-needed medications never join.
- The table's stock warning follows the user's runway threshold instead
  of a leftover four-dose constant; threshold off means no amber, empty
  stays red.
- The card itself shows the remaining runway in the warning tone once
  it crosses the threshold — only then, the card stays calm above.
- The low-stock push deep-links straight into the supply tab and its
  APNs payload keeps url, runwayDays and thresholdDays; supply tab and
  threshold setting link to each other; the reminder-thresholds and
  notification-prefs endpoints enter the OpenAPI contract.
This commit is contained in:
Marc-André Bombeck committed 2026-06-12 23:17:20 +02:00
1 parent 3943dcfb72
commit 6fef2081d0
24 files changed
+970 -40

No files matched your search

+2
View File
@@ -33,6 +33,7 @@ import { medicationPaths, medicationResource } from "./medications";
import { metaPaths } from "./meta";
import { moodPaths } from "./mood";
import { profilePaths } from "./profile";
import { settingsPaths } from "./settings";
import { syncPaths } from "./sync";
import { workoutPaths } from "./workouts";
@@ -53,6 +54,7 @@ export const openApiPaths: NonNullable<ZodOpenApiObject["paths"]> = {
...adminDiagnosticPaths,
...insightsPaths,
...moodPaths,
...settingsPaths,
};
export const openApiComponents: NonNullable<ZodOpenApiObject["components"]> = {
+123
View File
@@ -8,6 +8,7 @@
import { z } from "zod/v4";
import type { ZodOpenApiObject } from "zod-openapi";
import { coachPrefsSchema } from "@/lib/validations/coach-prefs";
import { notificationPrefsSchema } from "@/lib/validations/notification-prefs";
import { sourcePrioritySchema } from "@/lib/validations/source-priority";
import { dataEnvelope, errorEnvelope, stdResponses } from "./shared";
@@ -29,6 +30,80 @@ const disableCoachFlag = z
"Per-account Coach opt-out toggle (v1.4.47 W3). `true` hides the Coach FAB and short-circuits its API gates.",
});
// v1.16.11 — per-user notification preferences. The PATCH request body
// is the REAL runtime validator (`notificationPrefsSchema` — every
// category and field optional, deep-merged server-side); the response
// is the fully-resolved shape with the documented defaults filled in.
const notificationPrefsPatchRequest = notificationPrefsSchema.meta({
id: "NotificationPrefsPatchRequest",
description:
"Partial per-user notification preferences. Every category and field is optional; the server deep-merges the supplied keys over the persisted row, so a PATCH touching only `medication` leaves the siblings intact. `medication.lowStockRunwayDays` (1–60, nullable) is the low-stock alert threshold in remaining runway days — `null` switches the alert off.",
});
const notificationPrefsResolved = z
.object({
medication: z.object({
clientManaged: z
.boolean()
.describe(
"True when the iOS app owns local medication reminders; the server-side MEDICATION_REMINDER APNs cron is suppressed.",
),
deliveryDefault: z
.enum(["server", "client"])
.describe(
"Roaming user-level delivery default. \"client\" implies clientManaged: true.",
),
lowStockRunwayDays: z
.number()
.int()
.min(1)
.max(60)
.nullable()
.describe(
"Low-stock alert threshold as remaining runway days (1–60). null = alert off. Default 7.",
),
}),
mood: z.object({
reminderHour: z
.number()
.int()
.min(0)
.max(23)
.describe("Local-time hour for the daily mood reminder. Default 22."),
}),
cycle: z.object({
clientManaged: z
.boolean()
.describe(
"True when the iOS app owns local cycle reminders; the server-side cycle APNs cron is suppressed.",
),
}),
coach: z.object({
nudgesEnabled: z
.boolean()
.describe("Master switch for the proactive Coach nudge cron."),
nudgeMedication: z
.boolean()
.describe("Medication-group nudge triggers (compliance)."),
nudgeVitals: z
.boolean()
.describe("Vitals-group nudge triggers (bp / score / weight / sleep)."),
nudgeRoutine: z
.boolean()
.describe(
"Routine-group nudge triggers (measurement gap / self-context).",
),
nudgeFrequency: z
.enum(["weekly", "biweekly"])
.describe("Nudge frequency cap: one per 7 or per 14 days."),
}),
})
.meta({
id: "NotificationPrefs",
description:
"Fully-resolved per-user notification preferences. Every key is present — a null / drifted persisted row resolves to the documented defaults.",
});
// v1.8.6 — profile read/write surface for the native client. The
// runtime validation lives in `src/lib/validations/auth.ts`
// (`profileSchema`, whose transforms normalise empty → null and run the
@@ -413,4 +488,52 @@ export const profilePaths: NonNullable<ZodOpenApiObject["paths"]> = {
},
},
},
"/api/auth/me/notification-prefs": {
get: {
tags: ["Notifications"],
summary: "Read per-user notification preferences",
description:
"Returns the fully-resolved preferences with the documented defaults filled in. A null persisted row resolves to the defaults (server-managed reminders, low-stock threshold 7 days, mood reminder 22:00, Coach nudges on).",
responses: {
"200": {
description: "Resolved preferences.",
content: {
"application/json": {
schema: dataEnvelope(
notificationPrefsResolved,
"GetNotificationPrefsResponse",
),
},
},
},
...stdResponses,
},
},
patch: {
tags: ["Notifications"],
summary: "Update per-user notification preferences",
description:
"Deep-merges the supplied partial shape over the persisted row — a PATCH touching only one category leaves the siblings intact. Always returns the fully-resolved next state so clients can hard-set their optimistic update. `medication.clientManaged: true` (or `deliveryDefault: \"client\"`) suppresses server-side MEDICATION_REMINDER APNs; `medication.lowStockRunwayDays` (1–60, nullable, default 7) tunes the low-stock alert — null switches it off. Rate-limit 60/min per user.",
requestBody: {
required: true,
content: {
"application/json": { schema: notificationPrefsPatchRequest },
},
},
responses: {
"200": {
description: "Resolved next-state preferences echoed back.",
content: {
"application/json": {
schema: dataEnvelope(
notificationPrefsResolved,
"PatchNotificationPrefsResponse",
),
},
},
},
...stdResponses,
},
},
},
};
+70
View File
@@ -0,0 +1,70 @@
/**
* OpenAPI route table — settings read surfaces.
*
* Part of the OpenAPI route table; aggregated in `./index.ts`.
* Schemas come from `src/lib/validations/*` where shared with the
* runtime request parsing, so the wire contract stays single-source.
*/
import { z } from "zod/v4";
import type { ZodOpenApiObject } from "zod-openapi";
import { dataEnvelope, stdResponses } from "./shared";
// v1.16.11 — the one threshold read every dose-status consumer makes
// (cards, table, take-all-due derivation). `lateMinutes` /
// `missedMinutes` are the operator-level singleton; the low-stock
// runway threshold rides along but is PER-USER (written through
// `PATCH /api/auth/me/notification-prefs`).
const reminderThresholdsResponse = z
.object({
lateMinutes: z
.number()
.int()
.describe(
"Minutes after the dose anchor at which an open dose tiers 'late' (operator-level singleton; default 120).",
),
missedMinutes: z
.number()
.int()
.describe(
"Minutes after the dose anchor at which an open dose tiers 'missed' (operator-level singleton; default 240).",
),
lowStockRunwayDays: z
.number()
.int()
.min(1)
.max(60)
.nullable()
.describe(
"Per-user low-stock alert threshold as remaining runway days (1–60). null = the alert is off. Default 7. Written via PATCH /api/auth/me/notification-prefs.",
),
})
.meta({
id: "ReminderThresholdsResponse",
description:
"Medication reminder thresholds: the operator-level late/missed minute marks plus the calling user's low-stock runway threshold (v1.16.11).",
});
export const settingsPaths: NonNullable<ZodOpenApiObject["paths"]> = {
"/api/settings/reminder-thresholds": {
get: {
tags: ["Notifications"],
summary: "Read the medication reminder thresholds",
description:
"Returns the operator-level late/missed minute thresholds that tier an open dose's status, plus the calling user's low-stock runway threshold (days; null = alert off). One endpoint so every threshold consumer reads one shape. Auth via cookie or Bearer.",
responses: {
"200": {
description: "Resolved thresholds.",
content: {
"application/json": {
schema: dataEnvelope(
reminderThresholdsResponse,
"GetReminderThresholdsResponse",
),
},
},
},
...stdResponses,
},
},
},
};