From e66bac78eaf5908084a63e78a488b708aa61fc77 Mon Sep 17 00:00:00 2001 From: qscgyjqscgyj Date: Mon, 2 Sep 2024 15:54:19 +0200 Subject: [PATCH 1/2] Use aidbox $to-fromat for $assemble operations Ref #61 --- app/aidbox/operations.py | 28 ++++++++++++++++++++-------- app/converter/aidbox.py | 12 ++++++++++++ tests/sdc/test_assemble.py | 27 +++++++++++++-------------- 3 files changed, 45 insertions(+), 22 deletions(-) create mode 100644 app/converter/aidbox.py 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..f6f3320 100644 --- a/tests/sdc/test_assemble.py +++ b/tests/sdc/test_assemble.py @@ -551,7 +551,16 @@ async def test_assemble_sub_questionnaire_fhir(aidbox_client, safe_db): ], "status": "active", "resourceType": "Questionnaire", + "assembledFrom": q['id'], "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 +569,13 @@ 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", - }, ], } From 84e8ee300d9a74ad18a68456c23bd32413bb6b9a Mon Sep 17 00:00:00 2001 From: Ilya Beda Date: Tue, 3 Sep 2024 11:12:30 +1000 Subject: [PATCH 2/2] Fix test --- tests/sdc/test_assemble.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/sdc/test_assemble.py b/tests/sdc/test_assemble.py index f6f3320..701f1ca 100644 --- a/tests/sdc/test_assemble.py +++ b/tests/sdc/test_assemble.py @@ -551,7 +551,6 @@ async def test_assemble_sub_questionnaire_fhir(aidbox_client, safe_db): ], "status": "active", "resourceType": "Questionnaire", - "assembledFrom": q['id'], "extension": [ { "url": "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-targetStructureMap", @@ -577,5 +576,9 @@ async def test_assemble_sub_questionnaire_fhir(aidbox_client, safe_db): }, ], }, + { + "url": "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-assembledFrom", + "valueCanonical": q['id'] + }, ], }