From 6eb2c3bef6f542616515110597ccc67c88dcdc65 Mon Sep 17 00:00:00 2001 From: davidbgomes Date: Thu, 23 May 2024 20:22:12 -0400 Subject: [PATCH] apply blacklist on initial usecase --- .../entities/Questionnaire/Questionnaire.ts | 50 ++++++++++++++++++- .../ApplyInitialRulesToSurveyUseCase.ts | 30 +++++++---- src/webapp/components/survey/SurveyForm.tsx | 1 - .../components/survey/SurveySection.tsx | 22 +------- 4 files changed, 70 insertions(+), 33 deletions(-) diff --git a/src/domain/entities/Questionnaire/Questionnaire.ts b/src/domain/entities/Questionnaire/Questionnaire.ts index 1e0fe441..3d9ee524 100644 --- a/src/domain/entities/Questionnaire/Questionnaire.ts +++ b/src/domain/entities/Questionnaire/Questionnaire.ts @@ -2,7 +2,7 @@ import { generateUid } from "../../../utils/uid"; import { SurveyRule } from "../AMRSurveyModule"; import { Id, Ref } from "../Ref"; import _ from "../generic/Collection"; -import { Code, Question } from "./QuestionnaireQuestion"; +import { Code, Question, isAntibioticQuestion } from "./QuestionnaireQuestion"; import { QuestionnaireRule, getApplicableRules } from "./QuestionnaireRules"; import { QuestionnaireSection, QuestionnaireSectionM } from "./QuestionnaireSection"; @@ -320,4 +320,52 @@ export class Questionnaire { questions: updatedEntityQuestions, }; } + + static applyAntibioticsBlacklist( + questionnaire: Questionnaire, + antibioticsBlacklist: string[] + ): Questionnaire { + const updatedStages = questionnaire.stages.map(stage => { + return { + ...stage, + sections: stage.sections.map(section => { + return { + ...section, + questions: section.questions.map(question => { + if (isAntibioticQuestion(question)) { + const options = question.options.filter( + option => + !antibioticsBlacklist.some(blacklist => + option.name + .toLowerCase() + .includes(blacklist.toLowerCase()) + ) + ); + + return { + ...question, + options: options, + }; + } else { + if ( + antibioticsBlacklist.some(blacklist => + question.text + .toLowerCase() + .includes(blacklist.toLowerCase()) + ) + ) { + return { + ...question, + isVisible: false, + }; + } else return question; + } + }), + }; + }), + }; + }); + + return Questionnaire.updateQuestionnaireStages(questionnaire, updatedStages); + } } diff --git a/src/domain/usecases/ApplyInitialRulesToSurveyUseCase.ts b/src/domain/usecases/ApplyInitialRulesToSurveyUseCase.ts index c6e97fbb..21e24dc3 100644 --- a/src/domain/usecases/ApplyInitialRulesToSurveyUseCase.ts +++ b/src/domain/usecases/ApplyInitialRulesToSurveyUseCase.ts @@ -9,8 +9,9 @@ export class ApplyInitialRulesToSurveyUseCase { currentPPSSurveyForm: Id | undefined, currentPrevalenceSurveyForm: Id | undefined ): Questionnaire { + let updatedQuestionnaire: Questionnaire; //1. Apply program rules defined in metadata - const questionnaireWithProgramRules = + updatedQuestionnaire = Questionnaire.applyProgramRulesOnQuestionnaireInitialLoad(questionnaire); //2. Apply survey rules defined in the datastore @@ -26,15 +27,24 @@ export class ApplyInitialRulesToSurveyUseCase { surveyRule => surveyRule.formId === questionnaire.id ); - //no rules to apply, return questionnaire with program rules - if (!currentFormRule) return questionnaireWithProgramRules; - else { - const questionnaireWithSurveyRules = - Questionnaire.applySurveyRulesOnQuestionnaireInitialLoad( - questionnaire, - currentFormRule - ); - return questionnaireWithSurveyRules; + const currentSurveyAntibioticBlacklist = module?.rulesBySurvey?.find( + rule => rule.surveyId === currentParentId + )?.antibioticBlacklist; + + if (currentFormRule) { + updatedQuestionnaire = Questionnaire.applySurveyRulesOnQuestionnaireInitialLoad( + updatedQuestionnaire, + currentFormRule + ); } + + if (currentSurveyAntibioticBlacklist) { + updatedQuestionnaire = Questionnaire.applyAntibioticsBlacklist( + updatedQuestionnaire, + currentSurveyAntibioticBlacklist + ); + } + + return updatedQuestionnaire; } } diff --git a/src/webapp/components/survey/SurveyForm.tsx b/src/webapp/components/survey/SurveyForm.tsx index 5c355ee0..ffc69bad 100644 --- a/src/webapp/components/survey/SurveyForm.tsx +++ b/src/webapp/components/survey/SurveyForm.tsx @@ -162,7 +162,6 @@ export const SurveyForm: React.FC = props => { } questions={section.questions} viewOnly={hasReadOnlyAccess} - antibioticsBlacklist={antibioticsBlacklist} /> ); })} diff --git a/src/webapp/components/survey/SurveySection.tsx b/src/webapp/components/survey/SurveySection.tsx index 7fbfa6ff..e0188aa2 100644 --- a/src/webapp/components/survey/SurveySection.tsx +++ b/src/webapp/components/survey/SurveySection.tsx @@ -18,7 +18,6 @@ import { Typography } from "@material-ui/core"; import styled from "styled-components"; import { QuestionWidget } from "../survey-questions/QuestionWidget"; import { Question } from "../../../domain/entities/Questionnaire/QuestionnaireQuestion"; -import { useEffect, useState } from "react"; interface SurveySectionProps { title: string; @@ -32,26 +31,7 @@ export const SurveySection: React.FC = ({ questions, updateQuestion, viewOnly, - antibioticsBlacklist, }) => { - const [updatedQuestions, setUpdatedQuestions] = useState(questions); - - useEffect(() => { - if (antibioticsBlacklist) { - const updatedQuestions = questions.map(question => { - if ( - antibioticsBlacklist.some(blacklist => - question.text.toLowerCase().includes(blacklist.toLowerCase()) - ) - ) { - question.isVisible = false; - } - return question; - }); - setUpdatedQuestions(updatedQuestions); - } - }, [antibioticsBlacklist, questions]); - return ( @@ -64,7 +44,7 @@ export const SurveySection: React.FC = ({ - {updatedQuestions.map(question => { + {questions.map(question => { if (!question.isVisible) return null; return (