feat(medications): let a course end at an instant

A course's end date is a calendar day and counts in full: the doses of that
day stay due, reminded and expected until local midnight. That is right for
an end date set in the editor and wrong for "End course", whose dialog
promises that nothing more is due.

A course now carries `endedAt` (migration 0482, additive, nullable), the
instant it was ended on its `endsOn` day, projected onto the medication next
to `startsOn` / `endsOn` by the one course writer. From that instant:

- the schedule engine emits no slot, so a dose later that day is not due,
  not reminded, not projected onto the today list and never expected by
  compliance, while a dose earlier that day stays inside the course;
- the course reads ENDED (`courseStatusAt` / `courseStatusOn`), so the dose
  actions, `remindersActive` and the reminder gate stop at once, follow-ups
  of an earlier dose included;
- every engine context builder, compliance context, dose history, Coach
  snapshot, doctor report and the adherence storyline read it, through one
  select and one picker for the medication window.

Any write that moves a course's `endsOn` clears the instant, so an end date
chosen in the editor keeps meaning the whole day. Backups carry it in both
purposes and a restore projects it again. `courseStatusAt` moves next to the
other course rules.

Refs #1280
This commit is contained in:
Marc-André Bombeck committed 2026-10-24 22:20:06 +02:00
1 parent a773d839d4
commit 23a67f61e6
68 files changed
+813 -180

No files matched your search

@@ -15,6 +15,12 @@
* which it is. The scan asserts a floor on what it found, so a matcher that
* stopped matching fails instead of agreeing with an empty tree.
*
* #1280 — the same list carries the instant "End course" ended a course:
* every medication builder threads `endedAt` into the context, or the
* engine would keep a dose due after the course was ended. A builder passes
* when its block names `endedAt` or spreads a window that carries it
* (`...window` from `mintWindow`, `...medicationWindow(row)`).
*
* Limit, stated: a literal passed straight to the engine without the
* `RecurrenceContext` annotation is invisible here. Every medication path
* today goes through one of the annotated builders below.
@@ -69,19 +75,22 @@ function blockAt(text: string, open: number): string {
interface Builder {
file: string;
declaresCourseStart: boolean;
threadsEndedAt: boolean;
}
const THREADS_ENDED_AT = /\bendedAt\b|\.\.\.\s*(window\b|medicationWindow\()/;
function builders(): Builder[] {
const out: Builder[] = [];
for (const file of sourceFiles(SRC)) {
const text = readSourceText(file);
for (const m of text.matchAll(DECLARATION)) {
const open = m.index! + m[0].length - 1;
const block = blockAt(text, open);
out.push({
file: relative(ROOT, file),
declaresCourseStart: /rollingAnchor\s*:\s*"courseStart"/.test(
blockAt(text, open),
),
declaresCourseStart: /rollingAnchor\s*:\s*"courseStart"/.test(block),
threadsEndedAt: THREADS_ENDED_AT.test(block),
});
}
}
@@ -114,6 +123,18 @@ describe("the rolling course anchor is opt-in", () => {
expect(missing).toEqual([]);
});
it("has every medication builder thread the instant a course was ended", () => {
const medication = builders().filter((b) =>
MEDICATION_BUILDERS.includes(b.file),
);
expect(medication.length).toBeGreaterThanOrEqual(3);
expect(medication.filter((b) => !b.threadsEndedAt)).toEqual([]);
// The matcher, held against what it must and must not accept.
expect(THREADS_ENDED_AT.test("endedAt: row.endedAt,")).toBe(true);
expect(THREADS_ENDED_AT.test("...medicationWindow(row),")).toBe(true);
expect(THREADS_ENDED_AT.test("endsOn: row.endsOn,")).toBe(false);
});
it("keeps every other builder on the default (the last intake anchors)", () => {
const declaring = builders().filter(
(b) => NON_MEDICATION_BUILDERS.includes(b.file) && b.declaresCourseStart,
@@ -155,6 +155,7 @@ describe.each(EDGES)("day paths with the host in $zone", (edge) => {
startsOn: null,
// `endsOn` is a @db.Date column: the calendar date as UTC midnight.
endsOn: dayKeyAsUtcMidnight(edge.localDay),
endedAt: null,
},
edge.now,
edge.zone,