|
1 | 1 | from common.clients import logger |
2 | | -from typing import Dict, Any |
| 2 | +from typing import Dict, List, Any |
3 | 3 | from pds_details import pds_get_patient_id, pds_get_patient_details |
4 | 4 | from ieds_db_operations import ( |
5 | 5 | ieds_update_patient_id, |
@@ -77,16 +77,34 @@ def process_nhs_number(nhs_number: str) -> Dict[str, Any]: |
77 | 77 | } |
78 | 78 |
|
79 | 79 | # If at least one IEDS item matches demographics, proceed with update |
80 | | - if not all(demographics_match(pds_details, detail) for detail in ieds_details): |
81 | | - logger.info("Not all IEDS items matched PDS demographics; skipping update for %s", nhs_number) |
| 80 | + matching_records: List[Dict[str, Any]] = [] |
| 81 | + discarded_records: List[Dict[str, Any]] = [] |
| 82 | + for detail in ieds_details: |
| 83 | + if demographics_match(pds_details, detail): |
| 84 | + matching_records.append(detail) |
| 85 | + else: |
| 86 | + discarded_records.append(detail) |
| 87 | + |
| 88 | + if not matching_records: |
| 89 | + logger.info( |
| 90 | + "No records matched PDS demographics; skipping update for %s", |
| 91 | + nhs_number, |
| 92 | + ) |
82 | 93 | return { |
83 | 94 | "status": "success", |
84 | | - "message": "Not all IEDS items matched PDS demographics; update skipped", |
| 95 | + "message": "No records matched PDS demographics; update skipped", |
85 | 96 | "nhs_number": nhs_number, |
| 97 | + "matched": 0, |
| 98 | + "discarded": len(discarded_records), |
86 | 99 | } |
87 | 100 |
|
88 | | - response = ieds_update_patient_id(nhs_number, new_nhs_number) |
| 101 | + response = ieds_update_patient_id( |
| 102 | + nhs_number, new_nhs_number, items_to_update=matching_records |
| 103 | + ) |
89 | 104 | response["nhs_number"] = nhs_number |
| 105 | + # add counts for observability |
| 106 | + response["matched"] = len(matching_records) |
| 107 | + response["discarded"] = len(discarded_records) |
90 | 108 | return response |
91 | 109 |
|
92 | 110 |
|
@@ -144,16 +162,17 @@ def normalize_strings(item: Any) -> str | None: |
144 | 162 | logger.debug("demographics_match: no patient resource in IEDS table item") |
145 | 163 | return False |
146 | 164 |
|
147 | | - # normalize patient name |
| 165 | + # normalize patient fields from IEDS |
148 | 166 | ieds_name = normalize_strings(extract_normalized_name_from_patient(patient)) |
149 | | - |
150 | 167 | ieds_gender = normalize_strings(patient.get("gender")) |
151 | | - ieds_birth = patient.get("birthDate") |
| 168 | + ieds_birth = normalize_strings(patient.get("birthDate")) |
152 | 169 |
|
| 170 | + # All required fields must be present |
153 | 171 | if not all([pds_name, pds_gender, pds_birth, ieds_name, ieds_gender, ieds_birth]): |
154 | 172 | logger.debug("demographics_match: missing required demographics") |
155 | 173 | return False |
156 | 174 |
|
| 175 | + # Compare fields |
157 | 176 | if pds_birth != ieds_birth: |
158 | 177 | logger.debug("demographics_match: birthDate mismatch %s != %s", pds_birth, ieds_birth) |
159 | 178 | return False |
|
0 commit comments