diff --git a/app/aidbox/operations.py b/app/aidbox/operations.py index 04ac43f..cbc3420 100644 --- a/app/aidbox/operations.py +++ b/app/aidbox/operations.py @@ -2,7 +2,7 @@ from aiohttp import web -from app.converter import from_first_class_extension, to_first_class_extension +from app.converter.aidbox import from_first_class_extension, to_first_class_extension from ..sdc import ( assemble, @@ -30,7 +30,9 @@ async def assemble_op(request: AidboxSdcRequest): assembled_questionnaire_lazy = await assemble(request.fhir_client, questionnaire) assembled_questionnaire = json.loads(json.dumps(assembled_questionnaire_lazy, default=list)) if request.is_fhir: - assembled_questionnaire = from_first_class_extension(assembled_questionnaire) + assembled_questionnaire = await from_first_class_extension( + assembled_questionnaire, request.aidbox_client + ) return web.json_response(assembled_questionnaire) @@ -40,7 +42,9 @@ async def constraint_check_operation(request: AidboxSdcRequest): env = parameter_to_env(request.resource) questionnaire = ( - to_first_class_extension(env["Questionnaire"]) if request.is_fhir else env["Questionnaire"] + await to_first_class_extension(env["Questionnaire"], request.aidbox_client) + if request.is_fhir + else env["Questionnaire"] ) as_root = questionnaire.get("runOnBehalfOfRoot") client = client if as_root else get_user_sdk_client(request.request, request.client) @@ -54,7 +58,9 @@ async def get_questionnaire_context_operation(request: AidboxSdcRequest): env = parameter_to_env(request.resource) questionnaire = ( - to_first_class_extension(env["Questionnaire"]) if request.is_fhir else env["Questionnaire"] + await to_first_class_extension(env["Questionnaire"], request.aidbox_client) + if request.is_fhir + else env["Questionnaire"] ) as_root = questionnaire.get("runOnBehalfOfRoot") client = client if as_root else get_user_sdk_client(request.request, request.client) @@ -81,7 +87,7 @@ async def extract_questionnaire_operation(request: AidboxSdcRequest): elif resource["resourceType"] == "Parameters": env = parameter_to_env(request.resource) questionnaire = ( - to_first_class_extension(env["Questionnaire"]) + await to_first_class_extension(env["Questionnaire"], request.aidbox_client) if request.is_fhir else env["Questionnaire"] ) @@ -147,14 +153,18 @@ async def populate_questionnaire(request: AidboxSdcRequest): ) questionnaire = ( - to_first_class_extension(env["Questionnaire"]) if request.is_fhir else env["Questionnaire"] + await to_first_class_extension(env["Questionnaire"], request.aidbox_client) + if request.is_fhir + else env["Questionnaire"] ) as_root = questionnaire.get("runOnBehalfOfRoot") client = request.client if as_root else get_user_sdk_client(request.request, request.client) populated_resource = await populate(client, questionnaire, env) if request.is_fhir: - populated_resource = from_first_class_extension(populated_resource) + populated_resource = await from_first_class_extension( + populated_resource, request.aidbox_client + ) return web.json_response(populated_resource) @@ -172,7 +182,9 @@ async def populate_questionnaire_instance(request: AidboxSdcRequest): populated_resource = await populate(client, questionnaire, env) if request.is_fhir: - populated_resource = from_first_class_extension(populated_resource) + populated_resource = await from_first_class_extension( + populated_resource, request.aidbox_client + ) return web.json_response(populated_resource) diff --git a/app/converter/aidbox.py b/app/converter/aidbox.py new file mode 100644 index 0000000..f2c5258 --- /dev/null +++ b/app/converter/aidbox.py @@ -0,0 +1,12 @@ +async def from_first_class_extension(fce_resource, aidbox_client): + fhir_resource = await aidbox_client.execute( + "/$to-format/fhir", method="post", data=fce_resource, params=None + ) + return fhir_resource.get("resource") + + +async def to_first_class_extension(fhir_resource, aidbox_client): + fce_resource = await aidbox_client.execute( + "/$to-format/aidbox", method="post", data=fhir_resource, params=None + ) + return fce_resource.get("resource") diff --git a/tests/sdc/test_assemble.py b/tests/sdc/test_assemble.py index 3ee6127..701f1ca 100644 --- a/tests/sdc/test_assemble.py +++ b/tests/sdc/test_assemble.py @@ -552,6 +552,14 @@ async def test_assemble_sub_questionnaire_fhir(aidbox_client, safe_db): "status": "active", "resourceType": "Questionnaire", "extension": [ + { + "url": "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-targetStructureMap", + "valueCanonical": "StructureMap/create-another-patient", + }, + { + "url": "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-targetStructureMap", + "valueCanonical": "StructureMap/create-patient", + }, { "url": "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-launchContext", "extension": [ @@ -560,23 +568,17 @@ async def test_assemble_sub_questionnaire_fhir(aidbox_client, safe_db): "valueCoding": { "system": "http://hl7.org/fhir/uv/sdc/CodeSystem/launchContext", "code": "LaunchPatient", - "system": "http://hl7.org/fhir/uv/sdc/CodeSystem/launchContext", }, }, - {"url": "type", "valueCode": "Patient"}, + { + "url": "type", + "valueCode": "Patient", + }, ], }, { - "url": "https://jira.hl7.org/browse/FHIR-22356#assembledFrom", - "valueCanonical": q.id, - }, - { - "url": "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-targetStructureMap", - "valueCanonical": "StructureMap/create-another-patient", - }, - { - "url": "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-targetStructureMap", - "valueCanonical": "StructureMap/create-patient", + "url": "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-assembledFrom", + "valueCanonical": q['id'] }, ], }