|
1 | 1 | from common.clients import logger |
2 | | -from typing import Dict, List, Any |
| 2 | +from typing import Dict, 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, |
@@ -42,61 +42,33 @@ def process_nhs_number(nhs_number: str) -> Dict[str, Any]: |
42 | 42 | new_nhs_number = pds_get_patient_id(nhs_number) |
43 | 43 |
|
44 | 44 | if not new_nhs_number: |
45 | | - return { |
46 | | - "status": "success", |
47 | | - "message": "No patient ID found for NHS number", |
48 | | - "nhs_number": nhs_number, |
49 | | - } |
| 45 | + return log_status("No patient ID found for NHS number", nhs_number) |
50 | 46 |
|
51 | 47 | if new_nhs_number == nhs_number: |
52 | | - return { |
53 | | - "status": "success", |
54 | | - "message": "No update required", |
55 | | - "nhs_number": nhs_number, |
56 | | - } |
| 48 | + return log_status("No update required", nhs_number) |
57 | 49 | logger.info("Update patient ID from %s to %s", nhs_number, new_nhs_number) |
58 | 50 |
|
59 | 51 | try: |
60 | 52 | pds_details, ieds_details = fetch_demographic_details(nhs_number) |
61 | 53 | except Exception as e: |
62 | 54 | logger.exception("process_nhs_number: failed to fetch demographic details: %s", e) |
63 | | - return { |
64 | | - "status": "error", |
65 | | - "message": str(e), |
66 | | - "nhs_number": nhs_number, |
67 | | - } |
68 | | - |
69 | | - # If no IEDS items were returned, nothing to update — return a clear success |
70 | | - # message to match existing test expectations. |
| 55 | + return log_status(str(e), nhs_number, "error") |
| 56 | + |
71 | 57 | if not ieds_details: |
72 | 58 | logger.info("No IEDS records returned for NHS number: %s", nhs_number) |
73 | | - return { |
74 | | - "status": "success", |
75 | | - "message": f"No records returned for ID: {nhs_number}", |
76 | | - "nhs_number": nhs_number, |
77 | | - } |
78 | | - |
79 | | - # If at least one IEDS item matches demographics, proceed with update |
80 | | - matching_records: List[Dict[str, Any]] = [] |
81 | | - discarded_records: List[Dict[str, Any]] = [] |
| 59 | + return log_status(f"No records returned for ID: {nhs_number}", nhs_number) |
| 60 | + |
| 61 | + # Compare demographics from PDS to each IEDS item, keep only matching records |
| 62 | + matching_records, discarded_records = [], [] |
82 | 63 | for detail in ieds_details: |
83 | 64 | if demographics_match(pds_details, detail): |
84 | 65 | matching_records.append(detail) |
85 | 66 | else: |
86 | 67 | discarded_records.append(detail) |
87 | 68 |
|
88 | 69 | if not matching_records: |
89 | | - logger.info( |
90 | | - "No records matched PDS demographics; skipping update for %s", |
91 | | - nhs_number, |
92 | | - ) |
93 | | - return { |
94 | | - "status": "success", |
95 | | - "message": "No records matched PDS demographics; update skipped", |
96 | | - "nhs_number": nhs_number, |
97 | | - "matched": 0, |
98 | | - "discarded": len(discarded_records), |
99 | | - } |
| 70 | + logger.info("No records matched PDS demographics: %d", len(discarded_records)) |
| 71 | + return log_status("No records matched PDS demographics; update skipped", nhs_number) |
100 | 72 |
|
101 | 73 | response = ieds_update_patient_id( |
102 | 74 | nhs_number, new_nhs_number, items_to_update=matching_records |
@@ -189,3 +161,8 @@ def normalize_strings(item: Any) -> str | None: |
189 | 161 | except Exception: |
190 | 162 | logger.exception("demographics_match: comparison failed with exception") |
191 | 163 | return False |
| 164 | + |
| 165 | + |
| 166 | +def log_status(msg: str, nhs_number: str, status: str = "success") -> Dict[str, Any]: |
| 167 | + message = {"status": status, "message": msg, "nhs_number": nhs_number} |
| 168 | + return message |
0 commit comments