fix(vorsorge,coach): align reminders + coach chrome with the gold standard

Twice-daily BP protocol (RRULE BYHOUR=7,19) now fires at every clock
hour: byHourTimesOfDay expands BYHOUR into the engine's timesOfDay so
the label, RRULE, and engine output agree instead of collapsing to one
nudge at notifyHour.

Vorsorge surface:
- move create/edit into a ResponsiveSheet with a pinned Cancel/Save
  footer, matching Labs/Illness/Medications
- wire the unused PATCH support: Edit affordance opens the form
  pre-filled; an enable/disable Switch drives the enabled flag
- translate COACH-origin labels (the row stores an i18n key, not prose)
  and render a neutral Coach provenance badge + an until-<date> line
- surface origin + endsOn on the hook type
- pull-to-refresh + always-on rationale documented on the nav entry
- NativeSelect-vs-Radix choice documented as deliberate

Coach:
- expired COACH course-window reminders self-clean: the cron tick
  soft-deletes finished finite cadences instead of leaving dead rows
- accept-dedup gains a partial unique index (user, metric) where
  origin=COACH and not deleted, with a P2002 catch in the route
- per-user rate-limit bucket on the suggestion-action endpoint
- SUGGEST-REMINDER parser keeps prose written after the block
- document POST /api/coach/reminder-suggestions in OpenAPI

Coach chrome:
- HistoryRail hideHeading prop drops the duplicate Conversations
  heading on the page surface
- align the rail-tray strip inset to px-3 sm:px-4 so the dividers line
  up; reword the FAB inset comment

satisfyReminder closes the forward-only TOCTOU via a conditional
updateMany (count===0 no-op). Migration headers corrected to v1.18.1.
This commit is contained in:
Marc-André Bombeck committed 2026-06-16 23:11:08 +02:00
1 parent b28fa4d4a5
commit 7451e886f1
32 files changed
+983 -230

No files matched your search

+52 -16
View File
@@ -19,8 +19,10 @@
*/
import { NextRequest } from "next/server";
import { Prisma } from "@/generated/prisma/client";
import { prisma } from "@/lib/db";
import { auditLog } from "@/lib/auth/audit";
import { checkRateLimit } from "@/lib/rate-limit";
import { apiHandler, requireAuth } from "@/lib/api-handler";
import {
apiError,
@@ -55,6 +57,18 @@ async function postAction(request: NextRequest): Promise<Response> {
const gate = await requireModuleEnabled(user.id, "coach");
if (!gate.enabled) return gate.response;
// Per-user rate limit — the action card is one-tap, so a tight per-user
// bucket (userId, never IP) is the right granularity. Mirrors the
// labs/restore convention; a trust-violation can never widen it.
const rate = await checkRateLimit(
`coach:reminder-suggestion:${user.id}`,
30,
60_000,
);
if (!rate.allowed) {
return apiError("coach.suggestion.rateLimited", 429);
}
const { data: body, error: jsonError } = await safeJson(request, {
maxBytes: 4 * 1024,
});
@@ -150,22 +164,44 @@ async function postAction(request: NextRequest): Promise<Response> {
// Field-by-field — no mass assignment. `origin: COACH` is the
// provenance the UI labels and the SUGGEST gate dedupes against.
const created = await prisma.measurementReminder.create({
data: {
userId: user.id,
label: cadence.labelKey,
measurementType: cadence.measurementType,
intervalDays: cadence.intervalDays,
rrule: cadence.rrule,
anchorDate: null,
endsOn,
origin: "COACH",
notifyHour: cadence.notifyHour,
location: null,
enabled: true,
nextDueAt,
},
});
let created;
try {
created = await prisma.measurementReminder.create({
data: {
userId: user.id,
label: cadence.labelKey,
measurementType: cadence.measurementType,
intervalDays: cadence.intervalDays,
rrule: cadence.rrule,
anchorDate: null,
endsOn,
origin: "COACH",
notifyHour: cadence.notifyHour,
location: null,
enabled: true,
nextDueAt,
},
});
} catch (err) {
// Structural dedup backstop: the 0172 partial unique index
// (`origin = 'COACH' AND deleted_at IS NULL` per user+metric) wins the
// race the read-then-create above can lose. Treat the violation as the
// same idempotent "already have one" outcome.
if (
err instanceof Prisma.PrismaClientKnownRequestError &&
err.code === "P2002"
) {
annotate({
action: { name: "coach.reminder.accept.duplicate" },
meta: { cadenceId, reason: "unique-violation" },
});
return apiSuccess(
{ ok: true, action: "accept", reminder: null, duplicate: true },
200,
);
}
throw err;
}
await auditLog("measurementReminder.create.coach", {
userId: user.id,