Skip to content

Commit 001fda1

Browse files
committed
refactor all functions
1 parent b36d8ed commit 001fda1

File tree

2 files changed

+45
-36
lines changed

2 files changed

+45
-36
lines changed

lambdas/id_sync/src/ieds_db_operations.py

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
from boto3.dynamodb.conditions import Key
32
from os_vars import get_ieds_table_name
43
from common.aws_dynamodb import get_dynamodb_table
@@ -143,40 +142,17 @@ def get_items_from_patient_id(id: str, limit=BATCH_SIZE) -> list:
143142
exception=e
144143
)
145144

146-
def extract_patient_resource_from_item(item: dict) -> dict | None:
147-
"""Extract a Patient resource dict from an IEDS item.
148145

149-
Accepts common shapes: item['Resource'] (dict or JSON string), item['resource'], or a wrapper with 'resource'.
150-
Returns the patient resource dict or None if not found/parsible.
146+
def extract_patient_resource_from_item(item: dict) -> dict | None:
151147
"""
152-
if not isinstance(item, dict):
148+
Extract a Patient resource dict from an IEDS database.
149+
"""
150+
patient_resource = item.get("Resource", None)
151+
if not isinstance(patient_resource, dict):
153152
return None
154153

155-
candidate = item.get("Resource") or item.get("resource") or item.get("Body") or item.get("body")
156-
if not candidate:
157-
return None
154+
for res in patient_resource.get("contained", []):
155+
if isinstance(res, dict) and res.get("resourceType") == "Patient":
156+
return res
158157

159-
# candidate might be a JSON string
160-
if isinstance(candidate, str):
161-
try:
162-
candidate = json.loads(candidate)
163-
except Exception:
164-
return None
165-
166-
if isinstance(candidate, dict):
167-
# if wrapped like {"resource": {...}}
168-
if "resource" in candidate and isinstance(candidate["resource"], dict):
169-
candidate = candidate["resource"]
170-
171-
# If this dict is the Patient resource itself
172-
if candidate.get("resourceType") == "Patient":
173-
return candidate
174-
175-
# If it's a bundle/entry, search for Patient
176-
if isinstance(candidate.get("entry"), list):
177-
for entry in candidate.get("entry", []):
178-
r = entry.get("resource") or entry.get("Resource")
179-
if isinstance(r, dict) and r.get("resourceType") == "Patient":
180-
return r
181-
182-
return None
158+
return None

lambdas/id_sync/tests/test_ieds_db_operations.py

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,39 @@ def test_ieds_check_exist_limit_parameter(self):
673673
# Assert - Verify the limit parameter is correctly passed
674674
self.mock_get_items_from_patient_id.assert_called_once_with(patient_id, 1)
675675

676-
# ✅ Remove tests that are no longer relevant:
677-
# - test_ieds_check_exist_missing_count_field (no longer uses Count)
678-
# - test_ieds_check_exist_count_greater_than_one (no longer uses Count)
676+
677+
678+
class TestExtractPatientResource(TestIedsDbOperations):
679+
def test_extract_patient_from_immunization_contained(self):
680+
"""Should return the contained Patient resource from an Immunization Resource"""
681+
item = {
682+
"Resource": {
683+
"resourceType": "Immunization",
684+
"contained": [
685+
{
686+
"resourceType": "Patient",
687+
"id": "Pat1",
688+
"name": [{"family": "Taylor", "given": ["Sarah"]}],
689+
"gender": "unknown",
690+
"birthDate": "1965-02-28"
691+
}
692+
]
693+
}
694+
}
695+
696+
patient = ieds_db_operations.extract_patient_resource_from_item(item)
697+
self.assertIsNotNone(patient)
698+
self.assertEqual(patient.get("resourceType"), "Patient")
699+
self.assertEqual(patient.get("id"), "Pat1")
700+
701+
def test_extract_returns_none_when_no_patient_in_contained(self):
702+
"""Should return None when contained exists but has no Patient entries"""
703+
item = {"Resource": {"resourceType": "Immunization", "contained": [{"resourceType": "Practitioner"}]}}
704+
patient = ieds_db_operations.extract_patient_resource_from_item(item)
705+
self.assertIsNone(patient)
706+
707+
def test_extract_returns_none_when_resource_not_dict(self):
708+
"""Should return None when Resource attribute is present but not a dict"""
709+
item = {"Resource": '{"not": "a dict string"}'}
710+
patient = ieds_db_operations.extract_patient_resource_from_item(item)
711+
self.assertIsNone(patient)

0 commit comments

Comments
 (0)