1+ import json
12import os
23import uuid
34from datetime import datetime
5+ from typing import Any
46
57from aws_lambda_typing .events .sqs import SQSMessage
68
79from common .api_clients .get_pds_details import pds_get_patient_details
810from common .clients import logger
911from common .get_service_url import get_service_url
10- from constants import IMMUNISATION_TYPE , SPEC_VERSION , MnsNotificationPayload
11- from sqs_dynamo_utils import extract_sqs_imms_data
12+ from constants import DYNAMO_DB_TYPE_DESCRIPTORS , IMMUNISATION_TYPE , SPEC_VERSION , MnsNotificationPayload
1213
1314IMMUNIZATION_ENV = os .getenv ("IMMUNIZATION_ENV" )
1415IMMUNIZATION_BASE_PATH = os .getenv ("IMMUNIZATION_BASE_PATH" )
@@ -18,28 +19,37 @@ def create_mns_notification(sqs_event: SQSMessage) -> MnsNotificationPayload:
1819 """Create a notification payload for MNS."""
1920 immunisation_url = get_service_url (IMMUNIZATION_ENV , IMMUNIZATION_BASE_PATH )
2021
21- # Simple, direct extraction
22- imms_data = extract_sqs_imms_data (sqs_event )
22+ body = json .loads (sqs_event .get ("body" , "{}" ))
23+ new_image = body .get ("dynamodb" , {}).get ("NewImage" , {})
24+ imms_id = _unwrap_dynamodb_value (new_image .get ("ImmsID" , {}))
25+ supplier_system = _unwrap_dynamodb_value (new_image .get ("SupplierSystem" , {}))
26+ vaccine_type = _unwrap_dynamodb_value (new_image .get ("VaccineType" , {}))
27+ operation = _unwrap_dynamodb_value (new_image .get ("Operation" , {}))
2328
24- patient_age = calculate_age_at_vaccination (imms_data ["person_dob" ], imms_data ["date_and_time" ])
29+ imms_map = new_image .get ("Imms" , {}).get ("M" , {})
30+ nhs_number = _unwrap_dynamodb_value (imms_map .get ("NHS_NUMBER" , {}))
31+ person_dob = _unwrap_dynamodb_value (imms_map .get ("PERSON_DOB" , {}))
32+ date_and_time = _unwrap_dynamodb_value (imms_map .get ("DATE_AND_TIME" , {}))
33+ site_code = _unwrap_dynamodb_value (imms_map .get ("SITE_CODE" , {}))
2534
26- gp_ods_code = get_practitioner_details_from_pds (imms_data ["nhs_number" ])
35+ patient_age = calculate_age_at_vaccination (person_dob , date_and_time )
36+ gp_ods_code = get_practitioner_details_from_pds (nhs_number )
2737
2838 return {
2939 "specversion" : SPEC_VERSION ,
3040 "id" : str (uuid .uuid4 ()),
3141 "source" : immunisation_url ,
3242 "type" : IMMUNISATION_TYPE ,
33- "time" : imms_data [ " date_and_time" ] ,
34- "subject" : imms_data [ " nhs_number" ] ,
35- "dataref" : f"{ immunisation_url } /Immunization/{ imms_data [ ' imms_id' ] } " ,
43+ "time" : date_and_time ,
44+ "subject" : nhs_number ,
45+ "dataref" : f"{ immunisation_url } /Immunization/{ imms_id } " ,
3646 "filtering" : {
3747 "generalpractitioner" : gp_ods_code ,
38- "sourceorganisation" : imms_data [ " site_code" ] ,
39- "sourceapplication" : imms_data [ " supplier_system" ] ,
48+ "sourceorganisation" : site_code ,
49+ "sourceapplication" : supplier_system ,
4050 "subjectage" : str (patient_age ),
41- "immunisationtype" : imms_data [ " vaccine_type" ] ,
42- "action" : imms_data [ " operation" ] ,
51+ "immunisationtype" : vaccine_type ,
52+ "action" : operation ,
4353 },
4454 }
4555
@@ -95,3 +105,24 @@ def get_practitioner_details_from_pds(nhs_number: str) -> str | None:
95105 return None
96106
97107 return gp_ods_code
108+
109+
110+ def _unwrap_dynamodb_value (value : dict ) -> Any :
111+ """
112+ Unwrap DynamoDB type descriptor to get the actual value.
113+ DynamoDB types: S (String), N (Number), BOOL, M (Map), L (List), NULL
114+ """
115+ if not isinstance (value , dict ):
116+ return value
117+
118+ # DynamoDB type descriptors
119+ if "NULL" in value :
120+ return None
121+
122+ # Check other DynamoDB types
123+ for key in DYNAMO_DB_TYPE_DESCRIPTORS :
124+ if key in value :
125+ return value [key ]
126+
127+ # Not a DynamoDB type, return as-is
128+ return value
0 commit comments