feat: multi-point patient verification (MRN, Name, DOB) via URL parameters
This commit is contained in:
+16
-10
@@ -112,26 +112,30 @@ def get_latest_readings(
|
||||
if device_id:
|
||||
query = query.filter(VitalReading.device_id == device_id)
|
||||
|
||||
patient_filters = []
|
||||
patient_criteria = []
|
||||
if mrn:
|
||||
mrn_clean = mrn.strip()
|
||||
patient_filters.append(Patient.mrn == mrn_clean)
|
||||
patient_filters.append(Patient.patient_id == mrn_clean)
|
||||
patient_criteria.append(or_(Patient.mrn == mrn_clean, Patient.patient_id == mrn_clean))
|
||||
if name:
|
||||
name_clean = name.strip()
|
||||
patient_filters.append(Patient.name.ilike(f"%{name_clean}%"))
|
||||
patient_criteria.append(Patient.name.ilike(f"%{name_clean}%"))
|
||||
if dob:
|
||||
dob_clean = dob.strip()
|
||||
patient_filters.append(Patient.dob.ilike(f"%{dob_clean}%"))
|
||||
dob_clean = dob.strip().replace("-", "").replace("/", "")
|
||||
patient_criteria.append(or_(
|
||||
Patient.dob.ilike(f"%{dob.strip()}%"),
|
||||
Patient.dob.ilike(f"%{dob_clean}%"),
|
||||
Patient.dob.ilike(f"%{dob.strip()[:4]}%") if len(dob.strip()) >= 4 else False
|
||||
))
|
||||
|
||||
if patient_filters:
|
||||
matching_patients = db.query(Patient).filter(or_(*patient_filters)).all()
|
||||
if patient_criteria:
|
||||
from sqlalchemy import and_
|
||||
matching_patients = db.query(Patient).filter(and_(*patient_criteria)).all()
|
||||
if not matching_patients:
|
||||
return []
|
||||
matching_dev_ids = [p.device_id for p in matching_patients if p.device_id]
|
||||
matching_mrns = [p.mrn for p in matching_patients if p.mrn] + [p.patient_id for p in matching_patients if p.patient_id]
|
||||
|
||||
conds = []
|
||||
if mrn:
|
||||
conds.append(VitalReading.patient_id == mrn.strip())
|
||||
if matching_mrns:
|
||||
conds.append(VitalReading.patient_id.in_(matching_mrns))
|
||||
if matching_dev_ids:
|
||||
@@ -139,6 +143,8 @@ def get_latest_readings(
|
||||
|
||||
if conds:
|
||||
query = query.filter(or_(*conds))
|
||||
else:
|
||||
return []
|
||||
|
||||
readings = query.order_by(VitalReading.timestamp.desc()).limit(limit).all()
|
||||
return readings
|
||||
|
||||
Reference in New Issue
Block a user