Skip to content

Commit

Permalink
feat: Get all disease outbreak base repo impl
Browse files Browse the repository at this point in the history
  • Loading branch information
9sneha-n committed Jul 23, 2024
1 parent 9da61de commit 86ff4c6
Show file tree
Hide file tree
Showing 10 changed files with 2,036 additions and 2,029 deletions.
6 changes: 4 additions & 2 deletions src/CompositionRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { UserD2Repository } from "./data/repositories/UserD2Repository";
import { DiseaseOutbreakEventRepository } from "./domain/repositories/DiseaseOutbreakEventRepository";
import { UserRepository } from "./domain/repositories/UserRepository";
import { GetCurrentUserUseCase } from "./domain/usecases/GetCurrentUserUseCase";
import { GetDiseaseOutbreaksUseCase } from "./domain/usecases/GetDiseaseOutbreaksUseCase";
import { GetDiseaseOutbreakByIdUseCase } from "./domain/usecases/GetDiseaseOutbreakByIdUseCase";
import { D2Api } from "./types/d2-api";
import { OptionsRepository } from "./domain/repositories/OptionsRepository";
import { TeamMemberRepository } from "./domain/repositories/TeamMemberRepository";
Expand All @@ -16,6 +16,7 @@ import { OrgUnitD2Repository } from "./data/repositories/OrgUnitD2Repository";
import { OptionsTestRepository } from "./data/repositories/test/OptionsTestRepository";
import { TeamMemberTestRepository } from "./data/repositories/test/TeamMemberTestRepository";
import { OrgUnitTestRepository } from "./data/repositories/test/OrgUnitTestRepository";
import { GetAllDiseaseOutbreaksUseCase } from "./domain/usecases/GetAllDiseaseOutbreaksUseCase";

export type CompositionRoot = ReturnType<typeof getCompositionRoot>;

Expand All @@ -33,12 +34,13 @@ function getCompositionRoot(repositories: Repositories) {
getCurrent: new GetCurrentUserUseCase(repositories.usersRepository),
},
diseaseOutbreakEvent: {
get: new GetDiseaseOutbreaksUseCase(
get: new GetDiseaseOutbreakByIdUseCase(
repositories.diseaseOutbreakEventRepository,
repositories.optionsRepository,
repositories.teamMemberRepository,
repositories.orgUnitRepository
),
getAll: new GetAllDiseaseOutbreaksUseCase(repositories.diseaseOutbreakEventRepository),
},
};
}
Expand Down
35 changes: 25 additions & 10 deletions src/data/repositories/DiseaseOutbreakEventD2Repository.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
import { D2Api } from "@eyeseetea/d2-api/2.36";
import { DiseaseOutbreakEventRepository } from "../../domain/repositories/DiseaseOutbreakEventRepository";
import { FutureData } from "../api-futures";
import { apiToFuture, FutureData } from "../api-futures";
import {
DiseaseOutbreakEvent,
DiseaseOutbreakEventBaseAttrs,
} from "../../domain/entities/disease-outbreak-event/DiseaseOutbreakEvent";
import { Id, ConfigLabel } from "../../domain/entities/Ref";
import { getTrackerEntityAttributes } from "./utils/getTrackerEntityAttributes";
import { mapTrackedEntityAttributesToDiseaseOutbreak } from "./utils/mapTrackedEntityAttributesToDiseaseOutbreak";
import { RTSL_ZEBRA_ORG_UNIT_ID, RTSL_ZEBRA_PROGRAM_ID } from "./consts/DiseaseOutbreakConstants";

