Skip to content

Commit 5d60b2b

Browse files
committed
IEDS update - tests pass
1 parent 21d0ea6 commit 5d60b2b

File tree

8 files changed

+208
-512
lines changed

8 files changed

+208
-512
lines changed

lambdas/id_sync/src/id_sync.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
from common.clients import STREAM_NAME
33
from common.log_decorator import logging_decorator
44
from common.aws_lambda_event import AwsLambdaEvent
5-
from common.aws_lambda_sqs_event_record import AwsLambdaSqsEventRecord
65
from record_processor import process_record
76
'''
87
Lambda function handler for processing SQS events.Lambda for ID Sync. Fired by SQS
@@ -20,7 +19,6 @@ def handler(event_data, _):
2019
error_count = 0
2120
file_keys = []
2221
for record in event.records:
23-
record: AwsLambdaSqsEventRecord
2422
record_result = process_record(record, None)
2523
file_keys.append(record_result["file_key"])
2624
if record_result["status"] == "error":

lambdas/id_sync/src/ieds_db_operations.py

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from boto3.dynamodb.conditions import Key
22
from os_vars import get_ieds_table_name
3-
from common.clients import get_delta_table
3+
from common.aws_dynamodb import get_delta_table
4+
from common.clients import logger
45

56
ieds_table = None
67

@@ -14,7 +15,7 @@ def get_ieds_table():
1415
return ieds_table
1516

1617

17-
def check_record_exist_in_IEDS(id: str) -> bool:
18+
def ieds_check_exist(id: str) -> bool:
1819
"""Check if a record exists in the IEDS table for the given ID."""
1920
search_patient_pk = f"Patient#{id}"
2021

@@ -26,16 +27,35 @@ def check_record_exist_in_IEDS(id: str) -> bool:
2627
return response.get("Count", 0) > 0
2728

2829

29-
def update_patient_id_in_IEDS(old_id: str, new_id: str) -> dict:
30+
def ieds_update_patient_id(old_id: str, new_id: str) -> dict:
3031
"""Update the patient ID in the IEDS table."""
31-
# check if old_id and new_id are not empty
32-
if not old_id or not new_id:
32+
if not old_id or not new_id or not old_id.strip() or not new_id.strip():
3333
return {"status": "error", "message": "Old ID and New ID cannot be empty"}
34-
else:
35-
# update the table with new id
36-
get_ieds_table().update_item(
34+
35+
if old_id == new_id:
36+
return {"status": "success", "message": f"No change in patient ID: {old_id}"}
37+
38+
try:
39+
# Update the table with new id
40+
response = get_ieds_table().update_item(
3741
Key={"PK": f"Patient#{old_id}"},
3842
UpdateExpression="SET PK = :new_id",
3943
ExpressionAttributeValues={":new_id": f"Patient#{new_id}"}
4044
)
41-
return {"status": "success", "message": f"Updated IEDS, patient ID: {old_id} to {new_id}"}
45+
46+
# Check update successful
47+
if response['ResponseMetadata']['HTTPStatusCode'] == 200:
48+
return {
49+
"status": "success",
50+
"message": f"Updated IEDS, patient ID: {old_id} to {new_id}"
51+
}
52+
else:
53+
return {
54+
"status": "error",
55+
"message": f"Failed to update patient ID: {old_id}"
56+
}
57+
58+
except Exception as e:
59+
logger.exception("Error updating patient ID")
60+
# Handle any exceptions that occur during the update
61+
raise e

lambdas/id_sync/src/nhs_number_processor.py

Lines changed: 0 additions & 26 deletions
This file was deleted.

lambdas/id_sync/src/pds_details.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
pds_env = get_pds_env()
1111

1212

13-
def get_pds_patient_details(nhs_number: str) -> dict:
13+
def pds_get_patient_details(nhs_number: str) -> dict:
1414
try:
1515
logger.info(f"Get PDS patient details for {nhs_number}")
1616

lambdas/id_sync/src/record_processor.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,38 @@
22
record Processor
33
'''
44
from common.clients import logger
5-
import json
65
from typing import Optional
7-
from nhs_number_processor import process_nhs_number
6+
from pds_details import pds_get_patient_details
7+
from ieds_db_operations import ieds_check_exist, ieds_update_patient_id
88

99

1010
def process_record(event_record):
1111

1212
logger.info("Processing record: %s", event_record)
1313
body = event_record.get('body', {})
14-
nhs_number = get_id(body)
14+
nhs_number = body.get("subject")
1515
if nhs_number:
1616
return process_nhs_number(nhs_number)
1717
else:
1818
return {"status": "error", "message": "No NHS number found in event record"}
1919

2020

21-
def get_id(event_body) -> Optional[str]:
22-
"""Extract subject identifier from FHIR Bundle notification event"""
23-
try:
24-
# Parse JSON if it's a string
25-
if isinstance(event_body, str):
26-
data = json.loads(event_body)
27-
else:
28-
data = event_body
29-
# Navigate through the nested structure
30-
subject = data.get("subject")
31-
return subject
21+
def process_nhs_number(nhs_number: str) -> Optional[str]:
22+
# get patient details from PDS
23+
patient_details = pds_get_patient_details(nhs_number)
24+
if not patient_details:
25+
return {"status": "error", "message": f"No records returned for ID: {nhs_number}"}
26+
27+
patient_details_id = patient_details.get("id")
3228

33-
except (json.JSONDecodeError, KeyError, AttributeError) as e:
34-
logger.error("Error extracting subject identifier: %s", e)
35-
return None
29+
if patient_details_id:
30+
# if patient NHS != id, update patient index of vax events to new number
31+
if patient_details_id != nhs_number and patient_details_id:
32+
if ieds_check_exist(patient_details_id):
33+
return ieds_update_patient_id(patient_details_id, nhs_number)
34+
else:
35+
return {"status": "error", "message": f"No records returned for ID: {nhs_number}"}
36+
else:
37+
return {"status": "success", "message": "No update required"}
38+
else:
39+
return {"status": "error", "message": f"No patient ID found for NHS number: {nhs_number}"}

0 commit comments

Comments
 (0)