Ali2206 commited on
Commit
b553899
·
verified ·
1 Parent(s): 8f99597

Update api/routes/patients.py

Browse files
Files changed (1) hide show
  1. api/routes/patients.py +52 -3
api/routes/patients.py CHANGED
@@ -367,7 +367,7 @@ async def list_patients(
367
  skip: int = Query(0, ge=0)
368
  ):
369
  logger.info(f"Listing patients with search: {search}, limit: {limit}, skip: {skip}")
370
- query = {} # Removed the default "source": "synthea" filter
371
 
372
  if search:
373
  query["$or"] = [
@@ -392,7 +392,7 @@ async def list_patients(
392
  "medications": 1,
393
  "encounters": 1,
394
  "notes": 1,
395
- "source": 1 # Added source to projection
396
  }
397
 
398
  try:
@@ -431,7 +431,6 @@ async def list_patients(
431
  detail=f"Failed to retrieve patients: {str(e)}"
432
  )
433
 
434
-
435
  @router.get("/patients/{patient_id}", response_model=dict)
436
  async def get_patient(patient_id: str):
437
  logger.info(f"Retrieving patient: {patient_id}")
@@ -552,5 +551,55 @@ async def add_note(
552
  detail=f"Failed to add note: {str(e)}"
553
  )
554
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
555
  # Export the router as 'patients' for api.__init__.py
556
  patients = router
 
367
  skip: int = Query(0, ge=0)
368
  ):
369
  logger.info(f"Listing patients with search: {search}, limit: {limit}, skip: {skip}")
370
+ query = {}
371
 
372
  if search:
373
  query["$or"] = [
 
392
  "medications": 1,
393
  "encounters": 1,
394
  "notes": 1,
395
+ "source": 1
396
  }
397
 
398
  try:
 
431
  detail=f"Failed to retrieve patients: {str(e)}"
432
  )
433
 
 
434
  @router.get("/patients/{patient_id}", response_model=dict)
435
  async def get_patient(patient_id: str):
436
  logger.info(f"Retrieving patient: {patient_id}")
 
551
  detail=f"Failed to add note: {str(e)}"
552
  )
553
 
554
+ @router.delete("/patients/{patient_id}", status_code=status.HTTP_200_OK)
555
+ async def delete_patient(
556
+ patient_id: str,
557
+ current_user: dict = Depends(get_current_user)
558
+ ):
559
+ """Delete a patient from the database"""
560
+ logger.info(f"Attempting to delete patient {patient_id} by user {current_user.get('email')}")
561
+
562
+ if current_user.get('role') not in ['admin', 'doctor']:
563
+ logger.warning(f"Unauthorized delete attempt by {current_user.get('email')}")
564
+ raise HTTPException(
565
+ status_code=status.HTTP_403_FORBIDDEN,
566
+ detail="Only administrators and doctors can delete patients"
567
+ )
568
+
569
+ try:
570
+ # Attempt to delete patient by either _id or fhir_id
571
+ result = await patients_collection.delete_one({
572
+ "$or": [
573
+ {"_id": ObjectId(patient_id)},
574
+ {"fhir_id": patient_id}
575
+ ]
576
+ })
577
+
578
+ if result.deleted_count == 0:
579
+ logger.warning(f"Patient not found for deletion: {patient_id}")
580
+ raise HTTPException(
581
+ status_code=status.HTTP_404_NOT_FOUND,
582
+ detail="Patient not found"
583
+ )
584
+
585
+ logger.info(f"Successfully deleted patient {patient_id}")
586
+ return {
587
+ "status": "success",
588
+ "message": f"Patient {patient_id} deleted successfully"
589
+ }
590
+
591
+ except ValueError as ve:
592
+ logger.error(f"Invalid patient ID format: {patient_id}, error: {str(ve)}")
593
+ raise HTTPException(
594
+ status_code=status.HTTP_400_BAD_REQUEST,
595
+ detail="Invalid patient ID format"
596
+ )
597
+ except Exception as e:
598
+ logger.error(f"Failed to delete patient {patient_id}: {str(e)}")
599
+ raise HTTPException(
600
+ status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
601
+ detail=f"Failed to delete patient: {str(e)}"
602
+ )
603
+
604
  # Export the router as 'patients' for api.__init__.py
605
  patients = router