Skip to content

Commit 9da9a17

Browse files
committed
VED-755: update records whose vaccination match
1 parent 2ffffb7 commit 9da9a17

File tree

3 files changed

+39
-17
lines changed

3 files changed

+39
-17
lines changed

lambdas/id_sync/src/ieds_db_operations.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def get_ieds_table():
1919
BATCH_SIZE = 25
2020

2121

22-
def ieds_update_patient_id(old_id: str, new_id: str) -> dict:
22+
def ieds_update_patient_id(old_id: str, new_id: str, items_to_update: list | None = None) -> dict:
2323
"""Update the patient ID in the IEDS table."""
2424
logger.info(f"ieds_update_patient_id. Update patient ID from {old_id} to {new_id}")
2525
if not old_id or not new_id or not old_id.strip() or not new_id.strip():
@@ -33,8 +33,11 @@ def ieds_update_patient_id(old_id: str, new_id: str) -> dict:
3333

3434
new_patient_pk = f"Patient#{new_id}"
3535

36-
logger.info("Getting items to update in IEDS table...")
37-
items_to_update = get_items_from_patient_id(old_id)
36+
if items_to_update is None:
37+
logger.info("Getting items to update in IEDS table...")
38+
items_to_update = get_items_from_patient_id(old_id)
39+
else:
40+
logger.info("Using provided items_to_update list, size=%d", len(items_to_update))
3841

3942
if not items_to_update:
4043
logger.warning(f"No items found to update for patient ID: {old_id}")

lambdas/id_sync/src/record_processor.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from common.clients import logger
2-
from typing import Dict, Any
2+
from typing import Dict, List, Any
33
from pds_details import pds_get_patient_id, pds_get_patient_details
44
from ieds_db_operations import (
55
ieds_update_patient_id,
@@ -77,16 +77,34 @@ def process_nhs_number(nhs_number: str) -> Dict[str, Any]:
7777
}
7878

7979
# 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+
)
8293
return {
8394
"status": "success",
84-
"message": "Not all IEDS items matched PDS demographics; update skipped",
95+
"message": "No records matched PDS demographics; update skipped",
8596
"nhs_number": nhs_number,
97+
"matched": 0,
98+
"discarded": len(discarded_records),
8699
}
87100

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+
)
89104
response["nhs_number"] = nhs_number
105+
# add counts for observability
106+
response["matched"] = len(matching_records)
107+
response["discarded"] = len(discarded_records)
90108
return response
91109

92110

@@ -144,16 +162,17 @@ def normalize_strings(item: Any) -> str | None:
144162
logger.debug("demographics_match: no patient resource in IEDS table item")
145163
return False
146164

147-
# normalize patient name
165+
# normalize patient fields from IEDS
148166
ieds_name = normalize_strings(extract_normalized_name_from_patient(patient))
149-
150167
ieds_gender = normalize_strings(patient.get("gender"))
151-
ieds_birth = patient.get("birthDate")
168+
ieds_birth = normalize_strings(patient.get("birthDate"))
152169

170+
# All required fields must be present
153171
if not all([pds_name, pds_gender, pds_birth, ieds_name, ieds_gender, ieds_birth]):
154172
logger.debug("demographics_match: missing required demographics")
155173
return False
156174

175+
# Compare fields
157176
if pds_birth != ieds_birth:
158177
logger.debug("demographics_match: birthDate mismatch %s != %s", pds_birth, ieds_birth)
159178
return False

lambdas/id_sync/tests/test_record_processor.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,12 @@ def test_process_record_demographics_mismatch_skips_update(self):
121121
}
122122
self.mock_get_items_from_patient_id.return_value = [non_matching_item]
123123

124-
# Act
124+
# Act
125125
result = process_record(test_sqs_record)
126126

127127
# Assert
128128
self.assertEqual(result["status"], "success")
129-
self.assertEqual(result["message"], "Not all IEDS items matched PDS demographics; update skipped")
129+
self.assertEqual(result["message"], "No records matched PDS demographics; update skipped")
130130

131131
def test_invalid_body_parsing_returns_error(self):
132132
"""When body is a malformed string, process_record should return an error"""
@@ -200,7 +200,7 @@ def test_update_called_on_match(self):
200200

201201
result = process_record(test_sqs_record)
202202
self.assertEqual(result["status"], "success")
203-
self.mock_ieds_update_patient_id.assert_called_once_with(nhs_number, pds_id)
203+
self.mock_ieds_update_patient_id.assert_called_once_with(nhs_number, pds_id, items_to_update=[item])
204204

205205
def test_process_record_no_records_exist(self):
206206
"""Test when no records exist for the patient ID"""
@@ -315,7 +315,7 @@ def test_process_record_birthdate_mismatch_skips_update(self):
315315

316316
result = process_record(test_sqs_record)
317317
self.assertEqual(result["status"], "success")
318-
self.assertEqual(result["message"], "Not all IEDS items matched PDS demographics; update skipped")
318+
self.assertEqual(result["message"], "No records matched PDS demographics; update skipped")
319319

320320
def test_process_record_gender_mismatch_skips_update(self):
321321
"""If gender differs between PDS and IEDS, update should be skipped"""
@@ -352,7 +352,7 @@ def test_process_record_gender_mismatch_skips_update(self):
352352

353353
result = process_record(test_sqs_record)
354354
self.assertEqual(result["status"], "success")
355-
self.assertEqual(result["message"], "Not all IEDS items matched PDS demographics; update skipped")
355+
self.assertEqual(result["message"], "No records matched PDS demographics; update skipped")
356356

357357
def test_process_record_no_comparable_fields_skips_update(self):
358358
"""If PDS provides no comparable fields, do not update (skip)"""
@@ -385,4 +385,4 @@ def test_process_record_no_comparable_fields_skips_update(self):
385385

386386
result = process_record(test_sqs_record)
387387
self.assertEqual(result["status"], "success")
388-
self.assertEqual(result["message"], "Not all IEDS items matched PDS demographics; update skipped")
388+
self.assertEqual(result["message"], "No records matched PDS demographics; update skipped")

0 commit comments

Comments
 (0)