mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +00:00
A medication whose slots carry different doses (a whole tablet in the morning, a half at noon — #219) was consumed correctly but reported wrongly: the wire shipped only the raw nullable per-slot column (clients had to derive the inheritance), stockDosesRemaining divided by the medication-level units alone, and no runway figure existed outside the low-stock push. - Every schedule now carries resolvedUnitsPerDose, resolved server-side exactly like the intake consumption resolver. - stockDosesRemaining divides the units pool by the schedule-weighted average units per dose. - runwayDays (whole days of supply under the slot-aware burn rate) rides the list AND detail payloads; the detail GET also gains the stock aggregates the list already had. - The low-stock engine runs the same slot-aware math, so the push and the wire cannot disagree; the web cards, table, and detail Übersicht prefer the server figure over their client estimate. - OpenAPI regenerated with the new fields.
44 lines
1.7 KiB
TypeScript
44 lines
1.7 KiB
TypeScript
/**
|
|
* #219 — per-schedule `unitsPerDose` wire serialisation.
|
|
*
|
|
* The column is a nullable `Decimal(10,4)`; Prisma serialises a Decimal to a
|
|
* JSON STRING, which the client would then have to coerce. The medication-level
|
|
* `unitsPerDose` is already unwrapped to a JSON number at every response point
|
|
* (`Number(medication.unitsPerDose)`), so mirror that for the per-schedule
|
|
* column: convert to a number, keep NULL as NULL (NULL means "inherit the
|
|
* medication level"). One helper so the five medication read/write responses
|
|
* cannot drift on the shape.
|
|
*
|
|
* v1.37.19 (iOS #25 parity) — every schedule additionally carries
|
|
* `resolvedUnitsPerDose`: the EFFECTIVE units this slot consumes, resolved
|
|
* server-side (`schedule.unitsPerDose ?? medication.unitsPerDose`, matching
|
|
* the consumption resolver's >0 guard). Publish the resolved value; no
|
|
* client re-derives the inheritance rule. The raw nullable `unitsPerDose`
|
|
* stays beside it because the edit surface must distinguish "explicit"
|
|
* from "inherits".
|
|
*/
|
|
import type { Prisma } from "@/generated/prisma/client";
|
|
|
|
type ScheduleWithUnits = { unitsPerDose: Prisma.Decimal | null };
|
|
|
|
export function serializeScheduleUnitsPerDose<T extends ScheduleWithUnits>(
|
|
schedules: readonly T[],
|
|
medicationUnitsPerDose: Prisma.Decimal | number,
|
|
): Array<
|
|
Omit<T, "unitsPerDose"> & {
|
|
unitsPerDose: number | null;
|
|
resolvedUnitsPerDose: number;
|
|
}
|
|
> {
|
|
const fallback = Number(medicationUnitsPerDose);
|
|
return schedules.map((schedule) => {
|
|
const raw =
|
|
schedule.unitsPerDose === null ? null : Number(schedule.unitsPerDose);
|
|
return {
|
|
...schedule,
|
|
unitsPerDose: raw,
|
|
resolvedUnitsPerDose: raw !== null && raw > 0 ? raw : fallback,
|
|
};
|
|
});
|
|
}
|