@@ -92,7 +92,7 @@ async def group_patients_by_condition(current_doctor: str = Depends(get_current_
9292async def filter_patients (
9393 disease_name : Optional [str ] = Query (None , description = "Filter by disease name" ),
9494 condition : Optional [str ] = Query (None , description = "Filter by disease condition" ),
95- diagnosed_after_months : Optional [int ] = Query (None , description = "Filter by diagnoses in the last X months" ),
95+ diagnosed_after_months : Optional [str ] = Query (None , description = "Filter by diagnoses in the last X months" ),
9696 current_doctor : str = Depends (get_current_doctor )
9797):
9898 query = Patient .find (Patient .doctor_id == current_doctor )
@@ -103,14 +103,13 @@ async def filter_patients(
103103 if condition :
104104 query = query .find ({"diagnoses_history.condition" : condition })
105105
106- if diagnosed_after_months is not None :
107- # Convert empty string to None for diagnosed_after_months if it comes as empty string from frontend
108- if isinstance (diagnosed_after_months , str ) and diagnosed_after_months == "" :
109- diagnosed_after_months = None
110-
111- if diagnosed_after_months is not None :
112- from_date = date .today () - timedelta (days = diagnosed_after_months * 30 ) # Approximate months
113- query = query .find ({"diagnoses_history.diagnosis_on" : {"$gte" : from_date }})
106+ if diagnosed_after_months :
107+ try :
108+ diagnosed_after_months_int = int (diagnosed_after_months )
109+ from_date = date .today () - timedelta (days = diagnosed_after_months_int * 30 ) # Approximate months
110+ query = query .find ({"diagnoses_history.diagnosis_on" : {"$gte" : from_date }})
111+ except ValueError :
112+ raise HTTPException (status_code = 400 , detail = "diagnosed_after_months must be an integer" )
114113
115114 filtered_data = await query .to_list ()
116115 return filtered_data
0 commit comments