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.refresh(matched_patient)
# Fetch all patients to compute scores
# Fetch all patients to find an exact match
all_patients = db.query(Patient).all()
match_score = 0
best_match = None
for p in all_patients:
score = 0
# --- MRN check (primary, required) ---
# --- MRN check (must match exactly) ---
p_mrn = (p.mrn or p.patient_id or "").strip().upper()
if in_mrn and p_mrn == in_mrn:
score += 10
else:
continue # MRN must match
if p_mrn != in_mrn:
continue
# --- Gender check ---
if in_gender and p.gender:
if _gender_code(p.gender) == in_gender:
score += 3
# --- Gender check (must match exactly) ---
p_gender = _gender_code(p.gender or "")
if p_gender != in_gender:
continue
# --- DOB check ---
if in_dob and p.dob:
if _normalize_dob(p.dob) == in_dob:
score += 3
# --- DOB check (must match exactly) ---
p_dob = _normalize_dob(p.dob or "")
if p_dob != in_dob:
continue
# --- Age check ---
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
# If MRN, gender, and DOB all match, we have our matched patient
best_match = p
break
# Require MRN match (10) + at least one secondary attribute (gender/dob/age)
if not best_match or match_score < 12:
return {"matched": False, "reason": "No matching patient found in monitor database with 4-point verification (MRN + Gender + DOB + Age)"}
if not best_match:
return {
"matched": False,
"reason": "Patient verification failed. MRN, Gender, and DOB must match exactly."
}
matched_patient = best_match
@@ -484,6 +475,6 @@ def match_patient(
"bed_id": matched_patient.bed_id,
"device_status": device_status,
"last_reading_at": latest.timestamp.isoformat() if latest.timestamp else None,
"match_score": match_score,
"match_score": 100,
}