Skip to content

Commit 37e377a

Browse files
dlzhry2nhsAkol125
authored andcommitted
VED-897 Remove PID from id sync lambda (#968)
1 parent 9b611a0 commit 37e377a

File tree

22 files changed

+226
-446
lines changed

22 files changed

+226
-446
lines changed

.github/pull_request_template.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Add any other relevant notes or explanations here. **Remove this line if you hav
1919

2020
:information_source: This section is to be filled in by the **reviewer**.
2121

22-
- [ ] I have reviewed the changes in this PR and they fill all or part of the acceptance criteria of the ticket, and the code is in a mergeable state.
22+
- [ ] I have reviewed the changes in this PR and they fill all of the acceptance criteria of the ticket.
2323
- [ ] If there were infrastructure, operational, or build changes, I have made sure there is sufficient evidence that the changes will work.
24-
- [ ] I have ensured the changelog has been updated by the submitter, if necessary.
24+
- [ ] If there were changes that are outside of the regular release processes e.g. account infrastructure to setup, manual setup for external API integrations, secrets to set, then I have checked that the developer has flagged this to the Tech Lead as release steps.
25+
- [ ] I have checked that no Personal Identifiable Data (PID) is logged as part of the changes.
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
class IdSyncException(Exception):
22
"""Custom exception for ID Sync errors."""
33

4-
def __init__(self, message: str, nhs_numbers: list = None, exception=None):
4+
def __init__(self, message: str):
55
self.message = message
6-
self.nhs_numbers = nhs_numbers
7-
self.inner_exception = exception
86
super().__init__(message)

lambdas/id_sync/src/id_sync.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,20 @@ def handler(event_data: Dict[str, Any], _context) -> Dict[str, Any]:
2525

2626
logger.info("id_sync processing event with %d records", len(records))
2727

28-
results = []
29-
nhs_numbers = []
3028
error_count = 0
3129

3230
for record in records:
3331
result = process_record(record)
34-
results.append(result)
35-
36-
if "nhs_number" in result:
37-
nhs_numbers.append(result["nhs_number"])
3832

3933
if result.get("status") == "error":
4034
error_count += 1
4135

4236
if error_count > 0:
4337
raise IdSyncException(
4438
message=f"Processed {len(records)} records with {error_count} errors",
45-
nhs_numbers=nhs_numbers,
4639
)
4740

48-
response = {
49-
"status": "success",
50-
"message": f"Successfully processed {len(records)} records",
51-
"nhs_numbers": nhs_numbers,
52-
}
41+
response = {"status": "success", "message": f"Successfully processed {len(records)} records"}
5342

5443
logger.info("id_sync handler completed: %s", response)
5544
return response

lambdas/id_sync/src/ieds_db_operations.py

Lines changed: 14 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,15 @@ def get_ieds_table():
2121
return ieds_table
2222

2323

24-
def ieds_update_patient_id(old_id: str, new_id: str, items_to_update: list | None = None) -> dict:
24+
def ieds_update_patient_id(old_id: str, new_id: str, items_to_update: list) -> dict:
2525
"""Update the patient ID (new NHS number) in the IEDS table."""
26-
logger.info(f"ieds_update_patient_id. Update patient ID from {old_id} to {new_id}")
27-
if not old_id or not new_id or not old_id.strip() or not new_id.strip():
28-
return make_status("Old ID and New ID cannot be empty", old_id, "error")
26+
if not items_to_update:
27+
logger.info("No items found to update for patient NHS Number")
28+
return make_status("No items found to update for patient ID")
2929

30-
if old_id == new_id:
31-
return make_status(f"No change in patient ID: {old_id}", old_id)
30+
logger.info(f"Items to update: {len(items_to_update)}")
3231

3332
try:
34-
logger.info(f"Updating patient ID in IEDS from {old_id} to {new_id}")
35-
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))
41-
42-
if not items_to_update:
43-
logger.warning(f"No items found to update for patient ID: {old_id}")
44-
return make_status(f"No items found to update for patient ID: {old_id}", old_id)
45-
46-
logger.info(f"Items to update: {len(items_to_update)}")
47-
4833
# Build transact items and execute them in batches via helpers to keep
4934
# the top-level function easy to read and test.
5035
transact_items = build_transact_items(old_id, new_id, items_to_update)
@@ -55,25 +40,18 @@ def ieds_update_patient_id(old_id: str, new_id: str, items_to_update: list | Non
5540
logger.info(f"All batches complete. Total batches: {total_batches}, All successful: {all_batches_successful}")
5641

5742
if all_batches_successful:
58-
return make_status(
59-
f"IEDS update, patient ID: {old_id}=>{new_id}. {len(items_to_update)} updated {total_batches}.",
60-
old_id,
61-
)
43+
return make_status(f"IEDS update. {len(items_to_update)} item(s) updated in {total_batches} batch(es).")
6244
else:
6345
return make_status(
64-
f"Failed to update some batches for patient ID: {old_id}",
65-
old_id,
66-
"error",
46+
"Failed to update some batches for patient ID",
47+
status="error",
6748
)
6849

6950
except Exception as e:
7051
logger.exception("Error updating patient ID")
71-
logger.info("Error details: %s", e)
7252
raise IdSyncException(
73-
message=f"Error updating patient Id from :{old_id} to {new_id}",
74-
nhs_numbers=[old_id, new_id],
75-
exception=e,
76-
)
53+
message="Error updating patient ID",
54+
) from e
7755

