feat: enforce strict demographic matching for MRN, Gender, and DOB

This commit is contained in:
2026-07-15 12:17:57 +05:30
parent 801def2dc4
commit 10884623d0
+20 -29
View File
@@ -391,44 +391,35 @@ def match_patient(
db.commit() db.commit()
db.refresh(matched_patient) db.refresh(matched_patient)
# Fetch all patients to compute scores # Fetch all patients to find an exact match
all_patients = db.query(Patient).all() all_patients = db.query(Patient).all()
match_score = 0
best_match = None best_match = None
for p in all_patients: for p in all_patients:
score = 0 # --- MRN check (must match exactly) ---
# --- MRN check (primary, required) ---
p_mrn = (p.mrn or p.patient_id or "").strip().upper() p_mrn = (p.mrn or p.patient_id or "").strip().upper()
if in_mrn and p_mrn == in_mrn: if p_mrn != in_mrn:
score += 10 continue
else:
continue # MRN must match
# --- Gender check --- # --- Gender check (must match exactly) ---
if in_gender and p.gender: p_gender = _gender_code(p.gender or "")
if _gender_code(p.gender) == in_gender: if p_gender != in_gender:
score += 3 continue
# --- DOB check --- # --- DOB check (must match exactly) ---
if in_dob and p.dob: p_dob = _normalize_dob(p.dob or "")
if _normalize_dob(p.dob) == in_dob: if p_dob != in_dob:
score += 3 continue
# --- Age check --- # If MRN, gender, and DOB all match, we have our matched patient
if in_age is not None and p.dob:
calc_p_age = _calculate_age(p.dob)
if calc_p_age is not None and calc_p_age == in_age:
score += 2
if score > match_score:
match_score = score
best_match = p best_match = p
break
# Require MRN match (10) + at least one secondary attribute (gender/dob/age) if not best_match:
if not best_match or match_score < 12: return {
return {"matched": False, "reason": "No matching patient found in monitor database with 4-point verification (MRN + Gender + DOB + Age)"} "matched": False,
"reason": "Patient verification failed. MRN, Gender, and DOB must match exactly."
}
matched_patient = best_match matched_patient = best_match
@@ -484,6 +475,6 @@ def match_patient(
"bed_id": matched_patient.bed_id, "bed_id": matched_patient.bed_id,
"device_status": device_status, "device_status": device_status,
"last_reading_at": latest.timestamp.isoformat() if latest.timestamp else None, "last_reading_at": latest.timestamp.isoformat() if latest.timestamp else None,
"match_score": match_score, "match_score": 100,
} }