@@ -55,6 +55,8 @@ def handle_record(record) -> dict:
5555 "error" : str (error ),
5656 }
5757
58+ vaccine_type = "unknown"
59+ supplier = "unknown"
5860 expiry_timestamp = "unknown"
5961
6062 if bucket_name != SOURCE_BUCKET_NAME :
@@ -72,128 +74,64 @@ def handle_record(record) -> dict:
7274 message_id = "Message id was not created"
7375 created_at_formatted_string = "created_at_time not identified"
7476
75- message_id = str (uuid4 ())
76- s3_response = get_s3_client ().get_object (Bucket = bucket_name , Key = file_key )
77- created_at_formatted_string , expiry_timestamp = get_creation_and_expiry_times (s3_response )
78-
79- if file_key .startswith (EXTENDED_ATTRIBUTES_PREFIXES ):
80- return handle_extended_attributes_file (
81- file_key ,
82- bucket_name ,
83- message_id ,
84- created_at_formatted_string ,
85- expiry_timestamp ,
86- )
87- else :
88- return handle_batch_file (
89- file_key ,
90- bucket_name ,
91- message_id ,
92- created_at_formatted_string ,
93- expiry_timestamp ,
94- )
95-
96-
97- def get_file_status_for_error (error : Exception ) -> str :
98- """Creates a file status based on the type of error that was thrown"""
99- if isinstance (error , VaccineTypePermissionsError ):
100- return f"{ FileStatus .NOT_PROCESSED } - { FileNotProcessedReason .UNAUTHORISED } "
101-
102- return FileStatus .FAILED
103-
104-
105- def handle_unexpected_bucket_name (bucket_name : str , file_key : str ) -> dict :
106- """Handles scenario where Lambda was not invoked by the data-sources bucket. Should not occur due to terraform
107- config and overarching design"""
10877 try :
78+ message_id = str (uuid4 ())
79+ s3_response = get_s3_client ().get_object (Bucket = bucket_name , Key = file_key )
80+ created_at_formatted_string , expiry_timestamp = get_creation_and_expiry_times (s3_response )
81+
10982 if file_key .startswith (EXTENDED_ATTRIBUTES_PREFIXES ):
11083 extended_attribute_identifier = validate_extended_attributes_file_key (file_key )
111- logger .error (
112- "Unable to process file %s due to unexpected bucket name %s" ,
84+ move_file_outside_bucket (bucket_name , file_key , DPS_DESTINATION_BUCKET_NAME , f"archive/{ file_key } " )
85+ queue_name = extended_attribute_identifier
86+
87+ upsert_audit_table (
88+ message_id ,
11389 file_key ,
114- bucket_name ,
90+ created_at_formatted_string ,
91+ expiry_timestamp ,
92+ queue_name ,
93+ FileStatus .PROCESSING ,
11594 )
116- message = f"Failed to process file due to unexpected bucket name { bucket_name } "
11795 return {
118- "statusCode" : 500 ,
119- "message" : message ,
96+ "statusCode" : 200 ,
97+ "message" : "Extended Attributes file successfully processed" ,
12098 "file_key" : file_key ,
121- "vaccine_supplier_info" : extended_attribute_identifier ,
99+ "message_id" : message_id ,
100+ "queue_name" : queue_name ,
122101 }
123102 else :
124103 vaccine_type , supplier = validate_batch_file_key (file_key )
125- logger .error (
126- "Unable to process file %s due to unexpected bucket name %s" ,
104+ permissions = validate_vaccine_type_permissions (vaccine_type = vaccine_type , supplier = supplier )
105+ queue_name = f"{ supplier } _{ vaccine_type } "
106+ upsert_audit_table (
107+ message_id ,
127108 file_key ,
128- bucket_name ,
109+ created_at_formatted_string ,
110+ expiry_timestamp ,
111+ queue_name ,
112+ FileStatus .QUEUED ,
113+ )
114+ make_and_send_sqs_message (
115+ file_key ,
116+ message_id ,
117+ permissions ,
118+ vaccine_type ,
119+ supplier ,
120+ created_at_formatted_string ,
129121 )
130- message = f"Failed to process file due to unexpected bucket name { bucket_name } "
131122
123+ logger .info ("Lambda invocation successful for file '%s'" , file_key )
124+
125+ # Return details for logs
132126 return {
133- "statusCode" : 500 ,
134- "message" : message ,
127+ "statusCode" : 200 ,
128+ "message" : "Successfully sent to SQS for further processing" ,
135129 "file_key" : file_key ,
130+ "message_id" : message_id ,
136131 "vaccine_type" : vaccine_type ,
137132 "supplier" : supplier ,
138133 }
139134
140- except Exception as error :
141- logger .error (
142- "Unable to process file due to unexpected bucket name %s and file key %s" ,
143- bucket_name ,
144- file_key ,
145- )
146- message = f"Failed to process file due to unexpected bucket name { bucket_name } and file key { file_key } "
147-
148- return {
149- "statusCode" : 500 ,
150- "message" : message ,
151- "file_key" : file_key ,
152- "vaccine_type" : "unknown" ,
153- "supplier" : "unknown" ,
154- "error" : str (error ),
155- }
156-
157-
158- def handle_batch_file (file_key , bucket_name , message_id , created_at_formatted_string , expiry_timestamp ) -> dict :
159- """
160- Processes a single record for batch file.
161- Returns a dictionary containing information to be included in the logs.
162- """
163- vaccine_type = "unknown"
164- supplier = "unknown"
165- try :
166- vaccine_type , supplier = validate_batch_file_key (file_key )
167- permissions = validate_vaccine_type_permissions (vaccine_type = vaccine_type , supplier = supplier )
168- queue_name = f"{ supplier } _{ vaccine_type } "
169-
170- upsert_audit_table (
171- message_id ,
172- file_key ,
173- created_at_formatted_string ,
174- expiry_timestamp ,
175- queue_name ,
176- FileStatus .QUEUED ,
177- )
178- make_and_send_sqs_message (
179- file_key ,
180- message_id ,
181- permissions ,
182- vaccine_type ,
183- supplier ,
184- created_at_formatted_string ,
185- )
186- logger .info ("Lambda invocation successful for file '%s'" , file_key )
187-
188- return {
189- "statusCode" : 200 ,
190- "message" : "Batch file successfully processed" ,
191- "file_key" : file_key ,
192- "message_id" : message_id ,
193- "vaccine_type" : vaccine_type ,
194- "supplier" : supplier ,
195- "queue_name" : queue_name ,
196- }
197135 except ( # pylint: disable=broad-exception-caught
198136 VaccineTypePermissionsError ,
199137 InvalidFileKeyError ,
@@ -203,8 +141,8 @@ def handle_batch_file(file_key, bucket_name, message_id, created_at_formatted_st
203141 ) as error :
204142 logger .error ("Error processing file '%s': %s" , file_key , str (error ))
205143
206- file_status = get_file_status_for_error (error )
207144 queue_name = f"{ supplier } _{ vaccine_type } "
145+ file_status = get_file_status_for_error (error )
208146
209147 upsert_audit_table (
210148 message_id ,
@@ -224,6 +162,16 @@ def handle_batch_file(file_key, bucket_name, message_id, created_at_formatted_st
224162 move_file (bucket_name , file_key , f"archive/{ file_key } " )
225163
226164 # Return details for logs
165+ if file_key .startswith (EXTENDED_ATTRIBUTES_PREFIXES ):
166+ extended_attribute_identifier = validate_extended_attributes_file_key (file_key )
167+ return {
168+ "statusCode" : ERROR_TYPE_TO_STATUS_CODE_MAP .get (type (error ), 500 ),
169+ "message" : "Infrastructure Level Response Value - Processing Error" ,
170+ "file_key" : file_key ,
171+ "message_id" : message_id ,
172+ "vaccine_supplier_info" : extended_attribute_identifier ,
173+ "error" : str (error ),
174+ }
227175 return {
228176 "statusCode" : ERROR_TYPE_TO_STATUS_CODE_MAP .get (type (error ), 500 ),
229177 "message" : "Infrastructure Level Response Value - Processing Error" ,
@@ -235,61 +183,63 @@ def handle_batch_file(file_key, bucket_name, message_id, created_at_formatted_st
235183 }
236184
237185
238- def handle_extended_attributes_file (
239- file_key , bucket_name , message_id , created_at_formatted_string , expiry_timestamp
240- ) -> dict :
241- """
242- Processes a single record for extended attributes file.
243- Returns a dictionary containing information to be included in the logs.
244- """
245- try :
246- extended_attribute_identifier = validate_extended_attributes_file_key (file_key )
247- move_file_outside_bucket (bucket_name , file_key , DPS_DESTINATION_BUCKET_NAME , f"archive/{ file_key } " )
248- queue_name = extended_attribute_identifier
186+ def get_file_status_for_error (error : Exception ) -> str :
187+ """Creates a file status based on the type of error that was thrown"""
188+ if isinstance (error , VaccineTypePermissionsError ):
189+ return f"{ FileStatus .NOT_PROCESSED } - { FileNotProcessedReason .UNAUTHORISED } "
249190
250- upsert_audit_table (
251- message_id ,
252- file_key ,
253- created_at_formatted_string ,
254- expiry_timestamp ,
255- queue_name ,
256- FileStatus .PROCESSING ,
257- )
258- return {
259- "statusCode" : 200 ,
260- "message" : "Extended Attributes file successfully processed" ,
261- "file_key" : file_key ,
262- "message_id" : message_id ,
263- "queue_name" : queue_name ,
264- }
265- except ( # pylint: disable=broad-exception-caught
266- VaccineTypePermissionsError ,
267- InvalidFileKeyError ,
268- UnhandledAuditTableError ,
269- UnhandledSqsError ,
270- Exception ,
271- ) as error :
272- logger .error ("Error processing file '%s': %s" , file_key , str (error ))
191+ return FileStatus .FAILED
273192
274- file_status = get_file_status_for_error (error )
275- extended_attribute_identifier = validate_extended_attributes_file_key (file_key )
276- queue_name = extended_attribute_identifier
277193
278- upsert_audit_table (
279- message_id ,
194+ def handle_unexpected_bucket_name (bucket_name : str , file_key : str ) -> dict :
195+ """Handles scenario where Lambda was not invoked by the data-sources bucket. Should not occur due to terraform
196+ config and overarching design"""
197+ try :
198+ if file_key .startswith (EXTENDED_ATTRIBUTES_PREFIXES ):
199+ extended_attribute_identifier = validate_extended_attributes_file_key (file_key )
200+ logger .error (
201+ "Unable to process file %s due to unexpected bucket name %s" ,
202+ file_key ,
203+ bucket_name ,
204+ )
205+ message = f"Failed to process file due to unexpected bucket name { bucket_name } "
206+ return {
207+ "statusCode" : 500 ,
208+ "message" : message ,
209+ "file_key" : file_key ,
210+ "vaccine_supplier_info" : extended_attribute_identifier ,
211+ }
212+ else :
213+ vaccine_type , supplier = validate_batch_file_key (file_key )
214+ logger .error (
215+ "Unable to process file %s due to unexpected bucket name %s" ,
216+ file_key ,
217+ bucket_name ,
218+ )
219+ message = f"Failed to process file due to unexpected bucket name { bucket_name } "
220+
221+ return {
222+ "statusCode" : 500 ,
223+ "message" : message ,
224+ "file_key" : file_key ,
225+ "vaccine_type" : vaccine_type ,
226+ "supplier" : supplier ,
227+ }
228+
229+ except Exception as error :
230+ logger .error (
231+ "Unable to process file due to unexpected bucket name %s and file key %s" ,
232+ bucket_name ,
280233 file_key ,
281- created_at_formatted_string ,
282- expiry_timestamp ,
283- extended_attribute_identifier ,
284- file_status ,
285- error_details = str (error ),
286234 )
235+ message = f"Failed to process file due to unexpected bucket name { bucket_name } and file key { file_key } "
287236
288237 return {
289238 "statusCode" : 500 ,
290- "message" : f"Failed to process extended attributes file { file_key } from bucket { bucket_name } " ,
239+ "message" : message ,
291240 "file_key" : file_key ,
292- "message_id" : message_id ,
241+ "vaccine_type" : "unknown" ,
242+ "supplier" : "unknown" ,
293243 "error" : str (error ),
294244 }
295245
0 commit comments