Skip to content

Commit

Permalink
Delete all IncidentManagementTeam and change to IncidentManagementTea…
Browse files Browse the repository at this point in the history
…mInAggregateRoot
  • Loading branch information
anagperal committed Nov 7, 2024
1 parent fbc0328 commit 1d20f0d
Show file tree
Hide file tree
Showing 36 changed files with 273 additions and 350 deletions.
51 changes: 4 additions & 47 deletions src/data/repositories/DiseaseOutbreakEventD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ import { getProgramStage, getProgramTEAsMetadata } from "./utils/MetadataHelper"
import { assertOrError } from "./utils/AssertOrError";
import { Future } from "../../domain/entities/generic/Future";
import { getAllTrackedEntitiesAsync } from "./utils/getAllTrackedEntities";
import { TeamMember, TeamRole } from "../../domain/entities/incident-management-team/TeamMember";
import { IncidentManagementTeam } from "../../domain/entities/incident-management-team/IncidentManagementTeam";
import { TeamMember, TeamRole } from "../../domain/entities/TeamMember";
import { D2TrackerEvent } from "@eyeseetea/d2-api/api/trackerEvents";
import {
mapD2EventsToIncidentManagementTeam,
mapD2EventsToIncidentManagementTeamInAggregateRoot,
mapIncidentManagementTeamMemberToD2Event,
} from "./utils/IncidentManagementTeamMapper";
Expand Down Expand Up @@ -142,50 +140,9 @@ export class DiseaseOutbreakEventD2Repository implements DiseaseOutbreakEventRep
}

getIncidentManagementTeam(
diseaseOutbreakId: Id,
teamMembers: TeamMember[]
): FutureData<IncidentManagementTeam> {
return getProgramStage(
this.api,
RTSL_ZEBRA_INCIDENT_MANAGEMENT_TEAM_BUILDER_PROGRAM_STAGE_ID
)
.flatMap(incidentManagementTeamBuilderResponse =>
assertOrError(
incidentManagementTeamBuilderResponse.objects[0],
`Incident management team builder program stage not found`
)
)
.flatMap(programStageDataElementsMetadata => {
return apiToFuture(
this.api.tracker.events.get({
program: RTSL_ZEBRA_PROGRAM_ID,
orgUnit: RTSL_ZEBRA_ORG_UNIT_ID,
trackedEntity: diseaseOutbreakId,
programStage: RTSL_ZEBRA_INCIDENT_MANAGEMENT_TEAM_BUILDER_PROGRAM_STAGE_ID,
fields: {
dataValues: {
dataElement: dataElementFields,
value: true,
},
trackedEntity: true,
event: true,
},
})
)
.flatMap(response =>
assertOrError(response.instances, `Incident management team not found`)
)
.flatMap(d2Events => {
return Future.success(
mapD2EventsToIncidentManagementTeam(
diseaseOutbreakId,
d2Events,
teamMembers,
programStageDataElementsMetadata.programStageDataElements
)
);
});
});
diseaseOutbreakId: Id
): FutureData<IncidentManagementTeamInAggregateRoot> {
return this.getIncidentManagementTeamInAggregateRoot(diseaseOutbreakId);
}

