Skip to content

Commit

Permalink
fix: delete custom ast guideline from datastore on prevelance survey …
Browse files Browse the repository at this point in the history
…form deletion
  • Loading branch information
9sneha-n committed May 7, 2024
1 parent 562a264 commit dc09d3f
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 24 deletions.
5 changes: 4 additions & 1 deletion src/CompositionRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ function getCompositionRoot(repositories: Repositories) {
repositories.paginatedSurveyRepository,
repositories.surveyFormRepository
),
deleteSurvey: new DeleteSurveyUseCase(repositories.surveyFormRepository),
deleteSurvey: new DeleteSurveyUseCase(
repositories.surveyFormRepository,
repositories.astGuidelinesRepository
),
getChildCount: new GetChildCountUseCase(repositories.surveyFormRepository),
applyInitialRules: new ApplyInitialRulesToSurveyUseCase(),
},
Expand Down
4 changes: 4 additions & 0 deletions src/data/DataStoreClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ export class DataStoreClient {
public saveObject<T extends object>(key: string, value: T): FutureData<void> {
return apiToFuture(this.dataStore.save(key, value));
}

public removeObject(key: string): FutureData<boolean> {
return apiToFuture(this.dataStore.delete(key));
}
}
10 changes: 10 additions & 0 deletions src/data/repositories/ASTGuidelinesD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,14 @@ export class ASTGuidelinesD2Repository implements ASTGuidelinesRepository {
}
}
}

deleteCustomASTGuideline(surveyId: Id): FutureData<boolean> {
return this.dataStoreClient.removeObject(`CUSTOM_lists_${surveyId}`).flatMap(() => {
return this.dataStoreClient
.removeObject(`CUSTOM_matrix_${surveyId}`)
.flatMap((deleted: boolean) => {
return Future.success(deleted);
});
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { ASTGUIDELINE_TYPES, CurrentASTGuidelines } from "../../../domain/entiti
import { FutureData } from "../../api-futures";

export class ASTGuidelinesTestRepository implements ASTGuidelinesRepository {
deleteCustomASTGuideline(surveyId: string): FutureData<boolean> {
console.debug("deleteCustomASTGuideline", surveyId);
throw new Error("Method not implemented.");
}
saveByASTGuidelineType(
astGuidelineType: ASTGUIDELINE_TYPES,
surveyId: string
Expand Down
1 change: 1 addition & 0 deletions src/domain/repositories/ASTGuidelinesRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export interface ASTGuidelinesRepository {
): FutureData<CurrentASTGuidelines>;

saveByASTGuidelineType(astGuidelineType: ASTGUIDELINE_TYPES, surveyId: Id): FutureData<void>;
deleteCustomASTGuideline(surveyId: Id): FutureData<boolean>;
}
30 changes: 26 additions & 4 deletions src/domain/usecases/DeleteSurveyUseCase.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
import { FutureData } from "../../data/api-futures";
import { SURVEY_FORM_TYPES } from "../entities/Survey";
import { Id } from "../entities/Ref";

import { SurveyRepository } from "../repositories/SurveyRepository";
import { getProgramId } from "../utils/PPSProgramsHelper";
import { Future } from "../entities/generic/Future";
import { ASTGUIDELINE_TYPES } from "../entities/ASTGuidelines";
import { ASTGuidelinesRepository } from "../repositories/ASTGuidelinesRepository";

export class DeleteSurveyUseCase {
constructor(private surveyReporsitory: SurveyRepository) {}
constructor(
private surveyReporsitory: SurveyRepository,
private astGuidelinesRepository: ASTGuidelinesRepository
) {}

public execute(surveyFormType: SURVEY_FORM_TYPES, orgUnitId: Id, id: Id): FutureData<void> {
public execute(
surveyFormType: SURVEY_FORM_TYPES,
orgUnitId: Id,
id: Id,
astGuidelineType?: ASTGUIDELINE_TYPES
): FutureData<void> {
const programId = getProgramId(surveyFormType);
return this.surveyReporsitory.deleteSurvey(id, orgUnitId, programId);
return this.surveyReporsitory.deleteSurvey(id, orgUnitId, programId).flatMap(() => {
if (surveyFormType === "PrevalenceSurveyForm" && astGuidelineType === "CUSTOM") {
return this.astGuidelinesRepository
.deleteCustomASTGuideline(id)
.flatMap((deleted: boolean) => {
if (deleted) return Future.success(undefined);
else
return Future.error(
new Error("Error deleting the custom AST guideline in datastore")
);
});
} else return Future.success(undefined);
});
}
}
41 changes: 22 additions & 19 deletions src/webapp/components/survey-list/hook/useDeleteSurvey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useAppContext } from "../../../contexts/app-context";
import i18n from "@eyeseetea/feedback-component/locales";
import { Id } from "../../../../domain/entities/Ref";
import { ActionOutcome } from "../../../../domain/entities/generic/ActionOutcome";
import { ASTGUIDELINE_TYPES } from "../../../../domain/entities/ASTGuidelines";

export function useDeleteSurvey(
formType: SURVEY_FORM_TYPES,
Expand All @@ -25,33 +26,35 @@ export function useDeleteSurvey(
),
});
} else {
deleteSurvey(survey.id, survey.assignedOrgUnit.id);
deleteSurvey(survey.id, survey.assignedOrgUnit.id, survey.astGuideline);
}
};

const deleteSurvey = (surveyId: Id, orgUnitId: Id) => {
const deleteSurvey = (surveyId: Id, orgUnitId: Id, astGuidelineType?: ASTGUIDELINE_TYPES) => {
setLoading(true);

if (formType === "PPSWardRegister" || formType === "PPSPatientRegister")
orgUnitId = currentHospitalForm?.orgUnitId ?? "";
compositionRoot.surveys.deleteSurvey.execute(formType, orgUnitId, surveyId).run(
() => {
setDeleteCompleteState({
status: "success",
message: i18n.t("Survey deleted!"),
});
refreshSurveys({});
setLoading(false);
},
err => {
setDeleteCompleteState({
status: "error",
message: err ? err.message : i18n.t("Error deleting the survery"),
});
compositionRoot.surveys.deleteSurvey
.execute(formType, orgUnitId, surveyId, astGuidelineType)
.run(
() => {
setDeleteCompleteState({
status: "success",
message: i18n.t("Survey deleted!"),
});
refreshSurveys({});
setLoading(false);
},
err => {
setDeleteCompleteState({
status: "error",
message: err ? err.message : i18n.t("Error deleting the survery"),
});

setLoading(false);
}
);
setLoading(false);
}
);
};

return {
Expand Down

0 comments on commit dc09d3f

Please sign in to comment.