Draft: feat(patients): Auto-generate unique sequential book number
Close #49 (closed)
MR: Auto-generate Unique Sequential Book Number
Summary
This MR adds a backend endpoint to generate the smallest available book_no automatically, ensuring uniqueness and gap-filling without relying on manual assignment.
Changes
- Added
GET /api/v1/patients/book_no/generateendpoint - Implemented efficient gap-detection logic using ordered/index-friendly queries
- Ensured generated
book_nonever collides with existing records - Added proper error handling for database failures
- Preserved existing patient creation APIs
Data Integrity
-
book_nouniqueness enforced at DB level - Endpoint generates a candidate number only
- Patient creation handles rare race-condition retries safely
Testing
- Verified behavior with:
- Empty table
- Contiguous sequences
- Gapped sequences
- Large ranges (thousands of records)
Notes
- Backend-only change
- Scalable and production-safe implementation
Edited by Kushal Lagichetty