"""Handle allergy queries."""
import json
from ..data import PatientDataLoader
def handle_list_allergies(patient_id: str, loader: PatientDataLoader) -> str:
"""List patient allergies (summary with IDs and dates only)."""
if not loader.patient_exists(patient_id):
return json.dumps({"error": f"Patient {patient_id} not found"})
allergies = loader.get_allergies(patient_id)
summary = [
{
"id": allergy.id,
"allergen": allergy.allergen,
"severity": allergy.severity,
"recorded_date": str(allergy.recorded_date)
}
for allergy in allergies
]
return json.dumps(summary, indent=2)
def handle_get_allergy_details(patient_id: str, allergy_id: str, loader: PatientDataLoader) -> str:
"""Get detailed information for a specific allergy."""
if not loader.patient_exists(patient_id):
return json.dumps({"error": f"Patient {patient_id} not found"})
allergies = loader.get_allergies(patient_id)
for allergy in allergies:
if allergy.id == allergy_id:
return json.dumps(allergy.model_dump(mode='json'), indent=2, default=str)
return json.dumps({"error": f"Allergy {allergy_id} not found for patient {patient_id}"})