Skip to content

fix: resolve future-date camp registration and pre-commit issues

Ahlad Pataparla requested to merge fix/get-current-camp into develop

Overview

Fixes patient registration failures for future-date medical camps and resolves pre-commit linting/type-checking issues.

What does this MR do and why?

This MR addresses a critical bug where patients could not register for medical camps scheduled with future dates. The get_current_camp() function incorrectly filtered camps using camp_date <= today, causing future camps to be excluded and resulting in failed registrations.

Additionally, this MR fixes pre-commit failures:

  • Reformatted code using ruff format to meet project style standards
  • Fixed 1 mypy type error in sms_service.py by explicitly casting response.text to str

Changes Made

Technical Details

Bug Fix: get_current_camp() logic

  • Root Cause: The function filtered camps with .filter(MedicalCamp.camp_date <= today), which excluded future-date camps
  • Fix: Removed the date filter entirely; now returns the most recent camp by camp_date regardless of whether it's in the past or future
  • Impact: Patients can now register for camps scheduled on future dates

Pre-commit Fixes

  • sms_service.py: Added explicit str() cast to response.text to satisfy mypy type checking
  • Reformatted medical_camp_service.py, test_medical_camp_routes.py, test_role_request_service.py, test_sms_service.py, and test_user_service.py using ruff format

Files Modified

File Changes
app/services/current_camp_service.py Removed <= today filter; simplified query logic
app/services/medical_camp_service.py Reformatted conditional expressions for readability
app/services/sms_service.py Fixed mypy type error; removed trailing newline
tests/test_api/test_medical_camp_routes.py Reformatted test code
tests/test_services/test_role_request_service.py Fixed trailing whitespace in comments
tests/test_services/test_sms_service.py Reformatted test code
tests/test_services/test_user_service.py Removed extra blank line

Type of Change

  • 🐛 Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to change)
  • 📝 Documentation update
  • ️ Refactor (no functional changes)
  • Performance improvement
  • 🧪 Test update
  • 🔧 Configuration change
  • 🚨 Security fix
  • 🗑️ Deprecation (removing deprecated code)

Related Issues / References

  • Resolves patient registration failures for future-date medical camps
  • Fixes pre-commit hook failures (ruff format + mypy)

How to Validate Locally

Previous Behavior

  • Patients could not register for medical camps with future dates
  • get_current_camp() returned None for future camps, causing registration to fail
  • Pre-commit hooks failed due to formatting and type-checking issues

New Behavior

  • Patients can register for medical camps regardless of camp date (past or future)
  • get_current_camp() returns the most recent camp by date without filtering
  • Pre-commit hooks pass successfully

Validation Steps

  1. Test patient registration for future camp:

    • Create a medical camp with a future date
    • Attempt to register a patient for the camp
    • Verify registration succeeds
  2. Test pre-commit hooks:

    ruff check app/services/ tests/
    ruff format --check app/services/ tests/
    mypy app/services/sms_service.py
  3. Run test suite:

    pytest tests/test_services/test_sms_service.py -v
    pytest tests/test_services/test_user_service.py -v
    pytest tests/test_services/test_role_request_service.py -v
    pytest tests/test_api/test_medical_camp_routes.py -v

Testing Done

  • Unit tests added/updated
  • API endpoint tests passing

Test Commands Run:

# Run all tests
pytest

# Run specific test files
pytest tests/test_services/test_sms_service.py -v
pytest tests/test_services/test_user_service.py -v
pytest tests/test_services/test_role_request_service.py -v
pytest tests/test_api/test_medical_camp_routes.py -v

# Run with coverage
pytest --cov=app/services --cov-report=term-missing

# Code quality checks
ruff check app/services/ tests/
ruff format --check app/services/ tests/
mypy app/services/sms_service.py

Code Quality Checklist

Code Standards

  • Code follows project conventions (naming, structure, formatting)
  • No debug statements or commented-out code left (unless necessary and intended)
  • No unused imports, variables, or functions
  • No duplicate code (DRY principle followed)
  • Type hints are properly defined (no Any unless justified and no mypy type check errors)
  • Ruff checks pass:
    ruff check .
    ruff format . --check

Python & FastAPI Best Practices

  • Functions follow single-responsibility principle
  • Async/await used correctly (no blocking calls in async functions)
  • Dependency injection used appropriately
  • Pydantic models used for request/response validation
  • SQLAlchemy queries are optimized (no N+1 queries)
  • Error handling is comprehensive (try/except with proper logging)

API Design

  • RESTful conventions followed
  • Proper HTTP status codes returned
  • Input validation implemented
  • Authentication/authorization enforced
  • Role Base access control used for user restriction
  • API documentation (docstrings) updated

Database & Migrations

  • Database migrations created (if schema changed)
  • Database migrations version is pointing to the latest version (and version name follows project conventions)
  • Migrations are reversible (migrations contain downgrade scripts)
  • Indexes added for frequently queried fields
  • No raw SQL queries (using SQLAlchemy ORM)
  • Data integrity constraints maintained

Security

  • No sensitive data logged (passwords, tokens, PII)
  • SQL injection prevention verified (ORM used)
  • Input sanitization implemented
  • Authentication tokens handled securely
  • CORS settings appropriate
  • Security scan passes:
    bandit -r app/

Error Handling

  • Errors are caught and handled gracefully
  • User-friendly error messages returned
  • Errors are logged appropriately
  • HTTP error responses follow API standards

Documentation

  • README.md updated (if setup steps changed)
  • .env.example updated (if new env vars added)
  • API documentation updated (docstrings, OpenAPI specs)
  • CHANGELOG.md will be updated (if applicable)
  • Code comments explain complex logic (not what, but why)

Known Limitations / Technical Debt

Potential concern: The get_current_camp() function now returns the most recent camp by date without any date filtering. If multiple camps exist and the most recent one has already concluded, it will still be returned as the "current" camp. This behavior should be monitored to ensure it aligns with business requirements. If there's a need to strictly differentiate between "active", "past", and "future" camps, additional logic may be required.

Additional Notes

  • This is a focused fix with minimal scope
  • No database schema changes
  • No breaking changes to existing API contracts
  • All changes are backward compatible

MR Acceptance Checklist

Quality & Correctness

  • Code works as intended and solves the stated problem
  • No bugs introduced (existing functionality not broken)
  • Edge cases handled appropriately

Maintainability

  • Code is readable and well-organized
  • Code is testable and well-tested
  • Follows project patterns and conventions
Edited by Ahlad Pataparla

Merge request reports

Loading