Spaces:
Sleeping
Sleeping
Update api/routes.py
Browse files- api/routes.py +36 -12
api/routes.py
CHANGED
|
@@ -42,14 +42,27 @@ class Note(BaseModel):
|
|
| 42 |
author: Optional[str] = Field(None, example="Dr. Smith")
|
| 43 |
|
| 44 |
class Condition(BaseModel):
|
|
|
|
| 45 |
code: str = Field(..., example="Hypertension")
|
| 46 |
status: str = Field(..., example="Active")
|
| 47 |
onset_date: str = Field(..., example="2020-05-15")
|
|
|
|
|
|
|
| 48 |
|
| 49 |
class Medication(BaseModel):
|
|
|
|
| 50 |
name: str = Field(..., example="Lisinopril 10mg")
|
| 51 |
status: str = Field(..., example="Active")
|
| 52 |
prescribed_date: str = Field(..., example="2021-02-10")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
class PatientUpdate(BaseModel):
|
| 55 |
notes: Optional[List[Note]] = None
|
|
@@ -137,7 +150,7 @@ async def process_synthea_patient(bundle: dict, file_path: str) -> Optional[dict
|
|
| 137 |
'marital_status': resource.get('maritalStatus', {}).get('text', ''),
|
| 138 |
'language': standardize_language(resource.get('communication', [{}])[0].get('language', {}).get('text', '')),
|
| 139 |
'source': 'synthea',
|
| 140 |
-
'last_updated': datetime.utcnow()
|
| 141 |
}
|
| 142 |
|
| 143 |
elif resource_type == 'Encounter':
|
|
@@ -190,7 +203,7 @@ async def process_synthea_patient(bundle: dict, file_path: str) -> Optional[dict
|
|
| 190 |
'conditions': conditions,
|
| 191 |
'medications': medications,
|
| 192 |
'encounters': encounters,
|
| 193 |
-
'import_date': datetime.utcnow()
|
| 194 |
})
|
| 195 |
logger.info(f"Successfully processed patient {patient_data.get('fhir_id')} from {file_path}")
|
| 196 |
return patient_data
|
|
@@ -307,7 +320,7 @@ async def import_patients(
|
|
| 307 |
logger.info(f"Import request {request_id} completed: {imported} patients processed, "
|
| 308 |
f"{result.upserted_count} upserted, {len(errors)} errors")
|
| 309 |
except BulkWriteError as bwe:
|
| 310 |
-
logger.error(f"Partial bulk write failure
|
| 311 |
response.update({
|
| 312 |
"upserted": bwe.details.get('nUpserted', 0),
|
| 313 |
"existing": len(operations) - bwe.details.get('nUpserted', 0),
|
|
@@ -361,14 +374,18 @@ async def list_patients(
|
|
| 361 |
if min_conditions > 0:
|
| 362 |
query[f"conditions.{min_conditions-1}"] = {"$exists": True}
|
| 363 |
|
|
|
|
| 364 |
projection = {
|
| 365 |
"fhir_id": 1,
|
| 366 |
"full_name": 1,
|
| 367 |
"gender": 1,
|
| 368 |
"date_of_birth": 1,
|
| 369 |
-
"
|
| 370 |
-
"
|
| 371 |
-
"
|
|
|
|
|
|
|
|
|
|
| 372 |
}
|
| 373 |
|
| 374 |
try:
|
|
@@ -381,6 +398,13 @@ async def list_patients(
|
|
| 381 |
"fhir_id": patient.get("fhir_id"),
|
| 382 |
"full_name": patient.get("full_name"),
|
| 383 |
"gender": patient.get("gender"),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 384 |
"age": calculate_age(patient.get("date_of_birth")),
|
| 385 |
"stats": {
|
| 386 |
"notes": len(patient.get("notes", [])),
|
|
@@ -482,7 +506,7 @@ async def add_note(
|
|
| 482 |
note_data = note.dict()
|
| 483 |
note_data.update({
|
| 484 |
"author": current_user.get('full_name', 'System'),
|
| 485 |
-
"timestamp": datetime.utcnow()
|
| 486 |
})
|
| 487 |
|
| 488 |
result = await patients_collection.update_one(
|
|
@@ -492,7 +516,7 @@ async def add_note(
|
|
| 492 |
]},
|
| 493 |
{
|
| 494 |
"$push": {"notes": note_data},
|
| 495 |
-
"$set": {"last_updated": datetime.utcnow()}
|
| 496 |
}
|
| 497 |
)
|
| 498 |
|
|
@@ -538,8 +562,8 @@ async def signup(data: SignupForm):
|
|
| 538 |
"full_name": data.full_name.strip(),
|
| 539 |
"password": hashed_pw,
|
| 540 |
"role": "patient",
|
| 541 |
-
"created_at": datetime.utcnow(),
|
| 542 |
-
"updated_at": datetime.utcnow()
|
| 543 |
}
|
| 544 |
|
| 545 |
try:
|
|
@@ -587,8 +611,8 @@ async def create_doctor(
|
|
| 587 |
"role": "doctor",
|
| 588 |
"specialty": data.specialty,
|
| 589 |
"license_number": data.license_number,
|
| 590 |
-
"created_at": datetime.utcnow(),
|
| 591 |
-
"updated_at": datetime.utcnow()
|
| 592 |
}
|
| 593 |
|
| 594 |
try:
|
|
|
|
| 42 |
author: Optional[str] = Field(None, example="Dr. Smith")
|
| 43 |
|
| 44 |
class Condition(BaseModel):
|
| 45 |
+
id: str = Field(..., example="cond123")
|
| 46 |
code: str = Field(..., example="Hypertension")
|
| 47 |
status: str = Field(..., example="Active")
|
| 48 |
onset_date: str = Field(..., example="2020-05-15")
|
| 49 |
+
recorded_date: Optional[str] = Field(None, example="2020-05-16")
|
| 50 |
+
verification_status: Optional[str] = Field(None, example="Confirmed")
|
| 51 |
|
| 52 |
class Medication(BaseModel):
|
| 53 |
+
id: str = Field(..., example="med123")
|
| 54 |
name: str = Field(..., example="Lisinopril 10mg")
|
| 55 |
status: str = Field(..., example="Active")
|
| 56 |
prescribed_date: str = Field(..., example="2021-02-10")
|
| 57 |
+
requester: Optional[str] = Field(None, example="Dr. Smith")
|
| 58 |
+
dosage: Optional[str] = Field(None, example="10mg daily")
|
| 59 |
+
|
| 60 |
+
class Encounter(BaseModel):
|
| 61 |
+
id: str = Field(..., example="enc123")
|
| 62 |
+
type: str = Field(..., example="Outpatient Visit")
|
| 63 |
+
status: str = Field(..., example="Finished")
|
| 64 |
+
period: Dict = Field(..., example={"start": "2023-01-01T10:00:00Z"})
|
| 65 |
+
service_provider: Optional[str] = Field(None, example="City Hospital")
|
| 66 |
|
| 67 |
class PatientUpdate(BaseModel):
|
| 68 |
notes: Optional[List[Note]] = None
|
|
|
|
| 150 |
'marital_status': resource.get('maritalStatus', {}).get('text', ''),
|
| 151 |
'language': standardize_language(resource.get('communication', [{}])[0].get('language', {}).get('text', '')),
|
| 152 |
'source': 'synthea',
|
| 153 |
+
'last_updated': datetime.utcnow().isoformat()
|
| 154 |
}
|
| 155 |
|
| 156 |
elif resource_type == 'Encounter':
|
|
|
|
| 203 |
'conditions': conditions,
|
| 204 |
'medications': medications,
|
| 205 |
'encounters': encounters,
|
| 206 |
+
'import_date': datetime.utcnow().isoformat()
|
| 207 |
})
|
| 208 |
logger.info(f"Successfully processed patient {patient_data.get('fhir_id')} from {file_path}")
|
| 209 |
return patient_data
|
|
|
|
| 320 |
logger.info(f"Import request {request_id} completed: {imported} patients processed, "
|
| 321 |
f"{result.upserted_count} upserted, {len(errors)} errors")
|
| 322 |
except BulkWriteError as bwe:
|
| 323 |
+
logger.error(f"Partial bulk write failure for request {request_id}: {str(bwe.details)}")
|
| 324 |
response.update({
|
| 325 |
"upserted": bwe.details.get('nUpserted', 0),
|
| 326 |
"existing": len(operations) - bwe.details.get('nUpserted', 0),
|
|
|
|
| 374 |
if min_conditions > 0:
|
| 375 |
query[f"conditions.{min_conditions-1}"] = {"$exists": True}
|
| 376 |
|
| 377 |
+
# Removed $slice to return full arrays for the frontend
|
| 378 |
projection = {
|
| 379 |
"fhir_id": 1,
|
| 380 |
"full_name": 1,
|
| 381 |
"gender": 1,
|
| 382 |
"date_of_birth": 1,
|
| 383 |
+
"city": 1,
|
| 384 |
+
"state": 1,
|
| 385 |
+
"conditions": 1,
|
| 386 |
+
"medications": 1,
|
| 387 |
+
"encounters": 1,
|
| 388 |
+
"notes": 1
|
| 389 |
}
|
| 390 |
|
| 391 |
try:
|
|
|
|
| 398 |
"fhir_id": patient.get("fhir_id"),
|
| 399 |
"full_name": patient.get("full_name"),
|
| 400 |
"gender": patient.get("gender"),
|
| 401 |
+
"date_of_birth": patient.get("date_of_birth"),
|
| 402 |
+
"city": patient.get("city"),
|
| 403 |
+
"state": patient.get("state"),
|
| 404 |
+
"conditions": patient.get("conditions", []),
|
| 405 |
+
"medications": patient.get("medications", []),
|
| 406 |
+
"encounters": patient.get("encounters", []),
|
| 407 |
+
"notes": patient.get("notes", []),
|
| 408 |
"age": calculate_age(patient.get("date_of_birth")),
|
| 409 |
"stats": {
|
| 410 |
"notes": len(patient.get("notes", [])),
|
|
|
|
| 506 |
note_data = note.dict()
|
| 507 |
note_data.update({
|
| 508 |
"author": current_user.get('full_name', 'System'),
|
| 509 |
+
"timestamp": datetime.utcnow().isoformat()
|
| 510 |
})
|
| 511 |
|
| 512 |
result = await patients_collection.update_one(
|
|
|
|
| 516 |
]},
|
| 517 |
{
|
| 518 |
"$push": {"notes": note_data},
|
| 519 |
+
"$set": {"last_updated": datetime.utcnow().isoformat()}
|
| 520 |
}
|
| 521 |
)
|
| 522 |
|
|
|
|
| 562 |
"full_name": data.full_name.strip(),
|
| 563 |
"password": hashed_pw,
|
| 564 |
"role": "patient",
|
| 565 |
+
"created_at": datetime.utcnow().isoformat(),
|
| 566 |
+
"updated_at": datetime.utcnow().isoformat()
|
| 567 |
}
|
| 568 |
|
| 569 |
try:
|
|
|
|
| 611 |
"role": "doctor",
|
| 612 |
"specialty": data.specialty,
|
| 613 |
"license_number": data.license_number,
|
| 614 |
+
"created_at": datetime.utcnow().isoformat(),
|
| 615 |
+
"updated_at": datetime.utcnow().isoformat()
|
| 616 |
}
|
| 617 |
|
| 618 |
try:
|