Ali2206 commited on
Commit
b09d7f8
·
verified ·
1 Parent(s): 92c74a9

Update api/routes.py

Browse files
Files changed (1) hide show
  1. api/routes.py +36 -0
api/routes.py CHANGED
@@ -271,3 +271,39 @@ async def get_synthea_patient(id: str):
271
  return patient
272
  except Exception:
273
  raise HTTPException(status_code=400, detail="Invalid patient ID")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
271
  return patient
272
  except Exception:
273
  raise HTTPException(status_code=400, detail="Invalid patient ID")
274
+
275
+
276
+
277
+ @router.post("/ehr/auto-import")
278
+ async def auto_import_synthea_patients():
279
+ csv_path = Path("data/synthea/patients.csv")
280
+
281
+ if not csv_path.exists():
282
+ raise HTTPException(status_code=404, detail="Synthea patients.csv not found.")
283
+
284
+ try:
285
+ with open(csv_path, "r", encoding="utf-8") as f:
286
+ reader = csv.DictReader(f)
287
+ patients = []
288
+
289
+ for row in reader:
290
+ patients.append({
291
+ "synthea_id": row.get("Id"),
292
+ "full_name": f"{row.get('FIRST', '')} {row.get('LAST', '')}".strip(),
293
+ "gender": row.get("GENDER"),
294
+ "date_of_birth": datetime.strptime(row.get("BIRTHDATE"), "%Y-%m-%dT%H:%M:%SZ"),
295
+ "address": row.get("ADDRESS"),
296
+ "city": row.get("CITY"),
297
+ "state": row.get("STATE"),
298
+ "zip": row.get("ZIP"),
299
+ "created_at": datetime.utcnow()
300
+ })
301
+
302
+ if not patients:
303
+ raise HTTPException(status_code=400, detail="No patients found in CSV.")
304
+
305
+ result = await patients_collection.insert_many(patients)
306
+ return {"message": "Imported successfully", "count": len(result.inserted_ids)}
307
+
308
+ except Exception as e:
309
+ raise HTTPException(status_code=500, detail=str(e))