fix(prescription): allow verification for externally purchased medicines
Closes #47 (closed)
Merge Request
Overview
This MR fixes the prescription verification blockage when medicines are marked as "bought outside". Currently, the verification logic only considers internal pharmacy medicines, causing it to fail with a "No pending or replaced prescriptions found" error when only external medicines are present.
What does this MR do and why?
fix(prescription): allow verification for externally purchased medicines
Updated update_inventory_from_consultation_prescriptions in medicine_service.py to correctly identify and process prescriptions with the outside dispatch status. Previously, the system assumed all prescriptions must be internal, leading to a workflow blockage for patients who bought medicines externally. This change ensures that consultations can proceed to the medicine_verified and counselling_done stages regardless of whether the medicine was supplied by the camp pharmacy or bought outside.
Changes Made
-
[MODIFY]
medicine_service.py- Updated query filter to include
DispatchStatusEnum.outside - Added logic to skip inventory validation for outside medicines (since they aren't in camp inventory)
- Added logic to skip inventory deduction for outside medicines
- Updated error message:
"No pending, replaced, or outside prescriptions found for this consultation"
- Updated query filter to include
-
[MODIFY]
test_medicine_service.py- Updated existing failure test case with the new error message
- Added new test case
test_update_inventory_from_consultation_prescriptions_with_outside_unitto verify the success path for external medicines
Technical Details
Root Cause: The update_inventory_from_consultation_prescriptions service function had a hardcoded filter that only looked for pending or replaced statuses.
Fix: Expanded the filter to include outside status and ensured that these items are treated as "already satisfied" by skipping the internal inventory checks. The fix is minimal and avoids re-engineering the prescription flow.
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
- Related to: [Prescription verification fails for externally purchased medicines]
Screenshots or Screen Recordings
N/A (Backend logic change)
How to Validate Locally
Previous behaviour: Calling the Verify Medicines endpoint with only outside prescriptions returned a ValueError: "No pending or replaced prescriptions found", blocking the patient workflow.
Changes made: The query now includes outside dispatch status and skips inventory checks for those items.
New behaviour:
- Set up a consultation for a patient.
- Add a prescription and mark it as "bought outside" (sets
dispatch_statustooutside). - Call the Verify Medicines endpoint (triggers
update_inventory_from_consultation_prescriptions). -
Expected: Returns
{"success": true}and visit status updates tomedicine_verified.
Testing Done
-
Unit tests added/updated -
API endpoint tests passing
Test Cases Covered:
| Scenario | Expected Result | Status |
|---|---|---|
| Consultation with ONLY "outside" medicines | Returns Success, Updates status, No inventory deduction | |
| Mixed internal and external medicines | Returns Success, Deducts only internal, Updates status | |
| No prescriptions found | Raises ValueError with descriptive message |
Test Commands Run:
# Run the relevant service tests
uv run pytest tests/test_services/test_medicine_service.py
Code Quality Checklist
Code Standards
-
Code follows project conventions (naming, structure, formatting) -
No debug statements or commented-out code left -
No unused imports, variables, or functions -
No duplicate code (DRY principle followed) -
Type hints are properly defined -
Ruff checks pass:
ruff check .
ruff format . --check
Python & FastAPI Best Practices
-
Functions follow single-responsibility principle -
Async/await used correctly -
Dependency injection used appropriately -
Pydantic models used for request/response validation -
SQLAlchemy queries are optimized -
Error handling is comprehensive
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 -
Migrations are reversible -
Indexes added for frequently queried fields -
No raw SQL queries (using SQLAlchemy ORM) -
Data integrity constraints maintained
Security
-
No sensitive data logged -
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.exampleupdated (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
None.
Additional Notes
- Fix is minimal and avoids re-engineering the prescription flow.
- Ensure the frontend correctly sends the
outsidestatus before calling this verification service.
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
Acceptance Review
-
Reviewed by at least 1 teammate -
Reviewed by product owner