Files
HealthLog/src/lib/medications/intake-actionable.ts
T
Marc-André Bombeck 8295dfe9b6 fix(medications): stop offering a dose once the course has ended
A medication whose end date had passed still showed taken and skip in the
table and on its card, because the action row only looked at whether the
medication was active. The schedule engine already stops minting doses
after the end date, so reminders, the doses card and the today list were
quiet, but the medication itself kept inviting a dose that is no longer
part of the course.

The server now resolves one rule and publishes it on the list and detail
reads: `courseStatus` (UPCOMING, CURRENT or ENDED, in calendar days on the
user's clock, both ends inclusive) and `intakeActionable` (active, intake
tracked, course includes today). The table and both card variants render
their actions from it, the take-all flow skips such a medication, the card
and table name an ended course with an "Ended" badge, and no overdue
escalation is raised for it. A course that ends today keeps its actions
for the whole local day.

The detail header's "Ended" status now follows the same field. It compared
the end date's UTC midnight with the clock and so called the whole last
course day ended. Logging a past dose from the history is unchanged.
2026-09-27 13:49:21 +02:00

56 lines
2.0 KiB
TypeScript

/**
* v1.39.4 (#1040) — whether a medication offers "taken" / "skip" today.
*
* One rule, resolved on the server and published on the medication read
* wire as `intakeActionable` (with the `courseStatus` it derives from), so
* the web cards, the table, the take-all flow and the iOS app all answer
* the same question the same way instead of each re-deriving it:
*
* - the medication is active (not paused or archived),
* - its intake is tracked (`trackIntake`, see `intake-tracking.ts`), and
* - its course includes today on the user's clock (`courseStatusAt`:
* `startsOn <= today <= endsOn`, calendar days, both ends inclusive).
*
* The due-slot question is already the schedule engine's: it caps every
* slot at `endsOn` and floors it at `startsOn`, so the reminder worker, the
* dashboard doses card and the today list never mint a dose outside the
* course. What was missing was the action row on the medication itself,
* which only looked at `active` and so kept offering a dose for a course
* that had ended. Within the course the row stays available between slots
* on purpose: taking a dose early is a supported flow with its own
* double-dose guard.
*
* Logging a past dose from the history is a different verb and is not
* gated here; an ended course can still be completed after the fact.
*/
import {
courseStatusAt,
type CourseStatus,
} from "@/lib/medications/scheduling/recurrence";
import { isRecordOnly } from "@/lib/medications/intake-tracking";
export interface IntakeActionability {
courseStatus: CourseStatus;
intakeActionable: boolean;
}
export function resolveIntakeActionability(
medication: {
active: boolean;
trackIntake: boolean;
startsOn: Date | null;
endsOn: Date | null;
},
now: Date,
timeZone: string,
): IntakeActionability {
const courseStatus = courseStatusAt(medication, now, timeZone);
return {
courseStatus,
intakeActionable:
medication.active &&
!isRecordOnly(medication) &&
courseStatus === "CURRENT",
};
}