Files
HealthLog/src/lib/medications/intake-tracking.ts
T
Marc-André Bombeck bb563b6506 Answer the native client's open server asks
- Publish timezone, glucoseUnit and a features block on the account
  payload; glucoseUnit reads as the closed pair the contract names, and
  features.trackIntake tells a separately released client that
  medications take the record-only switch.
- Refuse a new dose for a record-only medication with
  medication.intake.notTracked on every dose-writing route; the bulk
  route reports it per entry as intake_not_tracked, and correcting an
  already resolved event stays open.
- Answer passkey sign-in failures by name instead of a 500:
  passkey.challenge.expired, passkey.unknown, passkey.response.invalid
  and passkey.verification.failed, each with an audit row.
- Carry one account-wide adherence figure in meta.aggregate on the
  batched compliance read, weighted by expected doses, and accept
  ?days=30|60|90|180|365 for a report window.
- Accept ?days=30|90|180|365 on the mood insights read so stability
  covers the period shown, and name the window it covers.
- Keep only the reminder that announces a dose off APNs when the phone
  owns medication reminders; follow-ups for a dose still open are
  pushed again, marked by a followUp flag the reminder cron sets.
- Document which batch validation issues belong to an entry and pin it
  with a test that splits and resends a batch; publish rejectedFields
  items as ValidationIssue.
2026-10-08 10:32:11 +02:00

81 lines
3.4 KiB
TypeScript

/**
* v1.39.1 (#1033) — per-medication intake tracking.
*
* `Medication.trackIntake = false` keeps a medication as a record: its dose,
* dates and schedule rows stay stored and shown, but nothing is ever due. An
* as-needed medication reaches "never due" structurally, by carrying no
* schedule rows at all. A record-only medication keeps its rows, so every
* reader that derives a due dose, a reminder, a projected slot or an
* adherence figure from them has to opt out explicitly. The helpers below are
* the one vocabulary for that, and
* `src/__tests__/medication-intake-tracking-guard.test.ts` fails when a
* schedule reader uses none of them and is not on its reviewed allowlist.
*/
/**
* Prisma `where` fragment for a medication read: only medications whose
* intake is tracked. Spread it next to `asNeeded: false` on every read that
* feeds a due dose, a reminder or an adherence figure.
*/
export const TRACKED_INTAKE_WHERE = { trackIntake: true } as const;
/**
* Prisma `where` fragment for an intake-event read that should only see
* events of tracked medications (the today list, the outstanding-dose badge).
*/
export const TRACKED_INTAKE_EVENT_WHERE = {
medication: { trackIntake: true },
} as const;
/**
* True only for an explicit `trackIntake: false`. The column is NOT NULL
* with a default of true, so on a full row the two readings agree; on a
* partial projection that did not select the flag, the medication reads as
* tracked, the behaviour every medication had before the switch existed.
*/
export function isRecordOnly(medication: { trackIntake?: boolean }): boolean {
return medication.trackIntake === false;
}
/**
* The schedule rows that are in force for due-dose purposes: the stored rows
* when intake is tracked, none when it is not. Era-aware readers pass this as
* the LIVE schedule, so the stretch since tracking was switched off expects
* nothing while archived eras before it keep their own schedule.
*/
export function dueSchedules<T>(medication: {
trackIntake: boolean;
schedules: readonly T[];
}): T[] {
return isRecordOnly(medication) ? [] : [...medication.schedules];
}
/**
* Wire shape for a medication's schedules. A record-only medication is served
* with `schedules: []`, the way an as-needed one is, so a client that arms
* local reminders or derives due doses from `schedules` (the shipped iOS app
* does both) stays silent without an update. The stored rows travel in the
* additive `recordedSchedules` field, which is present only on a record-only
* medication; a tracked medication carries them in `schedules` as before.
*/
export function scheduleWireFields<T>(
trackIntake: boolean,
schedules: T[],
): { schedules: T[]; recordedSchedules?: T[] } {
return trackIntake === false
? { schedules: [], recordedSchedules: schedules }
: { schedules };
}
/**
* Error code for a dose written against a record-only medication. Every route
* that records a dose answers it the same way (iOS #116, item 15): the history
* the medication already has stays, but nothing new is counted, so a new
* write is refused by name rather than stored and then counted nowhere.
* The bulk route reports the same decision per entry as `intake_not_tracked`.
*/
export const INTAKE_NOT_TRACKED_ERROR_CODE = "medication.intake.notTracked";
/** Per-entry skip reason for the bulk route, the batch form of the code. */
export const INTAKE_NOT_TRACKED_REASON = "intake_not_tracked";