|
3 | 3 | ''' |
4 | 4 | from common.aws_lambda_sqs_event_record import AwsLambdaSqsEventRecord |
5 | 5 | from common.clients import logger |
| 6 | +import json |
| 7 | +from typing import Optional |
6 | 8 |
|
7 | | - |
8 | | -def process_record(event_record, _): |
| 9 | +def process_record(event_record: AwsLambdaSqsEventRecord): |
9 | 10 | record = AwsLambdaSqsEventRecord(event_record) if isinstance(event_record, dict) else event_record |
10 | 11 | logger.info("Processing record: %s", record) |
| 12 | + |
| 13 | + id = get_id(event_record.body) |
| 14 | + |
| 15 | + exists = check_records_exist(id) |
| 16 | + |
11 | 17 | return f"hello world {record}" |
| 18 | + |
| 19 | + |
| 20 | +def get_id(event_body) -> Optional[str]: |
| 21 | + """Extract subject identifier from FHIR Bundle notification event""" |
| 22 | + try: |
| 23 | + # Parse JSON if it's a string |
| 24 | + if isinstance(event_body, str): |
| 25 | + data = json.loads(event_body) |
| 26 | + else: |
| 27 | + data = event_body |
| 28 | + # Navigate through the nested structure |
| 29 | + entries = data.get("entry", []) |
| 30 | + if not entries: |
| 31 | + logger.warning("No entries found in bundle") |
| 32 | + return None |
| 33 | + |
| 34 | + # Get the first entry's resource |
| 35 | + first_entry = entries[0] |
| 36 | + resource = first_entry.get("resource", {}) |
| 37 | + parameters = resource.get("parameter", []) |
| 38 | + |
| 39 | + # Find the "additional-context" parameter |
| 40 | + for param in parameters: |
| 41 | + if param.get("name") == "additional-context": |
| 42 | + parts = param.get("part", []) |
| 43 | + |
| 44 | + # Find the "subject" part within additional-context |
| 45 | + for part in parts: |
| 46 | + if part.get("name") == "subject": |
| 47 | + value_ref = part.get("valueReference", {}) |
| 48 | + identifier = value_ref.get("identifier", {}) |
| 49 | + subject_id = identifier.get("value") |
| 50 | + |
| 51 | + if subject_id: |
| 52 | + logger.info("Found subject identifier: %s", subject_id) |
| 53 | + return subject_id |
| 54 | + |
| 55 | + logger.warning("Subject identifier not found in notification event") |
| 56 | + return None |
| 57 | + |
| 58 | + except (json.JSONDecodeError, KeyError, AttributeError) as e: |
| 59 | + logger.error("Error extracting subject identifier: %s", e) |
| 60 | + return None |
0 commit comments