Ali2206 commited on
Commit
481365f
Β·
verified Β·
1 Parent(s): 8393885

Update api/routes.py

Browse files
Files changed (1) hide show
  1. api/routes.py +15 -8
api/routes.py CHANGED
@@ -161,18 +161,25 @@ async def delete_patient(patient_id: str, current_user: dict = Depends(get_curre
161
  @router.get("/patients/count")
162
  async def count_patients(current_user: dict = Depends(get_current_user)):
163
  try:
164
- email = current_user["email"]
165
- print("πŸ§ͺ Counting patients for email:", email)
166
 
167
- filter_query = { "created_by": email }
168
- print("πŸ” Mongo filter:", filter_query)
 
 
 
 
 
 
 
169
 
170
  count = await patients_collection.count_documents(filter_query)
171
- print("βœ… Count result:", count)
172
 
 
173
  return {"count": count}
174
- except Exception as e:
175
- print("❌ Exception in /patients/count:", repr(e))
176
- raise HTTPException(status_code=500, detail="Failed to count patients")
177
 
 
 
 
178
 
 
161
  @router.get("/patients/count")
162
  async def count_patients(current_user: dict = Depends(get_current_user)):
163
  try:
164
+ email = current_user.get("email")
165
+ print("πŸ“Œ Authenticated user email:", email)
166
 
167
+ if not email or not isinstance(email, str):
168
+ raise HTTPException(status_code=400, detail="Invalid email in token")
169
+
170
+ if patients_collection is None:
171
+ print("❌ patients_collection is not initialized")
172
+ raise HTTPException(status_code=500, detail="Database not connected")
173
+
174
+ filter_query = {"created_by": email}
175
+ print("πŸ” Running count_documents with:", filter_query)
176
 
177
  count = await patients_collection.count_documents(filter_query)
 
178
 
179
+ print("βœ… Patient count result:", count)
180
  return {"count": count}
 
 
 
181
 
182
+ except Exception as e:
183
+ print("❌ ERROR in /patients/count:", repr(e))
184
+ raise HTTPException(status_code=500, detail="Internal server error")
185