7856

7957
def get_items_from_patient_id(id: str) -> list:
@@ -82,18 +60,15 @@ def get_items_from_patient_id(id: str) -> list:
8260
Delegates actual paging to the internal helper `_paginate_items_for_patient_pk`.
8361
Raises IdSyncException on error.
8462
"""
85-
logger.info("Getting items for patient id: %s", id)
8663
patient_pk = f"Patient#{id}"
8764
try:
8865
return paginate_items_for_patient_pk(patient_pk)
8966
except IdSyncException:
9067
raise
91-
except Exception as e:
92-
logger.exception("Error querying items for patient PK: %s", patient_pk)
68+
except Exception:
69+
logger.exception("Error querying items for patient PK")
9370
raise IdSyncException(
94-
message=f"Error querying items for patient PK: {patient_pk}",
95-
nhs_numbers=[patient_pk],
96-
exception=e,
71+
message="Error querying items for patient PK",
9772
)
9873

9974

@@ -119,8 +94,6 @@ def paginate_items_for_patient_pk(patient_pk: str) -> list:
11994
logger.exception("Unexpected DynamoDB response: missing 'Items'")
12095
raise IdSyncException(
12196
message="No Items in DynamoDB response",
122-
nhs_numbers=[patient_pk],
123-
exception=response,
12497
)
12598

12699
items = response.get("Items", [])
@@ -131,7 +104,7 @@ def paginate_items_for_patient_pk(patient_pk: str) -> list:
131104
break
132105

133106
if not all_items:
134-
logger.warning("No items found for patient PK: %s", patient_pk)
107+
logger.info("No items found for patient PK in VEDS database")
135108
return []
136109

137110
return all_items

lambdas/id_sync/src/pds_details.py

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
# Get Patient details from external service PDS using NHS number from MNS notification
1919
def pds_get_patient_details(nhs_number: str) -> dict:
2020
try:
21-
logger.info(f"get patient details. nhs_number: {nhs_number}")
22-
2321
cache = Cache(directory=safe_tmp_dir)
2422
authenticator = AppRestrictedAuth(
2523
service=Service.PDS,
@@ -31,26 +29,12 @@ def pds_get_patient_details(nhs_number: str) -> dict:
3129
patient = pds_service.get_patient_details(nhs_number)
3230
return patient
3331
except Exception as e:
34-
msg = f"Error getting PDS patient details for {nhs_number}"
32+
msg = "Error retrieving patient details from PDS"
3533
logger.exception(msg)
36-
raise IdSyncException(message=msg, exception=e)
37-
38-
39-
# Extract Patient identifier value from PDS patient details
40-
def pds_get_patient_id(nhs_number: str) -> str:
41-
"""
42-
Get PDS patient ID from NHS number.
43-
:param nhs_number: NHS number of the patient
44-
:return: PDS patient ID
45-
"""
46-
try:
47-
patient_details = pds_get_patient_details(nhs_number)
48-
if not patient_details:
49-
return None
34+
raise IdSyncException(message=msg) from e
5035

51-
return patient_details["identifier"][0]["value"]
5236

53-
except Exception as e:
54-
msg = f"Error getting PDS patient ID for {nhs_number}"
55-
logger.exception(msg)
56-
raise IdSyncException(message=msg, exception=e)
37+
def get_nhs_number_from_pds_resource(pds_resource: dict) -> str:
38+
"""Simple helper to get the NHS Number from a PDS Resource. No handling as this is a mandatory field in the PDS
39+
response. Must only use where we have ensured an object has been returned."""
40+
return pds_resource["identifier"][0]["value"]

0 commit comments

Comments
 (0)