fix(medications): classify a plan's cadence the same for every range

realisedFamily probed for the closest two slots up to range.to only, so
the answer moved with the range. A Saturday + Sunday plan started the
weekend before the autumn DST change, read around its Saturday dose: the
±24 h range ends at 06:00 UTC on the Sunday, whose 08:00 slot is at 07:00
UTC after the change. Cut off, the closest slots were six days apart
(weekly); the wide re-mint saw the 25 h gap (daily). The two passes
disagreed, no band survived, and the dose was filed as unscheduled, so a
second backdated dose within a minute came back as the first one (200).
Probe the same reach on both sides of the range's middle.
This commit is contained in:
Marc-André Bombeck committed 2026-10-26 00:47:24 +01:00
1 parent 5765059737
commit 085864e63a
3 files changed
+127 -5

No files matched your search

+21 -5
View File
@@ -476,18 +476,34 @@ function buildOneShotBands(
* Mon/Thu `daysOfWeek` med as daily ±60min). Probe a window padded to ≥16
* weeks so a sparse cadence yields ≥2 slots and a real gap is observable; the
* minimum consecutive gap decides the scale.
*
* The probe reaches the same distance on both sides of the range's middle.
* It used to end at `range.to`, so the answer depended on where the range
* stopped: a Saturday + Sunday plan starting the weekend before the autumn
* DST change, read around its second Saturday dose, has its Sunday slot at
* 07:00 UTC while the ±24 h range ends at 06:00. With that slot cut off the
* closest two slots were six days apart (weekly); the wider re-mint saw the
* 25 h Saturday-to-Sunday gap (daily), the two passes disagreed, and the dose
* was filed as unscheduled. A cadence's family is a property of the plan, not
* of the window it is read through.
*/
function realisedFamily(
schedule: CanonicalSchedule,
ctx: RecurrenceContext,
range: { from: Date; to: Date },
): "daily" | "weekly" {
const probeDays = Math.max(
Math.ceil((range.to.getTime() - range.from.getTime()) / DAY_MS),
16 * 7,
const reach =
Math.max(
Math.ceil((range.to.getTime() - range.from.getTime()) / DAY_MS),
16 * 7,
) * DAY_MS;
const middle = (range.from.getTime() + range.to.getTime()) / 2;
const slots = occurrencesBetween(
schedule,
new Date(middle - reach),
new Date(middle + reach),
ctx,
);
const probeFrom = new Date(range.to.getTime() - probeDays * DAY_MS);
const slots = occurrencesBetween(schedule, probeFrom, range.to, ctx);
if (slots.length < 2) {
// Too few slots to measure a gap. Fall back to the field-shape signal:
// an explicit rolling/weekly/monthly shape is day-scale, else daily.