Skip to content

feat(patients): add API to fetch previously prescribed medicines

ashritha kunjeti requested to merge feat/new_medicne_repeat into develop

Overview

This MR introduces a new feature to retrieve previously prescribed medicines for a patient. This helps doctors quickly reference past prescriptions, avoid duplicate medicines, and improve treatment continuity.

What does this MR do and why?

Adds functionality to fetch a patient’s previous medicines.

Motivation: Helps reduce repeated prescriptions, ensures safer medication practices, improves patient history tracking, and enables faster medicine entry during consultations.

Approach: Introduced a new API endpoint /patients/{id}/previous-medicines that returns all past medicines for a given patient using patient medical history records.

Changes Made

  • Added new route /patients/{id}/previous-medicines in patient_routes.py
  • Added service method get_previous_medicines(patient_id) in patient_service.py
  • Added Pydantic response model PreviousMedicinesResponse
  • Updated database query to fetch medicines linked to patient history
  • Structured API response to include medicine name, dosage, frequency, and prescription date

Technical Details

Database:

  • Added optimized query to retrieve medicines based on patient_id
  • Results sorted by prescription date (descending) to show most recent medicines first

Service Layer:

  • Service returns structured medicine data including:
    • Medicine name
    • Dosage
    • Frequency
    • Date prescribed

API:

  • GET endpoint implementation
  • Requires valid patient ID
  • Returns 404 if patient not found
  • Returns structured response using Pydantic models

Type of Change

  • 🐛 Bug fix
  • New feature
  • 💥 Breaking change
  • 📝 Documentation update
  • ️ Refactor
  • Performance improvement
  • 🧪 Test update
  • 🔧 Configuration change
  • 🚨 Security fix
  • 🗑️ Deprecation

Related Issues / References

Closes #57 (closed)

Screenshots or Screen Recordings

  • Added API testing screenshot
  • Verified endpoint response using test data

How to Validate Locally

Previous behaviour:

  • No way to retrieve previously prescribed medicines for a patient.

Steps to test:

  1. Start the backend server
  2. Open API docs (/docs)
  3. Call: GET /patients/{id}/previous-medicines
  4. Provide a valid patient ID
  5. Verify response returns previously prescribed medicines

New behaviour:

  • System returns list of previous medicines with structured details
  • Returns empty list if no previous medicines exist
  • Returns 404 if patient does not exist

Testing Done

  • Unit tests added/updated
  • API endpoint tests passing

Test Cases Covered:

Scenario Expected Result Status Valid patient with history Returns medicine list Valid patient without history Returns empty list Invalid patient ID Returns 404 error

Test Commands Run:

pytest pytest tests/test_api_v1/test_patient_routes.py -v pytest --cov=app

Code Quality Checklist

Code Standards

  • Code follows project conventions
  • No debug statements left
  • No unused imports
  • No duplicate code
  • Type hints properly defined
  • Ruff checks pass

Python & FastAPI Best Practices

  • Functions follow single responsibility
  • Async/await used correctly
  • Pydantic models used for validation
  • SQLAlchemy queries optimized
  • Error handling implemented

API Design

  • RESTful conventions followed
  • Proper HTTP status codes returned
  • Input validation implemented
  • Authentication enforced
  • Role based access implemented
  • API documentation updated

Database & Migrations

  • No schema changes required
  • No raw SQL used
  • Data integrity maintained

Security

  • No sensitive data logged
  • SQL injection prevention verified
  • Input sanitization handled

Error Handling

  • Errors handled gracefully
  • Proper error messages returned
  • Errors logged appropriately

Documentation

  • README updated
  • .env.example updated
  • API documentation updated
  • CHANGELOG update pending
  • Code comments added where needed

Known Limitations / Technical Debt

  • Currently returns complete medicine history without filtering options
  • Future improvement could include:
    • Pagination
    • Date filtering
    • Medicine search

Additional Notes

This feature improves patient consultation workflow by allowing doctors to quickly reference past prescriptions. No existing functionality was modified.

MR Acceptance Checklist

Quality & Correctness

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

Maintainability

  • Code is readable
  • Additional automated tests can be added
  • Follows project conventions

Acceptance Review

  • Reviewed by teammate
  • Product owner review pending
Edited by Shanmukha varma Lanke

Merge request reports

Loading