feat: implement DOB-based age calculation
Overview
This MR replaces the usage of age with Date of Birth (DOB) and introduces dynamic age calculation. Additionally, it includes a migration strategy to handle existing users who previously had only age stored.
What does this MR do and why?
Previously, patient age was stored directly, which could become inaccurate over time.
This MR:
- Introduces DOB as the primary field
- Computes age dynamically from DOB
- Migrates existing users from age → DOB
This ensures:
- Accurate and consistent data
- Backward compatibility for existing users
- Smooth transition without data loss
Changes Made
-
Removed direct dependency on
age -
Added
date_of_birthfield -
Implemented age calculation utility from DOB
-
Added DOB validation (valid date, no future dates, 0–150 range)
-
Migration logic for existing users:
- Converted stored age → approximate DOB
- Ensured no null/invalid states
-
Updated API responses to return computed
patient_age
Technical Details
-
Age is no longer stored; it is derived from DOB
-
For existing users:
-
DOB is generated using:
DOB = current_date - age (years) -
This is an approximation (exact birthdate unknown)
-
-
Migration ensures:
- No breaking changes for old data
- All users now have DOB
Type of Change
-
🐛 Bug fix -
✨ New feature -
💥 Breaking change -
♻ ️ Refactor
How to Validate Locally
-
Check old users:
- Ensure DOB is populated after migration
-
Create new user with DOB:
- Verify validation works
-
Fetch user:
- Confirm age is correctly calculated
-
Compare:
- Old users → migrated DOB
- New users → actual DOB
Testing Done
-
Unit tests added/updated -
API endpoint tests passing
Test Cases Covered:
| Scenario | Expected Result | Status |
|---|---|---|
| Old user migration | DOB generated correctly | |
| Valid DOB input | Accepted | |
| Future DOB | Rejected | |
| Age calculation | Accurate |
Known Limitations / Technical Debt
- Migrated DOB is approximate, not exact
- Future improvement: allow users to update DOB manually
Additional Notes
- Ensures backward compatibility
- Smooth migration without breaking existing records
- Aligns backend fully with DOB-based system