diff --git a/src/CompositionRoot.ts b/src/CompositionRoot.ts index 488d880c..5906e733 100644 --- a/src/CompositionRoot.ts +++ b/src/CompositionRoot.ts @@ -20,7 +20,7 @@ import { OrgUnitTestRepository } from "./data/repositories/test/OrgUnitTestRepos import { GetAllDiseaseOutbreaksUseCase } from "./domain/usecases/GetAllDiseaseOutbreaksUseCase"; import { SaveDiseaseOutbreakUseCase } from "./domain/usecases/SaveDiseaseOutbreakUseCase"; import { GetDiseaseOutbreakWithOptionsUseCase } from "./domain/usecases/GetDiseaseOutbreakWithOptionsUseCase"; -import { MapDiseaseOutbreakToAlertUseCase } from "./domain/usecases/MapDiseaseOutbreakToAlertsUseCase"; +import { MapDiseaseOutbreakToAlertsUseCase } from "./domain/usecases/MapDiseaseOutbreakToAlertsUseCase"; import { AlertRepository } from "./domain/repositories/AlertRepository"; import { AlertTestRepository } from "./data/repositories/test/AlertTestRepository"; @@ -45,7 +45,7 @@ function getCompositionRoot(repositories: Repositories) { getWithOptions: new GetDiseaseOutbreakWithOptionsUseCase(repositories), getAll: new GetAllDiseaseOutbreaksUseCase(repositories.diseaseOutbreakEventRepository), save: new SaveDiseaseOutbreakUseCase(repositories.diseaseOutbreakEventRepository), - mapDiseaseOutbreakEventToAlert: new MapDiseaseOutbreakToAlertUseCase( + mapDiseaseOutbreakEventToAlerts: new MapDiseaseOutbreakToAlertsUseCase( repositories.alertRepository ), }, diff --git a/src/data/repositories/AlertD2Repository.ts b/src/data/repositories/AlertD2Repository.ts index f0268403..3cd85930 100644 --- a/src/data/repositories/AlertD2Repository.ts +++ b/src/data/repositories/AlertD2Repository.ts @@ -1,7 +1,7 @@ import { D2Api } from "@eyeseetea/d2-api/2.36"; import { apiToFuture, FutureData } from "../api-futures"; import { - RTSL_ZEBRA_ALERTS_EVENT_ID_TEA_ID, + RTSL_ZEBRA_ALERTS_NATIONAL_DISEASE_OUTBREAK_EVENT_ID_TEA_ID, RTSL_ZEBRA_ALERTS_PROGRAM_ID, RTSL_ZEBRA_ORG_UNIT_ID, } from "./consts/DiseaseOutbreakConstants"; @@ -21,22 +21,22 @@ export class AlertD2Repository implements AlertRepository { const { eventId, filter } = alertOptions; return this.getTrackedEntitiesByTEACode(filter).flatMap(response => { - const trackedEntitiesToPost = response.map(trackedEntity => ({ + const alertsToMap = response.map(trackedEntity => ({ ...trackedEntity, attributes: [ { - attribute: RTSL_ZEBRA_ALERTS_EVENT_ID_TEA_ID, + attribute: RTSL_ZEBRA_ALERTS_NATIONAL_DISEASE_OUTBREAK_EVENT_ID_TEA_ID, value: eventId, }, ], })); - if (trackedEntitiesToPost.length === 0) return Future.success(undefined); + if (alertsToMap.length === 0) return Future.success(undefined); return apiToFuture( this.api.tracker.post( { importStrategy: "UPDATE" }, - { trackedEntities: trackedEntitiesToPost } + { trackedEntities: alertsToMap } ) ).map(saveResponse => { if (saveResponse.status === "ERROR") @@ -45,7 +45,10 @@ export class AlertD2Repository implements AlertRepository { }); } - private async getTrackedEntitiesByTEACodeAsync(filter: { id: Id; value: string }) { + private async getTrackedEntitiesByTEACodeAsync(filter: { + id: Id; + value: string; + }): Promise { const d2TrackerTrackedEntities: D2TrackerTrackedEntity[] = []; const pageSize = 250; @@ -72,10 +75,6 @@ export class AlertD2Repository implements AlertRepository { }) .getData(); - if (!result.total) { - throw new Error("No tracked entities found"); - } - d2TrackerTrackedEntities.push(...result.instances); page++; @@ -86,7 +85,10 @@ export class AlertD2Repository implements AlertRepository { } } - private getTrackedEntitiesByTEACode(filter: { id: Id; value: string }) { + private getTrackedEntitiesByTEACode(filter: { + id: Id; + value: string; + }): FutureData { return Future.fromPromise(this.getTrackedEntitiesByTEACodeAsync(filter)); } } diff --git a/src/data/repositories/consts/DiseaseOutbreakConstants.ts b/src/data/repositories/consts/DiseaseOutbreakConstants.ts index 54add55c..e7d560d2 100644 --- a/src/data/repositories/consts/DiseaseOutbreakConstants.ts +++ b/src/data/repositories/consts/DiseaseOutbreakConstants.ts @@ -10,7 +10,7 @@ export const RTSL_ZEBRA_ORG_UNIT_ID = "PS5JpkoHHio"; export const RTSL_ZEBRA_TRACKED_ENTITY_TYPE_ID = "lIzNjLOUAKA"; export const RTSL_ZEBRA_ALERTS_PROGRAM_ID = "MQtbs8UkBxy"; -export const RTSL_ZEBRA_ALERTS_EVENT_ID_TEA_ID = "Pq1drzz2HJk"; +export const RTSL_ZEBRA_ALERTS_NATIONAL_DISEASE_OUTBREAK_EVENT_ID_TEA_ID = "Pq1drzz2HJk"; export const RTSL_ZEBRA_ALERTS_DISEASE_TEA_ID = "agsVaIpit4S"; export const RTSL_ZEBRA_ALERTS_EVENT_TYPE_TEA_ID = "ydsfY6zyvt7"; diff --git a/src/domain/usecases/MapDiseaseOutbreakToAlertsUseCase.ts b/src/domain/usecases/MapDiseaseOutbreakToAlertsUseCase.ts index 0f523281..aa48ba20 100644 --- a/src/domain/usecases/MapDiseaseOutbreakToAlertsUseCase.ts +++ b/src/domain/usecases/MapDiseaseOutbreakToAlertsUseCase.ts @@ -10,7 +10,9 @@ import { RTSL_ZEBRA_ALERTS_EVENT_TYPE_TEA_ID, } from "../../data/repositories/consts/DiseaseOutbreakConstants"; -export class MapDiseaseOutbreakToAlertUseCase { +const incidentDataSourceCode = "IBS"; + +export class MapDiseaseOutbreakToAlertsUseCase { constructor(private alertRepository: AlertRepository) {} public execute( @@ -24,7 +26,7 @@ export class MapDiseaseOutbreakToAlertUseCase { return Future.error(new Error("Disease Outbreak Event Id is required")); const filter = - dataSourceCode === "IBS" + dataSourceCode === incidentDataSourceCode ? { id: RTSL_ZEBRA_ALERTS_DISEASE_TEA_ID, value: suspectedDiseaseCode } : { id: RTSL_ZEBRA_ALERTS_EVENT_TYPE_TEA_ID, value: hazardTypeCode }; diff --git a/src/webapp/pages/form-page/disease-outbreak-event/useDiseaseOutbreakEventForm.ts b/src/webapp/pages/form-page/disease-outbreak-event/useDiseaseOutbreakEventForm.ts index 9191e018..c5c2d05b 100644 --- a/src/webapp/pages/form-page/disease-outbreak-event/useDiseaseOutbreakEventForm.ts +++ b/src/webapp/pages/form-page/disease-outbreak-event/useDiseaseOutbreakEventForm.ts @@ -131,7 +131,7 @@ export function useDiseaseOutbreakEventForm(diseaseOutbreakEventId?: Id): State diseaseOutbreakEventId => { setIsSaved(true); - compositionRoot.diseaseOutbreakEvent.mapDiseaseOutbreakEventToAlert + compositionRoot.diseaseOutbreakEvent.mapDiseaseOutbreakEventToAlerts .execute(diseaseOutbreakEventId, diseaseOutbreakEventData) .run( () => { @@ -157,13 +157,7 @@ export function useDiseaseOutbreakEventForm(diseaseOutbreakEventId?: Id): State setIsSaved(false); } ); - }, [ - compositionRoot.diseaseOutbreakEvent.mapDiseaseOutbreakEventToAlert, - compositionRoot.diseaseOutbreakEvent.save, - currentUser.username, - diseaseOutbreakEventWithOptions, - formState, - ]); + }, [compositionRoot, currentUser.username, diseaseOutbreakEventWithOptions, formState]); const onCancelForm = useCallback(() => { goTo(RouteName.DASHBOARD);