@@ -44,6 +44,9 @@ def handle_record(record) -> dict:
4444 logger .error ("Error obtaining file_key: %s" , error )
4545 return {"statusCode" : 500 , "message" : "Failed to download file key" , "error" : str (error )}
4646
47+ vaccine_type = "unknown"
48+ supplier = "unknown"
49+
4750 if "data-sources" in bucket_name :
4851
4952 # The lambda is unintentionally invoked when a file is moved into a different folder in the source bucket.
@@ -56,8 +59,6 @@ def handle_record(record) -> dict:
5659 # Set default values for file-specific variables
5760 message_id = "Message id was not created"
5861 created_at_formatted_string = "created_at_time not identified"
59- vaccine_type = "unknown"
60- supplier = "unknown"
6162
6263 try :
6364 # If the record contains a message_id, then the lambda has been invoked by a file already in the queue
@@ -66,9 +67,10 @@ def handle_record(record) -> dict:
6667 # Get message_id if the file is not new, else assign one
6768 message_id = record .get ("message_id" , str (uuid4 ()))
6869
70+ vaccine_type , supplier = validate_file_key (file_key )
71+
6972 created_at_formatted_string = get_created_at_formatted_string (bucket_name , file_key )
7073
71- vaccine_type , supplier = validate_file_key (file_key )
7274 permissions = validate_vaccine_type_permissions (vaccine_type = vaccine_type , supplier = supplier )
7375 if not is_existing_file :
7476 ensure_file_is_not_a_duplicate (file_key , created_at_formatted_string )
@@ -145,12 +147,15 @@ def handle_record(record) -> dict:
145147 except Exception as error : # pylint: disable=broad-except
146148 logger .error ("Error uploading to cache for file '%s': %s" , file_key , error )
147149 message = "Failed to upload file content to cache"
148- return {"statusCode" : 500 , "message" : message , "file_key" : file_key , "error" : str (error )}
150+ return {"statusCode" : 500 , "message" : message , "file_key" : file_key , "error" : str (error ),
151+ "vaccine_type" : vaccine_type , "supplier" : supplier }
149152
150153 else :
151154 logger .error ("Unable to process file %s due to unexpected bucket name %s" , file_key , bucket_name )
152155 message = f"Failed to process file due to unexpected bucket name { bucket_name } "
153- return {"statusCode" : 500 , "message" : message , "file_key" : file_key }
156+
157+ return {"statusCode" : 500 , "message" : message , "file_key" : file_key ,
158+ "vaccine_type" : vaccine_type , "supplier" : supplier }
154159
155160
156161def lambda_handler (event : dict , context ) -> None : # pylint: disable=unused-argument
@@ -161,3 +166,23 @@ def lambda_handler(event: dict, context) -> None: # pylint: disable=unused-argu
161166 handle_record (record )
162167
163168 logger .info ("Filename processor lambda task completed" )
169+
170+
171+ if __name__ == "__main__" :
172+
173+ event = {
174+ "Records" : [
175+ {
176+ "s3" : {
177+ "bucket" : {
178+ "name" : "immunisation-batch-internal-dev-data-sources"
179+ },
180+ "object" : {
181+ "key" : "FLU_Vaccinations_v5_YGM41_20000101T00000001.csv"
182+ }
183+ }
184+ }
185+ ]
186+ }
187+ print (event )
188+ print (lambda_handler (event = event , context = {}))
0 commit comments