Spaces:
Sleeping
Sleeping
Update api/routes/patients.py
Browse files- api/routes/patients.py +22 -37
api/routes/patients.py
CHANGED
|
@@ -341,28 +341,20 @@ async def import_patients(
|
|
| 341 |
@router.get("/", response_model=List[dict])
|
| 342 |
async def list_patients(
|
| 343 |
search: Optional[str] = Query(None),
|
| 344 |
-
min_notes: int = Query(0, ge=0),
|
| 345 |
-
min_conditions: int = Query(0, ge=0),
|
| 346 |
-
limit: int = Query(100, ge=1, le=500),
|
| 347 |
skip: int = Query(0, ge=0),
|
|
|
|
| 348 |
current_user: dict = Depends(get_current_user)
|
| 349 |
):
|
| 350 |
-
|
| 351 |
-
query = {}
|
| 352 |
-
|
| 353 |
-
if search:
|
| 354 |
-
query["$or"] = [
|
| 355 |
-
{"full_name": {"$regex": search, "$options": "i"}},
|
| 356 |
-
{"fhir_id": search}
|
| 357 |
-
]
|
| 358 |
-
|
| 359 |
-
if min_notes > 0:
|
| 360 |
-
query[f"notes.{min_notes-1}"] = {"$exists": True}
|
| 361 |
-
|
| 362 |
-
if min_conditions > 0:
|
| 363 |
-
query[f"conditions.{min_conditions-1}"] = {"$exists": True}
|
| 364 |
-
|
| 365 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 366 |
cursor = patients_collection.find(query).skip(skip).limit(limit)
|
| 367 |
patients = []
|
| 368 |
|
|
@@ -371,14 +363,13 @@ async def list_patients(
|
|
| 371 |
patient["age"] = calculate_age(patient.get("date_of_birth"))
|
| 372 |
patients.append(patient)
|
| 373 |
|
| 374 |
-
logger.info(f"Retrieved {len(patients)} patients")
|
| 375 |
return patients
|
| 376 |
-
|
| 377 |
except Exception as e:
|
| 378 |
logger.error(f"Failed to list patients: {str(e)}")
|
| 379 |
raise HTTPException(
|
| 380 |
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 381 |
-
detail=
|
| 382 |
)
|
| 383 |
|
| 384 |
@router.get("/{patient_id}", response_model=dict)
|
|
@@ -386,33 +377,27 @@ async def get_patient(
|
|
| 386 |
patient_id: str,
|
| 387 |
current_user: dict = Depends(get_current_user)
|
| 388 |
):
|
| 389 |
-
|
| 390 |
try:
|
| 391 |
-
#
|
| 392 |
try:
|
| 393 |
patient = await patients_collection.find_one({"_id": ObjectId(patient_id)})
|
| 394 |
-
except
|
| 395 |
-
#
|
| 396 |
patient = await patients_collection.find_one({"fhir_id": patient_id})
|
| 397 |
-
|
| 398 |
if not patient:
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
status_code=status.HTTP_404_NOT_FOUND,
|
| 402 |
-
detail="Patient not found"
|
| 403 |
-
)
|
| 404 |
-
|
| 405 |
patient["id"] = str(patient["_id"])
|
| 406 |
patient["age"] = calculate_age(patient.get("date_of_birth"))
|
| 407 |
-
|
| 408 |
-
logger.info(f"Successfully retrieved patient: {patient_id}")
|
| 409 |
return patient
|
| 410 |
-
|
| 411 |
except Exception as e:
|
| 412 |
-
logger.error(f"Failed to
|
| 413 |
raise HTTPException(
|
| 414 |
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 415 |
-
detail=
|
| 416 |
)
|
| 417 |
|
| 418 |
@router.post("/{patient_id}/notes", status_code=status.HTTP_201_CREATED)
|
|
|
|
| 341 |
@router.get("/", response_model=List[dict])
|
| 342 |
async def list_patients(
|
| 343 |
search: Optional[str] = Query(None),
|
|
|
|
|
|
|
|
|
|
| 344 |
skip: int = Query(0, ge=0),
|
| 345 |
+
limit: int = Query(100, ge=1, le=500),
|
| 346 |
current_user: dict = Depends(get_current_user)
|
| 347 |
):
|
| 348 |
+
"""List all patients with optional search and pagination"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
try:
|
| 350 |
+
query = {}
|
| 351 |
+
if search:
|
| 352 |
+
query["$or"] = [
|
| 353 |
+
{"full_name": {"$regex": search, "$options": "i"}},
|
| 354 |
+
{"fhir_id": search},
|
| 355 |
+
{"_id": search}
|
| 356 |
+
]
|
| 357 |
+
|
| 358 |
cursor = patients_collection.find(query).skip(skip).limit(limit)
|
| 359 |
patients = []
|
| 360 |
|
|
|
|
| 363 |
patient["age"] = calculate_age(patient.get("date_of_birth"))
|
| 364 |
patients.append(patient)
|
| 365 |
|
|
|
|
| 366 |
return patients
|
| 367 |
+
|
| 368 |
except Exception as e:
|
| 369 |
logger.error(f"Failed to list patients: {str(e)}")
|
| 370 |
raise HTTPException(
|
| 371 |
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 372 |
+
detail="Failed to retrieve patients"
|
| 373 |
)
|
| 374 |
|
| 375 |
@router.get("/{patient_id}", response_model=dict)
|
|
|
|
| 377 |
patient_id: str,
|
| 378 |
current_user: dict = Depends(get_current_user)
|
| 379 |
):
|
| 380 |
+
"""Get a specific patient by ID or FHIR ID"""
|
| 381 |
try:
|
| 382 |
+
# Try as ObjectId first
|
| 383 |
try:
|
| 384 |
patient = await patients_collection.find_one({"_id": ObjectId(patient_id)})
|
| 385 |
+
except:
|
| 386 |
+
# Fall back to FHIR ID
|
| 387 |
patient = await patients_collection.find_one({"fhir_id": patient_id})
|
| 388 |
+
|
| 389 |
if not patient:
|
| 390 |
+
raise HTTPException(status_code=404, detail="Patient not found")
|
| 391 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 392 |
patient["id"] = str(patient["_id"])
|
| 393 |
patient["age"] = calculate_age(patient.get("date_of_birth"))
|
|
|
|
|
|
|
| 394 |
return patient
|
| 395 |
+
|
| 396 |
except Exception as e:
|
| 397 |
+
logger.error(f"Failed to get patient {patient_id}: {str(e)}")
|
| 398 |
raise HTTPException(
|
| 399 |
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 400 |
+
detail="Failed to retrieve patient"
|
| 401 |
)
|
| 402 |
|
| 403 |
@router.post("/{patient_id}/notes", status_code=status.HTTP_201_CREATED)
|