|
3 | 3 | ''' |
4 | 4 | from common.aws_lambda_sqs_event_record import AwsLambdaSqsEventRecord |
5 | 5 | from common.clients import logger |
| 6 | +from pds_details import get_pds_patient_details |
| 7 | +import json |
| 8 | +from typing import Optional |
6 | 9 |
|
7 | 10 |
|
8 | | -def process_record(event_record, _): |
| 11 | +def process_record(event_record: AwsLambdaSqsEventRecord): |
9 | 12 | record = AwsLambdaSqsEventRecord(event_record) if isinstance(event_record, dict) else event_record |
10 | 13 | logger.info("Processing record: %s", record) |
11 | | - return f"hello world {record}" |
| 14 | + |
| 15 | + id = get_id(event_record.body) |
| 16 | + |
| 17 | + if id: |
| 18 | + # TODO This code is a placeholder for checking if records exist in the database - defaulting to True for now |
| 19 | + exists = check_records_exist(id) |
| 20 | + |
| 21 | + if exists: |
| 22 | + # get patient details from PDS |
| 23 | + patient_details = get_pds_patient_details(id) |
| 24 | + if not patient_details: |
| 25 | + return {"status": "error", "message": f"No records returned for ID: {id}"} |
| 26 | + |
| 27 | + patient_details_id = patient_details.get("id") |
| 28 | + |
| 29 | + # if patient NHS != id, update patient index of vax events to new number |
| 30 | + if patient_details_id != id: |
| 31 | + return update_patient_index(id, patient_details_id) |
| 32 | + else: |
| 33 | + return {"status": "success", "message": "No update required"} |
| 34 | + else: |
| 35 | + return {"status": "error", "message": f"No records found for ID: {id}"} |
| 36 | + else: |
| 37 | + return {"status": "error", "message": "No ID found in event record"} |
| 38 | + |
| 39 | + |
| 40 | +def check_records_exist(id: str) -> bool: |
| 41 | + # TODO: Implement logic to check if records exist in the database |
| 42 | + logger.info(f"TODO Check if records exist for {id}") |
| 43 | + return True |
| 44 | + |
| 45 | + |
| 46 | +def update_patient_index(old_id: str, new_id: str): |
| 47 | + # TODO: Implement logic to update patient index in Redis or other data store |
| 48 | + logger.info(f"TODO Update patient index from {old_id} to {new_id}") |
| 49 | + return {"status": "success", "message": f"Updated patient idx from {old_id} to {new_id}", "TODO": "Implement logic"} |
| 50 | + |
| 51 | + |
| 52 | +def get_id(event_body) -> Optional[str]: |
| 53 | + """Extract subject identifier from FHIR Bundle notification event""" |
| 54 | + try: |
| 55 | + # Parse JSON if it's a string |
| 56 | + if isinstance(event_body, str): |
| 57 | + data = json.loads(event_body) |
| 58 | + else: |
| 59 | + data = event_body |
| 60 | + # Navigate through the nested structure |
| 61 | + subject = data.get("subject") |
| 62 | + return subject |
| 63 | + |
| 64 | + except (json.JSONDecodeError, KeyError, AttributeError) as e: |
| 65 | + logger.error("Error extracting subject identifier: %s", e) |
| 66 | + return None |
0 commit comments