fix(medications): make inventory Bestand summary server-authoritative

The medication detail clients computed the supply headline in the browser
via summariseSupply, risking web/iOS drift and pulling the shared math
into the client bundle. The GET /api/medications/[id]/inventory route now
computes the canonical SupplySummary server-side through summariseSupply
(the single source of truth) and ships it in the DTO; the Bestand and
Übersicht surfaces render the ready figures. Adds the MedicationSupplySummary
schema to the inventory list response and regenerates the OpenAPI contract.
This commit is contained in:
Marc-André Bombeck committed 2026-06-21 10:19:41 +02:00
1 parent 7540d0b508
commit 2386d1fe78
10 files changed
+292 -60

No files matched your search

+3 -1
View File
@@ -22,6 +22,7 @@ import {
medicationListEntry,
medicationDetailEntry,
medicationInventoryItemResource,
medicationSupplySummaryResource,
medicationIntakeEventResource,
medicationCadenceResponse,
medicationComplianceResponse,
@@ -285,7 +286,7 @@ export const medicationPaths: NonNullable<ZodOpenApiObject["paths"]> = {
tags: ["Medications"],
summary: "List a medication's supply containers",
description:
"Returns every inventory item (all states) for the medication, ordered by state, then `expiresAt`, then `createdAt`. Items count UNITS; divide by the medication's `unitsPerDose` for dose-level figures.",
"Returns every inventory item (all states) for the medication, ordered by state, then `expiresAt`, then `createdAt`. Items count UNITS; divide by the medication's `unitsPerDose` for dose-level figures. v1.19.0 (iOS#25) — also returns a server-computed `summary` (the canonical {`unitsRemaining`, `unitsTotal`, `dosesRemaining`, `dosesTotal`, `expiredUnits`}); clients render it directly rather than re-deriving the Bestand headline, so web and iOS agree.",
requestParams: {
path: z.object({ id: z.string() }),
},
@@ -297,6 +298,7 @@ export const medicationPaths: NonNullable<ZodOpenApiObject["paths"]> = {
schema: dataEnvelope(
z.object({
items: z.array(medicationInventoryItemResource),
summary: medicationSupplySummaryResource,
meta: z.object({ total: z.number().int().nonnegative() }),
}),
"ListMedicationInventoryResponse",
@@ -319,6 +319,41 @@ export const medicationInventoryItemResource = z
"One supply container (pen / blister pack / bottle) of a medication. Counts UNITS — the medication's `unitsPerDose` maps units to doses. The intake write paths consume from the open container first, then first-expiry-first-out over unopened stock.",
});
// v1.19.0 (iOS#25) — server-computed canonical supply summary returned
// alongside the inventory list. Replaces the former client-side
// derivation so web and iOS render identical Bestand figures from one
// DTO. Pools ACTIVE / IN_USE containers with units left; EXPIRED stock
// is surfaced separately and never counts as available.
export const medicationSupplySummaryResource = z
.object({
unitsRemaining: z
.number()
.describe(
"Pooled units across available (ACTIVE / IN_USE, units left) containers. Floored at 0 — a corrupt / legacy negative row can never surface a negative Bestand.",
),
unitsTotal: z
.number()
.describe("Pooled capacity across the same available containers."),
dosesRemaining: z
.number()
.describe(
"Dose-derived headline: `floor(unitsRemaining / unitsPerDose)` (whole doses; a partial dose is not a dose).",
),
dosesTotal: z
.number()
.describe("Dose-derived capacity: `floor(unitsTotal / unitsPerDose)`."),
expiredUnits: z
.number()
.describe(
"Units still sitting in EXPIRED containers — visible to the user as a muted suffix, never folded into the available headline or the runway estimate.",
),
})
.meta({
id: "MedicationSupplySummary",
description:
"Server-authoritative supply summary for a medication's containers. Computed from the same availability predicate the medications-list payload and the GLP-1 endpoint use, so every surface agrees on what 'remaining' means.",
});
export const medicationIntakeEventResource = z
.object({
id: z.string(),