-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathid_sync.py
More file actions
50 lines (40 loc) · 1.92 KB
/
id_sync.py
File metadata and controls
50 lines (40 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""
- Parses the incoming AWS event into `AwsLambdaEvent` and iterate its `records`.
- Delegate each record to `process_record` and collect `nhs_number` from each result.
- If any record has status == "error" raise `IdSyncException` with aggregated nhs_numbers.
- Any unexpected error is wrapped into `IdSyncException(message="Error processing id_sync event")`.
"""
from typing import Any, Dict
from common.clients import logger, STREAM_NAME
from common.log_decorator import logging_decorator
from common.aws_lambda_event import AwsLambdaEvent
from exceptions.id_sync_exception import IdSyncException
from record_processor import process_record
@logging_decorator(prefix="id_sync", stream_name=STREAM_NAME)
def handler(event_data: Dict[str, Any], _context) -> Dict[str, Any]:
try:
event = AwsLambdaEvent(event_data)
records = event.records
if not records:
return {"status": "success", "message": "No records found in event"}
logger.info("id_sync processing event with %d records", len(records))
results = [process_record(record) for record in records]
nhs_numbers = [result["nhs_number"] for result in results]
error_count = sum(1 for result in results if result.get("status") == "error")
if error_count:
raise IdSyncException(message=f"Processed {len(records)} records with {error_count} errors",
nhs_numbers=nhs_numbers)
response = {
"status": "success",
"message": f"Successfully processed {len(records)} records",
"nhs_numbers": nhs_numbers
}
logger.info("id_sync handler completed: %s", response)
return response
except IdSyncException as e:
logger.exception(f"id_sync error: {e.message}")
raise
except Exception:
msg = "Error processing id_sync event"
logger.exception(msg)
raise IdSyncException(message=msg)