feat(medications): schedules remember their history

Editing a schedule's times no longer rewrites the past. Each material
change archives the outgoing cadence as a dated revision, and every
band consumer — history, compliance, write attribution, the auto-miss
cutoff — mints past periods from the schedule that was valid at the
time, so a dose era taken at its then-correct times stays on time
forever. The repair script can infer historical eras from consistent
old anchors behind an explicit flag. The welcome tour scrolls its
target into view, flips above it when space below runs out and falls
back to a centred sheet on any viewport, so no step can strand its
buttons below the fold. The avatar menu drops the about entry — that
lives in settings.
This commit is contained in:
Marc-André Bombeck committed 2026-06-10 22:21:44 +02:00
1 parent 5bfafa6f51
commit bd78cd7f7a
26 files changed
+1806 -223

No files matched your search

@@ -0,0 +1,37 @@
-- v1.16.3 — schedule-revision archive (effective dating).
--
-- A medication whose schedule times are edited used to lose its history: the
-- wholesale replace (deleteMany + create) left only the CURRENT rows, so
-- bands for PAST days were minted from the new times — the entire old era
-- read "unscheduled" and the new times' past slots read "missed".
--
-- This table archives each SUPERSEDED schedule state as one revision row
-- covering `[valid_from, valid_until)`. The LIVE rows stay in
-- `medication_schedules`; a revision never describes the current state, so
-- `valid_until` is NOT NULL by construction. `payload` holds the full
-- snapshot of the replaced schedule rows (timesOfDay, windows, cadence
-- fields, doseWindows, label, dose, reminderGraceMinutes) — enough to
-- rebuild the canonical schedule list for era minting.
--
-- `starts_on` stays the global floor: the first era begins at
-- max(medication.created_at, starts_on).
--
-- Additive + non-destructive: a new empty table; no existing row changes.
CREATE TABLE "medication_schedule_revisions" (
"id" TEXT NOT NULL,
"medication_id" TEXT NOT NULL,
"valid_from" TIMESTAMPTZ(6) NOT NULL,
"valid_until" TIMESTAMPTZ(6) NOT NULL,
"payload" JSONB NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "medication_schedule_revisions_pkey" PRIMARY KEY ("id")
);
CREATE INDEX "medication_schedule_revisions_medication_id_valid_from_idx"
ON "medication_schedule_revisions"("medication_id", "valid_from");
ALTER TABLE "medication_schedule_revisions"
ADD CONSTRAINT "medication_schedule_revisions_medication_id_fkey"
FOREIGN KEY ("medication_id") REFERENCES "medications"("id")
ON DELETE CASCADE ON UPDATE CASCADE;
+47
View File
@@ -1335,6 +1335,12 @@ model Medication {
/// on the linked MedicationIntakeEvent rows refresh the row inline;
/// the read paths consume this instead of the per-event findMany.
complianceRollups MedicationComplianceRollup[]
/// v1.16.3 — archived schedule eras (effective dating). Superseded
/// schedule states land here when a wholesale schedule replace changes
/// cadence-relevant fields; the band minter segments historical minting
/// ranges into eras so past days read against the schedule that was
/// live THEN, not the current one.
scheduleRevisions MedicationScheduleRevision[]
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@ -1431,6 +1437,47 @@ model MedicationSchedule {
@@map("medication_schedules")
}
/// v1.16.3 — schedule-revision archive (effective dating). One row per
/// SUPERSEDED schedule state of a medication: when the schedule set is
/// wholesale-replaced with materially different cadence fields, the old
/// rows are archived here as a single revision covering
/// `[validFrom, validUntil)`. The LIVE rows stay in
/// `medication_schedules` — a revision never describes the current
/// state, so `validUntil` is always set (no open-ended rows).
///
/// The band minter segments any historical minting range into eras:
/// each revision interval mints with ITS archived schedules, the
/// remainder (from the newest `validUntil` onward) mints with the live
/// rows. Era boundary rule: a slot belongs to the era its anchor lies
/// in; an on-time/late tail may reach past the boundary, but no new
/// anchor is minted beyond it.
///
/// `startsOn` stays the global floor for ALL eras: the first era begins
/// at `max(medication.createdAt, startsOn)`; a times-only edit must
/// never move `startsOn`.
model MedicationScheduleRevision {
id String @id @default(cuid())
medicationId String @map("medication_id")
/// Inclusive start of the era this archived state was live for.
/// Chains from the previous revision's `validUntil`, or from
/// `medication.createdAt` for the first revision.
validFrom DateTime @map("valid_from") @db.Timestamptz(6)
/// Exclusive end of the era — the instant the replace happened.
validUntil DateTime @map("valid_until") @db.Timestamptz(6)
/// Full snapshot of the superseded schedule rows: an array of
/// `{ timesOfDay, windowStart, windowEnd, daysOfWeek, rrule,
/// rollingIntervalDays, scheduleType, cyclicOnWeeks, cyclicOffWeeks,
/// doseWindows, label, dose, reminderGraceMinutes }` objects — enough
/// to rebuild a `CanonicalSchedule` list for era minting.
payload Json
createdAt DateTime @default(now()) @map("created_at")
medication Medication @relation(fields: [medicationId], references: [id], onDelete: Cascade)
@@index([medicationId, validFrom])
@@map("medication_schedule_revisions")
}
enum IntakeSource {
WEB
API