Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) O3-4137: Add support for nested obsGroup #427

Merged
merged 2 commits into from
Nov 26, 2024
Merged

Conversation

usamaidrsk
Copy link
Member

Requirements

  • This PR has a title that briefly describes the work done including the ticket number. If there is a ticket, make sure your PR title includes a conventional commit label. See existing PR titles for inspiration.
  • My work conforms to the OpenMRS 3.0 Styleguide and design documentation.
  • My work includes tests or is validated by existing tests.

Summary

This PR introduces functionality for handling nested obsGroup questions. With this enhancement, users can now nest obsGroup elements within other obsGroup elements, allowing for complex hierarchical structures within forms. This feature supports scenarios requiring parent-child relationships among grouped observations, providing a more flexible and detailed data-capture process.

Example schema

{
  "name": "Test Nested obsGroup",
  "pages": [
    {
      "label": "Test Nested obsGroup",
      "sections": [
        {
          "label": "Test Nested obsGroup",
          "isExpanded": "true",
          "questions": [
            {
              "label": "Parent obsGroup",
              "type": "obsGroup",
              "required": false,
              "id": "parentObsgroup",
              "questionOptions": {
                "rendering": "group",
                "concept": "163770AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
              },
              "questions": [
                {
                  "label": "Health Center",
                  "type": "obs",
                  "required": false,
                  "id": "healthCenter",
                  "questionOptions": {
                    "rendering": "select",
                    "concept": "1745AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
                    "conceptMappings": [
                      {
                        "relationship": "SAME-AS",
                        "type": "SNOMED MVP",
                        "value": "17451000105003"
                      },
                      {
                        "relationship": "NARROWER-THAN",
                        "type": "SNOMED CT",
                        "value": "43741000"
                      },
                      {
                        "relationship": "SAME-AS",
                        "type": "CIEL",
                        "value": "1745"
                      }
                    ],
                    "answers": [
                      {
                        "concept": "1560AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
                        "label": "Family member"
                      },
                      {
                        "concept": "1588AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
                        "label": "Health clinic/post"
                      },
                      {
                        "concept": "5622AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
                        "label": "Other"
                      }
                    ]
                  },
                  "validators": []
                },
                {
                  "label": "Nested obsGroup",
                  "type": "obsGroup",
                  "required": false,
                  "id": "nestedObsgroup",
                  "questionOptions": {
                    "rendering": "group",
                    "concept": "3f824eeb-8452-4df0-b346-6ed056cbc5b9"
                  },
                  "questions": [
                    {
                      "label": "Comment",
                      "type": "obs",
                      "required": false,
                      "id": "comment",
                      "questionOptions": {
                        "rendering": "textarea",
                        "concept": "161011AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
                        "conceptMappings": [
                          {
                            "relationship": "BROADER-THAN",
                            "type": "LOINC",
                            "value": "48767-8"
                          },
                          {
                            "relationship": "SAME-AS",
                            "type": "CIEL",
                            "value": "161011"
                          }
                        ]
                      },
                      "validators": []
                    },
                    {
                      "label": "Other Diagnoses",
                      "type": "obs",
                      "required": false,
                      "id": "otherDiagnoses",
                      "questionOptions": {
                        "rendering": "select",
                        "concept": "159947AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
                        "conceptMappings": [
                          {
                            "relationship": "SAME-AS",
                            "type": "org.openmrs.module.emrapi",
                            "value": "Diagnosis Concept Set"
                          },
                          {
                            "relationship": "SAME-AS",
                            "type": "CIEL",
                            "value": "159947"
                          }
                        ],
                        "answers": [
                          {
                            "concept": "159394AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
                            "label": "Diagnosis certainty"
                          }
                        ]
                      },
                      "validators": []
                    }
                  ],
                  "validators": []
                }
              ],
              "validators": []
            }
          ]
        }
      ]
    }
  ],
  "processor": "EncounterFormProcessor",
  "encounterType": "",
  "referencedForms": [],
  "uuid": "",
  "description": "Test Nested obsGroup"
}

Screenshots

image

Related Issue

https://openmrs.atlassian.net/browse/O3-4137

Other

Copy link

github-actions bot commented Nov 21, 2024

Size Change: +705 B (+0.06%)

Total Size: 1.25 MB

ℹ️ View Unchanged
Filename Size Change
dist/151.js 379 kB 0 B
dist/225.js 2.57 kB 0 B
dist/254.js 88.7 kB 0 B
dist/277.js 1.85 kB 0 B
dist/300.js 642 B 0 B
dist/335.js 968 B 0 B
dist/353.js 3.02 kB 0 B
dist/41.js 3.37 kB 0 B
dist/539.js 263 kB +450 B (+0.17%)
dist/540.js 2.63 kB 0 B
dist/55.js 758 B 0 B
dist/585.js 112 kB 0 B
dist/635.js 14.4 kB 0 B
dist/690.js 11.5 kB 0 B
dist/70.js 483 B 0 B
dist/979.js 6.87 kB 0 B
dist/99.js 691 B 0 B
dist/993.js 3.09 kB 0 B
dist/main.js 355 kB +255 B (+0.07%)
dist/openmrs-esm-form-engine-lib.js 3.8 kB 0 B

compressed-size-action

@usamaidrsk
Copy link
Member Author

This is a fix for issues that where reported from this PR, after this revert

Copy link
Member

@samuelmale samuelmale left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just a few minor comments.

field.questions
?.filter((child) => !child.isHidden)
.map((child, index) => {
const keyId = `${child.id}_${index}`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const keyId = `${child.id}_${index}`;
const key = `${child.id}_${index}`;

],
});

const createNestedObs = () => ({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const createNestedObs = () => ({
const createEncounterWithNestedObs = () => ({

const conceptReferencesTemp = new Set<string>();

const flattenFields = (fields: FormField[]) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is recursive and causes side effects to external states, making it less pure. Below is an example of how we can simplify this logic (AI generated)

const processFlattenedFields = (fields: FormField[]): {
      flattenedFields: FormField[];
      conceptReferences: Set<string>;
    } => {
      const flattenedFields: FormField[] = [];
      const conceptReferences = new Set<string>();

      const processField = (field: FormField, parentGroupId?: string) => {
        // Add group ID to nested fields if applicable
        const processedField = parentGroupId 
          ? { ...field, meta: { ...field.meta, groupId: parentGroupId } }
          : field;

        // Add field to flattened list
        flattenedFields.push(processedField);

        // Collect concept references
        if (processedField.questionOptions?.concept) {
          conceptReferences.add(processedField.questionOptions.concept);
        }

        // Collect concept references from answers
        processedField.questionOptions?.answers?.forEach((answer) => {
          if (answer.concept) {
            conceptReferences.add(answer.concept);
          }
        });

        // Recursively process nested questions for obsGroup
        if (processedField.type === 'obsGroup' && processedField.questions) {
          processedField.questions.forEach((nestedField) => {
            processField(nestedField, processedField.id);
          });
        }
      };

      // Process all input fields
      fields.forEach(processField);

      return { flattenedFields, conceptReferences };
    };
    

@usamaidrsk usamaidrsk merged commit c175478 into main Nov 26, 2024
4 checks passed
@usamaidrsk usamaidrsk deleted the ft-nested-obs-group branch November 26, 2024 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants