From fad54e251bea773b3e76a5a00a10cea1dcd9fcd4 Mon Sep 17 00:00:00 2001 From: Ivan Cvitkovic Date: Thu, 21 Nov 2024 12:50:23 -0800 Subject: [PATCH] Fixup FHIR Bundles (#232) * Fixup FHIR compliance; add missing `resource` nesting * Fixup code style * Fixup tests * Assign resource to new variable --- patientsearch/api.py | 4 +++- patientsearch/models/sync.py | 10 ++++++---- tests/test_sync.py | 4 ++-- .../external_patient_active_search.json | 15 ++++++++------- tests/test_sync/external_patient_search.json | 16 +++++++++------- 5 files changed, 28 insertions(+), 21 deletions(-) diff --git a/patientsearch/api.py b/patientsearch/api.py index 2697935a..eea163fc 100644 --- a/patientsearch/api.py +++ b/patientsearch/api.py @@ -629,7 +629,9 @@ def external_search(resource_type): audit_entry("multiple patients returned from PDMP", extra=extra, level="warn") if external_match_count: - external_search_bundle["entry"][0].setdefault("id", local_fhir_patient["id"]) + external_search_bundle["entry"][0]["resource"].setdefault( + "id", local_fhir_patient["id"] + ) message = "PDMP found match" if external_match_count else "fEMR found match" audit_entry(message, extra=extra) diff --git a/patientsearch/models/sync.py b/patientsearch/models/sync.py index ceb53645..1a8dd486 100644 --- a/patientsearch/models/sync.py +++ b/patientsearch/models/sync.py @@ -16,7 +16,8 @@ def add_identifier_to_resource_type(bundle, resource_type, identifier): if "entry" not in result: return result - for resource in result["entry"]: + for entry in result["entry"]: + resource = entry["resource"] if resource.get("resourceType") != resource_type: continue identifiers = resource.get("identifier", []) @@ -164,11 +165,12 @@ def sync_bundle(token, bundle, consider_active=False): raise ValueError(f"Expected bundle; can't process {bundle.get('resourceType')}") for entry in bundle.get("entry"): + resource = entry["resource"] # Restrict to what is expected for now - if entry["resourceType"] != "Patient": - raise ValueError(f"Can't sync resourceType {entry['resourceType']}") + if resource["resourceType"] != "Patient": + raise ValueError(f"Can't sync resourceType {resource['resourceType']}") - patient = sync_patient(token, entry, consider_active) + patient = sync_patient(token, resource, consider_active) # TODO handle multiple external matches (if it ever happens!) # currently returning first return patient diff --git a/tests/test_sync.py b/tests/test_sync.py index 7eb484e6..d1fd540c 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -150,8 +150,8 @@ def test_adding_identifier(external_patient_search): result = add_identifier_to_resource_type(external_patient_search, "Patient", ident) assert result != external_patient_search for entry in result["entry"]: - assert len(entry["identifier"]) == 1 - assert entry["identifier"][0] == ident + assert len(entry["resource"]["identifier"]) == 1 + assert entry["resource"]["identifier"][0] == ident def test_existing( diff --git a/tests/test_sync/external_patient_active_search.json b/tests/test_sync/external_patient_active_search.json index 77584bfe..503f0888 100644 --- a/tests/test_sync/external_patient_active_search.json +++ b/tests/test_sync/external_patient_active_search.json @@ -1,6 +1,7 @@ { - "entry": [ - { + "entry": [ + { + "resource": { "active": true, "birthDate": "1977-01-12", "gender": "male", @@ -10,8 +11,8 @@ }, "resourceType": "Patient" } - ], - "resourceType": "Bundle", - "type": "searchset" - } - \ No newline at end of file + } + ], + "resourceType": "Bundle", + "type": "searchset" +} diff --git a/tests/test_sync/external_patient_search.json b/tests/test_sync/external_patient_search.json index 93b8ff80..67301369 100644 --- a/tests/test_sync/external_patient_search.json +++ b/tests/test_sync/external_patient_search.json @@ -1,13 +1,15 @@ { "entry": [ { - "birthDate": "1977-01-12", - "gender": "male", - "name": { - "family": "skywalker", - "given": ["luke"] - }, - "resourceType": "Patient" + "resource": { + "birthDate": "1977-01-12", + "gender": "male", + "name": { + "family": "skywalker", + "given": ["luke"] + }, + "resourceType": "Patient" + } } ], "resourceType": "Bundle",