diff --git a/infrastructure/abri/abri_client.py b/infrastructure/abri/abri_client.py index 16f74e17b..3d5a7f537 100644 --- a/infrastructure/abri/abri_client.py +++ b/infrastructure/abri/abri_client.py @@ -30,12 +30,18 @@ def _format_appointment_date(appointment_date: date) -> str: return appointment_date.strftime("%d/%m/%Y") +def _phone_priority(entry: ET.Element) -> Tuple[int, int]: + """Sort key: lowest numeric priority first; unusable priorities last.""" + try: + return (0, int((entry.get("priority") or "").strip())) + except ValueError: + return (1, 0) + + def _select_phone_number(entries: List[ET.Element]) -> Optional[str]: - for entry in entries: - number = (entry.text or "").strip() - if number: - return number - return None + usable = [entry for entry in entries if (entry.text or "").strip()] + ranked = sorted(usable, key=_phone_priority) # stable: ties keep document order + return (ranked[0].text or "").strip() if ranked else None class AbriClient: