Let a medication have several courses

A medication taken now and then (#1024) keeps one row and gains course
rows: calendar spans, both ends inclusive, an optional encrypted note, no
schedule or dose of their own. Courses never overlap and every course but
the latest has ended, so the latest course is always the one the
medication's startsOn/endsOn project. src/lib/medications/courses.ts is
the only writer of those two columns for a medication with courses; it
writes under a per-medication advisory lock and re-projects in the same
transaction, so reminders, the next-due line and intakeActionable keep
reading the columns and stay right.

- Migration 0368 adds medication_courses and backfills one course per
  medication window (an end-only window starts on the creation day, or
  on its end day when that lies earlier); a medication without a window
  stays course-less. Idempotent, data-preserving.
- GET/POST /api/medications/{id}/courses and PATCH/DELETE
  .../courses/{courseId} (owner-only); create seeds the first course
  from a window; the medication PUT edits the latest course and refuses
  a window that would break the rules (medications.course.*).
- List, detail, create and update carry courses, courseCount,
  previousCourseEndedOn and canStartCourse, resolved on the server.
- Compliance expands over the span of all courses and drops the slots
  between them; every compliance read selects the courses. A rolling
  cadence is not anchored by an intake from a previous course.
- The card names the last course's end and the running course's number;
  the detail Lifecycle group lists the courses with start, edit and
  delete. The Coach compliance block, the MCP resource, the doctor
  report window and table, the clinician view and FHIR (one
  MedicationStatement per course with effectivePeriod and status) read
  the courses.
- Backup carries courses per medication and the restore writes them and
  re-projects; an older file derives the one course its window
  describes. Wipe plan, encrypted columns, rotation and OpenAPI follow.
This commit is contained in:
Marc-André Bombeck committed 2026-10-03 22:39:25 +02:00
1 parent 9b71231010
commit a14174fe4d
71 files changed
+3635 -90

No files matched your search

+18
View File
@@ -212,6 +212,20 @@ const medicationPauseEraSchema = z
})
.passthrough();
/**
* v1.40 (#1024) — one course of a medication: a calendar span (both ends
* inclusive, `YYYY-MM-DD`). The note travels decrypted in both purposes and
* is sealed again on restore, like the custom category label.
*/
const medicationCourseSchema = z
.object({
startsOn: z.iso.date(),
endsOn: z.iso.date().nullable(),
note: z.string().max(280).nullable().optional(),
createdAt: isoDateTime.optional(),
})
.passthrough();
/**
* One step of a titration: when the dose moved and to what.
*
@@ -413,6 +427,10 @@ const medicationSchema = z
// Same default again. A drug whose plan has never been replaced has no
// archived era, and a file written before the eras travelled has no key.
scheduleRevisions: z.array(medicationScheduleRevisionSchema).default([]),
// v1.40 (#1024) — NOT defaulted: absent means a file written before
// courses existed, and the restore then derives the one course the
// medication's own window describes, exactly as the migration did.
courses: z.array(medicationCourseSchema).optional(),
})
.passthrough();
+54
View File
@@ -0,0 +1,54 @@
import { z } from "zod/v4";
/**
* v1.40 (#1024) — the bodies of `POST /api/medications/{id}/courses` and
* `PATCH /api/medications/{id}/courses/{courseId}`. A course carries a
* calendar span and an optional note; no schedule or dose of its own.
*/
const courseDate = z.iso.date().transform((s) => new Date(s));
const courseNote = z
.string()
.trim()
.max(280, "Note must be at most 280 characters")
.nullable();
export const createMedicationCourseSchema = z
.object({
startsOn: courseDate.describe(
"First day of the course (ISO `YYYY-MM-DD`, inclusive, the person's calendar).",
),
endsOn: courseDate
.nullable()
.optional()
.describe(
"Last day (inclusive). NULL or absent = open; only the latest course may be open.",
),
note: courseNote
.optional()
.describe("Why this course, in the person's words. Encrypted at rest."),
})
.meta({
id: "CreateMedicationCourseRequest",
description:
"v1.40 — start a course. Refused when it shares a day with another course (422 `medications.course.overlap`), when it would sit after a course that has not ended (422 `medications.course.currentOpen`), on a one-time medication that already has one (422 `medications.course.oneShot`), or when it ends before it starts (422 `medications.course.invalidRange`).",
});
export const updateMedicationCourseSchema = z
.object({
startsOn: courseDate.optional(),
endsOn: courseDate.nullable().optional(),
note: courseNote.optional(),
})
.refine(
(v) =>
v.startsOn !== undefined ||
v.endsOn !== undefined ||
v.note !== undefined,
"At least one field is required",
)
.meta({
id: "UpdateMedicationCourseRequest",
description:
"v1.40 — edit a course field by field. Same refusals as the create.",
});
+1
View File
@@ -14,3 +14,4 @@ export * from "./schedule";
export * from "./create-update";
export * from "./intake";
export * from "./inventory";
export * from "./course";