Skip to content

Commit

Permalink
feat: delete incident response action
Browse files Browse the repository at this point in the history
  • Loading branch information
deeonwuli committed Nov 27, 2024
1 parent 1af13b6 commit 0d67318
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 149 deletions.
2 changes: 0 additions & 2 deletions src/CompositionRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ import { CompleteEventTrackerUseCase } from "./domain/usecases/CompleteEventTrac
import { UserGroupD2Repository } from "./data/repositories/UserGroupD2Repository";
import { UserGroupRepository } from "./domain/repositories/UserGroupRepository";
import { UserGroupTestRepository } from "./data/repositories/test/UserGroupTestRepository";
import { DeleteIncidentResponseActionUseCase } from "./domain/usecases/DeleteIncidentResponseActionUseCase";

export type CompositionRoot = ReturnType<typeof getCompositionRoot>;

Expand Down Expand Up @@ -118,7 +117,6 @@ function getCompositionRoot(repositories: Repositories) {
incidentActionPlan: {
get: new GetIncidentActionByIdUseCase(repositories),
updateResponseAction: new UpdateIncidentResponseActionUseCase(repositories),
deleteResponseAction: new DeleteIncidentResponseActionUseCase(repositories),
},
incidentManagementTeam: {
get: new GetIncidentManagementTeamByIdUseCase(repositories),
Expand Down
17 changes: 13 additions & 4 deletions src/data/repositories/IncidentActionD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ export class IncidentActionD2Repository implements IncidentActionRepository {

saveIncidentAction(
formData: ActionPlanFormData | ResponseActionFormData | SingleResponseActionFormData,
diseaseOutbreakId: Id
diseaseOutbreakId: Id,
formOptionsToDelete?: Id[]
): FutureData<void> {
const programStageId = this.getProgramStageByFormType(formData.type);

Expand Down Expand Up @@ -178,17 +179,25 @@ export class IncidentActionD2Repository implements IncidentActionRepository {
if (saveResponse.status === "ERROR" || !diseaseOutbreakId) {
return Future.error(new Error(`Error saving Incident Action`));
} else {
if (formOptionsToDelete && formOptionsToDelete.length > 0) {
return this.deleteIncidentResponseAction(formOptionsToDelete);
}

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

deleteIncidentResponseAction(eventId: Id): FutureData<void> {
deleteIncidentResponseAction(events: Id[]): FutureData<void> {
return apiToFuture(
// @ts-ignore
this.api.tracker.post({ importStrategy: "DELETE" }, { events: [{ event: eventId }] })
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 Response Action`));
Expand Down
10 changes: 5 additions & 5 deletions src/data/repositories/test/IncidentActionTestRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export class IncidentActionTestRepository implements IncidentActionRepository {
throw new Error("Method not implemented.");
}

saveIncidentAction(_formData: any, _diseaseOutbreakId: Id): FutureData<void> {
throw new Error("Method not implemented.");
}

deleteIncidentResponseAction(_eventId: Id): FutureData<void> {
saveIncidentAction(
_formData: any,
_diseaseOutbreakId: Id,
_formOptionsToDelete?: Id[]
): FutureData<void> {
throw new Error("Method not implemented.");
}

Expand Down
4 changes: 2 additions & 2 deletions src/domain/repositories/IncidentActionRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export interface IncidentActionRepository {
): FutureData<Maybe<IncidentResponseActionDataValues[]>>;
saveIncidentAction(
formData: ActionPlanFormData | ResponseActionFormData | SingleResponseActionFormData,
diseaseOutbreakId: Id
diseaseOutbreakId: Id,
formOptionsToDelete?: Id[]
): FutureData<void>;
deleteIncidentResponseAction(eventId: Id): FutureData<void>;
updateIncidentResponseAction(options: UpdateIncidentResponseActionOptions): FutureData<void>;
}

Expand Down
11 changes: 0 additions & 11 deletions src/domain/usecases/DeleteIncidentResponseActionUseCase.ts

This file was deleted.

6 changes: 4 additions & 2 deletions src/domain/usecases/SaveEntityUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export class SaveEntityUseCase {

public execute(
formData: ConfigurableForm,
configurations: Configurations
configurations: Configurations,
formOptionsToDelete?: Id[]
): FutureData<void | Id> {
if (!formData || !formData.entity) return Future.error(new Error("No form data found"));
switch (formData.type) {
Expand Down Expand Up @@ -56,7 +57,8 @@ export class SaveEntityUseCase {
case "incident-response-action":
return this.options.incidentActionRepository.saveIncidentAction(
formData,
formData.eventTrackerDetails.id
formData.eventTrackerDetails.id,
formOptionsToDelete
);
case "incident-management-team-member-assignment": {
const isIncidentManager = formData.entity.teamRoles?.find(
Expand Down
34 changes: 24 additions & 10 deletions src/webapp/pages/form-page/mapFormStateToEntityData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,37 +557,38 @@ function mapFormStateToIncidentResponseActions(

const incidentResponseActions: ResponseAction[] = formState.sections
.filter(section => !section.id.includes("addNewResponseActionSection"))
.map((_, index): ResponseAction => {
.map((section): ResponseAction => {
const sectionIndex = extractIndex(section.id);

const mainTask = allFields.find(field =>
field.id.includes(`${responseActionConstants.mainTask}_${index}`)
field.id.includes(`${responseActionConstants.mainTask}_${sectionIndex}`)
)?.value as string;
const subActivities = allFields.find(field =>
field.id.includes(`${responseActionConstants.subActivities}_${index}`)
field.id.includes(`${responseActionConstants.subActivities}_${sectionIndex}`)
)?.value as string;
const subPillar = allFields.find(field =>
field.id.includes(`${responseActionConstants.subPillar}_${index}`)
field.id.includes(`${responseActionConstants.subPillar}_${sectionIndex}`)
)?.value as string;
const dueDate = allFields.find(field =>
field.id.includes(`${responseActionConstants.dueDate}_${index}`)
field.id.includes(`${responseActionConstants.dueDate}_${sectionIndex}`)
)?.value as Date;

const searchAssignROValue = allFields.find(field =>
field.id.includes(`${responseActionConstants.searchAssignRO}_${index}`)
field.id.includes(`${responseActionConstants.searchAssignRO}_${sectionIndex}`)
)?.value as string;
const searchAssignRO = formData.options.searchAssignRO.find(
option => option.id === searchAssignROValue
);
console.log({ allFields, formState, searchAssignRO, searchAssignROValue, formData });
if (!searchAssignRO) throw new Error("Responsible officer not found");

const statusValue = allFields.find(field =>
field.id.includes(`${responseActionConstants.status}_${index}`)
field.id.includes(`${responseActionConstants.status}_${sectionIndex}`)
)?.value as string;
const status = formData.options.status.find(option => option.id === statusValue);
if (!status) throw new Error("Status not found");

const verificationValue = allFields.find(field =>
field.id.includes(`${responseActionConstants.verification}_${index}`)
field.id.includes(`${responseActionConstants.verification}_${sectionIndex}`)
)?.value as string;
const verification = formData.options.verification.find(
option => option.id === verificationValue
Expand All @@ -597,8 +598,13 @@ function mapFormStateToIncidentResponseActions(
};
if (!verification) throw new Error("Verification not found");

const selectedEntityData =
sectionIndex !== undefined ? formData.entity[sectionIndex] : undefined;
const isResponseActionValid = selectedEntityData !== undefined;
const responseActionId = isResponseActionValid ? selectedEntityData.id : "";

const responseAction = new ResponseAction({
id: formData.entity?.[index]?.id ?? "",
id: responseActionId,
mainTask: mainTask,
subActivities: subActivities,
subPillar: subPillar,
Expand Down Expand Up @@ -749,3 +755,11 @@ function mapFormStateToIncidentManagementTeamMember(
status: teamMemberAssigned?.status,
});
}

function extractIndex(input: string): number | undefined {
const parts = input.split("_");
const lastPart = parts[parts.length - 1];
const index = Number(lastPart);

return isNaN(index) ? undefined : index;
}
Loading

0 comments on commit 0d67318

Please sign in to comment.