Spaces:
Sleeping
Sleeping
Update api/routes/patients.py
Browse files- api/routes/patients.py +63 -2
api/routes/patients.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from fastapi import APIRouter, HTTPException, Depends, Query, status
|
| 2 |
from db.mongo import patients_collection
|
| 3 |
from core.security import get_current_user
|
| 4 |
from utils.db import create_indexes
|
|
@@ -33,6 +33,68 @@ BASE_DIR = Path(__file__).resolve().parent.parent.parent
|
|
| 33 |
SYNTHEA_DATA_DIR = BASE_DIR / "output" / "fhir"
|
| 34 |
os.makedirs(SYNTHEA_DATA_DIR, exist_ok=True)
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
@router.post("/", status_code=status.HTTP_201_CREATED)
|
| 37 |
async def create_patient(
|
| 38 |
patient_data: PatientCreate,
|
|
@@ -361,7 +423,6 @@ async def list_patients(
|
|
| 361 |
if min_conditions > 0:
|
| 362 |
query[f"conditions.{min_conditions-1}"] = {"$exists": True}
|
| 363 |
|
| 364 |
-
# Removed $slice to return full arrays for the frontend
|
| 365 |
projection = {
|
| 366 |
"fhir_id": 1,
|
| 367 |
"full_name": 1,
|
|
|
|
| 1 |
+
from fastapi import APIRouter, HTTPException, Depends, Query, status
|
| 2 |
from db.mongo import patients_collection
|
| 3 |
from core.security import get_current_user
|
| 4 |
from utils.db import create_indexes
|
|
|
|
| 33 |
SYNTHEA_DATA_DIR = BASE_DIR / "output" / "fhir"
|
| 34 |
os.makedirs(SYNTHEA_DATA_DIR, exist_ok=True)
|
| 35 |
|
| 36 |
+
@router.post("/ehr/patients", status_code=status.HTTP_201_CREATED)
|
| 37 |
+
async def create_patient_ehr(
|
| 38 |
+
patient_data: PatientCreate,
|
| 39 |
+
current_user: dict = Depends(get_current_user)
|
| 40 |
+
):
|
| 41 |
+
"""Create a new patient in the database via /ehr/patients endpoint"""
|
| 42 |
+
logger.info(f"Creating new patient via /ehr/patients by user {current_user.get('email')}")
|
| 43 |
+
|
| 44 |
+
if current_user.get('role') not in ['admin', 'doctor']:
|
| 45 |
+
logger.warning(f"Unauthorized create attempt by {current_user.get('email')}")
|
| 46 |
+
raise HTTPException(
|
| 47 |
+
status_code=status.HTTP_403_FORBIDDEN,
|
| 48 |
+
detail="Only administrators and doctors can create patients"
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
try:
|
| 52 |
+
# Prepare the patient document
|
| 53 |
+
patient_doc = patient_data.dict()
|
| 54 |
+
now = datetime.utcnow().isoformat()
|
| 55 |
+
|
| 56 |
+
# Add system-generated fields
|
| 57 |
+
patient_doc.update({
|
| 58 |
+
"fhir_id": str(uuid.uuid4()),
|
| 59 |
+
"import_date": now,
|
| 60 |
+
"last_updated": now,
|
| 61 |
+
"source": "manual",
|
| 62 |
+
"created_by": current_user.get('email')
|
| 63 |
+
})
|
| 64 |
+
|
| 65 |
+
# Ensure arrays exist even if empty
|
| 66 |
+
for field in ['conditions', 'medications', 'encounters', 'notes']:
|
| 67 |
+
if field not in patient_doc:
|
| 68 |
+
patient_doc[field] = []
|
| 69 |
+
|
| 70 |
+
# Insert the patient document
|
| 71 |
+
result = await patients_collection.insert_one(patient_doc)
|
| 72 |
+
|
| 73 |
+
# Return the created patient with the generated ID
|
| 74 |
+
created_patient = await patients_collection.find_one(
|
| 75 |
+
{"_id": result.inserted_id}
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
if not created_patient:
|
| 79 |
+
logger.error("Failed to retrieve created patient")
|
| 80 |
+
raise HTTPException(
|
| 81 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 82 |
+
detail="Failed to retrieve created patient"
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
created_patient["id"] = str(created_patient["_id"])
|
| 86 |
+
del created_patient["_id"]
|
| 87 |
+
|
| 88 |
+
logger.info(f"Successfully created patient {created_patient['fhir_id']} via /ehr/patients")
|
| 89 |
+
return created_patient
|
| 90 |
+
|
| 91 |
+
except Exception as e:
|
| 92 |
+
logger.error(f"Failed to create patient via /ehr/patients: {str(e)}")
|
| 93 |
+
raise HTTPException(
|
| 94 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 95 |
+
detail=f"Failed to create patient: {str(e)}"
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
@router.post("/", status_code=status.HTTP_201_CREATED)
|
| 99 |
async def create_patient(
|
| 100 |
patient_data: PatientCreate,
|
|
|
|
| 423 |
if min_conditions > 0:
|
| 424 |
query[f"conditions.{min_conditions-1}"] = {"$exists": True}
|
| 425 |
|
|
|
|
| 426 |
projection = {
|
| 427 |
"fhir_id": 1,
|
| 428 |
"full_name": 1,
|