Skip to content

Commit

Permalink
Fixup FHIR Bundles (#232)
Browse files Browse the repository at this point in the history
* Fixup FHIR compliance; add missing `resource` nesting

* Fixup code style

* Fixup tests

* Assign resource to new variable
  • Loading branch information
ivan-c authored Nov 21, 2024
1 parent 513a1d8 commit fad54e2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
4 changes: 3 additions & 1 deletion patientsearch/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 6 additions & 4 deletions patientsearch/models/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", [])
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
15 changes: 8 additions & 7 deletions tests/test_sync/external_patient_active_search.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"entry": [
{
"entry": [
{
"resource": {
"active": true,
"birthDate": "1977-01-12",
"gender": "male",
Expand All @@ -10,8 +11,8 @@
},
"resourceType": "Patient"
}
],
"resourceType": "Bundle",
"type": "searchset"
}
}
],
"resourceType": "Bundle",
"type": "searchset"
}
16 changes: 9 additions & 7 deletions tests/test_sync/external_patient_search.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit fad54e2

Please sign in to comment.