Skip to content

Commit

Permalink
fix: section hiding
Browse files Browse the repository at this point in the history
  • Loading branch information
9sneha-n committed Sep 25, 2024
1 parent 92247a3 commit 93ea9a5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 49 deletions.
51 changes: 23 additions & 28 deletions src/domain/entities/Questionnaire/Questionnaire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
QuestionnaireQuestion,
isAntibioticQuestion,
} from "./QuestionnaireQuestion";
import { QuestionnaireRule } from "./QuestionnaireRules";
import { getApplicableRules, QuestionnaireRule } from "./QuestionnaireRules";
import { QuestionnaireSection, QuestionnaireSectionM } from "./QuestionnaireSection";

export interface QuestionnaireBase {
Expand Down Expand Up @@ -259,33 +259,28 @@ export class Questionnaire {
initialLoad = false
): Questionnaire {
//For the updated question, get all rules that are applicable
// const allQsInQuestionnaireStages = questionnaire.stages.flatMap(
// (stage: QuestionnaireStage) => {
// return stage.sections.flatMap(section => {
// return section.questions.map(question => question);
// });
// }
// );

// const allQsInQuestionnaire = [
// ...(questionnaire.entity?.questions || []),
// ...allQsInQuestionnaireStages,
// ];

// const applicableRules = getApplicableRules(
// updatedQuestion,
// questionnaire.rules,
// allQsInQuestionnaire
// );

const applicableRules = questionnaire.rules.filter(
rule =>
rule.dataElementIds.includes(updatedQuestion.id) ||
rule.teAttributeIds.includes(updatedQuestion.id) ||
rule.actions.some(action => action.dataElement?.id === updatedQuestion.id) ||
rule.actions.some(
action => action.trackedEntityAttribute?.id === updatedQuestion.id
)
const allQsInQuestionnaireStages = questionnaire.stages.flatMap(
(stage: QuestionnaireStage) => {
return stage.sections.flatMap(section => {
return section.questions.map(question => question);
});
}
);

const allQsInQuestionnaire = [
...(questionnaire.entity?.questions || []),
...allQsInQuestionnaireStages,
];

const updatedAllQsInQuestionnaire = allQsInQuestionnaire.map(question => {
if (question.id === updatedQuestion.id) return updatedQuestion;
else return question;
});

const applicableRules = getApplicableRules(
updatedQuestion,
questionnaire.rules,
updatedAllQsInQuestionnaire
);

if (initialLoad && applicableRules.length === 0) return questionnaire;
Expand Down
19 changes: 7 additions & 12 deletions src/domain/entities/Questionnaire/QuestionnaireQuestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class QuestionnaireQuestion {
static updateQuestions(
questions: Question[],
updatedQuestion: Question,
_rules: QuestionnaireRule[],
rules: QuestionnaireRule[],
questionnaire: Questionnaire,
parentSectionHidden?: boolean
): Question[] {
Expand All @@ -180,7 +180,7 @@ export class QuestionnaireQuestion {

//Get list question ids that require update
const allQuestionsRequiringUpdate = _(
questionnaire.rules.flatMap(rule => {
rules.flatMap(rule => {
const actionUpdates = rule.actions.flatMap(
action => action?.dataElement?.id || action.trackedEntityAttribute?.id
);
Expand All @@ -196,15 +196,10 @@ export class QuestionnaireQuestion {
const parsedAndUpdatedQuestions = updatedQuestions.map(question => {
//Get the rules that are applicable for the current question
//this is done to take care of any "side-effects" of an updated question.
// const rulesApplicableForCurrentQuestion =
// question.id !== updatedQuestion.id
// ? getApplicableRules(question, questionnaire.rules, updatedQuestions)
// : rules;
const rulesApplicableForCurrentQuestion = getApplicableRules(
question,
questionnaire.rules,
updatedQuestions
);
const rulesApplicableForCurrentQuestion =
question.id !== updatedQuestion.id
? getApplicableRules(question, questionnaire.rules, updatedQuestions)
: rules;

//If the question is part of any of the rule actions, update the section
const parsedAndUpdatedQuestion =
Expand Down Expand Up @@ -232,7 +227,7 @@ export class QuestionnaireQuestion {
return this.updateQuestions(
acc,
{ ...hiddenQuestion, value: undefined },
questionnaire.rules,
rules,
questionnaire
);
}, parsedAndUpdatedQuestions);
Expand Down
3 changes: 0 additions & 3 deletions src/domain/entities/Questionnaire/QuestionnaireRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,6 @@ const parseConditionWithExpressionParser = (rule: QuestionnaireRule, questions:
value: new xp.VariableValueJs(xp.ValueType.STRING, null, [], null),
};
const value = getQuestionValueByType(currentQuestion);
//SNEHA DEBUG : This is my guess.
// const candidates = currentQuestion.type === "select" ? currentQuestion.options : [];
// const eventDate = new Date();

const variableValue = getVariableValueByType(
currentQuestion,
Expand Down
10 changes: 4 additions & 6 deletions src/domain/entities/Questionnaire/QuestionnaireSection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,27 @@ export class QuestionnaireSectionM {
sections: QuestionnaireSection[],
updatedQuestion: Question,
questionnaire: Questionnaire,
_rules: QuestionnaireRule[]
rules: QuestionnaireRule[]
): QuestionnaireSection[] {
//Get all the sections that require update
const allSectionsRequiringUpdate = _(
questionnaire.rules.flatMap(rule =>
rule.actions.flatMap(action => action.programStageSection?.id)
)
rules.flatMap(rule => rule.actions.flatMap(action => action.programStageSection?.id))
)
.compact()
.value();

const updatedSections = sections.map(section => {
//If the section is part of any of the rule actions, update the section
const updatedSection = allSectionsRequiringUpdate.includes(section.code)
? this.updateSection(section, questionnaire.rules)
? this.updateSection(section, rules)
: section;

return {
...updatedSection,
questions: QuestionnaireQuestion.updateQuestions(
updatedSection.questions,
updatedQuestion,
questionnaire.rules,
rules,
questionnaire,
updatedSection.isVisible === false && section.isVisible === true
),
Expand Down

0 comments on commit 93ea9a5

Please sign in to comment.