fix(consultations): resolve route conflict and UUID handling in reassign doctor API
Merge Request
Overview
This MR fixes an issue in the reassign doctor API where valid UUIDs were being rejected with "Invalid consultation ID format". The problem was caused by incorrect route matching and improper UUID handling.
What does this MR do and why?
The reassign doctor endpoint (/consultations/reassign-doctor) was incorrectly being matched as a dynamic route (/{consultation_id}), causing FastAPI to interpret "reassign-doctor" as a consultation ID and fail UUID validation.
To resolve this:
- Routes were reordered to ensure static routes are matched before dynamic ones
- UUID handling was standardized across schema and service layers
- Redundant manual validation logic was removed
This ensures correct routing and proper handling of UUID inputs without affecting existing functionality.
Changes Made
-
Modified
app/api/v1/routes/consultations_routes.py- Reordered routes to place
/reassign-doctorbefore/{consultation_id}
- Reordered routes to place
-
Updated request schema
- Changed
consultation_idanddoctor_idtypes toUUID
- Changed
-
Updated service layer
- Changed function signature to accept
UUIDinstead ofstr - Removed manual UUID validation logic
- Fixed direct UUID comparison in queries
- Changed function signature to accept
Technical Details
Root Cause:
FastAPI route matching prioritized the dynamic route /{consultation_id} over /reassign-doctor, causing incorrect parsing of path parameters.
Fix: Reordered routes so that static routes are defined before dynamic routes, ensuring correct endpoint matching.
Additionally:
- Removed unnecessary UUID parsing (
uuid.UUID()) - Relied on Pydantic for automatic validation
- Ensured consistent UUID usage across layers
Type of Change
-
🐛 Bug fix (non-breaking change that fixes an issue)
Related Issues / References
- Fixes route matching issue in consultations API
- Resolves UUID validation error in reassign doctor endpoint
- closes #60 (closed)
How to Validate Locally
- Start the backend:
docker compose up
- Login to get token:
curl -X POST "http://localhost:8000/api/v1/auth/login" \
-H "Content-Type: application/json" \
-d '{
"phone_number": "8225665117",
"password": "admin123"
}'
- Call reassign doctor API:
curl -X PUT "http://localhost:8000/api/v1/consultations/reassign-doctor" \
-H "Authorization: Bearer <ACCESS_TOKEN>" \
-H "Content-Type: application/json" \
-d '{
"consultation_id": "14d69109-0c90-4ce8-bfcc-0e4887b8ed94",
"doctor_id": "a00097cc-4969-4a17-9e0a-1b06f1788b8b"
}'
- Verify response:
- Should return success message
- Doctor ID should be updated in DB
Testing Done
-
Unit tests added/updated -
API endpoint tests passing
Test Cases Covered:
| Scenario | Expected Result | Status |
|---|---|---|
| Valid reassignment | Doctor updated successfully | |
| Same doctor reassignment | Returns "Doctor already assigned" | |
| Invalid consultation ID | Returns "Consultation not found" | |
| Invalid doctor ID | Returns "Doctor not found" |
Test Commands Run:
pytest
Code Quality Checklist
Code Standards
-
Code follows project conventions -
No debug statements left -
No unused imports -
No duplicate code -
Proper type hints used
Python & FastAPI Best Practices
-
Functions follow single responsibility -
Pydantic models used for validation -
Dependency injection used correctly -
SQLAlchemy ORM used properly -
Proper error handling implemented
API Design
-
RESTful conventions followed -
Proper HTTP status codes returned -
Input validation implemented -
Authentication enforced
Database & Migrations
-
No schema changes -
ORM queries used -
Data integrity maintained
Security
-
No sensitive data exposed -
Token-based authentication used -
Input validation handled
Error Handling
-
Proper HTTPException usage -
Clear error messages returned
Documentation
-
API documentation updated via schema changes
Known Limitations / Technical Debt
- Analytics-related test failures are unrelated to this change and stem from prior schema modifications
Additional Notes
This fix ensures correct routing behavior in FastAPI by prioritizing static routes over dynamic routes, which is a common best practice to avoid route conflicts.
MR Acceptance Checklist
Quality & Correctness
-
Code works as intended -
No bugs introduced -
Edge cases handled
Maintainability
-
Code is clean and readable -
Follows project conventions
Acceptance Review
-
Reviewed by
