Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixup FHIR Bundles #232

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 8 additions & 6 deletions patientsearch/models/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def add_identifier_to_resource_type(bundle, resource_type, identifier):
return result

for resource in result["entry"]:
ivan-c marked this conversation as resolved.
Show resolved Hide resolved
if resource.get("resourceType") != resource_type:
if resource["resource"].get("resourceType") != resource_type:
continue
identifiers = resource.get("identifier", [])
identifiers = resource["resource"].get("identifier", [])
found = False
for i in identifiers:
if (
Expand All @@ -30,7 +30,7 @@ def add_identifier_to_resource_type(bundle, resource_type, identifier):
break
if not found:
identifiers.append(identifier)
resource["identifier"] = identifiers
resource["resource"]["identifier"] = identifiers
return result


Expand Down Expand Up @@ -165,10 +165,12 @@ def sync_bundle(token, bundle, consider_active=False):

for entry in bundle.get("entry"):
# Restrict to what is expected for now
if entry["resourceType"] != "Patient":
raise ValueError(f"Can't sync resourceType {entry['resourceType']}")
if entry["resource"]["resourceType"] != "Patient":
raise ValueError(
f"Can't sync resourceType {entry['resource']['resourceType']}"
)

patient = sync_patient(token, entry, consider_active)
patient = sync_patient(token, entry["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
Loading