Skip to content

Commit

Permalink
apply blacklist on initial usecase
Browse files Browse the repository at this point in the history
davidbgomes committed May 24, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent d289a31 commit 6eb2c3b
Showing 4 changed files with 70 additions and 33 deletions.
50 changes: 49 additions & 1 deletion src/domain/entities/Questionnaire/Questionnaire.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
30 changes: 20 additions & 10 deletions src/domain/usecases/ApplyInitialRulesToSurveyUseCase.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
1 change: 0 additions & 1 deletion src/webapp/components/survey/SurveyForm.tsx
Original file line number Diff line number Diff line change
@@ -162,7 +162,6 @@ export const SurveyForm: React.FC<SurveyFormProps> = props => {
}
questions={section.questions}
viewOnly={hasReadOnlyAccess}
antibioticsBlacklist={antibioticsBlacklist}
/>
);
})}
22 changes: 1 addition & 21 deletions src/webapp/components/survey/SurveySection.tsx
Original file line number Diff line number Diff line change
@@ -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<SurveySectionProps> = ({
questions,
updateQuestion,
viewOnly,
antibioticsBlacklist,
}) => {
const [updatedQuestions, setUpdatedQuestions] = useState<Question[]>(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 (
<StyledSection key={title}>
<DataTable>
@@ -64,7 +44,7 @@ export const SurveySection: React.FC<SurveySectionProps> = ({
</TableHead>

<TableBody>
{updatedQuestions.map(question => {
{questions.map(question => {
if (!question.isVisible) return null;
return (
<DataTableRow key={question.id}>

0 comments on commit 6eb2c3b

Please sign in to comment.