fix(medications): every taken dose moves the stock, in units that match the product

Stock only decremented on the web card's take button — the status
toggle, the phone sync, the Telegram buttons and the token API all
recorded the dose and left the inventory untouched, which is why a
fresh pack kept reading full. Consumption now stamps the intake row
itself: every path that turns a row taken consumes through one module
— open container first, then earliest expiry with auto-open, spilling
into the next pack and flooring at zero — and every path that takes it
back (undo, delete, a take turned skip, a converged duplicate)
refunds exactly what the stamp recorded. Replays and pending sync
echoes can never double-charge, and the nightly convergence pass
refunds the rows it tombstones. A medication can declare units per
dose, so two 2 mg tablets book as one 4 mg dose and stock reads in
doses with the unit count alongside; containers carry a named type
including tablet packs, hold up to a thousand units for inhalers, and
the GLP-1 card reads the same stock as the inventory tab instead of a
separate ledger. The inventory endpoints join the documented API.
This commit is contained in:
Marc-André Bombeck committed 2026-06-12 17:42:31 +02:00
1 parent dacdcccfd2
commit 7d3557a3fd
53 files changed
+4118 -641

No files matched your search

@@ -82,6 +82,15 @@ export const GET = apiHandler(async (request: NextRequest) => {
orderBy: { takenAt: "desc" },
take: 50,
},
// v1.16.10 divergence note: the stock READOUTS (glp1 details
// endpoint, Coach snapshot) moved to the per-item
// MedicationInventoryItem entities, but this timeline renders the
// append-only MedicationInventoryEvent LEDGER as point-in-time
// entries ("+2 pens purchased") — a per-item swap is not
// mechanical (items carry no per-mutation history). The ledger
// only grows through the legacy `POST /api/medications/[id]/glp1`
// inventory branch, so timelines on item-tracked medications show
// no new inventory entries.
inventoryEvents: { orderBy: { occurredAt: "desc" }, take: 20 },
},
});