saveIncidentManagementTeamMemberRole(
Expand Down
2 changes: 1 addition & 1 deletion src/data/repositories/RoleD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { D2Api, MetadataPick } from "../../types/d2-api";
import { apiToFuture, FutureData } from "../api-futures";
import { assertOrError } from "./utils/AssertOrError";
import { Future } from "../../domain/entities/generic/Future";
import { Role } from "../../domain/entities/incident-management-team/Role";
import { Role } from "../../domain/entities/Role";
import { RoleRepository } from "../../domain/repositories/RoleRepository";
import { RTSL_ZEBRA_INCIDENT_MANAGEMENT_TEAM_BUILDER_PROGRAM_STAGE_ID } from "./consts/DiseaseOutbreakConstants";
import { RTSL_ZEBRA_INCIDENT_MANAGEMENT_TEAM_BUILDER_IDS_WITHOUT_ROLES } from "./consts/IncidentManagementTeamBuilderConstants";
Expand Down
2 changes: 1 addition & 1 deletion src/data/repositories/TeamMemberD2Repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { D2Api, MetadataPick } from "../../types/d2-api";
import { TeamMember } from "../../domain/entities/incident-management-team/TeamMember";
import { TeamMember } from "../../domain/entities/TeamMember";
import { Id } from "../../domain/entities/Ref";
import { TeamMemberRepository } from "../../domain/repositories/TeamMemberRepository";
import { apiToFuture, FutureData } from "../api-futures";
Expand Down
15 changes: 8 additions & 7 deletions src/data/repositories/test/DiseaseOutbreakEventTestRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import {
DiseaseOutbreakEventBaseAttrs,
NationalIncidentStatus,
} from "../../../domain/entities/disease-outbreak-event/DiseaseOutbreakEvent";
import { DiseaseOutbreakEventAggregateRoot } from "../../../domain/entities/disease-outbreak-event/DiseaseOutbreakEventAggregateRoot";
import {
DiseaseOutbreakEventAggregateRoot,
IncidentManagementTeamInAggregateRoot,
} from "../../../domain/entities/disease-outbreak-event/DiseaseOutbreakEventAggregateRoot";
import { Future } from "../../../domain/entities/generic/Future";
import { IncidentManagementTeam } from "../../../domain/entities/incident-management-team/IncidentManagementTeam";
import { TeamMember, TeamRole } from "../../../domain/entities/incident-management-team/TeamMember";
import { TeamMember, TeamRole } from "../../../domain/entities/TeamMember";
import { Id } from "../../../domain/entities/Ref";
import { DiseaseOutbreakEventRepository } from "../../../domain/repositories/DiseaseOutbreakEventRepository";
import { FutureData } from "../../api-futures";
Expand Down Expand Up @@ -127,11 +129,10 @@ export class DiseaseOutbreakEventTestRepository implements DiseaseOutbreakEventR
}

