Skip to content

Commit

Permalink
refactor: some code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
9sneha-n committed Jul 22, 2024
1 parent ccf94da commit 9da61de
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 168 deletions.
14 changes: 6 additions & 8 deletions src/data/repositories/DiseaseOutbreakEventD2Repository.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { D2Api } from "@eyeseetea/d2-api/2.36";
import { DiseaseOutbreakEventRepository } from "../../domain/repositories/DiseaseOutbreakEventRepository";
import { FutureData } from "../api-futures";
import { DiseaseOutbreakEvent } from "../../domain/entities/disease-outbreak-event/DiseaseOutbreakEvent";
import {
DiseaseOutbreakEvent,
DiseaseOutbreakEventBaseAttrs,
} from "../../domain/entities/disease-outbreak-event/DiseaseOutbreakEvent";
import { Id, ConfigLabel } from "../../domain/entities/Ref";
import { getTrackerEntityAttributes } from "./utils/getTrackerEntityAttributes";

const RTSL_ZEBRA_PROGRAM_ID = "qkOTdxkte8V";
const RTSL_ZEBRA_ORG_UNIT_ID = "PS5JpkoHHio";
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<DiseaseOutbreakEvent> {
get(id: Id): FutureData<DiseaseOutbreakEventBaseAttrs> {
return getTrackerEntityAttributes(
this.api,
RTSL_ZEBRA_PROGRAM_ID,
Expand All @@ -28,9 +29,6 @@ export class DiseaseOutbreakEventD2Repository implements DiseaseOutbreakEventRep
delete(_id: Id): FutureData<void> {
throw new Error("Method not implemented.");
}
// getOptions(): FutureData<DiseaseOutbreakEventOption[]> {
// throw new Error("Method not implemented.");
// }
getConfigStrings(): FutureData<ConfigLabel[]> {
throw new Error("Method not implemented.");
}
Expand Down
7 changes: 6 additions & 1 deletion src/data/repositories/OptionsD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ import { D2Api } from "@eyeseetea/d2-api/2.36";
import { Id, NamedRef } from "../../domain/entities/Ref";
import { apiToFuture, FutureData } from "../api-futures";
import { OptionsRepository } from "../../domain/repositories/OptionsRepository";
import { Future } from "../../domain/entities/generic/Future";

export class OptionsD2Repository implements OptionsRepository {
constructor(private api: D2Api) {}

get(id: Id): FutureData<NamedRef> {
if (!id) return Future.success({ id: "", name: "" });
return apiToFuture(
this.api.metadata.get({
options: { fields: { id: true, name: true }, filter: { id: { eq: id } } },
})
).map(response => {
if (!response.options[0]) throw new Error("Option not found");
const option: NamedRef = { id: response.options[0].id, name: response.options[0].name };
const option: NamedRef = {
id: response.options[0].id,
name: response.options[0].name,
};
return option;
});
}
Expand Down
31 changes: 31 additions & 0 deletions src/data/repositories/consts/DiseaseOutbreakConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const RTSL_ZEBRA_PROGRAM_ID = "qkOTdxkte8V";
export const RTSL_ZEBRA_ORG_UNIT_ID = "PS5JpkoHHio";

export const DiseaseOutbreakCodes = {
name: "RTSL_ZEB_TEA_EVENT_NAME",
hazardType: "RTSL_ZEB_TEA_HAZARD_TYPE",
mainSyndrome: "RTSL_ZEB_TEA_MAIN_SYNDROME",
suspectedDisease: "RTSL_ZEB_TEA_SUSPECTED_DISEASE",
notificationSource: "RTSL_ZEB_TEA_NOTIFICATION_SOURCE",
areasAffectedProvinces: "RTSL_ZEB_TEA_AREAS_AFFECTED_PROVINCES",
areasAffectedDistricts: "RTSL_ZEB_TEA_AREAS_AFFECTED_DISTRICTS",
incidentStatus: "RTSL_ZEB_TEA_INCIDENT_STATUS",
emergedDate: "RTSL_ZEB_TEA_DATE_EMERGED",
emergedNarrative: "RTSL_ZEB_TEA_DATE_EMERGED_NARRATIVE",
detectedDate: "RTSL_ZEB_TEA_DATE_DETECTED",
detectedNarrative: "RTSL_ZEB_TEA_DATE_DETECTED_NARRATIVE",
notifiedDate: "RTSL_ZEB_TEA_DATE_NOTIFIED",
notifiedNarrative: "RTSL_ZEB_TEA_DATE_NOTIFIED_NARRATIVE",
initiateInvestigation: "RTSL_ZEB_TEA_INITIATE_INVESTIGATION",
conductEpidemiologicalAnalysis: "RTSL_ZEB_TEA_CONDUCT_EPIDEMIOLOGICAL_ANALYSIS",
laboratoryConfirmationNA: "RTSL_ZEB_TEA_LABORATORY_CONFIRMATION",
laboratoryConfirmationDate: "RTSL_ZEB_TEA_SPECIFY_DATE1",
appropriateCaseManagementNA: "RTSL_ZEB_TEA_APPROPRIATE_CASE_MANAGEMENT",
appropriateCaseManagementDate: "RTSL_ZEB_TEA_SPECIFY_DATE2",
initiateRiskCommunicationNA: "RTSL_ZEB_TEA_APPROPRIATE_RISK_COMMUNICATION",
initiateRiskCommunicationDate: "RTSL_ZEB_TEA_SPECIFY_DATE4",
establishCoordination: "RTSL_ZEB_TEA_ESTABLISH_COORDINATION_MECHANISM",
responseNarrative: "RTSL_ZEB_TEA_RESPONSE_NARRATIVE",
incidentManager: "RTSL_ZEB_TEA_ASSIGN_INCIDENT_MANAGER",
notes: "RTSL_ZEB_TEA_NOTES",
} as const;
82 changes: 34 additions & 48 deletions src/data/repositories/test/DiseaseOutbreakEventTestRepository.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,43 @@
import { DiseaseOutbreakEvent } from "../../../domain/entities/disease-outbreak-event/DiseaseOutbreakEvent";
import {
DiseaseOutbreakEvent,
DiseaseOutbreakEventBaseAttrs,
} from "../../../domain/entities/disease-outbreak-event/DiseaseOutbreakEvent";
import { Future } from "../../../domain/entities/generic/Future";
import { TeamMember } from "../../../domain/entities/incident-management-team/TeamMember";
import { Id, ConfigLabel } from "../../../domain/entities/Ref";
import { DiseaseOutbreakEventRepository } from "../../../domain/repositories/DiseaseOutbreakEventRepository";
import { FutureData } from "../../api-futures";

export class DiseaseOutbreakEventTestRepository implements DiseaseOutbreakEventRepository {
get(id: Id): FutureData<DiseaseOutbreakEvent> {
return Future.success(
new DiseaseOutbreakEvent({
id: id,
name: "Disease Outbreak 1",
created: new Date(),
lastUpdated: new Date(),
createdBy: undefined,
hazardType: "Biological:Animal",
mainSyndrome: { id: "1", name: "Syndrome 1" },
suspectedDisease: { id: "1", name: "Suspected Disease 1" },
notificationSource: { id: "1", name: "Notification Source 1" },
areasAffectedProvinces: [],
areasAffectedDistricts: [],
incidentStatus: "Watch",
emerged: { date: new Date(), narrative: "emerged" },
detected: { date: new Date(), narrative: "detected" },
notified: { date: new Date(), narrative: "notified" },
earlyResponseActions: {
initiateInvestigation: new Date(),
conductEpidemiologicalAnalysis: new Date(),
laboratoryConfirmation: { date: new Date(), na: false },
appropriateCaseManagement: { date: new Date(), na: false },
initiatePublicHealthCounterMeasures: { date: new Date(), na: false },
initiateRiskCommunication: { date: new Date(), na: false },
establishCoordination: new Date(),
responseNarrative: "responseNarrative",
},
incidentManager: new TeamMember({
id: "1",
name: "incidentManager",
email: "",
phone: "",
role: { id: "1", name: "role" },
status: "Available",
photo: undefined,
}),
notes: undefined,
riskAssessments: [],
incidentActionPlan: undefined,
incidentManagementTeam: undefined,
})
);
get(id: Id): FutureData<DiseaseOutbreakEventBaseAttrs> {
return Future.success({
id: id,
name: "Disease Outbreak 1",
created: new Date(),
lastUpdated: new Date(),
createdByName: "createdByName",
hazardType: "Biological:Animal",
mainSyndromeId: "1",
suspectedDiseaseId: "1",
notificationSourceId: "1",
areasAffectedDistrictIds: [],
areasAffectedProvinceIds: [],
incidentStatus: "Watch",
emerged: { date: new Date(), narrative: "emerged" },
detected: { date: new Date(), narrative: "detected" },
notified: { date: new Date(), narrative: "notified" },
earlyResponseActions: {
initiateInvestigation: new Date(),
conductEpidemiologicalAnalysis: new Date(),
laboratoryConfirmation: { date: new Date(), na: false },
appropriateCaseManagement: { date: new Date(), na: false },
initiatePublicHealthCounterMeasures: { date: new Date(), na: false },
initiateRiskCommunication: { date: new Date(), na: false },
establishCoordination: new Date(),
responseNarrative: "responseNarrative",
},
incidentManagerName: "incidentManager",
notes: undefined,
});
}
getAll(): FutureData<DiseaseOutbreakEvent[]> {
throw new Error("Method not implemented.");
Expand All @@ -59,9 +48,6 @@ export class DiseaseOutbreakEventTestRepository implements DiseaseOutbreakEventR
delete(_id: Id): FutureData<void> {
throw new Error("Method not implemented.");
}
// getOptions(): FutureData<DiseaseOutbreakEventOption[]> {
// throw new Error("Method not implemented.");
// }
getConfigStrings(): FutureData<ConfigLabel[]> {
throw new Error("Method not implemented.");
}
Expand Down
93 changes: 17 additions & 76 deletions src/data/repositories/utils/getTrackerEntityAttributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,19 @@ import { D2Api } from "@eyeseetea/d2-api/2.36";
import { Id } from "../../../domain/entities/Ref";
import { apiToFuture, FutureData } from "../../api-futures";
import {
DiseaseOutbreakEvent,
DiseaseOutbreakEventBaseAttrs,
HazardType,
IncidentStatusType,
} from "../../../domain/entities/disease-outbreak-event/DiseaseOutbreakEvent";
import { D2TrackerTrackedEntity } from "@eyeseetea/d2-api/api/trackerTrackedEntities";
import { TeamMember } from "../../../domain/entities/incident-management-team/TeamMember";

const DiseaseOutbreakCodes = {
name: "RTSL_ZEB_TEA_EVENT_NAME",
hazardType: "RTSL_ZEB_TEA_HAZARD_TYPE",
mainSyndrome: "RTSL_ZEB_TEA_MAIN_SYNDROME",
suspectedDisease: "RTSL_ZEB_TEA_SUSPECTED_DISEASE",
notificationSource: "RTSL_ZEB_TEA_NOTIFICATION_SOURCE",
areasAffectedProvinces: "RTSL_ZEB_TEA_AREAS_AFFECTED_PROVINCES",
areasAffectedDistricts: "RTSL_ZEB_TEA_AREAS_AFFECTED_DISTRICTS",
incidentStatus: "RTSL_ZEB_TEA_INCIDENT_STATUS",
emergedDate: "RTSL_ZEB_TEA_DATE_EMERGED",
emergedNarrative: "RTSL_ZEB_TEA_DATE_EMERGED_NARRATIVE",
detectedDate: "RTSL_ZEB_TEA_DATE_DETECTED",
detectedNarrative: "RTSL_ZEB_TEA_DATE_DETECTED_NARRATIVE",
notifiedDate: "RTSL_ZEB_TEA_DATE_NOTIFIED",
notifiedNarrative: "RTSL_ZEB_TEA_DATE_NOTIFIED_NARRATIVE",
initiateInvestigation: "RTSL_ZEB_TEA_INITIATE_INVESTIGATION",
conductEpidemiologicalAnalysis: "RTSL_ZEB_TEA_CONDUCT_EPIDEMIOLOGICAL_ANALYSIS",
laboratoryConfirmationNA: "RTSL_ZEB_TEA_LABORATORY_CONFIRMATION",
laboratoryConfirmationDate: "RTSL_ZEB_TEA_SPECIFY_DATE1",
appropriateCaseManagementNA: "RTSL_ZEB_TEA_APPROPRIATE_CASE_MANAGEMENT",
appropriateCaseManagementDate: "RTSL_ZEB_TEA_SPECIFY_DATE2",
initiateRiskCommunicationNA: "RTSL_ZEB_TEA_APPROPRIATE_RISK_COMMUNICATION",
initiateRiskCommunicationDate: "RTSL_ZEB_TEA_SPECIFY_DATE4",
establishCoordination: "RTSL_ZEB_TEA_ESTABLISH_COORDINATION_MECHANISM",
responseNarrative: "RTSL_ZEB_TEA_RESPONSE_NARRATIVE",
incidentManager: "RTSL_ZEB_TEA_ASSIGN_INCIDENT_MANAGER",
notes: "RTSL_ZEB_TEA_NOTES",
} as const;
import { DiseaseOutbreakCodes } from "../consts/DiseaseOutbreakConstants";

export function getTrackerEntityAttributes(
api: D2Api,
programId: Id,
orgUnitId: Id,
trackedEntityId: Id
): FutureData<DiseaseOutbreakEvent> {
): FutureData<DiseaseOutbreakEventBaseAttrs> {
return apiToFuture(
api.tracker.trackedEntities.get({
program: programId,
Expand All @@ -59,44 +30,24 @@ export function getTrackerEntityAttributes(

function mapTrackedEntityAttributesToDiseaseOutbreak(
trackedEntity: D2TrackerTrackedEntity
): DiseaseOutbreakEvent {
): DiseaseOutbreakEventBaseAttrs {
if (!trackedEntity.trackedEntity) throw new Error("Tracked entity not found");

const diseaseOutbreak: DiseaseOutbreakEvent = new DiseaseOutbreakEvent({
const diseaseOutbreak: DiseaseOutbreakEventBaseAttrs = {
id: trackedEntity.trackedEntity,
name: getValueFromMap("name", trackedEntity),
created: trackedEntity.createdAt ? new Date(trackedEntity.createdAt) : new Date(),
lastUpdated: trackedEntity.updatedAt ? new Date(trackedEntity.updatedAt) : new Date(),
createdBy: undefined,
createdByName: undefined,
hazardType: getValueFromMap("hazardType", trackedEntity) as HazardType,
mainSyndrome: {
id: getValueFromMap("mainSyndrome", trackedEntity),
name: "",
},
suspectedDisease: {
id: getValueFromMap("suspectedDisease", trackedEntity),
name: "",
},
notificationSource: {
id: getValueFromMap("notificationSource", trackedEntity),
name: "",
},
areasAffectedProvinces: [
//TO DO : handle multiple provinces when metadata change is done
{
id: getValueFromMap("areasAffectedProvinces", trackedEntity),
code: "",
name: "",
},
],
areasAffectedDistricts: [
//TO DO : handle multiple provinces when metadata change is done
{
id: getValueFromMap("areasAffectedDistricts", trackedEntity),
code: "",
name: "",
},
],
mainSyndromeId: getValueFromMap("mainSyndrome", trackedEntity),
suspectedDiseaseId: getValueFromMap("suspectedDisease", trackedEntity),

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
incidentStatus: getValueFromMap("incidentStatus", trackedEntity) as IncidentStatusType,
emerged: {
date: new Date(getValueFromMap("emergedDate", trackedEntity)),
Expand All @@ -110,15 +61,8 @@ function mapTrackedEntityAttributesToDiseaseOutbreak(
date: new Date(getValueFromMap("notifiedDate", trackedEntity)),
narrative: getValueFromMap("notifiedNarrative", trackedEntity),
},
incidentManager: new TeamMember({
id: getValueFromMap("incidentManager", trackedEntity),
name: "",
phone: undefined,
email: undefined,
status: undefined,
role: undefined,
photo: undefined,
}),
incidentManagerName: getValueFromMap("incidentManager", trackedEntity),

earlyResponseActions: {
initiateInvestigation: new Date(
getValueFromMap("initiateInvestigation", trackedEntity)
Expand Down Expand Up @@ -148,10 +92,7 @@ function mapTrackedEntityAttributesToDiseaseOutbreak(
responseNarrative: getValueFromMap("responseNarrative", trackedEntity),
},
notes: getValueFromMap("notes", trackedEntity),
riskAssessments: undefined,
incidentActionPlan: undefined,
incidentManagementTeam: undefined,
});
};

return diseaseOutbreak;
}
Expand Down
3 changes: 2 additions & 1 deletion src/domain/entities/OrgUnit.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Maybe } from "../../utils/ts-utils";
import { CodedNamedRef } from "./Ref";

type OrgUnitLevelType = "Province" | "District";

export type OrgUnit = CodedNamedRef & {
level?: OrgUnitLevelType;
level: Maybe<OrgUnitLevelType>;
};
29 changes: 20 additions & 9 deletions src/domain/entities/disease-outbreak-event/DiseaseOutbreakEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IncidentActionPlan } from "../incident-action-plan/IncidentActionPlan";
import { IncidentManagementTeam } from "../incident-management-team/IncidentManagementTeam";
import { TeamMember } from "../incident-management-team/TeamMember";
import { OrgUnit } from "../OrgUnit";
import { NamedRef } from "../Ref";
import { Id, NamedRef } from "../Ref";
import { RiskAssessment } from "../risk-assessment/RiskAssessment";
import { Maybe } from "../../../utils/ts-utils";

Expand Down Expand Up @@ -37,27 +37,38 @@ type EarlyResponseActions = {
responseNarrative: string;
};

type DiseaseOutbreakEventAttrs = NamedRef & {
export type DiseaseOutbreakEventBaseAttrs = NamedRef & {
created: Date;
lastUpdated: Date;
createdBy: Maybe<TeamMember>;
createdByName: Maybe<string>;
hazardType: HazardType;
mainSyndrome: NamedRef;
suspectedDisease: NamedRef;
notificationSource: NamedRef;
areasAffectedProvinces: OrgUnit[];
areasAffectedDistricts: OrgUnit[];
mainSyndromeId: Id;
suspectedDiseaseId: Id;
notificationSourceId: Id;
areasAffectedProvinceIds: Id[];
areasAffectedDistrictIds: Id[];
incidentStatus: IncidentStatusType;
emerged: DateWithNarrative;
detected: DateWithNarrative;
notified: DateWithNarrative;
earlyResponseActions: EarlyResponseActions; //TO DO : mandatory field
incidentManager: TeamMember;
incidentManagerName: string;
notes: Maybe<string>;
};

type DiseaseOutbreakEventAttrs = DiseaseOutbreakEventBaseAttrs & {
createdBy: Maybe<TeamMember>;
mainSyndrome: NamedRef;
suspectedDisease: NamedRef;
notificationSource: NamedRef;
areasAffectedProvinces: OrgUnit[];
areasAffectedDistricts: OrgUnit[];
incidentManager: TeamMember;
riskAssessments: Maybe<RiskAssessment[]>;
incidentActionPlan: Maybe<IncidentActionPlan>;
incidentManagementTeam: Maybe<IncidentManagementTeam>;
};

/**
* Note: DiseaseOutbreakEvent represents Event in the Figma.
* Not using event as it is a keyword and can also be confused with dhis event
Expand Down
Loading

0 comments on commit 9da61de

Please sign in to comment.