Skip to content

Commit

Permalink
Merge pull request #52 from EyeSeeTea/fix/remove-repeatable-stage
Browse files Browse the repository at this point in the history
remove repeatable ward stage fix
  • Loading branch information
MiquelAdell authored May 22, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents efcd31f + 1e7ee77 commit 1e0bbb9
Showing 7 changed files with 50 additions and 8 deletions.
4 changes: 4 additions & 0 deletions src/CompositionRoot.ts
Original file line number Diff line number Diff line change
@@ -37,6 +37,7 @@ import { ASTGuidelinesRepository } from "./domain/repositories/ASTGuidelinesRepo
import { GetASTGuidelinesUseCase } from "./domain/usecases/GetASTGuidelinesUseCase";
import { ASTGuidelinesD2Repository } from "./data/repositories/ASTGuidelinesD2Repository";
import { ASTGuidelinesTestRepository } from "./data/repositories/testRepositories/ASTGuidelinesTestRepository";
import { RemoveRepeatableProgramStageUseCase } from "./domain/usecases/RemoveRepeatableProgramStageUseCase";

export type CompositionRoot = ReturnType<typeof getCompositionRoot>;

@@ -87,6 +88,9 @@ function getCompositionRoot(repositories: Repositories) {
),
getChildCount: new GetChildCountUseCase(repositories.surveyFormRepository),
applyInitialRules: new ApplyInitialRulesToSurveyUseCase(),
removeRepeatableStage: new RemoveRepeatableProgramStageUseCase(
repositories.surveyFormRepository
),
},
astGuidelines: {
getGuidelines: new GetASTGuidelinesUseCase(repositories.astGuidelinesRepository),
Original file line number Diff line number Diff line change
@@ -11,6 +11,9 @@ import { ASTGUIDELINE_TYPES } from "../../../domain/entities/ASTGuidelines";
import { SurveyChildCountType } from "../../utils/surveyChildCountHelper";

export class SurveyTestRepository implements SurveyRepository {
deleteEventSurvey(_eventId: string, _orgUnitId: string, _programId: string): FutureData<void> {
throw new Error("Method not implemented.");
}
getSurveyNameAndASTGuidelineFromId(
id: string,
surveyFormType: SURVEY_FORM_TYPES
4 changes: 2 additions & 2 deletions src/data/utils/surveyFormMappers.ts
Original file line number Diff line number Diff line change
@@ -212,7 +212,7 @@ const getRepeatedStageEvents = (
dataElements: ProgramDataElement[],
options: Option[],
programStageSections?: ProgramStageSection[]
) => {
): QuestionnaireStage[] | undefined => {
const repeatedStageEvents = trackedEntity?.enrollments
?.at(0)
?.events.filter(e => e.programStage === stage.id);
@@ -254,7 +254,7 @@ const getRepeatedStageEvents = (
instanceId: repeatedStageEvt.event,
sortOrder: stage.sortOrder,
repeatable: stage.repeatable,
userAdded: index === 0 ? false : true,
isAddedByUser: index === 0 ? false : true,
};
});
};
1 change: 1 addition & 0 deletions src/domain/repositories/SurveyRepository.ts
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ export interface SurveyRepository {
): FutureData<Questionnaire>;

deleteSurvey(id: Id, orgUnitId: Id, programId: Id): FutureData<void>;
deleteEventSurvey(eventId: Id, orgUnitId: Id, programId: Id): FutureData<void>;

getSurveyNameAndASTGuidelineFromId(
id: Id,
28 changes: 28 additions & 0 deletions src/domain/usecases/RemoveRepeatableProgramStageUseCase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { FutureData } from "../../data/api-futures";
import { PREVALENCE_FACILITY_LEVEL_FORM_ID } from "../../data/entities/D2Survey";
import { Questionnaire } from "../entities/Questionnaire/Questionnaire";
import { Future } from "../entities/generic/Future";
import { SurveyRepository } from "../repositories/SurveyRepository";

export class RemoveRepeatableProgramStageUseCase {
constructor(private surveyRepository: SurveyRepository) {}

execute(questionnaire: Questionnaire, stageId: string): FutureData<Questionnaire> {
//Repeatable Program Stages are only applicable to Prevalence Facility forms

const eventId = questionnaire.stages.find(stage => stage.id === stageId)?.instanceId;

if (!eventId)
return Future.error(new Error("Cannot find event Id correspoding to the stage"));

return this.surveyRepository
.deleteEventSurvey(eventId, questionnaire.orgUnit.id, PREVALENCE_FACILITY_LEVEL_FORM_ID)
.flatMap(() => {
const updatedQuestionnaire = Questionnaire.removeProgramStage(
questionnaire,
stageId
);
return Future.success(updatedQuestionnaire);
});
}
}
2 changes: 1 addition & 1 deletion src/domain/usecases/SaveFormDataUseCase.ts
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ export class SaveFormDataUseCase {
surveyFormType === "PPSSurveyForm" && orgUnitId === "" ? GLOBAL_OU_ID : orgUnitId;

//Do not allow creation of multiple Prevalence Facility Level Forms for the same facility.
if (surveyFormType === "PrevalenceFacilityLevelForm") {
if (!eventId && surveyFormType === "PrevalenceFacilityLevelForm") {
return this.surveyReporsitory
.getSurveys(surveyFormType, programId, ouId, false)
.flatMap(surveys => {
16 changes: 11 additions & 5 deletions src/webapp/components/survey/hook/useSurveyForm.ts
Original file line number Diff line number Diff line change
@@ -180,14 +180,20 @@ export function useSurveyForm(formType: SURVEY_FORM_TYPES, eventId: string | und
const removeProgramStage = useCallback(
(stageId: Id) => {
if (questionnaire) {
const updatedQuestionnaire = Questionnaire.removeProgramStage(
questionnaire,
stageId
setLoading(true);
compositionRoot.surveys.removeRepeatableStage.execute(questionnaire, stageId).run(
updatedQuestionnaire => {
setQuestionnaire(updatedQuestionnaire);
setLoading(false);
},
err => {
setLoading(false);
setError(err.message);
}
);
setQuestionnaire(updatedQuestionnaire);
}
},
[questionnaire]
[compositionRoot.surveys, questionnaire]
);

return {

0 comments on commit 1e0bbb9

Please sign in to comment.