getIncidentManagementTeam(
_diseaseOutbreakId: Id,
_teamMembers: TeamMember[]
): FutureData<IncidentManagementTeam> {
_diseaseOutbreakId: Id
): FutureData<IncidentManagementTeamInAggregateRoot> {
return Future.success(
new IncidentManagementTeam({
new IncidentManagementTeamInAggregateRoot({
teamHierarchy: [],
lastUpdated: new Date(),
})
Expand Down
2 changes: 1 addition & 1 deletion src/data/repositories/test/RoleTestRepository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Future } from "../../../domain/entities/generic/Future";
import { Role } from "../../../domain/entities/incident-management-team/Role";
import { Role } from "../../../domain/entities/Role";
import { RoleRepository } from "../../../domain/repositories/RoleRepository";
import { FutureData } from "../../api-futures";

Expand Down
2 changes: 1 addition & 1 deletion src/data/repositories/test/TeamMemberTestRepository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Future } from "../../../domain/entities/generic/Future";
import { TeamMember } from "../../../domain/entities/incident-management-team/TeamMember";
import { TeamMember } from "../../../domain/entities/TeamMember";
import { Id } from "../../../domain/entities/Ref";
import { TeamMemberRepository } from "../../../domain/repositories/TeamMemberRepository";
import { FutureData } from "../../api-futures";
Expand Down
110 changes: 1 addition & 109 deletions src/data/repositories/utils/IncidentManagementTeamMapper.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { D2TrackerEvent, DataValue } from "@eyeseetea/d2-api/api/trackerEvents";

import _c from "../../../domain/entities/generic/Collection";
import { Maybe } from "../../../utils/ts-utils";
import { IncidentManagementTeam } from "../../../domain/entities/incident-management-team/IncidentManagementTeam";
import { getPopulatedDataElement, getValueById } from "./helpers";
import {
RTSL_ZEBRA_INCIDENT_MANAGEMENT_TEAM_BUILDER_PROGRAM_STAGE_ID,
RTSL_ZEBRA_ORG_UNIT_ID,
RTSL_ZEBRA_PROGRAM_ID,
} from "../consts/DiseaseOutbreakConstants";
import { TeamMember, TeamRole } from "../../../domain/entities/incident-management-team/TeamMember";
import { TeamMember, TeamRole } from "../../../domain/entities/TeamMember";
import { Id } from "../../../domain/entities/Ref";
import {
incidentManagementTeamBuilderCodesWithoutRoles,
Expand All @@ -22,112 +20,6 @@ import {
} from "../../../domain/entities/disease-outbreak-event/DiseaseOutbreakEventAggregateRoot";
import { getISODateAsLocaleDateString } from "./DateTimeHelper";

export function mapD2EventsToIncidentManagementTeam(
diseaseOutbreakId: Id,
d2Events: D2TrackerEvent[],
teamMembers: TeamMember[],
incidentManagementTeamProgramStageDataElements: D2ProgramStageDataElementsMetadata[]
): IncidentManagementTeam {
const teamHierarchy: TeamMember[] = teamMembers.reduce(
(acc: TeamMember[], teamMember: TeamMember) => {
const memberRoleEvents = d2Events.filter(event => {
const teamMemberAssignedUsername = getValueById(
event.dataValues,
RTSL_ZEBRA_INCIDENT_MANAGEMENT_TEAM_BUILDER_IDS_WITHOUT_ROLES.teamMemberAssigned
);
return teamMemberAssignedUsername === teamMember.username;
});

if (memberRoleEvents.length === 0) {
return acc;
} else {
const teamRoles = getTeamMemberIncidentManagementTeamRoles(
diseaseOutbreakId,
teamMember,
memberRoleEvents,
incidentManagementTeamProgramStageDataElements
);

return teamRoles.length === 0
? acc
: [...acc, new TeamMember({ ...teamMember, teamRoles: teamRoles })];
}
},
[]
);

const sortedByUpdatedDates = d2Events.sort(function (a, b) {
if (!a.updatedAt) return -1;
if (!b.updatedAt) return 1;
return a.updatedAt < b.updatedAt ? -1 : a.updatedAt > b.updatedAt ? 1 : 0;
});

return new IncidentManagementTeam({
teamHierarchy: teamHierarchy,
lastUpdated: sortedByUpdatedDates[0]?.updatedAt
? getISODateAsLocaleDateString(sortedByUpdatedDates[0]?.updatedAt)
: undefined,
});
}

function getTeamMemberIncidentManagementTeamRoles(
diseaseOutbreakId: Id,
teamMemberAssigned: TeamMember,
events: D2TrackerEvent[],
incidentManagementTeamProgramStageDataElements: D2ProgramStageDataElementsMetadata[]
): TeamRole[] {
return events.reduce((acc: TeamRole[], event: D2TrackerEvent) => {
if (
teamMemberAssigned.username ===
getValueById(
event.dataValues,
RTSL_ZEBRA_INCIDENT_MANAGEMENT_TEAM_BUILDER_IDS_WITHOUT_ROLES.teamMemberAssigned
)
) {
const teamRole = getTeamRole(
diseaseOutbreakId,
event.event,
event.dataValues,
incidentManagementTeamProgramStageDataElements
);

return teamRole ? [...acc, teamRole] : acc;
}
return acc;
}, []);
}

function getTeamRole(
diseaseOutbreakId: Id,
eventId: Id,
dataValues: DataValue[],
incidentManagementTeamProgramStageDataElements: D2ProgramStageDataElementsMetadata[]
): Maybe<TeamRole> {
const selectedRoleId = incidentManagementTeamProgramStageDataElements.find(programStage => {
const role = getValueById(dataValues, programStage.dataElement.id);
return role === "true";
})?.dataElement.id;

const roleSelected = incidentManagementTeamProgramStageDataElements.find(
programStage => programStage.dataElement.id === selectedRoleId
);

const reportsToUsername = getValueById(
dataValues,
RTSL_ZEBRA_INCIDENT_MANAGEMENT_TEAM_BUILDER_IDS_WITHOUT_ROLES.reportsToUsername
);

if (selectedRoleId && roleSelected) {
return {
id: eventId,
diseaseOutbreakId: diseaseOutbreakId,
roleId: selectedRoleId,
name: roleSelected?.dataElement.name,
reportsToUsername: reportsToUsername,
};
}
}

export function mapIncidentManagementTeamMemberToD2Event(
teamMemberRole: TeamRole,
incidentManagementTeamMember: TeamMember,
Expand Down
4 changes: 2 additions & 2 deletions src/domain/entities/AppConfigurations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import {
RiskAssessmentQuestionnaireOptions,
RiskAssessmentSummaryOptions,
} from "./ConfigurableForm";
import { Role } from "./incident-management-team/Role";
import { TeamMember } from "./incident-management-team/TeamMember";
import { Role } from "./Role";
import { TeamMember } from "./TeamMember";

import {
LowPopulationAtRisk,
Expand Down
8 changes: 4 additions & 4 deletions src/domain/entities/ConfigurableForm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Maybe } from "../../utils/ts-utils";
import { TeamMember } from "./incident-management-team/TeamMember";
import { TeamMember } from "./TeamMember";
import { Id, Option } from "./Ref";
import { Rule } from "./Rule";
import {
Expand All @@ -12,8 +12,8 @@ import { RiskAssessmentSummary } from "./risk-assessment/RiskAssessmentSummary";
import { RiskAssessmentQuestionnaire } from "./risk-assessment/RiskAssessmentQuestionnaire";
import { ActionPlanAttrs } from "./incident-action-plan/ActionPlan";
import { ResponseAction } from "./incident-action-plan/ResponseAction";
import { IncidentManagementTeam } from "./incident-management-team/IncidentManagementTeam";
import { Role } from "./incident-management-team/Role";
import { Role } from "./Role";
import { IncidentManagementTeamInAggregateRoot } from "./disease-outbreak-event/DiseaseOutbreakEventAggregateRoot";

export type DiseaseOutbreakEventOptions = {
dataSources: Option[];
Expand Down Expand Up @@ -124,7 +124,7 @@ export type IncidentManagementTeamMemberFormData = BaseFormData & {
eventTrackerDetails: DiseaseOutbreakEvent;
entity: Maybe<TeamMember>;
incidentManagementTeamRoleId: Maybe<Id>;
currentIncidentManagementTeam: Maybe<IncidentManagementTeam>;
currentIncidentManagementTeam: Maybe<IncidentManagementTeamInAggregateRoot>;
options: IncidentManagementTeamRoleOptions;
};

Expand Down
3 changes: 3 additions & 0 deletions src/domain/entities/Role.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { CodedNamedRef } from "./Ref";

export type Role = CodedNamedRef;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Maybe } from "../../../utils/ts-utils";
import { Id, NamedRef } from "../Ref";
import { Struct } from "../generic/Struct";
import { Maybe } from "../../utils/ts-utils";
import { Id, NamedRef } from "./Ref";
import { Struct } from "./generic/Struct";

type PhoneNumber = string;
type Email = string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Struct } from "../generic/Struct";
import { IncidentActionPlan } from "../incident-action-plan/IncidentActionPlan";
import { IncidentManagementTeam } from "../incident-management-team/IncidentManagementTeam";
import { TeamMember } from "../incident-management-team/TeamMember";
import { TeamMember } from "../TeamMember";
import { Code, NamedRef } from "../Ref";
import { RiskAssessment } from "../risk-assessment/RiskAssessment";
import { Maybe } from "../../../utils/ts-utils";
import { ValidationError } from "../ValidationError";
import { IncidentManagementTeamInAggregateRoot } from "./DiseaseOutbreakEventAggregateRoot";

export const hazardTypes = [
"Biological:Human",
Expand Down Expand Up @@ -79,7 +79,7 @@ export type DiseaseOutbreakEventAttrs = DiseaseOutbreakEventBaseAttrs & {
incidentManager: Maybe<TeamMember>; //TO DO : make mandatory once form rules applied.
riskAssessment: Maybe<RiskAssessment>;
incidentActionPlan: Maybe<IncidentActionPlan>;
incidentManagementTeam: Maybe<IncidentManagementTeam>;
incidentManagementTeam: Maybe<IncidentManagementTeamInAggregateRoot>;
};

/**
Expand Down
Loading

0 comments on commit 1d20f0d

Please sign in to comment.