Skip to content

feat(camps): implement login/logout time tracking for camp attendance

Vandana reddy Balannagari requested to merge logout-tracking into develop

Overview

This MR implements login and logout time tracking for medical camp attendance and restricts attendance updates to admin users while extending the system to support automated attendance tracking across all user roles (patients, doctors, volunteers, admins, and coordinators).

The system now records when a participant enters and leaves a medical camp using login_time and logout_time fields. In addition, role-based automation has been introduced to capture attendance without manual intervention in most workflows.

This improves monitoring, accountability, automation, and access control during medical camps.

:contentReference[oaicite:0]{index=0}


What does this MR do and why?

feat(camps): implement login/logout tracking with admin-controlled and automated role-based attendance flows

Previously the system only stored whether a user attended a medical camp and did not track when users entered or exited the camp.

This change introduces timestamp tracking:

login_time → when a participant enters the camp
logout_time → when the participant leaves the camp

Additionally:

  • Attendance updates are restricted to admin users
  • Automated attendance tracking is introduced for different roles

Role-wise Attendance Behavior

Patients

  • login_time → automatically recorded when patient registers to medical camp
  • logout_time → automatically recorded when patient completes full workflow (after counselling)

Admin / Coordinator

  • login_time → recorded on login
  • logout_time → recorded when admin marks logout via staff attendance

Doctors

  • login_time → recorded when Login button is clicked in Manage Doctors
  • logout_time → recorded when Logout button is clicked

Volunteers

  • login_time → recorded when QR is scanned by admin (entry)
  • logout_time → recorded when QR is scanned again (exit)

Changes Made

Database & Model Changes

  • Added login_time and logout_time fields to camp_visits table
  • Updated CampVisit SQLAlchemy model

Core Logic & Services

  • Updated app/services/auth_service.py

    • Records login_time for staff on login
    • Updates camp_role if required
    • Sets attendance = true
  • Updated app/services/patient_service.py

    • Sets login_time when patient registers for camp
  • Updated app/services/medical_camp_service.py

    • Sets login_time when user signs up for camp
  • Updated app/services/consultation_queue_service.py

    • Sets logout_time when visit status becomes completed
  • Extended attendance update logic

    • Supports login/logout timestamps
    • Maintains admin-only control

API Changes

  • Reused existing endpoint:

PUT /api/v1/medical-camps/camp/{user_id}/attendance

  • Removed custom logout endpoint
  • Standardized logout flow using attendance API

Authorization Changes

  • Restricted attendance updates to admin users
  • Replaced require_coordinator_or_admin() with require_admin()

Technical Details

Database Changes

Added two nullable timestamp columns:

  • login_time
  • logout_time

Service Logic (Admin Controlled)

If logout = false:

  • login_time is recorded
  • logout_time remains null
  • attendance = true

If logout = true:

  • logout_time is recorded
  • attendance remains true

Automated Attendance Flow

Role login_time logout_time
Patient On registration On completion
Admin/Coordinator On login Via staff attendance
Doctor Login button Logout button
Volunteer QR scan entry QR scan exit

Architecture

No new endpoints introduced.

Existing endpoint extended:

PUT /api/v1/medical-camps/camp/{user_id}/attendance


Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Refactor
  • Performance improvement
  • Test update
  • Configuration change
  • Security fix
  • Deprecation

Related Issues / References

Implements full attendance tracking system with timestamps.

Closes #93 (closed)


Screenshots or Screen Recordings

| Only attendance flag stored | login_time & logout_time tracked | | Manual attendance only | Automated + admin-controlled | | No duration tracking | Full visit duration tracking | Screenshot_from_2026-04-14_14-36-53 Screenshot_from_2026-04-14_14-43-52 Screenshot_from_2026-04-14_14-58-01 Screenshot_from_2026-04-17_11-30-16 Screenshot_from_2026-04-17_13-45-02 Screenshot_from_2026-04-17_13-45-22


How to Validate Locally

1 Run database migrations
alembic upgrade heads

2 Start backend
uvicorn app.main:app --reload

3 Login as admin
curl -X POST http://127.0.0.1:8000/api/v1/auth/login

4 Test flows:

  • Patient: Register → login_time
    Complete flow → logout_time

  • Doctor: Login/Logout buttons → timestamps

  • Volunteer: QR scan → login/logout

  • Admin: Login → login_time
    Staff attendance logout → logout_time

5 Verify in DB:

SELECT user_id, login_time, logout_time FROM camp_visits;


Testing Done

  • Unit tests updated
  • API tests passing

Test Cases Covered

Admin marks doctor login → login_time recorded
Admin marks doctor logout → logout_time recorded
Patient registration → login_time recorded
Patient completion → logout_time recorded
Volunteer QR scan → login/logout recorded
Doctor login/logout → timestamps recorded
Unauthorized attendance update → Access denied


Test Commands Run

pytest
pytest --cov=app


Code Quality Checklist

Code Standards

  • Code follows project conventions
  • No debug statements
  • No unused imports
  • Type hints used
  • Ruff checks pass

Backend Best Practices

  • Service layer used correctly
  • SQLAlchemy ORM used
  • Pydantic validation
  • Role-based access control

API Design

  • RESTful conventions followed
  • Proper responses returned
  • Authentication enforced

Database

  • Migration added
  • ORM used

Security

  • No sensitive data exposed
  • Token-based authentication

Documentation

  • CHANGELOG.md updated
  • No new environment variables

Known Limitations / Technical Debt

  • Patient login_time depends on registration flow
  • logout_time depends on workflow completion
  • Future improvement: dedicated audit logs

Additional Notes

This implementation maintains backward compatibility while extending attendance tracking across all roles with both automated and admin-controlled flows.


MR Acceptance Checklist

Quality & Correctness

  • Code works as intended
  • No existing functionality broken
  • Edge cases handled

Maintainability

  • Code is clean and readable
  • Properly tested
  • Follows project conventions

Acceptance Review

  • Reviewed by at least 1 teammate
  • Reviewed by product owner
Edited by Vandana reddy Balannagari

Merge request reports

Loading