Skip to content

Commit

Permalink
feat: map alerts to incident status code
Browse files Browse the repository at this point in the history
  • Loading branch information
deeonwuli committed Aug 20, 2024
1 parent 612e12a commit 6da8ad3
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 28 deletions.
82 changes: 57 additions & 25 deletions src/data/repositories/AlertD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,45 @@ export class AlertD2Repository implements AlertRepository {
updateAlerts(alertOptions: AlertOptions): FutureData<void> {
const { eventId, filter, incidentStatus } = alertOptions;

return this.getTrackedEntitiesByTEACode(filter).flatMap(response => {
const alertsToMap = response.map(trackedEntity => ({
...trackedEntity,
attributes: [
{
attribute: RTSL_ZEBRA_ALERTS_NATIONAL_DISEASE_OUTBREAK_EVENT_ID_TEA_ID,
value: eventId,
},
{
attribute: RTSL_ZEBRA_ALERTS_NATIONAL_INCIDENT_STATUS_TEA_ID,
value: incidentStatus,
},
],
}));
return this.getTEAOptions(RTSL_ZEBRA_ALERTS_NATIONAL_INCIDENT_STATUS_TEA_ID).flatMap(
response => {
const options = response.objects[0]?.optionSet.options ?? [];
const incidentStatusCode = options.find(
option => option.name === incidentStatus
)?.code;

if (!incidentStatusCode) throw new Error("Incident status code not found");

if (alertsToMap.length === 0) return Future.success(undefined);
return this.getTrackedEntitiesByTEACode(filter).flatMap(response => {
const alertsToMap = response.map(trackedEntity => ({
...trackedEntity,
attributes: [
{
attribute:
RTSL_ZEBRA_ALERTS_NATIONAL_DISEASE_OUTBREAK_EVENT_ID_TEA_ID,
value: eventId,
},
{
attribute: RTSL_ZEBRA_ALERTS_NATIONAL_INCIDENT_STATUS_TEA_ID,
value: incidentStatusCode,
},
],
}));

return apiToFuture(
this.api.tracker.post(
{ importStrategy: "UPDATE" },
{ trackedEntities: alertsToMap }
)
).map(saveResponse => {
if (saveResponse.status === "ERROR")
throw new Error("Error mapping disease outbreak event id to alert");
});
});
if (alertsToMap.length === 0) return Future.success(undefined);

return apiToFuture(
this.api.tracker.post(
{ importStrategy: "UPDATE" },
{ trackedEntities: alertsToMap }
)
).map(saveResponse => {
if (saveResponse.status === "ERROR")
throw new Error("Error mapping disease outbreak event id to alert");
});
});
}
);
}

private async getTrackedEntitiesByTEACodeAsync(filter: {
Expand Down Expand Up @@ -97,4 +109,24 @@ export class AlertD2Repository implements AlertRepository {
}): FutureData<D2TrackerTrackedEntity[]> {
return Future.fromPromise(this.getTrackedEntitiesByTEACodeAsync(filter));
}

private getTEAOptions(trackedEntityAttributeId: Id) {
return apiToFuture(
this.api.models.trackedEntityAttributes.get({
fields: {
optionSet: {
options: {
code: true,
name: true,
},
},
},
filter: {
id: {
eq: trackedEntityAttributeId,
},
},
})
);
}
}
11 changes: 10 additions & 1 deletion src/data/repositories/consts/DiseaseOutbreakConstants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
DiseaseOutbreakEventBaseAttrs,
HazardType,
IncidentStatusType,
} from "../../../domain/entities/disease-outbreak-event/DiseaseOutbreakEvent";
import { GetValue, Maybe } from "../../../utils/ts-utils";
import { getDateAsIsoString } from "../utils/DateTimeHelper";
Expand All @@ -13,7 +14,7 @@ export const RTSL_ZEBRA_ALERTS_PROGRAM_ID = "MQtbs8UkBxy";
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";
export const RTSL_ZEBRA_ALERTS_NATIONAL_INCIDENT_STATUS_TEA_ID = "IpGLuK0W5y2";
export const RTSL_ZEBRA_ALERTS_NATIONAL_INCIDENT_STATUS_TEA_ID = "KeUbzfFQYCX";

export const hazardTypeCodeMap: Record<HazardType, string> = {
"Biological:Human": "BIOLOGICAL_HUMAN",
Expand All @@ -24,6 +25,14 @@ export const hazardTypeCodeMap: Record<HazardType, string> = {
Unknown: "UNKNOWN",
};

export const incidentStatusTypeMap: Record<string, IncidentStatusType> = {
WATCH: "Watch",
ALERT: "Alert",
RESPOND: "Respond",
CLOSED: "Closed",
DISCARDED: "Discarded",
};

export const diseaseOutbreakCodes = {
name: "RTSL_ZEB_TEA_EVENT_NAME",
dataSource: "RTSL_ZEB_TEA_DATA_SOURCE",
Expand Down
2 changes: 1 addition & 1 deletion src/domain/repositories/AlertRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ export interface AlertRepository {
export type AlertOptions = {
eventId: Id;
filter: { id: Id; value: Maybe<string> };
incidentStatus: IncidentStatusType;
incidentStatus: Maybe<IncidentStatusType>;
};
5 changes: 4 additions & 1 deletion src/domain/usecases/MapDiseaseOutbreakToAlertsUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DiseaseOutbreakEventBaseAttrs } from "../entities/disease-outbreak-even
import { Future } from "../entities/generic/Future";
import {
hazardTypeCodeMap,
incidentStatusTypeMap,
RTSL_ZEBRA_ALERTS_DISEASE_TEA_ID,
RTSL_ZEBRA_ALERTS_EVENT_TYPE_TEA_ID,
} from "../../data/repositories/consts/DiseaseOutbreakConstants";
Expand All @@ -26,7 +27,9 @@ export class MapDiseaseOutbreakToAlertsUseCase {
): FutureData<void> {
const { dataSource, hazardType, incidentStatus, suspectedDiseaseCode } =
diseaseOutbreakEventData;

const hazardTypeCode = hazardType ? hazardTypeCodeMap[hazardType] : undefined;
const incidentStatusTypeCode = incidentStatusTypeMap[incidentStatus];

if (!diseaseOutbreakEventId)
return Future.error(new Error("Disease Outbreak Event Id is required"));
Expand All @@ -39,7 +42,7 @@ export class MapDiseaseOutbreakToAlertsUseCase {
return this.alertRepository.updateAlerts({
eventId: diseaseOutbreakEventId,
filter: filter,
incidentStatus: incidentStatus,
incidentStatus: incidentStatusTypeCode,
});
}
}

0 comments on commit 6da8ad3

Please sign in to comment.