feat(medications): attribute an off-schedule intake to its slot, visibly and reversibly

An off-schedule row now tells the user when the dose had been due, and
its row menu offers to attribute it to that slot when the slot is still
unserved. A pinned intake is a deliberate decision with a visible badge,
it always counts as taken late — never on time — and the pin can be
released again. The read model binds pinned rows by their slot anchor so
a pin outside the catch-up window survives reloads, and the suggestion
respects the schedule's own windows instead of a hard-coded tolerance.
This commit is contained in:
Marc-André Bombeck committed 2026-06-10 15:39:55 +02:00
1 parent d7bf0df10b
commit 0cf9bc9348
21 files changed
+802 -17

No files matched your search

@@ -91,6 +91,7 @@ describe("parseNotificationPrefs", () => {
medication: { clientManaged: true, deliveryDefault: "server" },
mood: { reminderHour: DEFAULT_MOOD_REMINDER_HOUR },
cycle: { clientManaged: false },
coach: { nudgesEnabled: true },
});
});
@@ -99,6 +100,7 @@ describe("parseNotificationPrefs", () => {
medication: { clientManaged: false, deliveryDefault: "server" },
mood: { reminderHour: DEFAULT_MOOD_REMINDER_HOUR },
cycle: { clientManaged: false },
coach: { nudgesEnabled: true },
});
});
@@ -109,6 +111,7 @@ describe("parseNotificationPrefs", () => {
medication: { clientManaged: true, deliveryDefault: "client" },
mood: { reminderHour: DEFAULT_MOOD_REMINDER_HOUR },
cycle: { clientManaged: false },
coach: { nudgesEnabled: true },
});
});
@@ -135,6 +138,7 @@ describe("resolveNotificationPrefs (deep-merge)", () => {
medication: { clientManaged: true, deliveryDefault: "server" },
mood: { reminderHour: DEFAULT_MOOD_REMINDER_HOUR },
cycle: { clientManaged: false },
coach: { nudgesEnabled: true },
});
});
@@ -149,6 +153,7 @@ describe("resolveNotificationPrefs (deep-merge)", () => {
medication: { clientManaged: true, deliveryDefault: "server" },
mood: { reminderHour: DEFAULT_MOOD_REMINDER_HOUR },
cycle: { clientManaged: false },
coach: { nudgesEnabled: true },
});
});
@@ -160,6 +165,7 @@ describe("resolveNotificationPrefs (deep-merge)", () => {
medication: { clientManaged: true, deliveryDefault: "server" },
mood: { reminderHour: DEFAULT_MOOD_REMINDER_HOUR },
cycle: { clientManaged: false },
coach: { nudgesEnabled: true },
});
});
@@ -171,6 +177,7 @@ describe("resolveNotificationPrefs (deep-merge)", () => {
medication: { clientManaged: true, deliveryDefault: "client" },
mood: { reminderHour: DEFAULT_MOOD_REMINDER_HOUR },
cycle: { clientManaged: false },
coach: { nudgesEnabled: true },
});
});
@@ -183,6 +190,7 @@ describe("resolveNotificationPrefs (deep-merge)", () => {
medication: { clientManaged: true, deliveryDefault: "server" },
mood: { reminderHour: 8 },
cycle: { clientManaged: false },
coach: { nudgesEnabled: true },
});
});
+25
View File
@@ -0,0 +1,25 @@
/**
* v1.15.20 — request schema for `PUT /api/coach/about-me`.
*
* Lives outside the route file so the OpenAPI registry
* (`src/lib/openapi/routes.ts`) can import it without touching the
* route module (route files may only export handlers + config), and
* without dragging the Prisma-backed helper module
* (`src/lib/ai/coach/about-me.ts`) into the generator script.
*/
import { z } from "zod/v4";
/** Hard cap enforced BEFORE encryption. */
export const ABOUT_ME_MAX_CHARS = 4000;
export const aboutMePutSchema = z.object({
/**
* The user's free-text self-description. An empty / whitespace-only
* value clears the stored text.
*/
aboutMe: z
.string()
.max(ABOUT_ME_MAX_CHARS, `Maximum ${ABOUT_ME_MAX_CHARS} characters`),
});
export type AboutMePutInput = z.infer<typeof aboutMePutSchema>;
+7
View File
@@ -27,6 +27,13 @@ export const registerSchema = z.object({
* keep working.
*/
timezone: z.string().max(64).optional(),
/**
* v1.15.20 — registration invite token (`hlv_<64hex>`, 68 chars).
* Only consulted when the operator has disabled open registration:
* a valid, unexpired invite still admits the signup. Ignored when
* registration is open.
*/
inviteToken: z.string().max(80).optional(),
});
export const loginPasswordSchema = z.object({
+31
View File
@@ -0,0 +1,31 @@
/**
* v1.15.20 — request schema for `POST /api/admin/invites`.
*
* Lives outside the route file (and outside the Prisma-backed helper
* `src/lib/auth/invite-token.ts`) so the OpenAPI registry can import it
* without dragging `@/lib/db` into the generator script — the same
* split as `src/lib/validations/about-me.ts`.
*/
import { z } from "zod/v4";
/** Hard ceiling on the admin-selectable invite lifetime (days). */
export const INVITE_MAX_TTL_DAYS = 30;
/** Hard ceiling on multi-use invites — generous for a household / small
* practice, far below anything that would re-open registration at scale. */
export const INVITE_MAX_USES_CAP = 50;
export const inviteCreateSchema = z.object({
/** Lifetime in days. The admin UI offers 7 / 14 / 30; the API accepts
* any integer 1–30 so an operator can script shorter invites. */
expiresInDays: z
.number()
.int()
.min(1)
.max(INVITE_MAX_TTL_DAYS)
.default(7),
/** How many signups this invite admits. Default: single-use. */
maxUses: z.number().int().min(1).max(INVITE_MAX_USES_CAP).default(1),
});
export type InviteCreateInput = z.infer<typeof inviteCreateSchema>;
+8 -2
View File
@@ -730,19 +730,25 @@ export const updateIntakeEventSchema = z
* take onto a chosen slot; the server validates the instant is a real
* scheduled slot (422 otherwise). Absent → the edit re-runs band
* attribution on the new `takenAt`.
*
* v1.15.20 — an explicit `null` UNPINS: the dose re-attributes by window
* band on its (unchanged or edited) `takenAt` and the binding provenance
* resets to AUTO — the "Zuordnung lösen" path. A pin / unpin no longer
* requires `takenAt` or `skipped` to change in the same request.
*/
forceSlotInstant: z.iso
.datetime({ offset: true })
.transform((s) => new Date(s))
.nullable()
.optional()
.describe(
"Late-take override on edit: pin the edited dose onto the named scheduled slot instead of re-attributing by window band. Must be a real scheduled slot of this medication on its day (validated server-side); a non-slot instant returns 422. Absent re-runs the default window-band attribution on the edited `takenAt`.",
"Late-take override on edit: pin the edited dose onto the named scheduled slot instead of re-attributing by window band. Must be a real scheduled slot of this medication on its day (validated server-side); a non-slot instant returns 422. Explicit null unpins: the dose re-attributes by window band on its takenAt (ad-hoc when no band matches). Absent re-runs the default window-band attribution on the edited `takenAt`.",
),
})
.meta({
id: "UpdateMedicationIntakeEventRequest",
description:
"Edit a single intake event. v1.15.18 re-runs window-band slot attribution whenever `takenAt` or `skipped` change, snapping `scheduledFor` to the matched slot (or the take's own time when it falls in no window). `forceSlotInstant` overrides that to pin the take onto a named real slot; an explicit `scheduledFor` still wins when supplied directly. `takenAt` must not be in the future (5-minute clock-skew allowance) nor more than 5 years in the past; a `takenAt` before the medication's start date returns 422.",
"Edit a single intake event. v1.15.18 re-runs window-band slot attribution whenever `takenAt` or `skipped` change, snapping `scheduledFor` to the matched slot (or the take's own time when it falls in no window). `forceSlotInstant` overrides that to pin the take onto a named real slot (explicit null unpins, re-attributing by band); an explicit `scheduledFor` still wins when supplied directly. `takenAt` must not be in the future (5-minute clock-skew allowance) nor more than 5 years in the past; a `takenAt` before the medication's start date returns 422.",
});
/**
+45
View File
@@ -70,6 +70,20 @@ const cyclePrefsSchema = z
})
.partial();
/**
* v1.15.20 — coach category schema. `nudgesEnabled` gates the proactive
* Coach nudge cron (05:15 Europe/Berlin): default ON (the nudge is an
* opt-out surface — it only fires for users who kept the Coach enabled
* AND have a working provider), flipped off from Settings →
* Notifications. Sub-object form keeps the layout open for future
* coach-notification knobs (quiet days, trigger selection, …).
*/
const coachPrefsSchema = z
.object({
nudgesEnabled: z.boolean(),
})
.partial();
/**
* v1.7.0 — per-device delivery override schema for the device PATCH.
* NULL clears the override (the device inherits the user-level roaming
@@ -88,6 +102,7 @@ export const notificationPrefsSchema = z
medication: medicationPrefsSchema,
mood: moodPrefsSchema,
cycle: cyclePrefsSchema,
coach: coachPrefsSchema,
})
.partial();
@@ -116,6 +131,13 @@ export interface NotificationPrefs {
*/
clientManaged: boolean;
};
coach: {
/**
* v1.15.20 — proactive Coach nudge opt-out. Default `true`; the
* 05:15 nudge cron skips the user when `false`.
*/
nudgesEnabled: boolean;
};
}
/**
@@ -141,6 +163,9 @@ export const DEFAULT_NOTIFICATION_PREFS: NotificationPrefs = {
cycle: {
clientManaged: false,
},
coach: {
nudgesEnabled: true,
},
};
/**
@@ -181,6 +206,10 @@ export function resolveNotificationPrefs(
...base.cycle,
...(incoming.cycle ?? {}),
},
coach: {
...base.coach,
...(incoming.coach ?? {}),
},
});
}
@@ -206,6 +235,7 @@ function applyDeliveryDefaultMapping(prefs: NotificationPrefs): NotificationPref
},
mood: { ...prefs.mood },
cycle: { ...prefs.cycle },
coach: { ...prefs.coach },
};
}
@@ -262,11 +292,22 @@ export function resolveMoodReminderHour(raw: unknown): number {
return parseNotificationPrefs(raw).mood.reminderHour;
}
/**
* v1.15.20 — cron-side helper. Returns `true` when the proactive Coach
* nudge may fire for this user (the default). Tolerates a null /
* drifted prefs row. The nudge cron layers this on top of the
* disableCoach / kill-switch / provider gates.
*/
export function resolveCoachNudgesEnabled(raw: unknown): boolean {
return parseNotificationPrefs(raw).coach.nudgesEnabled;
}
function cloneDefaults(): NotificationPrefs {
return {
medication: { ...DEFAULT_NOTIFICATION_PREFS.medication },
mood: { ...DEFAULT_NOTIFICATION_PREFS.mood },
cycle: { ...DEFAULT_NOTIFICATION_PREFS.cycle },
coach: { ...DEFAULT_NOTIFICATION_PREFS.coach },
};
}
@@ -286,5 +327,9 @@ function mergeOverDefaults(
...DEFAULT_NOTIFICATION_PREFS.cycle,
...(input.cycle ?? {}),
},
coach: {
...DEFAULT_NOTIFICATION_PREFS.coach,
...(input.coach ?? {}),
},
});
}