Skip to content

Commit

Permalink
WIP multitenant polulate
Browse files Browse the repository at this point in the history
  • Loading branch information
ir4y committed Sep 7, 2023
1 parent c910f97 commit 18a1dbf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
24 changes: 18 additions & 6 deletions app/aidbox/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from ..sdc.utils import parameter_to_env
from ..utils import get_extract_services
from .sdk import sdk
from .utils import get_aidbox_fhir_client, get_user_sdk_client
from .utils import get_aidbox_fhir_client, get_organization_client, get_user_sdk_client


@sdk.operation(["GET"], ["Questionnaire", {"name": "id"}, "$assemble"])
Expand Down Expand Up @@ -166,20 +166,32 @@ async def populate_questionnaire(operation, request):
return web.json_response(populated_resource)


@sdk.operation(
["POST"],
["Organization", {"name": "org_id"}, "fhir", "Questionnaire", {"name": "id"}, "$populate"],
)
@sdk.operation(["POST"], ["Questionnaire", {"name": "id"}, "$populate"])
@sdk.operation(["POST"], ["fhir", "Questionnaire", {"name": "id"}, "$populate"])
async def populate_questionnaire_instance(operation, request):
is_fhir = operation["request"][1] == "fhir"
client = request["app"]["client"]
aidbox_client = request["app"]["client"]
if operation["request"][1] == "Organization":
is_fhir = True
fhir_client = get_organization_client(aidbox_client, request["route-params"]["org_id"])
else:
is_fhir = operation["request"][1] == "fhir"
fhir_client = get_aidbox_fhir_client(aidbox_client)
questionnaire = (
await client.resources("Questionnaire").search(_id=request["route-params"]["id"]).get()
await aidbox_client.resources("Questionnaire")
.search(_id=request["route-params"]["id"])
.get()
)
env = parameter_to_env(request["resource"])
env["Questionnaire"] = questionnaire
client = client if questionnaire.get("runOnBehalfOfRoot") else get_user_sdk_client(request)
# TODO handle runOnBehalfOfRoot
# client = fhir_client if questionnaire.get("runOnBehalfOfRoot") else get_user_sdk_client(request)

populated_resource = await populate(
get_aidbox_fhir_client(client) if is_fhir else client, questionnaire, env
fhir_client if is_fhir else aidbox_client, questionnaire, env
)
if is_fhir:
populated_resource = from_first_class_extension(populated_resource)
Expand Down
6 changes: 5 additions & 1 deletion app/aidbox/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ def get_aidbox_fhir_client(aidbox_client):


def get_organization_client(aidbox_client, organization):
if isinstance(organization, str):
org_id = organization
else:
org_id = organization.id
return AsyncFHIRClient(
f"{aidbox_client.url}/Organization/{organization.id}/fhir/",
f"{aidbox_client.url}/Organization/{org_id}/fhir/",
authorization=aidbox_client.authorization,
extra_headers=aidbox_client.extra_headers,
)
2 changes: 1 addition & 1 deletion tests/aidbox/test_multitenant.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async def test_populate(aidbox_client, safe_db):

launch_patient = {"resourceType": "Patient", "id": patient1.id}

p = await q.execute("$populate", data=create_parameters(LaunchPatient=launch_patient))
p = await q.execute("$populate", data=create_parameters(patient=launch_patient))

assert (
fhirpath(
Expand Down
1 change: 1 addition & 0 deletions tests_zen_project/zrc/questionnaire-profile.edn
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
:zen.fhir/type "Questionnaire"
:type zen/map
:zen.fhir/version "0.6.23-1"
:confirms #{hl7-fhir-r4-core.Questionnaire/schema}
:keys {:mapping {:type zen/vector
:every {:confirms #{hl7-fhir-r4-core.Reference/schema}
:fhir/extensionUri "http://beda.software/fhir-extensions/questionnaire-mapper"}}
Expand Down

0 comments on commit 18a1dbf

Please sign in to comment.