@@ -128,54 +128,44 @@ def extract_normalized_name_from_patient(patient: dict) -> str | None:
128128
129129
130130def demographics_match (pds_details : dict , ieds_item : dict ) -> bool :
131- """Compare PDS patient details to an IEDS item (FHIR Patient resource).
132-
133- Parameters:
134- - pds_details: dict returned by PDS (patient details)
135- - ieds_item: dict representing a single IEDS item containing a FHIR Patient resource
136-
131+ """Compare PDS patient details from PDS to an IEDS item (FHIR Patient resource).
137132 Returns True if name, birthDate and gender match (when present in both sources).
138133 If required fields are missing or unparsable on the IEDS side the function returns False.
139134 """
140135 try :
141- # extract pds values
142- pds_name = normalize_name_from_pds (pds_details ) if isinstance (pds_details , dict ) else None
143- pds_gender = pds_details .get ("gender" ) if isinstance (pds_details , dict ) else None
144- pds_birth = pds_details .get ("birthDate" ) if isinstance (pds_details , dict ) else None
145-
136+
137+ def normalize_strings (item : Any ) -> str | None :
138+ return str (item ).strip ().lower () if item else None
139+
140+ # Retrieve patient resource from PDS
141+ pds_name = normalize_strings (normalize_name_from_pds (pds_details ))
142+ pds_gender = normalize_strings (pds_details .get ("gender" ))
143+ pds_birth = normalize_strings (pds_details .get ("birthDate" ))
144+
145+ # Retrieve patient resource from IEDS item
146146 patient = extract_patient_resource_from_item (ieds_item )
147147 if not patient :
148- logger .debug ("demographics_match: no patient resource in item" )
148+ logger .debug ("demographics_match: no patient resource in IEDS table item" )
149149 return False
150150
151- # normalize incoming patient name
152- incoming_name = extract_normalized_name_from_patient (patient )
153-
154- incoming_gender = patient .get ("gender" )
155- incoming_birth = patient .get ("birthDate" )
151+ # normalize patient name
152+ ieds_name = extract_normalized_name_from_patient (patient )
156153
157- def _norm_str ( x ):
158- return str ( x ). strip (). lower () if x is not None else None
154+ ieds_gender = normalize_strings ( patient . get ( "gender" ))
155+ ieds_birth = patient . get ( "birthDate" )
159156
160- # Compare birthDate (strict if both present)
161- if pds_birth and incoming_birth :
162- if str (pds_birth ).strip () != str (incoming_birth ).strip ():
163- logger .debug ("demographics_match: birthDate mismatch %s != %s" , pds_birth , incoming_birth )
164- return False
157+ if pds_birth and ieds_birth and pds_birth != ieds_birth :
158+ logger .debug ("demographics_match: birthDate mismatch %s != %s" , pds_birth , ieds_birth )
159+ return False
165160
166- # Compare gender (case-insensitive)
167- if pds_gender and incoming_gender :
168- if _norm_str (pds_gender ) != _norm_str (incoming_gender ):
169- logger .debug ("demographics_match: gender mismatch %s != %s" , pds_gender , incoming_gender )
170- return False
161+ if pds_gender and ieds_gender and pds_gender != ieds_gender :
162+ logger .debug ("demographics_match: gender mismatch %s != %s" , pds_gender , ieds_gender )
163+ return False
171164
172- # Compare names if both present (normalized)
173- if pds_name and incoming_name :
174- if _norm_str (pds_name ) != _norm_str (incoming_name ):
175- logger .debug ("demographics_match: name mismatch %s != %s" , pds_name , incoming_name )
176- return False
165+ if pds_name and ieds_name and pds_name != ieds_name :
166+ logger .debug ("demographics_match: name mismatch %s != %s" , pds_name , ieds_name )
167+ return False
177168
178- # If we reached here, all present fields matched (or were not present to compare)
179169 return True
180170 except Exception :
181171 logger .exception ("demographics_match: comparison failed with exception" )
0 commit comments