Skip to content

Commit f7c5872

Browse files
committed
VED-755: refactor handler
1 parent 1bd0b96 commit f7c5872

File tree

3 files changed

+38
-36
lines changed

3 files changed

+38
-36
lines changed

lambdas/id_sync/README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,4 @@
2828

2929
- Code is located in the `lambdas/id_sync/src/` directory.
3030
- Unit tests are in the `lambdas/id_sync/tests/` directory.
31-
- Use the provided Makefile and Dockerfile for building, testing, and packaging.
32-
33-
## License
34-
35-
This project is maintained by NHS. See [LICENSE](../LICENSE) for details.
31+
- Use the provided Makefile and Dockerfile for building, testing, and packaging.

lambdas/id_sync/src/id_sync.py

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,53 @@
1-
from common.clients import logger
2-
from common.clients import STREAM_NAME
1+
from typing import Any, Dict
2+
3+
from common.clients import logger, STREAM_NAME
34
from common.log_decorator import logging_decorator
45
from common.aws_lambda_event import AwsLambdaEvent
56
from exceptions.id_sync_exception import IdSyncException
67
from record_processor import process_record
7-
'''
8-
Lambda function handler for processing SQS events.Lambda for ID Sync. Fired by SQS
9-
'''
108

9+
"""
10+
- Parses the incoming AWS event into `AwsLambdaEvent` and iterate its `records`.
11+
- Delegate each record to `process_record` and collect `nhs_number` from each result.
12+
- If any record has status == "error" raise `IdSyncException` with aggregated nhs_numbers.
13+
- Any unexpected error is wrapped into `IdSyncException(message="Error processing id_sync event")`.
14+
"""
1115

1216
@logging_decorator(prefix="id_sync", stream_name=STREAM_NAME)
13-
def handler(event_data, _):
14-
17+
def handler(event_data: Dict[str, Any], _context) -> Dict[str, Any]:
1518
try:
1619
logger.info("id_sync handler invoked")
1720
event = AwsLambdaEvent(event_data)
18-
record_count = len(event.records)
19-
if record_count > 0:
20-
logger.info("id_sync processing event with %d records", record_count)
21-
error_count = 0
22-
nhs_numbers = []
23-
for record in event.records:
24-
record_result = process_record(record)
25-
nhs_numbers.append(record_result["nhs_number"])
26-
if record_result["status"] == "error":
27-
error_count += 1
28-
if error_count > 0:
29-
raise IdSyncException(message=f"Processed {record_count} records with {error_count} errors",
30-
nhs_numbers=nhs_numbers)
31-
32-
else:
33-
response = {"status": "success",
34-
"message": f"Successfully processed {record_count} records",
35-
"nhs_numbers": nhs_numbers}
36-
else:
37-
response = {"status": "success", "message": "No records found in event"}
21+
records = event.records
22+
23+
if not records:
24+
return {"status": "success", "message": "No records found in event"}
25+
26+
logger.info("id_sync processing event with %d records", len(records))
27+
28+
# Process records in order. Let any unexpected exception bubble to the outer handler
29+
# so tests that expect a wrapped IdSyncException keep working.
30+
results = [process_record(record) for record in records]
31+
nhs_numbers = [result["nhs_number"] for result in results]
32+
error_count = sum(1 for result in results if result.get("status") == "error")
33+
34+
if error_count:
35+
raise IdSyncException(message=f"Processed {len(records)} records with {error_count} errors",
36+
nhs_numbers=nhs_numbers)
37+
38+
response = {"status": "success",
39+
"message": f"Successfully processed {len(records)} records",
40+
"nhs_numbers": nhs_numbers}
41+
3842
logger.info("id_sync handler completed: %s", response)
3943
return response
44+
4045
except IdSyncException as e:
46+
# Preserve domain exceptions but ensure they're logged
4147
logger.exception(f"id_sync error: {e.message}")
42-
raise e
43-
except Exception as e:
48+
raise
49+
except Exception:
4450
msg = "Error processing id_sync event"
4551
logger.exception(msg)
46-
raise IdSyncException(message=msg, exception=e)
52+
# Raise a domain exception with a predictable message for callers/tests
53+
raise IdSyncException(message=msg)

lambdas/id_sync/src/pds_details.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ def pds_get_patient_id(nhs_number: str) -> str:
4848

4949
return patient_details["identifier"][0]["value"]
5050

51-
# ✅ Remove the IdSyncException catch since you're just re-raising
5251
except Exception as e:
5352
msg = f"Error getting PDS patient ID for {nhs_number}"
5453
logger.exception(msg)

0 commit comments

Comments
 (0)