Skip to content

Commit 2697aca

Browse files
committed
extract patient resource
1 parent 01cdff5 commit 2697aca

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lambdas/id_sync/src/ieds_db_operations.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
from os_vars import get_ieds_table_name
33
from common.aws_dynamodb import get_dynamodb_table
44
from common.clients import logger, dynamodb_client
5+
import json
6+
import ast
7+
58
from utils import make_status
69
from exceptions.id_sync_exception import IdSyncException
710

@@ -136,13 +139,34 @@ def extract_patient_resource_from_item(item: dict) -> dict | None:
136139
Extract a Patient resource dict from an IEDS database.
137140
"""
138141
patient_resource = item.get("Resource", None)
142+
logger.info(f"patient_resource (raw): {patient_resource}")
143+
144+
# Accept either a dict (preferred) or a JSON / Python-literal string
145+
if isinstance(patient_resource, str):
146+
# Try JSON first, then fall back to ast.literal_eval for single-quotes
147+
try:
148+
patient_resource_parsed = json.loads(patient_resource)
149+
except Exception:
150+
try:
151+
patient_resource_parsed = ast.literal_eval(patient_resource)
152+
except Exception:
153+
logger.debug("extract_patient_resource_from_item: Resource is a string but could not be parsed")
154+
return None
155+
patient_resource = patient_resource_parsed
156+
139157
if not isinstance(patient_resource, dict):
140158
return None
141159

142-
for response in patient_resource.get("contained", []):
160+
# The Patient resource may be nested under 'contained' or be the resource itself
161+
contained = patient_resource.get("contained") or []
162+
for response in contained:
143163
if isinstance(response, dict) and response.get("resourceType") == "Patient":
144164
return response
145165

166+
# Fallback: if the resource is itself a Patient, return it
167+
if patient_resource.get("resourceType") == "Patient":
168+
return patient_resource
169+
146170
return None
147171

148172

lambdas/id_sync/src/record_processor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def process_nhs_number(nhs_number: str) -> Dict[str, Any]:
7171
discarded_count = 0
7272
discarded_records = []
7373
for detail in ieds_resources:
74+
logger.info("Processing IEDS record: %s", detail)
7475
if demographics_match(pds_patient_resource, detail):
7576
matching_records.append(detail)
7677
else:
@@ -79,7 +80,7 @@ def process_nhs_number(nhs_number: str) -> Dict[str, Any]:
7980

8081
if not matching_records:
8182
logger.info("No records matched PDS demographics: %d\nDiscarded:\n%s", discarded_count,
82-
json.dumps(discarded_records, indent=2))
83+
json.dumps(discarded_records, indent=2), default=str)
8384
return make_status("No records matched PDS demographics; update skipped", nhs_number)
8485

8586
response = ieds_update_patient_id(

0 commit comments

Comments
 (0)