mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-15 23:12:04 +00:00
fix(medications): every intake path converges on the slot, and caches cannot resurrect a pre-write body
The external ingest endpoint was the one intake writer that never attributed to the minted band: it stored a bare row beside the pending slot, so an early take through the API showed as the last intake while the slot kept demanding a dose, the ledger recorded an extra plus a miss, and the day tally counted both. It now resolves the band and converges onto the pending row like every other path, the Telegram confirm and skip buttons do the same preferring the reminder's own slot, and the nightly convergence pass heals the pairs this has already produced — one band match, one live anchor row, both days' rollups recomputed. The cache layer fences in-flight builders on invalidation so a read can neither join a pre-write build nor have a background refresh re-cache a pre-write body as fresh. Ad-hoc rows no longer resolve a different slot through the six-hour radius, a bounded early grace credits a take up to an hour before the window without crossing the previous band, day-scale cadences taken early downgrade the slot-day prompt instead of inviting a double dose, the overdue pill ignores the projector's pending mints, created intakes carry the same plausibility bounds as edits, slot moves keep the recorded injection site and dose, both affected days recompute on a moved toggle, deliberate slot-less skips converge instead of orphaning, the coach reads adherence from the ledger, the cards and quick-add run in the user's timezone with the served next-due as the gate, and schedule-revision, wipe and restore writes invalidate the caches they feed.
This commit is contained in:
1 parent
ea29e9cf1b
commit
aaecd141ee
53 files changed
+2550
-202
No files matched your search
@@ -116,6 +116,65 @@ describe("pickDefaultMedicationId — auto-select heuristic", () => {
|
||||
expect(result).toBe("b");
|
||||
});
|
||||
|
||||
it("does not pre-select a medication whose server next-due is in the future (v1.16.9)", () => {
|
||||
// The band alone reads "in window" at 10:00 Berlin, but the server
|
||||
// says the next unresolved dose is tomorrow (a rolling cadence whose
|
||||
// dose was taken) — the gate must keep the heuristic off it.
|
||||
const now = new Date("2026-05-12T08:00:00Z"); // 10:00 Berlin (UTC+2)
|
||||
const result = pickDefaultMedicationId(
|
||||
[
|
||||
makeMed("a", { name: "Aaa" }),
|
||||
makeMed("b", {
|
||||
name: "Bbb",
|
||||
nextDueAt: "2026-05-13T06:00:00.000Z", // tomorrow
|
||||
nextDueOverdue: false,
|
||||
schedules: [
|
||||
{
|
||||
id: "s1",
|
||||
windowStart: "08:00",
|
||||
windowEnd: "11:00",
|
||||
daysOfWeek: null,
|
||||
label: null,
|
||||
dose: null,
|
||||
},
|
||||
],
|
||||
}),
|
||||
],
|
||||
now,
|
||||
);
|
||||
// Nothing genuinely due → alphabetical fallback ("Aaa").
|
||||
expect(result).toBe("a");
|
||||
});
|
||||
|
||||
it("does not pre-select a day-scale medication already taken early in its period (v1.16.9)", () => {
|
||||
// Weekly Tuesday med (2026-05-12 is a Tuesday), taken two days ago.
|
||||
const now = new Date("2026-05-12T08:00:00Z"); // 10:00 Berlin
|
||||
const result = pickDefaultMedicationId(
|
||||
[
|
||||
makeMed("a", { name: "Aaa" }),
|
||||
makeMed("b", {
|
||||
name: "Bbb",
|
||||
lastTakenAt: "2026-05-10T07:00:00.000Z", // Sunday — early take
|
||||
nextDueAt: "2026-05-12T07:00:00.000Z", // today's slot (unresolved)
|
||||
nextDueOverdue: true,
|
||||
schedules: [
|
||||
{
|
||||
id: "s1",
|
||||
windowStart: "09:00",
|
||||
windowEnd: "09:00",
|
||||
daysOfWeek: "2",
|
||||
timesOfDay: ["09:00"],
|
||||
label: null,
|
||||
dose: null,
|
||||
},
|
||||
],
|
||||
}),
|
||||
],
|
||||
now,
|
||||
);
|
||||
expect(result).toBe("a");
|
||||
});
|
||||
|
||||
it("falls back to the alphabetical-first active medication when nothing is due", () => {
|
||||
// 03:00 Berlin time: no schedules overlap. The helper should
|
||||
// alphabetically sort and pick the leading id, "ramipril".
|
||||
|
||||
@@ -80,6 +80,15 @@ export interface MedicationOption {
|
||||
schedules: Schedule[];
|
||||
lastTakenAt: string | null;
|
||||
todayEventCount: number | null;
|
||||
/**
|
||||
* v1.16.9 — the server display-due verdict from `GET /api/medications`
|
||||
* (`nextDueAt` + `nextDueOverdue`). Threaded into the default pick so
|
||||
* the quick-add gates its "due now" heuristic exactly like the cards:
|
||||
* a rolling cadence whose next dose is days away must not pre-select.
|
||||
* Optional for older mocks; absent keeps the legacy band-only pick.
|
||||
*/
|
||||
nextDueAt?: string | null;
|
||||
nextDueOverdue?: boolean;
|
||||
}
|
||||
|
||||
interface MedicationIntakeQuickAddProps {
|
||||
@@ -127,6 +136,10 @@ export function pickDefaultMedicationId(
|
||||
|
||||
const nowBerlin = toBerlinDate(now);
|
||||
const due = actives.find((m) => {
|
||||
// v1.16.9 — the same server display-due gate the cards apply: a
|
||||
// future (non-overdue) next-due suppresses the overdue tiers and a
|
||||
// day-scale dose taken early in its period must not pre-select.
|
||||
const nextDueMs = m.nextDueAt ? new Date(m.nextDueAt).getTime() : NaN;
|
||||
const status = reduceCurrentWindowStatus({
|
||||
schedules: m.schedules,
|
||||
nowBerlin,
|
||||
@@ -135,8 +148,14 @@ export function pickDefaultMedicationId(
|
||||
active: true,
|
||||
lastTakenAt: m.lastTakenAt,
|
||||
todayEventCount: m.todayEventCount ?? 0,
|
||||
nextDue:
|
||||
m.nextDueAt === undefined
|
||||
? undefined
|
||||
: Number.isFinite(nextDueMs)
|
||||
? { at: new Date(nextDueMs), overdue: m.nextDueOverdue === true }
|
||||
: null,
|
||||
});
|
||||
return status.status !== null;
|
||||
return status.status !== null && !status.takenEarly;
|
||||
});
|
||||
if (due) return due.id;
|
||||
|
||||
|
||||
Reference in new issue
Block a user