|
| 1 | +import json |
| 2 | +from time import time |
| 3 | +from boto3 import resource |
| 4 | +from boto3 import client |
| 5 | +from lambdas.batch_processing.csv_to_model import read_csv_to_immunizations |
| 6 | +from lambdas.batch_processing.model_to_fhir import convert_to_fhir |
| 7 | + |
| 8 | + |
| 9 | +# This is yet to be integrated with main logic |
| 10 | +def lambda_handler(_event, _context): |
| 11 | + s3_client = client("s3") |
| 12 | + records = _event.get("Records", []) |
| 13 | + s3_event = records[0].get("s3", {}) |
| 14 | + source_bucket_name = s3_event.get("bucket", {}).get("name") |
| 15 | + bucket_key_name = s3_event.get("object", {}).get("key") |
| 16 | + |
| 17 | + dest_bucket_name = source_bucket_name.replace("source", "destination") |
| 18 | + output_bucket = resource("s3").Bucket(dest_bucket_name) |
| 19 | + |
| 20 | + # Read the contents of the CSV file from S3 |
| 21 | + try: |
| 22 | + response = s3_client.get_object(Bucket=source_bucket_name, Key=bucket_key_name) |
| 23 | + csv_data = response["Body"].read().decode("utf-8") |
| 24 | + |
| 25 | + immunizations = read_csv_to_immunizations(csv_data) |
| 26 | + |
| 27 | + for immunization in immunizations: |
| 28 | + |
| 29 | + fhir_imms = convert_to_fhir(immunization) |
| 30 | + |
| 31 | + expected_json = json.loads(fhir_imms.get_immunization()) |
| 32 | + expected_json["patient"] = json.loads(fhir_imms.get_patient()) |
| 33 | + expected_json["reportOrigin"] = json.loads(fhir_imms.get_report_origin()) |
| 34 | + expected_json["reasonCode"] = json.loads(fhir_imms.get_reason_code()) |
| 35 | + expected_json["recorded"] = fhir_imms.get_recorded() |
| 36 | + expected_json["manufacturer"] = json.loads(fhir_imms.get_manufacturer()) |
| 37 | + expected_json["performer"] = fhir_imms.get_actor() |
| 38 | + # Add "resourceType" to the "location" element |
| 39 | + expected_json["location"]["resourceType"] = "Location" |
| 40 | + print(json.dumps(expected_json)) |
| 41 | + |
| 42 | + # Write some placeholder bytestring data to a file in the bucket |
| 43 | + filename = f"output_report_{int(time())}.txt" |
| 44 | + data = ( |
| 45 | + f"Test file to see if the lambda writes to the correct S3 bucket. " |
| 46 | + f"This was the name of the original file: {bucket_key_name}. " |
| 47 | + f"content of file: {immunizations}. " |
| 48 | + f"If our AWS bill skyrockets, this file has been written to the wrong bucket!" |
| 49 | + ) |
| 50 | + |
| 51 | + output_bucket.put_object(Body=data, Key=filename) |
| 52 | + |
| 53 | + return {"statusCode": 200} |
| 54 | + except Exception as e: |
| 55 | + print(f"Error reading CSV file: {str(e)}") |
| 56 | + return {"statusCode": 500, "body": "Error reading CSV file from S3."} |
0 commit comments