From df99bb9f1cad9f7f2628f2302544f360bea08397 Mon Sep 17 00:00:00 2001 From: Marco Ferreira Date: Sun, 16 May 2021 17:35:04 +0200 Subject: [PATCH 1/3] Fix fhir response parser for schemas with groups --- src/resp-mapper.ts | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/resp-mapper.ts b/src/resp-mapper.ts index e55305b..39d02d8 100644 --- a/src/resp-mapper.ts +++ b/src/resp-mapper.ts @@ -35,35 +35,37 @@ export const FhirJsonResp = ( }; // https://stackoverflow.com/questions/15523514/find-by-key-deep-in-a-nested-array -const getObject = function(theObject: Object | Object[], theProperty: string) { +const getObject = function(theObject: Object | Object[], theProperty: string, returnObjects?: boolean) { var result = null; if (theObject instanceof Array) { for (var i = 0; i < theObject.length; i++) { - result = getObject(theObject[i], theProperty); + // end of the road of value is a string + if (typeof theObject[i] === "string") { + break; + } + + result = getObject(theObject[i], theProperty, returnObjects); if (result) { break; } } } else { for (var prop in theObject) { - //console.log(prop + ': ' + theObject[prop]); - if (prop === theProperty) { - if (theObject[prop] instanceof Object) { - } else { - return theObject[prop]; - } + if (prop === theProperty && (!(theObject[prop] instanceof Object) || returnObjects) ) { + return theObject[prop]; } if ( theObject[prop] instanceof Object || theObject[prop] instanceof Array ) { - result = getObject(theObject[prop], theProperty); + result = getObject(theObject[prop], theProperty, returnObjects); if (result) { break; } } } } + return result; }; @@ -72,13 +74,14 @@ const formValueToFhirAnswer = ( fhirElement: R4.IQuestionnaireResponse_Answer, jsonSchema: FhirForm['schema'], linkId: string -) => +) => supportedValueTypes.reduce( (answer: Array<{ [x: string]: any }>, propertyName) => { if (fhirElement && fhirElement.hasOwnProperty(propertyName)) { - const enumNames = jsonSchema.properties[linkId]?.enumNames; + const linkProperties = getObject(jsonSchema.properties, linkId, true) + const enumNames = linkProperties?.enumNames; if (enumNames) { - const valueIndex = jsonSchema.properties[linkId].enum.indexOf( + const valueIndex = linkProperties.enum.indexOf( formDataValue ); answer.push({ @@ -94,6 +97,6 @@ const formValueToFhirAnswer = ( } } return answer; - }, + }, [] - ); +); From a1f3967698d1e5efa57b3eb597efd8242b8bd8f6 Mon Sep 17 00:00:00 2001 From: Marco Ferreira Date: Sun, 16 May 2021 18:09:02 +0200 Subject: [PATCH 2/3] Update response test response --- src/resp-mapper.ts | 2 +- test/resp.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/resp-mapper.ts b/src/resp-mapper.ts index 39d02d8..b712ce4 100644 --- a/src/resp-mapper.ts +++ b/src/resp-mapper.ts @@ -87,7 +87,7 @@ const formValueToFhirAnswer = ( answer.push({ [propertyName]: { code: formDataValue, - display: enumNames[valueIndex], + display: enumNames[valueIndex] || null, }, }); } else { diff --git a/test/resp.test.ts b/test/resp.test.ts index f75b9c9..e8ca341 100644 --- a/test/resp.test.ts +++ b/test/resp.test.ts @@ -11,7 +11,7 @@ const formData = '{"1": true,"2": {"2.1": "Male", "2.2": "12/12/2020", "2.3": "Canada", "2.4": "Married"},"3": {"3.1": true,"3.2": true}}'; const parsedResponse: R4.IQuestionnaireResponse = JSON.parse( - ' {"resourceType":"QuestionnaireResponse","item":[{"linkId":"1","text":"Do you have allergies?","answer":[{"valueBoolean":true}]},{"linkId":"sex","text":"Sex","answer":[{"valueCoding":null}]},{"linkId":"2.2","text":"What is your date of birth?","answer":[{"valueDate":"12/12/2020"}]},{"linkId":"2.3","text":"What is your country of birth?","answer":[{"valueString":"Canada"}]},{"linkId":"2.4","text":"What is your marital status?","answer":[{"valueString":"Married"}]},{"linkId":"3.1","text":"Do you smoke?","answer":[{"valueBoolean":true}]},{"linkId":"3.2","text":"Do you drink alchohol?","answer":[{"valueBoolean":true}]}],"status":"in-progress"}' + ' {"resourceType":"QuestionnaireResponse","item":[{"linkId":"1","text":"Do you have allergies?","answer":[{"valueBoolean":true}]},{"linkId":"sex","text":"Sex","answer":[{"valueCoding":{"code":null,"display":null}}]},{"linkId":"2.2","text":"What is your date of birth?","answer":[{"valueDate":"12/12/2020"}]},{"linkId":"2.3","text":"What is your country of birth?","answer":[{"valueString":"Canada"}]},{"linkId":"2.4","text":"What is your marital status?","answer":[{"valueString":"Married"}]},{"linkId":"3.1","text":"Do you smoke?","answer":[{"valueBoolean":true}]},{"linkId":"3.2","text":"Do you drink alchohol?","answer":[{"valueBoolean":true}]}],"status":"in-progress"}' ); describe('map', () => { From 2a09a73988c58b450546c633fccd191d72fe6825 Mon Sep 17 00:00:00 2001 From: beapen Date: Sun, 16 May 2021 20:10:34 -0400 Subject: [PATCH 3/3] update version to 0.8.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e66665a..86a61d3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "fhirformjs", "description": "FHIR Questionnaire To Form Converter for rendering", - "version": "0.8.0", + "version": "0.8.1", "license": "MIT", "main": "dist/index.js", "typings": "dist/index.d.ts",