88from botocore .exceptions import ClientError
99from log_firehose import FirehoseLogger
1010from Converter import Converter
11+ from common .mappings import ActionFlag , Operation , EventName
1112
1213failure_queue_url = os .environ ["AWS_SQS_QUEUE_URL" ]
1314delta_table_name = os .environ ["DELTA_TABLE_NAME" ]
@@ -37,6 +38,7 @@ def get_vaccine_type(patientsk) -> str:
3738
3839
3940def handler (event , context ):
41+ ret = True
4042 logger .info ("Starting Delta Handler" )
4143 log_data = dict ()
4244 firehose_log = dict ()
@@ -61,14 +63,14 @@ def handler(event, context):
6163 response = str ()
6264 imms_id = str ()
6365 operation = str ()
64- if record ["eventName" ] != "REMOVE" :
66+ if record ["eventName" ] != EventName . DELETE_PHYSICAL :
6567 new_image = record ["dynamodb" ]["NewImage" ]
6668 imms_id = new_image ["PK" ]["S" ].split ("#" )[1 ]
6769 vaccine_type = get_vaccine_type (new_image ["PatientSK" ]["S" ])
6870 supplier_system = new_image ["SupplierSystem" ]["S" ]
6971 if supplier_system not in ("DPSFULL" , "DPSREDUCED" ):
7072 operation = new_image ["Operation" ]["S" ]
71- action_flag = "NEW" if operation == " CREATE" else operation
73+ action_flag = ActionFlag . CREATE if operation == Operation . CREATE else operation
7274 resource_json = json .loads (new_image ["Resource" ]["S" ])
7375 FHIRConverter = Converter (json .dumps (resource_json ))
7476 flat_json = FHIRConverter .runConversion (resource_json ) # Get the flat JSON
@@ -94,17 +96,17 @@ def handler(event, context):
9496 firehose_log ["event" ] = log_data
9597 firehose_logger .send_log (firehose_log )
9698 logger .info (f"Record from DPS skipped for { imms_id } " )
97- return { "statusCode" : 200 , "body" : f"Record from DPS skipped for { imms_id } " }
99+ continue
98100 else :
99- operation = "REMOVE"
101+ operation = Operation . DELETE_PHYSICAL
100102 new_image = record ["dynamodb" ]["Keys" ]
101103 logger .info (f"Record to delta:{ new_image } " )
102104 imms_id = new_image ["PK" ]["S" ].split ("#" )[1 ]
103105 response = delta_table .put_item (
104106 Item = {
105107 "PK" : str (uuid .uuid4 ()),
106108 "ImmsID" : imms_id ,
107- "Operation" : "REMOVE" ,
109+ "Operation" : Operation . DELETE_PHYSICAL ,
108110 "VaccineType" : "default" ,
109111 "SupplierSystem" : "default" ,
110112 "DateTimeStamp" : approximate_creation_time .isoformat (),
@@ -131,7 +133,6 @@ def handler(event, context):
131133 firehose_log ["event" ] = log_data
132134 firehose_logger .send_log (firehose_log )
133135 logger .info (log )
134- return {"statusCode" : 200 , "body" : "Records processed successfully" }
135136 else :
136137 log = f"Record NOT created for { imms_id } "
137138 operation_outcome ["statusCode" ] = "500"
@@ -140,7 +141,7 @@ def handler(event, context):
140141 firehose_log ["event" ] = log_data
141142 firehose_logger .send_log (firehose_log )
142143 logger .info (log )
143- return { "statusCode" : 500 , "body" : "Records not processed successfully" }
144+ ret = False
144145
145146 except Exception as e :
146147 operation_outcome ["statusCode" ] = "500"
@@ -155,7 +156,5 @@ def handler(event, context):
155156 log_data ["operation_outcome" ] = operation_outcome
156157 firehose_log ["event" ] = log_data
157158 firehose_logger .send_log (firehose_log )
158- return {
159- "statusCode" : 500 ,
160- "body" : "Records not processed" ,
161- }
159+ ret = False
160+ return ret
0 commit comments