export class DiseaseOutbreakEventD2Repository implements DiseaseOutbreakEventRepository {
constructor(private api: D2Api) {}

get(id: Id): FutureData<DiseaseOutbreakEventBaseAttrs> {
return getTrackerEntityAttributes(
this.api,
RTSL_ZEBRA_PROGRAM_ID,
RTSL_ZEBRA_ORG_UNIT_ID,
id
);
return apiToFuture(
this.api.tracker.trackedEntities.get({
program: RTSL_ZEBRA_PROGRAM_ID,
orgUnit: RTSL_ZEBRA_ORG_UNIT_ID,
trackedEntity: id,
fields: { attributes: true, trackedEntity: true },
})
).map(trackedEntity => {
if (!trackedEntity.instances[0]) throw new Error("Tracked entity not found");
return mapTrackedEntityAttributesToDiseaseOutbreak(trackedEntity.instances[0]);
});
}
getAll(): FutureData<DiseaseOutbreakEvent[]> {
throw new Error("Method not implemented.");
getAll(): FutureData<DiseaseOutbreakEventBaseAttrs[]> {
return apiToFuture(
this.api.tracker.trackedEntities.get({
program: RTSL_ZEBRA_PROGRAM_ID,
orgUnit: RTSL_ZEBRA_ORG_UNIT_ID,
fields: { attributes: true, trackedEntity: true },
})
).map(response => {
return response.instances.map(trackedEntity => {
return mapTrackedEntityAttributesToDiseaseOutbreak(trackedEntity);
});
});
}
save(_diseaseOutbreak: DiseaseOutbreakEvent): FutureData<void> {
throw new Error("Method not implemented.");
Expand Down
3 changes: 0 additions & 3 deletions src/data/repositories/OrgUnitD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ export class OrgUnitD2Repository implements OrgUnitRepository {
},
})
).map(response => {
if (response.organisationUnits.length < 1)
throw new Error("District/Province not found");

const orgUnits: OrgUnit[] = response.organisationUnits.map(ou => {
return {
id: ou.id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,13 @@
import { D2Api } from "@eyeseetea/d2-api/2.36";
import { Id } from "../../../domain/entities/Ref";
import { apiToFuture, FutureData } from "../../api-futures";
import {
DiseaseOutbreakEventBaseAttrs,
HazardType,
IncidentStatusType,
} from "../../../domain/entities/disease-outbreak-event/DiseaseOutbreakEvent";
import { D2TrackerTrackedEntity } from "@eyeseetea/d2-api/api/trackerTrackedEntities";
import { DiseaseOutbreakCodes } from "../consts/DiseaseOutbreakConstants";
import _c from "../../../domain/entities/generic/Collection";

export function getTrackerEntityAttributes(
api: D2Api,
programId: Id,
orgUnitId: Id,
trackedEntityId: Id
): FutureData<DiseaseOutbreakEventBaseAttrs> {
return apiToFuture(
api.tracker.trackedEntities.get({
program: programId,
orgUnit: orgUnitId,
trackedEntity: trackedEntityId,
fields: { attributes: true, trackedEntity: true },
})
).map(trackedEntity => {
if (!trackedEntity.instances[0]) throw new Error("Tracked entity not found");
return mapTrackedEntityAttributesToDiseaseOutbreak(trackedEntity.instances[0]);
});
}

function mapTrackedEntityAttributesToDiseaseOutbreak(
export function mapTrackedEntityAttributesToDiseaseOutbreak(
trackedEntity: D2TrackerTrackedEntity
): DiseaseOutbreakEventBaseAttrs {
if (!trackedEntity.trackedEntity) throw new Error("Tracked entity not found");
Expand All @@ -45,9 +24,12 @@ function mapTrackedEntityAttributesToDiseaseOutbreak(

notificationSourceId: getValueFromMap("notificationSource", trackedEntity),

areasAffectedProvinceIds: [getValueFromMap("areasAffectedProvinces", trackedEntity)], //TO DO : handle multiple provinces when metadata change is done

areasAffectedDistrictIds: [getValueFromMap("areasAffectedDistricts", trackedEntity)], //TO DO : handle multiple provinces when metadata change is done
areasAffectedProvinceIds: [getValueFromMap("areasAffectedProvinces", trackedEntity)].filter(
ou => ou !== ""
), //TO DO : handle multiple provinces when metadata change is done
areasAffectedDistrictIds: [getValueFromMap("areasAffectedDistricts", trackedEntity)].filter(
ou => ou !== ""
), //TO DO : handle multiple provinces when metadata change is done
incidentStatus: getValueFromMap("incidentStatus", trackedEntity) as IncidentStatusType,
emerged: {
date: new Date(getValueFromMap("emergedDate", trackedEntity)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type DiseaseOutbreakEventAttrs = DiseaseOutbreakEventBaseAttrs & {
notificationSource: NamedRef;
areasAffectedProvinces: OrgUnit[];
areasAffectedDistricts: OrgUnit[];
incidentManager: TeamMember;
incidentManager: Maybe<TeamMember>; //TO DO : make mandatory once form rules applied.
riskAssessments: Maybe<RiskAssessment[]>;
incidentActionPlan: Maybe<IncidentActionPlan>;
incidentManagementTeam: Maybe<IncidentManagementTeam>;
Expand Down
2 changes: 1 addition & 1 deletion src/domain/repositories/DiseaseOutbreakEventRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ConfigLabel, Id } from "../entities/Ref";

export interface DiseaseOutbreakEventRepository {
get(id: Id): FutureData<DiseaseOutbreakEventBaseAttrs>;
getAll(): FutureData<DiseaseOutbreakEvent[]>;
getAll(): FutureData<DiseaseOutbreakEventBaseAttrs[]>;
save(diseaseOutbreak: DiseaseOutbreakEvent): FutureData<void>;
delete(id: Id): FutureData<void>;
getConfigStrings(): FutureData<ConfigLabel[]>;
Expand Down
11 changes: 11 additions & 0 deletions src/domain/usecases/GetAllDiseaseOutbreaksUseCase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { FutureData } from "../../data/api-futures";
import { DiseaseOutbreakEventBaseAttrs } from "../entities/disease-outbreak-event/DiseaseOutbreakEvent";
import { DiseaseOutbreakEventRepository } from "../repositories/DiseaseOutbreakEventRepository";

export class GetAllDiseaseOutbreaksUseCase {
constructor(private diseaseOutbreakRepository: DiseaseOutbreakEventRepository) {}

public execute(): FutureData<DiseaseOutbreakEventBaseAttrs[]> {
return this.diseaseOutbreakRepository.getAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { OptionsRepository } from "../repositories/OptionsRepository";
import { OrgUnitRepository } from "../repositories/OrgUnitRepository";
import { TeamMemberRepository } from "../repositories/TeamMemberRepository";

export class GetDiseaseOutbreaksUseCase {
export class GetDiseaseOutbreakByIdUseCase {
constructor(
private diseaseOutbreakRepository: DiseaseOutbreakEventRepository,
private optionsRepository: OptionsRepository,
Expand All @@ -30,7 +30,9 @@ export class GetDiseaseOutbreaksUseCase {
mainSyndrome: this.optionsRepository.get(mainSyndromeId),
suspectedDisease: this.optionsRepository.get(suspectedDiseaseId),
notificationSource: this.optionsRepository.get(notificationSourceId),
incidentManager: this.teamMemberRepository.get(incidentManagerName),
incidentManager: incidentManagerName
? this.teamMemberRepository.get(incidentManagerName)
: Future.success(undefined),
areasAffectedProvinces: this.orgUnitRepository.get(areasAffectedProvinceIds),
areasAffectedDistricts: this.orgUnitRepository.get(areasAffectedDistrictIds),
}).flatMap(
Expand Down
12 changes: 10 additions & 2 deletions src/webapp/pages/dashboard/DashboardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@ export const DashboardPage: React.FC = React.memo(() => {
useEffect(() => {
compositionRoot.diseaseOutbreakEvent.get.execute("daFY6ofh6CU").run(
diseaseOutbreakEvent => {
console.debug("Disease outbreak event", JSON.stringify(diseaseOutbreakEvent));
console.debug(diseaseOutbreakEvent);
},
err => {
console.error("Error fetching disease outbreak event", err);
}
);
}, [compositionRoot.diseaseOutbreakEvent.get]);

compositionRoot.diseaseOutbreakEvent.getAll.execute().run(
diseaseOutbreakEvents => {
console.debug(diseaseOutbreakEvents);
},
err => {
console.error("Error fetching disease outbreak events", err);
}
);
}, [compositionRoot.diseaseOutbreakEvent.get, compositionRoot.diseaseOutbreakEvent.getAll]);
return (
<Layout title={i18n.t("Dashboard")} showCreateEvent>
<Section title={i18n.t("Respond, alert, watch")}>Respond, alert, watch content</Section>
Expand Down
Loading

0 comments on commit 86ff4c6

Please sign in to comment.