Case study 02 / Public pharmacy operations
DispensaMais
The right medicine, in the right amount, from the right lot.
Built end to end · private repositoryThe problem
A pharmacy needs to know more than whether a medicine is in stock. Staff must check who is authorized to receive it, how much remains in the current coverage cycle and which lot should be dispensed. Repeated requests and concurrent edits must not silently create additional stock movements.
System structure
A boundary worth making explicit.
A React interface calls a FastAPI service backed by relational models for patient authorizations, dispensations, items and stock events. Authorization and dispensing are separate records. Tenant-scoped queries, transaction locks and a tenant-level idempotency constraint protect the write path.
- Authorization
Patient · medicine · version
- Eligibility checks
Active dates · cycle balance
- Dispensing
Lot · quantity · stock event
Engineering decisions
Check the version before moving stock
The dispensing service compares the expected authorization version with the current record. A stale request is rejected before a stock mutation. Row locks coordinate access to the authorization during the transaction.
Give retries a stable identity
Dispensations have an idempotency key unique within a tenant and a payload hash. The intent is to recognize a repeated request instead of treating every retry as a new dispensing event.
Keep the domain visible in the data model
Dispensation items retain the authorization, batch and stock-event references. The model also records the recommended batch and an override reason, making lot-selection decisions inspectable.
Verification & current state
The repository includes integration scenarios for partial dispensing, authorization changes within a cycle, concurrent requests, repeat idempotency keys, negative-stock prevention and cross-tenant isolation.
Operational workflows are implemented. National health-system integrations require additional configuration, and production readiness has separate release requirements.