mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +00:00
- The escalated card row keeps its time anchor: the red badge carries the matched window or the overdue-slot value beside it, and the inline row's range stays visible on mobile, where it was the only clock on the card. - Take-all-due collects what the cards offer: a dose escalated past its catch-up band joins the set with exactly the slot the card's own button would record; as-needed medications never join. - The table's stock warning follows the user's runway threshold instead of a leftover four-dose constant; threshold off means no amber, empty stays red. - The card itself shows the remaining runway in the warning tone once it crosses the threshold — only then, the card stays calm above. - The low-stock push deep-links straight into the supply tab and its APNs payload keeps url, runwayDays and thresholdDays; supply tab and threshold setting link to each other; the reminder-thresholds and notification-prefs endpoints enter the OpenAPI contract.
253 lines
10 KiB
TypeScript
253 lines
10 KiB
TypeScript
/**
|
|
* v1.16.11 (#316) — "take all due at once": the due-set derivation plus
|
|
* the confirm-time orchestration behind the medications-page header
|
|
* button. A user with several medications due in the current window
|
|
* confirms them in ONE action instead of per-card taps.
|
|
*
|
|
* Due-set derivation reuses the EXACT pipeline a single card runs —
|
|
* `reduceCurrentWindowStatus` (band model + server display-due gate +
|
|
* taken-early downgrade) and `resolveDisplayedSlotInstant` (the canonical
|
|
* slot the card's own "Genommen" button would record) — so the set the
|
|
* dialog lists is, by construction, the set of cards currently showing a
|
|
* take-now / overdue pill. No new endpoint: every input rides on the
|
|
* `GET /api/medications` list payload the page already holds.
|
|
*
|
|
* Recording loops the per-medication `POST /api/medications/{id}/intake`
|
|
* (the same call `runRecordIntake` makes for one card) rather than the
|
|
* `POST /api/medications/intake/bulk` endpoint, deliberately:
|
|
*
|
|
* - The bulk route is the iOS SyncMode surface: its taken path requires
|
|
* an explicit `takenAt` and attributes by band membership
|
|
* (`resolveSlotForWriteByBand`) or a `forceSlotInstant` user pin. The
|
|
* interactive card semantics instead send the DISPLAYED slot as
|
|
* `scheduledFor` and let the server stamp `takenAt = now` — for an
|
|
* overdue slot whose catch-up tail extends past the band, the two
|
|
* attributions can diverge. Looping the single route keeps slot
|
|
* attribution, inventory consumption (`consumedTransition` gate) and
|
|
* the canonical-slot upsert idempotency byte-identical to N
|
|
* individual taps.
|
|
* - Per-medication failure isolation falls out naturally: each POST
|
|
* succeeds or fails alone, the summary toast reports both counts, and
|
|
* the failed medications simply stay due (the invalidation refetch
|
|
* re-derives them into the set).
|
|
*
|
|
* The due set is small (a handful of medications, never the 500-entry
|
|
* sync scale), so N sequential round trips are negligible.
|
|
*/
|
|
|
|
import type { QueryClient } from "@tanstack/react-query";
|
|
import { toast } from "sonner";
|
|
|
|
import { apiPost } from "@/lib/api/api-fetch";
|
|
import { invalidateKeys, medicationDependentKeys } from "@/lib/query-keys";
|
|
import {
|
|
reduceCurrentWindowStatus,
|
|
toZonedDate,
|
|
type ScheduleWindowInput,
|
|
} from "@/lib/medications/window-status";
|
|
import { resolveDisplayedSlotInstant } from "@/components/medications/card-parts/displayed-slot-instant";
|
|
import type { DoseStatus } from "@/lib/analytics/compliance";
|
|
|
|
type Translator = (
|
|
key: string,
|
|
params?: Record<string, string | number>,
|
|
) => string;
|
|
|
|
/** Schedule shape the derivation needs — the list payload is a superset. */
|
|
export interface DueDerivationSchedule extends ScheduleWindowInput {
|
|
dose: string | null;
|
|
}
|
|
|
|
/** Medication shape the derivation needs — the list payload is a superset. */
|
|
export interface DueDerivationMedication {
|
|
id: string;
|
|
name: string;
|
|
dose: string;
|
|
active: boolean;
|
|
pausedAt: string | null;
|
|
lastTakenAt: string | null;
|
|
todayEventCount?: number | null;
|
|
/** Server display-due verdict from `GET /api/medications`. */
|
|
nextDueAt?: string | null;
|
|
nextDueOverdue?: boolean;
|
|
/** v1.16.11 (#316) — as-needed (PRN): never due, never in the set. */
|
|
asNeeded?: boolean;
|
|
schedules: DueDerivationSchedule[];
|
|
}
|
|
|
|
/** One medication the confirm dialog lists and the take loop records. */
|
|
export interface DueMedication {
|
|
id: string;
|
|
name: string;
|
|
/** Matched schedule dose, falling back to the medication-level dose. */
|
|
dose: string;
|
|
/** HH:mm bounds of the matched dose band, for the dialog's window line. */
|
|
window: { start: string; end: string } | null;
|
|
/**
|
|
* Canonical slot instant the take records (threaded as `scheduledFor`,
|
|
* exactly like the card's own button). Null preserves the PRN /
|
|
* unscheduled now-snap path.
|
|
*/
|
|
scheduledFor: Date | null;
|
|
}
|
|
|
|
const DEFAULT_THRESHOLDS = { lateMinutes: 120, missedMinutes: 240 };
|
|
|
|
/**
|
|
* Derive the medications that are due RIGHT NOW — active, unpaused, with
|
|
* a non-null current-window status (in_window / late / very_late) that is
|
|
* not the day-scale taken-early downgrade. Input order is preserved so
|
|
* the dialog lists medications in the same order the page renders them.
|
|
*
|
|
* The due set ADDITIONALLY includes escalated medications: an active,
|
|
* unpaused, non-as-needed medication whose batched compliance
|
|
* `currentDose.status` (`doseStatusById`, the same
|
|
* `useMedicationComplianceSummaryAll` rows the page and the table hold)
|
|
* is `overdue` / `missed`. A dose whose band + catch-up tail have closed
|
|
* renders the red escalation value with an ACTIVE take button on its
|
|
* card, so it must be takeable from the batch too — the window-only
|
|
* derivation silently dropped it. The recorded slot for such an entry is
|
|
* exactly what the card's own take button records: the SAME
|
|
* `resolveDisplayedSlotInstant` call over the same (closed) window
|
|
* status, which resolves to the server's `nextDueAt` instant (or null,
|
|
* preserving the unscheduled now-snap path). Entries dedupe by
|
|
* construction — the window branch and the escalation branch are
|
|
* mutually exclusive per medication.
|
|
*/
|
|
export function deriveDueMedications(
|
|
medications: DueDerivationMedication[],
|
|
options: {
|
|
now?: Date;
|
|
/** IANA timezone (profile timezone; Berlin is the legacy fallback). */
|
|
tz?: string;
|
|
thresholds?: { lateMinutes: number; missedMinutes: number };
|
|
/**
|
|
* Batched compliance dose status per medication id (the page's
|
|
* `useMedicationComplianceSummaryAll` rows). Absent / unknown ids
|
|
* keep the window-only derivation — the legacy behaviour.
|
|
*/
|
|
doseStatusById?: ReadonlyMap<string, DoseStatus>;
|
|
} = {},
|
|
): DueMedication[] {
|
|
const now = options.now ?? new Date();
|
|
const tz = options.tz ?? "Europe/Berlin";
|
|
const { lateMinutes, missedMinutes } =
|
|
options.thresholds ?? DEFAULT_THRESHOLDS;
|
|
const nowLocal = toZonedDate(now, tz);
|
|
|
|
const due: DueMedication[] = [];
|
|
for (const m of medications) {
|
|
// Paused courses keep their schedules but must never surface here —
|
|
// the cards suppress their pills the same way. As-needed (PRN)
|
|
// medications are never due, structurally — even a (bogus)
|
|
// compliance row for one must not pull it into the set.
|
|
if (!m.active || m.pausedAt || m.asNeeded) continue;
|
|
|
|
const nextDueMs = m.nextDueAt ? new Date(m.nextDueAt).getTime() : NaN;
|
|
const status = reduceCurrentWindowStatus({
|
|
schedules: m.schedules,
|
|
nowBerlin: nowLocal,
|
|
lateMinutes,
|
|
missedMinutes,
|
|
active: m.active,
|
|
lastTakenAt: m.lastTakenAt,
|
|
todayEventCount: m.todayEventCount ?? 0,
|
|
tz,
|
|
// The same server display-due gate the cards apply: a future
|
|
// (non-overdue) next-due suppresses the overdue tiers; `undefined`
|
|
// (older mocks) keeps the legacy band-only behaviour.
|
|
nextDue:
|
|
m.nextDueAt === undefined
|
|
? undefined
|
|
: Number.isFinite(nextDueMs)
|
|
? { at: new Date(nextDueMs), overdue: m.nextDueOverdue === true }
|
|
: null,
|
|
});
|
|
// `takenEarlyDaysAgo` non-null = a day-scale dose already on board
|
|
// earlier in its period; including it would build a double-dose
|
|
// confirm list. The card suppresses every prompt tier (incl. the red
|
|
// escalation value) the same way.
|
|
if (status.takenEarlyDaysAgo !== null) continue;
|
|
|
|
// Escalation alignment with the card: a compliance-derived
|
|
// overdue / missed dose keeps an active take button on its card even
|
|
// after the band's catch-up tail closed (window status null).
|
|
const complianceStatus = options.doseStatusById?.get(m.id);
|
|
const escalated =
|
|
complianceStatus === "overdue" || complianceStatus === "missed";
|
|
if (!status.status && !escalated) continue;
|
|
|
|
due.push({
|
|
id: m.id,
|
|
name: m.name,
|
|
dose: status.schedule?.dose ?? m.dose,
|
|
window: status.window
|
|
? { start: status.window.start, end: status.window.end }
|
|
: null,
|
|
scheduledFor: resolveDisplayedSlotInstant({
|
|
currentWindowStatus: status,
|
|
nextDueAt: m.nextDueAt ?? null,
|
|
now,
|
|
timeZone: tz,
|
|
}),
|
|
});
|
|
}
|
|
return due;
|
|
}
|
|
|
|
/**
|
|
* Record every due medication through the per-medication intake route,
|
|
* sequentially, with per-medication failure isolation (see the module doc
|
|
* for the loop-vs-bulk decision). Dependencies are injected so this is
|
|
* unit-testable without a React render, mirroring `runRecordIntake`.
|
|
*
|
|
* Toast contract: all-success → one success summary; partial → an error
|
|
* summary carrying both counts ("3 erfasst, 1 fehlgeschlagen"); all-fail
|
|
* → a plain failure prompt. Failed medications stay due — the dependent-
|
|
* key invalidation refetches the list and the derivation re-surfaces
|
|
* them. The optional per-card injection-site follow-up prompt is
|
|
* intentionally NOT part of the batch confirm (it is skippable on the
|
|
* single path too); the site stays editable from the intake history.
|
|
*/
|
|
export async function runTakeAllDue(deps: {
|
|
medications: DueMedication[];
|
|
t: Translator;
|
|
queryClient: QueryClient;
|
|
}): Promise<{ taken: number; failed: number }> {
|
|
const { medications, t, queryClient } = deps;
|
|
let taken = 0;
|
|
let failed = 0;
|
|
|
|
for (const med of medications) {
|
|
try {
|
|
await apiPost(
|
|
`/api/medications/${med.id}/intake`,
|
|
med.scheduledFor
|
|
? { skipped: false, scheduledFor: med.scheduledFor.toISOString() }
|
|
: { skipped: false },
|
|
);
|
|
taken += 1;
|
|
} catch {
|
|
failed += 1;
|
|
}
|
|
}
|
|
|
|
// One invalidation for the whole batch (the bundle includes the
|
|
// dashboard snapshot key, so the hero dose tally refreshes too). A
|
|
// zero-success run changed nothing server-side — skip the refetch and
|
|
// keep the due set as-is for the retry.
|
|
if (taken > 0) {
|
|
await invalidateKeys(queryClient, medicationDependentKeys);
|
|
}
|
|
|
|
if (failed === 0) {
|
|
toast.success(t("medications.takeAllDue.successToast", { count: taken }));
|
|
} else if (taken > 0) {
|
|
toast.error(t("medications.takeAllDue.partialToast", { taken, failed }));
|
|
} else {
|
|
toast.error(t("medications.takeAllDue.failedToast"));
|
|
}
|
|
|
|
return { taken, failed };
|
|
}
|