LHNCBC / lforms

See the project website at http://lhncbc.github.io/lforms/, or view the demo website at https://lhcforms.nlm.nih.gov/lhcforms.
Other
99 stars 55 forks source link

Merging Questionnaire with QuestionnaireResponse do not hide items #114

Closed happyagosmith closed 1 year ago

happyagosmith commented 1 year ago

I have Questionnaire with hidden items. I also have QuestionnaireResponse which contains value for the hidden items. After merging Questionnaire with QuestionnaireReponse, the merged form displays the hidden items. Is this by design?

This the Questionnaire:

{
  "resourceType": "Questionnaire",
  "status": "active",
  "name": "calculatedExpression",
  "id":"calculatedExpression",
  "description": "Test re-rendering calculatedExpression",
  "subjectType": [
    "Patient"
  ],
  "code": [
    {
      "display": "Clinico"
    }
  ],
  "item": [
    {
      "linkId": "CE-HIDDEN",
      "type": "decimal",
      "text": "CE-HIDDEN",
      "extension": [
        {
          "url": "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-calculatedExpression",
          "valueExpression": {
            "language": "text/fhirpath",
                 "expression": "%resource.item.where(linkId='ITEM-VISIBLE').answer.value"
          }
        },
        {
          "url": "http://hl7.org/fhir/StructureDefinition/questionnaire-hidden",
          "valueBoolean": true
        }
      ]
    },
    {
      "linkId": "CE-VISIBLE",
      "type": "decimal",
      "text": "CE-VISIBLE",
      "extension": [
        {
          "url": "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-calculatedExpression",
          "valueExpression": {
            "language": "text/fhirpath",
                 "expression": "%resource.item.where(linkId='ITEM-VISIBLE').answer.value"
          }
        }
      ]
    },
    {
      "linkId": "ITEM-VISIBLE",
      "text": "Type one number:",
      "type": "decimal",
      "required": true
    },
    {
      "linkId": "GROUP-VISIBLE",
      "type": "group",
      "text": "group that contains a hidden item",
      "item": [
        {
          "linkId": "GROUP-VISIBLE/CE-HIDDEN",
          "type": "decimal",
          "text": "GROUP-VISIBLE/CE-HIDDEN/1/1",
          "extension": [
            {
              "url": "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-calculatedExpression",
              "valueExpression": {
              "language": "text/fhirpath",
                "expression": "%resource.item.where(linkId='ITEM-VISIBLE').answer.value"
              }
            },
            {
              "url": "http://hl7.org/fhir/StructureDefinition/questionnaire-hidden",
              "valueBoolean": true
            }
          ]
        },
        {
          "linkId": "GROUP-VISIBLE/CE-VISIBLE",
          "type": "decimal",
          "text": "GROUP-VISIBLE/CE-VISIBLE/1/1",
          "extension": [
            {
              "url": "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-calculatedExpression",
              "valueExpression": {
              "language": "text/fhirpath",
                "expression": "%resource.item.where(linkId='ITEM-VISIBLE').answer.value"
              }
            }
          ]
        }
      ]
    },
    {
      "linkId": "GROUP-HIDDEN",
      "type": "group",
      "text": "hidden group",
      "extension": [
        {
          "url": "http://hl7.org/fhir/StructureDefinition/questionnaire-hidden",
          "valueBoolean": true
        }
      ],
      "item": [
        {
          "linkId": "GROUP-HIDDEN/CE-HIDDEN",
          "type": "decimal",
          "text": "GROUP-HIDDEN/CE-HIDDEN/1/1",
          "extension": [
            {
              "url": "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-calculatedExpression",
              "valueExpression": {
              "language": "text/fhirpath",
                "expression": "%resource.item.where(linkId='ITEM-VISIBLE').answer.value"
              }
            }
          ]
        }
      ]
    }
  ]
}

and this is the QuestionnaireResponse

