Spaces:
Sleeping
Sleeping
Update api/routes.py
Browse files- api/routes.py +34 -11
api/routes.py
CHANGED
|
@@ -69,7 +69,6 @@ async def list_doctors():
|
|
| 69 |
})
|
| 70 |
return doctors
|
| 71 |
|
| 72 |
-
|
| 73 |
# --- LOGIN ---
|
| 74 |
@router.post("/login", response_model=TokenResponse)
|
| 75 |
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
|
|
@@ -96,7 +95,6 @@ async def get_me(current_user: dict = Depends(get_current_user)):
|
|
| 96 |
"created_at": user.get("created_at", "")
|
| 97 |
}
|
| 98 |
|
| 99 |
-
|
| 100 |
# --- ADD NEW PATIENT ---
|
| 101 |
@router.post("/patients")
|
| 102 |
async def add_patient(data: PatientCreate, current_user: dict = Depends(get_current_user)):
|
|
@@ -139,18 +137,20 @@ async def count_patients(current_user: dict = Depends(get_current_user)):
|
|
| 139 |
count = await patients_collection.count_documents({"created_by": current_user["email"]})
|
| 140 |
return {"count": count}
|
| 141 |
|
| 142 |
-
#
|
| 143 |
-
# APPOINTMENT ROUTES
|
| 144 |
-
# =========================
|
| 145 |
-
|
| 146 |
-
# --- CREATE APPOINTMENT (doctor only) ---
|
| 147 |
@router.post("/appointments")
|
| 148 |
async def create_appointment(data: AppointmentCreate, current_user: dict = Depends(get_current_user)):
|
| 149 |
-
if current_user.get("role") != "
|
| 150 |
-
raise HTTPException(status_code=403, detail="Only
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
appointment_doc = {
|
| 153 |
-
"patient_id":
|
| 154 |
"doctor_id": ObjectId(data.doctor_id),
|
| 155 |
"date": data.date,
|
| 156 |
"time": data.time,
|
|
@@ -159,7 +159,30 @@ async def create_appointment(data: AppointmentCreate, current_user: dict = Depen
|
|
| 159 |
"created_at": datetime.utcnow()
|
| 160 |
}
|
| 161 |
await appointments_collection.insert_one(appointment_doc)
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
# --- LIST DOCTOR'S APPOINTMENTS ---
|
| 165 |
@router.get("/appointments/doctor")
|
|
|
|
| 69 |
})
|
| 70 |
return doctors
|
| 71 |
|
|
|
|
| 72 |
# --- LOGIN ---
|
| 73 |
@router.post("/login", response_model=TokenResponse)
|
| 74 |
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
|
|
|
|
| 95 |
"created_at": user.get("created_at", "")
|
| 96 |
}
|
| 97 |
|
|
|
|
| 98 |
# --- ADD NEW PATIENT ---
|
| 99 |
@router.post("/patients")
|
| 100 |
async def add_patient(data: PatientCreate, current_user: dict = Depends(get_current_user)):
|
|
|
|
| 137 |
count = await patients_collection.count_documents({"created_by": current_user["email"]})
|
| 138 |
return {"count": count}
|
| 139 |
|
| 140 |
+
# --- CREATE APPOINTMENT ---
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
@router.post("/appointments")
|
| 142 |
async def create_appointment(data: AppointmentCreate, current_user: dict = Depends(get_current_user)):
|
| 143 |
+
if current_user.get("role") != "patient":
|
| 144 |
+
raise HTTPException(status_code=403, detail="Only patients can book appointments")
|
| 145 |
|
| 146 |
+
# Get patient user info
|
| 147 |
+
patient_user = await users_collection.find_one({"email": current_user["email"]})
|
| 148 |
+
if not patient_user:
|
| 149 |
+
raise HTTPException(status_code=404, detail="Patient user not found")
|
| 150 |
+
|
| 151 |
+
# Insert appointment
|
| 152 |
appointment_doc = {
|
| 153 |
+
"patient_id": patient_user["_id"],
|
| 154 |
"doctor_id": ObjectId(data.doctor_id),
|
| 155 |
"date": data.date,
|
| 156 |
"time": data.time,
|
|
|
|
| 159 |
"created_at": datetime.utcnow()
|
| 160 |
}
|
| 161 |
await appointments_collection.insert_one(appointment_doc)
|
| 162 |
+
|
| 163 |
+
# Auto-add to doctor's patient list if not already
|
| 164 |
+
existing = await patients_collection.find_one({
|
| 165 |
+
"user_email": current_user["email"],
|
| 166 |
+
"created_by": await get_doctor_email_by_id(data.doctor_id)
|
| 167 |
+
})
|
| 168 |
+
if not existing:
|
| 169 |
+
await patients_collection.insert_one({
|
| 170 |
+
"full_name": patient_user.get("full_name", ""),
|
| 171 |
+
"user_email": patient_user["email"],
|
| 172 |
+
"gender": "",
|
| 173 |
+
"created_by": await get_doctor_email_by_id(data.doctor_id),
|
| 174 |
+
"created_at": datetime.utcnow()
|
| 175 |
+
})
|
| 176 |
+
|
| 177 |
+
return {"message": "Appointment booked successfully"}
|
| 178 |
+
|
| 179 |
+
# --- Helper function ---
|
| 180 |
+
async def get_doctor_email_by_id(doctor_id: str) -> Optional[str]:
|
| 181 |
+
try:
|
| 182 |
+
doc = await users_collection.find_one({"_id": ObjectId(doctor_id), "role": "doctor"})
|
| 183 |
+
return doc["email"] if doc else None
|
| 184 |
+
except Exception:
|
| 185 |
+
return None
|
| 186 |
|
| 187 |
# --- LIST DOCTOR'S APPOINTMENTS ---
|
| 188 |
@router.get("/appointments/doctor")
|