Ali2206 commited on
Commit
592ea39
·
verified ·
1 Parent(s): 3680b78

Update api/routes.py

Browse files
Files changed (1) hide show
  1. api/routes.py +16 -10
api/routes.py CHANGED
@@ -191,23 +191,29 @@ async def list_doctor_appointments(current_user: dict = Depends(get_current_user
191
  if current_user.get("role") != "doctor":
192
  raise HTTPException(status_code=403, detail="Only doctors can view this")
193
 
194
- cursor = appointments_collection.find({"doctor_id": {"$exists": True}})
195
  appointments = []
196
 
197
  async for a in cursor:
198
- patient = await users_collection.find_one({"_id": a["patient_id"]})
199
- patient_info = {
200
- "full_name": patient.get("full_name", ""),
201
- "email": patient.get("email", ""),
202
- } if patient else {}
 
 
 
203
 
204
  appointments.append({
205
  "_id": str(a["_id"]),
206
  "doctor_id": str(a["doctor_id"]),
207
- "patient": patient_info,
208
- "date": a["date"].strftime("%Y-%m-%d") if isinstance(a["date"], datetime) else a["date"],
209
- "time": a["time"],
210
- "reason": a["reason"]
 
 
 
211
  })
212
 
213
  return appointments
 
191
  if current_user.get("role") != "doctor":
192
  raise HTTPException(status_code=403, detail="Only doctors can view this")
193
 
194
+ cursor = appointments_collection.find({"doctor_id": ObjectId(current_user["_id"])})
195
  appointments = []
196
 
197
  async for a in cursor:
198
+ try:
199
+ patient_id = a.get("patient_id")
200
+ if not isinstance(patient_id, ObjectId):
201
+ patient_id = ObjectId(patient_id)
202
+
203
+ patient = await users_collection.find_one({"_id": patient_id})
204
+ except Exception:
205
+ patient = None
206
 
207
  appointments.append({
208
  "_id": str(a["_id"]),
209
  "doctor_id": str(a["doctor_id"]),
210
+ "patient": {
211
+ "full_name": patient.get("full_name", "Unknown") if patient else "Unknown",
212
+ "email": patient.get("email", "") if patient else "",
213
+ },
214
+ "date": a.get("date").strftime("%Y-%m-%d") if isinstance(a.get("date"), datetime) else a.get("date"),
215
+ "time": a.get("time", ""),
216
+ "reason": a.get("reason", "")
217
  })
218
 
219
  return appointments