{
  "resourceType": "QuestionnaireResponse",
  "id": "calculatedExpressionResponse",
  "questionnaire": "Questionnaire/calculatedExpression",
  "meta": {
    "profile": [
      "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaireresponse|2.7"
    ],
    "tag": [
      {
        "code": "lformsVersion: 33.0.0"
      }
    ]
  },
  "status": "completed",
  "authored": "2022-12-03T16:01:06.719Z",
  "item": [
    {
      "linkId": "CE-HIDDEN",
      "text": "CE-HIDDEN",
      "answer": [
        {
          "valueDecimal": 2
        }
      ]
    },
    {
      "linkId": "CE-VISIBLE",
      "text": "CE-VISIBLE",
      "answer": [
        {
          "valueDecimal": 2
        }
      ]
    },
    {
      "linkId": "ITEM-VISIBLE",
      "text": "Type one number:",
      "answer": [
        {
          "valueDecimal": 2
        }
      ]
    },
    {
      "linkId": "GROUP-VISIBLE",
      "text": "group that contains a hidden item",
      "item": [
        {
          "linkId": "GROUP-VISIBLE/CE-VISIBLE",
          "text": "GROUP-VISIBLE/CE-VISIBLE/1/1",
          "answer": [
            {
              "valueDecimal": 2
            }
          ]
        },
        {
          "linkId": "GROUP-VISIBLE/CE-HIDDEN",
          "text": "GROUP-VISIBLE/CE-HIDDEN/1/1",
          "answer": [
            {
              "valueDecimal": 2
            }
          ]
        }
      ]
    },
    {
      "linkId": "GROUP-HIDDEN",
      "text": "hidden group",
      "item": [
        {
          "linkId": "GROUP-HIDDEN/CE-HIDDEN",
          "text": "GROUP-HIDDEN/CE-HIDDEN/1/1",
          "answer": [
            {
              "valueDecimal": 2
            }
          ]
        }
      ]
    }
  ]
}

With the execution of the following code

const formDef = window.LForms.Util.convertFHIRQuestionnaireToLForms(questionnaire, FHIR_VERSION);
let mergedFormData = window.LForms.Util.mergeFHIRDataIntoLForms(qr, formDef, FHIR_VERSION);
// request to have the calculated values in place of the saved user data in QuestionnaireResponse
mergedFormData.hasSavedData = false;
window.LForms.Util.addFormToPage(mergedFormData, 'myFormContainer');

I would expect the following cypress statements to pass

cy.get('[id="CE-HIDDEN/1"]').should('not.exist');
cy.get('[id="GROUP-VISIBLE/CE-HIDDEN/1/1"]').should('not.exist');
cy.get('[id="GROUP-HIDDEN/CE-HIDDEN/1/1"]').should('not.exist');

but it is not the case because all the three items are rendered

my workaround is the following

const formDef = window.LForms.Util.convertFHIRQuestionnaireToLForms(questionnaire, FHIR_VERSION);
let mergedFormData = window.LForms.Util.mergeFHIRDataIntoLForms(qr, formDef, FHIR_VERSION);
// request to have the calculated values in place of the saved user data in QuestionnaireResponse
mergedFormData.hasSavedData = false;
// workaround to hide hidden items when rendering mergedFormData
(function setHiddenInDef(mergedItems: any, formItems: any) {
  mergedItems.forEach((mergedItem: any, index: any) => {
    mergedItem["_isHiddenInDef"] = formItems[index]["_isHiddenInDef"]
    mergedItem["items"] && setHiddenInDef(mergedItem["items"], formItems[index]["items"])
  })
})(mergedFormData["items"], formDef["items"])

window.LForms.Util.addFormToPage(mergedFormData, 'myFormContainer');

Is it as expected?

plynchnlm commented 1 year ago

Thanks for the report. We're investigating. The short answer is, no, items hidden with questionnaire-hidden should stay hidden.

lhcye commented 1 year ago

A fix will be released soon. Do you mind if I include your sample data (a modified version) in the tests?

happyagosmith commented 1 year ago

Yes, sure. Don’t hesitate.

lhcye commented 1 year ago

Thanks. The fix is in version 33.1.1. Let me know if you see any problems.