Skip to content

Commit

Permalink
feat: delete response action
Browse files Browse the repository at this point in the history
  • Loading branch information
deeonwuli committed Nov 27, 2024
1 parent 0d67318 commit 05a544d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/data/repositories/IncidentActionD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export class IncidentActionD2Repository implements IncidentActionRepository {
});
}

deleteIncidentResponseAction(events: Id[]): FutureData<void> {
private deleteIncidentResponseAction(events: Id[]): FutureData<void> {
return apiToFuture(
this.api.tracker.post(
{ importStrategy: "DELETE" },
Expand Down
31 changes: 28 additions & 3 deletions src/data/repositories/RiskAssessmentD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ export class RiskAssessmentD2Repository implements RiskAssessmentRepository {
| RiskAssessmentGradingFormData
| RiskAssessmentSummaryFormData
| RiskAssessmentQuestionnaireFormData,
diseaseOutbreakId: Id
diseaseOutbreakId: Id,
formOptionsToDelete?: Id[]
): FutureData<void> {
if (formData.type === "risk-assessment-questionnaire") {
const { stdQuestionnaireStageId, customQuestionnaireStageId } =
Expand Down Expand Up @@ -295,7 +296,8 @@ export class RiskAssessmentD2Repository implements RiskAssessmentRepository {
customQuestionnaireDataElements,
formData,
diseaseOutbreakId,
customQuestionnaireStageId
customQuestionnaireStageId,
formOptionsToDelete
);
}
);
Expand Down Expand Up @@ -336,7 +338,8 @@ export class RiskAssessmentD2Repository implements RiskAssessmentRepository {
| RiskAssessmentSummaryFormData
| RiskAssessmentQuestionnaireFormData,
diseaseOutbreakId: Id,
programStageId: Id
programStageId: Id,
formOptionsToDelete?: Id[]
): FutureData<void> {
//Get the enrollment Id for the disease outbreak
return apiToFuture(
Expand Down Expand Up @@ -368,11 +371,33 @@ export class RiskAssessmentD2Repository implements RiskAssessmentRepository {
if (saveResponse.status === "ERROR" || !diseaseOutbreakId) {
return Future.error(new Error(`Error Risk Assessment Grading`));
} else {
if (formOptionsToDelete && formOptionsToDelete.length > 0) {
return this.deleteCustomQuestion(formOptionsToDelete);
}

return Future.success(undefined);
}
});
});
}

private deleteCustomQuestion(events: Id[]): FutureData<void> {
return apiToFuture(
this.api.tracker.post(
{ importStrategy: "DELETE" },
// an array of event ids is sufficient for TrackerPostRequest with DELETE importStrategy payload
// @ts-ignore
{ events: events.map(event => ({ event: event })) }
)
).flatMap(response => {
if (response.status === "ERROR") {
return Future.error(new Error(`Error deleting Custom Question`));
} else {
return Future.success(undefined);
}
});
}

private getProgramStageByFormType(formType: string) {
switch (formType) {
case "risk-assessment-grading":
Expand Down
3 changes: 2 additions & 1 deletion src/domain/repositories/RiskAssessmentRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface RiskAssessmentRepository {
| RiskAssessmentGradingFormData
| RiskAssessmentSummaryFormData
| RiskAssessmentQuestionnaireFormData,
diseaseOutbreakId: Id
diseaseOutbreakId: Id,
formOptionsToDelete?: Id[]
): FutureData<void>;
}
3 changes: 2 additions & 1 deletion src/domain/usecases/SaveEntityUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export class SaveEntityUseCase {
case "risk-assessment-questionnaire":
return this.options.riskAssessmentRepository.saveRiskAssessment(
formData,
formData.eventTrackerDetails.id
formData.eventTrackerDetails.id,
formOptionsToDelete
);
case "incident-action-plan":
case "incident-response-actions":
Expand Down
13 changes: 9 additions & 4 deletions src/webapp/pages/form-page/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,12 @@ export function useForm(formType: FormType, id?: Id): State {
(id: string) => {
if (formState.kind !== "loaded" || !entityData) return;

const sectionIndexToDelete = formState.data.sections.findIndex(
section => section.id === id
);

switch (entityData.type) {
case "risk-assessment-questionnaire": {
const sectionIndexToDelete = formState.data.sections
.filter(section => section.title?.includes("Custom Question"))
.findIndex(section => section.id === id);

const entityId = entityData.entity?.additionalQuestions?.find(
(_, index) => index === sectionIndexToDelete
)?.id;
Expand All @@ -260,11 +260,16 @@ export function useForm(formType: FormType, id?: Id): State {
),
} as RiskAssessmentQuestionnaire,
};

setEntityData(updatedEntityData);
}
break;
}
case "incident-response-actions": {
const sectionIndexToDelete = formState.data.sections.findIndex(
section => section.id === id
);

const entityId = entityData.entity?.find(
(_, index) => index === sectionIndexToDelete
)?.id;
Expand Down

0 comments on commit 05a544d

Please sign in to comment.