Cabinet Médical
Seven different businesses - lab tests, parapharmacy, rental, stock, invoicing, HR, payroll - one pivot that holds them all.
An Algerian medical centre runs seven distinct activities on separate media: lab test orders, parapharmacy sales, medical vehicle rental, stock, invoicing, HR and CNAS contributions. The initial scope was priced and approved, then HR and payroll were added along the way - without any of these blocks becoming one more silo.
A SaaS built around a pivot entity, Order, which routes the three flow-generating businesses (lab tests, parapharmacy, rental) through the same quote → purchase order → delivery or contract → invoice path, with public QR tracking for client companies. HR and payroll live in a separate domain, with their own contribution and leave balance calculations.
Results
- 7
- business modules
- 22
- data models
- 23
- admin pages
- 100%
- audited mutations
Interactive demo
Seven businesses, one document flow
Change the order type and its status: the same pipeline opens up step by step.
Order type
Current status
Quote
can be generated
Purchase order
from CONFIRMEE
Delivery note
from EN_COURS
Invoice
from TERMINEE
The three businesses use the same flow: only rental swaps the delivery note for a contract. The lock is on a minimum status, not on the previous step, because real orders skip steps: a lab test regularly goes from quote to invoice without a delivery note.
Mechanism rebuilt for this site, not a screenshot of the product. Rule taken from lib/documents.ts, documentsGenerables(). No data: the demo only shows the rule.
From the interface down to the data
01
Interface
23 App Router pages, a generic shadcn/ui theme pending the client's final brand guidelines. The 360° detail sheets (patient, company, vehicle, product) all follow the same pattern: two columns, full history, collapsible edit form.
02
Pivot domain
Order, with a discriminating type lab | parapharma | rental, carries status and QR token and feeds the same quote → PO → delivery/contract → invoice pipeline. HR remains a separate domain, outside this pivot.
03
Server actions
13 Server Action modules validated by Zod. Every mutation writes to the append-only audit log, in the same transaction as the write it documents.
04
Persistence
PostgreSQL through Prisma 7 and @prisma/adapter-pg, a database-level EXCLUDE constraint against overlapping bookings, and a cabinetId already present on every business table.
Engineering decisions
The choices worth defending, and why.
- 01
One pivot entity for three businesses, a separate domain for HR
Order - discriminated by a type lab | parapharma | rental - carries status and QR token and feeds the same quote → purchase order → delivery or contract → invoice path, whatever the business. When HR was added later, the temptation was to fit it into the same pivot to reuse the document flow. Bad idea: an employment contract is not an order. HR stayed a domain of its own - Employee → Employment contract → Leave / Payslip - with its own cabinetId on each table rather than an implicit dependency on the employee relation.
- lib/actions/commandes.ts
- prisma/schema.prisma
- 02
Preventing double booking in the database, not in the code
Two bookings must never overlap on the same vehicle. Rather than checking it in the application - and so in a way a concurrent write can bypass - the guarantee is a PostgreSQL EXCLUDE constraint. The application code detects the violation through an error code (P2039) that is not officially documented, obtained through a fallback path of the Prisma runtime and checked empirically against the installed version. It is a fragile coupling, accepted as such: a generic safety net exists if this code changes at the next Prisma upgrade, and the fix is already identified - retest by deliberately triggering an overlap.
- prisma/migrations/20260830130533_reservation_no_overlap
- lib/actions/commandes.ts
- 03
An invoice number that can never be duplicated
Sequential numbering per (centre, document type, year) goes through an upsert on a dedicated counter, in the same transaction as the document creation. Each document freezes its total amount at generation and is never recalculated: correcting a mistake cancels the order, never a document already issued. The same caution as for bookings - the atomicity of this upsert has only been checked under sequential writes, not yet under heavy concurrency.
- lib/actions/documents.ts
- lib/documents.ts
- 04
External tracking never sees clinical content
Client companies - occupational health, B2B parapharma accounts - never get a staff account, only a tracking link. The token is opaque, random and expiring: never the order's sequential identifier in a URL. The public page filters the exposed fields in the query itself - status and dates, never medical data - so that an interface mistake cannot be enough to leak clinical content.
- app/suivi/[token]/page.tsx
- lib/audit.ts
- 05
Compliance chose the hosting before the code existed
Algerian law on health data (18-07/25-11) requires hosting physically located in Algeria as soon as real patient data is involved - even in so-called stateless serverless compute. This demo, with fictitious data, is therefore explicitly scoped to Vercel + Supabase for client validation; the real deployment will move to Algeria the day real patient data enters it. The schema does not wait for that day: a cabinetId is already set on every business table while V1 remains single-centre, so as not to pay for a heavy migration later.
- CLAUDE.md
- docs/cadre-legal.html
Tech stack
- Next.js 16
- React 19
- TypeScript
- Prisma 7
- PostgreSQL
- Supabase
- @react-pdf/renderer
- Zod
A similar project?
Other projects
in productionFleetOra
B2B SaaS · own product
in productionBioInteraction DZ
Website & technical catalogue · client
in productionHSI Pro
B2B and consumer shop · client
delivered locallySuivi Commercial
Internal application · client
in serviceBio Réactifs
Desktop applications · internal tools
in local testingSocialPlug
Publishing tool · personal use