fix: login logout buttons in manage doctor
Merge Request
Overview
This MR enhances the /doctors API to include real-time login/logout status (login_time, logout_time) for each doctor based on the active medical camp.
This change ensures the frontend can accurately determine doctor availability without relying on temporary client-side state.
What does this MR do and why?
Previously, the /doctors API returned only static doctor details and did not include attendance information. As a result:
- Frontend could not determine login/logout state after refresh
- UI relied on cache hacks (
setQueryData) - State inconsistencies occurred
This MR solves the problem by:
- Fetching
CampVisitrecords for the active camp - Mapping login/logout timestamps to each doctor
- Returning enriched doctor data directly from backend
This makes backend the single source of truth and ensures consistent UI behavior.
Changes Made
-
Modified
/doctorsAPI endpoint to include:login_timelogout_time
-
Added logic to:
- Fetch active medical camp
- Query
CampVisitrecords filtered byCampRoleEnum.doctor
-
Implemented efficient mapping:
- Created
visit_mapusinguser_id
- Created
-
Attached attendance data to doctor response
-
Avoided N+1 queries using bulk fetch and mapping
Technical Details
Root Cause:
- Attendance data (
CampVisit) was not linked to doctor API response - Frontend lacked required state after refresh
Fix:
-
Fetch all
CampVisitentries for active camp -
Filter for doctor role
-
Build dictionary:
visit_map = {visit.user_id: visit} -
Enrich doctor objects:
doctor.login_time = visit.login_time doctor.logout_time = visit.logout_time
Performance Optimization:
- Single query for all visits
- No per-doctor DB queries (avoids N+1)
Type of Change
-
🐛 Bug fix -
✨ New feature -
⚡ Performance improvement
Related Issues / References
- Closes #
- Related to frontend MR for UI state sync
Screenshots or Screen Recordings
-
API response updated in
/docs(FastAPI Swagger) -
Verified response includes:
login_timelogout_time
How to Validate Locally
-
Start backend:
uvicorn app.main:app --reload -
Open API docs:
http://localhost:8000/docs -
Call:
GET /doctors -
Verify response:
- Each doctor includes
login_timeandlogout_time
- Each doctor includes
-
Test flow:
- Login a doctor
- Call
/doctors - Confirm
login_timeupdated - Logout doctor
- Confirm
logout_timeupdated
Testing Done
-
Unit tests added/updated -
API endpoint tested manually
Test Cases Covered:
| Scenario | Expected Result | Status |
|---|---|---|
| Doctor logged in | login_time set, logout_time null | |
| Doctor logged out | logout_time set | |
| No visit record | both fields null |
Test Commands Run:
pytest
pytest --cov=app
Code Quality Checklist
Code Standards
-
Code follows project conventions -
No debug statements -
No unused imports -
DRY principle followed -
Proper type hints used -
Ruff checks passed
Python & FastAPI Best Practices
-
Functions follow single responsibility -
Dependency injection used -
Pydantic models used -
SQLAlchemy optimized queries (no N+1) -
Proper error handling
API Design
-
RESTful conventions followed -
Proper status codes -
Input validation ensured -
Role-based filtering (doctor role)
Database & Migrations
-
No schema changes required -
Existing relationships reused -
Query optimized with mapping
Security
-
No sensitive data exposed -
ORM used (no SQL injection) -
Proper access control
Error Handling
-
Graceful error handling -
Proper logging -
API returns meaningful errors
Documentation
-
README update not required -
API docs updated automatically via FastAPI -
CHANGELOG optional
Known Limitations / Technical Debt
- Only supports active camp context
- Multiple camps scenario needs future enhancement
- No historical attendance tracking exposed via this API
Additional Notes
- Frontend no longer needs cache hacks
- System is now fully backend-driven
- Improves consistency and reliability
MR Acceptance Checklist
Quality & Correctness
-
Works as intended -
No regression issues -
Edge cases handled
Maintainability
-
Clean and readable code -
Follows project patterns -
Easy to extend
Acceptance Review
-
Reviewed by teammate -
Reviewed by product owner
closes #101 (closed)
