Skip to content

Commit

Permalink
refactor: fetch option sets on app load
Browse files Browse the repository at this point in the history
  • Loading branch information
9sneha-n committed Oct 26, 2024
1 parent e5980d4 commit 9040305
Show file tree
Hide file tree
Showing 32 changed files with 1,062 additions and 1,129 deletions.
25 changes: 17 additions & 8 deletions src/CompositionRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import { UserRepository } from "./domain/repositories/UserRepository";
import { GetCurrentUserUseCase } from "./domain/usecases/GetCurrentUserUseCase";
import { GetDiseaseOutbreakByIdUseCase } from "./domain/usecases/GetDiseaseOutbreakByIdUseCase";
import { D2Api } from "./types/d2-api";
import { OptionsRepository } from "./domain/repositories/OptionsRepository";

import { TeamMemberRepository } from "./domain/repositories/TeamMemberRepository";
import { OrgUnitRepository } from "./domain/repositories/OrgUnitRepository";
import { OptionsD2Repository } from "./data/repositories/OptionsD2Repository";

import { TeamMemberD2Repository } from "./data/repositories/TeamMemberD2Repository";
import { OrgUnitD2Repository } from "./data/repositories/OrgUnitD2Repository";
import { AlertD2Repository } from "./data/repositories/AlertD2Repository";
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";
Expand Down Expand Up @@ -64,6 +63,10 @@ import { SystemRepository } from "./domain/repositories/SystemRepository";
import { SystemD2Repository } from "./data/repositories/SystemD2Repository";
import { SystemTestRepository } from "./data/repositories/test/SystemTestRepository";
import { GetOverviewCardsUseCase } from "./domain/usecases/GetOverviewCardsUseCase";
import { GetAllAppConfigurationsUseCase } from "./domain/usecases/GetAllAppConfigurationsUseCase";
import { AppConfigurationRepository } from "./domain/repositories/AppConfigurationRepository";
import { AppConfigurationD2Repository } from "./data/repositories/AppConfigurationD2Repository";
import { AppConfigurationTestRepository } from "./data/repositories/test/AppConfigurationTestRepository";

export type CompositionRoot = ReturnType<typeof getCompositionRoot>;

Expand All @@ -72,7 +75,7 @@ type Repositories = {
diseaseOutbreakEventRepository: DiseaseOutbreakEventRepository;
alertRepository: AlertRepository;
alertSyncRepository: AlertSyncRepository;
optionsRepository: OptionsRepository;

teamMemberRepository: TeamMemberRepository;
orgUnitRepository: OrgUnitRepository;
riskAssessmentRepository: RiskAssessmentRepository;
Expand All @@ -83,6 +86,7 @@ type Repositories = {
incidentManagementTeamRepository: IncidentManagementTeamRepository;
chartConfigRepository: ChartConfigRepository;
systemRepository: SystemRepository;
eventTrackerConfigurationRespository: AppConfigurationRepository;
};

function getCompositionRoot(repositories: Repositories) {
Expand All @@ -97,8 +101,11 @@ function getCompositionRoot(repositories: Repositories) {
getAll: new GetAllDiseaseOutbreaksUseCase(repositories.diseaseOutbreakEventRepository),
mapDiseaseOutbreakEventToAlerts: new MapDiseaseOutbreakToAlertsUseCase(
repositories.alertRepository,
repositories.alertSyncRepository,
repositories.optionsRepository
repositories.alertSyncRepository
),
getConfigurations: new GetAllAppConfigurationsUseCase(
repositories.eventTrackerConfigurationRespository,
repositories.teamMemberRepository
),
},
incidentActionPlan: {
Expand Down Expand Up @@ -141,7 +148,7 @@ export function getWebappCompositionRoot(api: D2Api) {
diseaseOutbreakEventRepository: new DiseaseOutbreakEventD2Repository(api),
alertRepository: new AlertD2Repository(api),
alertSyncRepository: new AlertSyncDataStoreRepository(api),
optionsRepository: new OptionsD2Repository(api),

teamMemberRepository: new TeamMemberD2Repository(api),
orgUnitRepository: new OrgUnitD2Repository(api),
riskAssessmentRepository: new RiskAssessmentD2Repository(api),
Expand All @@ -152,6 +159,7 @@ export function getWebappCompositionRoot(api: D2Api) {
incidentManagementTeamRepository: new IncidentManagementTeamD2Repository(api),
chartConfigRepository: new ChartConfigD2Repository(dataStoreClient),
systemRepository: new SystemD2Repository(api),
eventTrackerConfigurationRespository: new AppConfigurationD2Repository(api),
};

return getCompositionRoot(repositories);
Expand All @@ -163,7 +171,7 @@ export function getTestCompositionRoot() {
diseaseOutbreakEventRepository: new DiseaseOutbreakEventTestRepository(),
alertRepository: new AlertTestRepository(),
alertSyncRepository: new AlertSyncDataStoreTestRepository(),
optionsRepository: new OptionsTestRepository(),

teamMemberRepository: new TeamMemberTestRepository(),
orgUnitRepository: new OrgUnitTestRepository(),
riskAssessmentRepository: new RiskAssessmentTestRepository(),
Expand All @@ -174,6 +182,7 @@ export function getTestCompositionRoot() {
incidentManagementTeamRepository: new IncidentManagementTeamTestRepository(),
chartConfigRepository: new ChartConfigTestRepository(),
systemRepository: new SystemTestRepository(),
eventTrackerConfigurationRespository: new AppConfigurationTestRepository(),
};

return getCompositionRoot(repositories);
Expand Down
234 changes: 234 additions & 0 deletions src/data/repositories/AppConfigurationD2Repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
import { D2Api, MetadataPick } from "@eyeseetea/d2-api/2.36";
import { AppConfigurationRepository } from "../../domain/repositories/AppConfigurationRepository";
import { Option } from "../../domain/entities/Ref";
import { apiToFuture, FutureData } from "../api-futures";

import _ from "../../domain/entities/generic/Collection";
import { getHazardTypeByCode } from "./consts/DiseaseOutbreakConstants";

import { Future } from "../../domain/entities/generic/Future";
import { AppConfigurations } from "../../domain/entities/AppConfigurations";
import { RiskAssessmentGrading } from "../../domain/entities/risk-assessment/RiskAssessmentGrading";
const MAIN_SYNDROME_OPTION_SET_CODE = "AGENTS";
export const SUSPECTED_DISEASE_OPTION_SET_CODE = "RTSL_ZEB_OS_DISEASE";
export const NOTIFICATION_SOURCE_OPTION_SET_CODE = "RTSL_ZEB_OS_SOURCE";
const optionSetCode = {
dataSources: "RTSL_ZEB_OS_DATA_SOURCE",
hazardTypes: "RTSL_ZEB_OS_HAZARD_TYPE",
hazardTypesByCode: "RTSL_ZEB_OS_HAZARD_TYPE",
mainSyndromes: "AGENTS",
suspectedDiseases: SUSPECTED_DISEASE_OPTION_SET_CODE,
notificationSources: NOTIFICATION_SOURCE_OPTION_SET_CODE,
incidentStatus: "RTSL_ZEB_OS_INCIDENT_STATUS",
populationAtRisk: "RTSL_ZEB_OS_POPULATION_AT_RISK",
lowMediumHigh: "RTSL_ZEB_OS_LOW_MEDIUM_HIGH",
geographicalSpread: "RTSL_ZEB_OS_GEOGRAPHICAL_SPREAD",
capability: "RTSL_ZEB_OS_CAPABILITY",
capacity: "RTSL_ZEB_OS_CAPACITY",
};
export const optionSetsFields = {
name: true,
code: true,
options: { id: true, name: true, code: true },
} as const;

export type D2OptionSet = MetadataPick<{
optionSets: { fields: typeof optionSetsFields };
}>["optionSets"][number];

export class AppConfigurationD2Repository implements AppConfigurationRepository {
constructor(private api: D2Api) {}

getAppConfigurations(): FutureData<AppConfigurations> {
return apiToFuture(
this.api.metadata.get({
optionSets: { fields: optionSetsFields },
})
).flatMap(response => {
const appConfig: AppConfigurations = {
eventTrackerConfigurations: {
dataSources: [],
hazardTypes: [],
mainSyndromes: [],
suspectedDiseases: [],
notificationSources: [],
incidentStatus: [],
incidentManagers: [],
},
riskAssessmentGradingConfigurations: {
populationAtRisk: [],
lowMediumHigh: [],
geographicalSpread: [],
capability: [],
capacity: [],
},
riskAssessmentSummaryConfigurations: {
overAllConfidencGlobal: [],
overAllConfidencNational: [],
overAllConfidencRegional: [],
overallRiskGlobal: [],
overallRiskNational: [],
overallRiskRegional: [],
riskAssessors: [],
},
riskAssessmentQuestionnaireConfigurations: {
consequences: [],
likelihood: [],
risk: [],
},
incidentActionPlanConfigurations: {
iapType: [],
phoecLevel: [],
},
incidentResponseActionConfigurations: {
searchAssignRO: [],
status: [],
verification: [],
},
};

Object.entries(optionSetCode).map(([key, value]) => {
if (key === "dataSources") {
const dataSources = response.optionSets.find(
optionSet => optionSet.code === value
);
if (dataSources)
appConfig.eventTrackerConfigurations.dataSources =
this.mapD2OptionSetToOptions(dataSources);
} else if (key === "hazardTypes") {
const hazardTypes = response.optionSets.find(
optionSet => optionSet.code === value
);
if (hazardTypes) {
const hazardOptions = this.mapD2OptionSetToOptions(hazardTypes);
// appConfig.eventTrackerConfigurations.hazardTypesByCode =
// this.mapD2OptionSetToOptions(hazardTypes);
appConfig.eventTrackerConfigurations.hazardTypes =
this.getHazardTypes(hazardOptions);
}
} else if (key === "mainSyndromes") {
const mainSyndromes = response.optionSets.find(
optionSet => optionSet.code === value
);
if (mainSyndromes)
appConfig.eventTrackerConfigurations.mainSyndromes =
this.mapD2OptionSetToOptions(mainSyndromes);
} else if (key === "suspectedDiseases") {
const suspectedDiseases = response.optionSets.find(
optionSet => optionSet.code === value
);
if (suspectedDiseases)
appConfig.eventTrackerConfigurations.suspectedDiseases =
this.mapD2OptionSetToOptions(suspectedDiseases);
} else if (key === "notificationSources") {
const notificationSources = response.optionSets.find(
optionSet => optionSet.code === value
);
if (notificationSources)
appConfig.eventTrackerConfigurations.notificationSources =
this.mapD2OptionSetToOptions(notificationSources);
} else if (key === "incidentStatus") {
const incidentStatus = response.optionSets.find(
optionSet => optionSet.code === value
);
if (incidentStatus)
appConfig.eventTrackerConfigurations.incidentStatus =
this.mapD2OptionSetToOptions(incidentStatus);
} else if (key === "populationAtRisk") {
const populationAtRisk = response.optionSets.find(
optionSet => optionSet.code === value
);
if (populationAtRisk)
appConfig.riskAssessmentGradingConfigurations.populationAtRisk =
populationAtRisk.options.map(populationAtRisk => {
return RiskAssessmentGrading.getOptionTypeByCodePopulation(
populationAtRisk.code
);
});
} else if (key === "lowMediumHigh") {
const lowMediumHighOptions = response.optionSets.find(
optionSet => optionSet.code === value
);
if (lowMediumHighOptions) {
appConfig.riskAssessmentGradingConfigurations.lowMediumHigh =
lowMediumHighOptions.options.map(lowMediumHigh => {
return RiskAssessmentGrading.getOptionTypeByCodeWeighted(
lowMediumHigh.code
);
});
appConfig.riskAssessmentSummaryConfigurations.overallRiskGlobal =
this.mapD2OptionSetToOptions(lowMediumHighOptions);
appConfig.riskAssessmentSummaryConfigurations.overallRiskNational =
this.mapD2OptionSetToOptions(lowMediumHighOptions);
appConfig.riskAssessmentSummaryConfigurations.overallRiskRegional =
this.mapD2OptionSetToOptions(lowMediumHighOptions);
appConfig.riskAssessmentSummaryConfigurations.overAllConfidencGlobal =
this.mapD2OptionSetToOptions(lowMediumHighOptions);
appConfig.riskAssessmentSummaryConfigurations.overAllConfidencNational =
this.mapD2OptionSetToOptions(lowMediumHighOptions);
appConfig.riskAssessmentSummaryConfigurations.overAllConfidencRegional =
this.mapD2OptionSetToOptions(lowMediumHighOptions);
}
} else if (key === "geographicalSpread") {
const geographicalSpreadOptions = response.optionSets.find(
optionSet => optionSet.code === value
);
if (geographicalSpreadOptions)
appConfig.riskAssessmentGradingConfigurations.geographicalSpread =
geographicalSpreadOptions.options.map(geographicalSpread => {
return RiskAssessmentGrading.getOptionTypeByCodeGeographicalSpread(
geographicalSpread.code
);
});
} else if (key === "capability") {
const capabilityOptions = response.optionSets.find(
optionSet => optionSet.code === value
);
if (capabilityOptions)
appConfig.riskAssessmentGradingConfigurations.capability =
capabilityOptions.options.map(capability => {
return RiskAssessmentGrading.getOptionTypeByCodeCapability(
capability.code
);
});
} else if (key === "capacity") {
const capacityOptions = response.optionSets.find(
optionSet => optionSet.code === value
);
if (capacityOptions)
appConfig.riskAssessmentGradingConfigurations.capacity =
capacityOptions.options.map(capacity => {
return RiskAssessmentGrading.getOptionTypeByCodeCapacity(
capacity.code
);
});
}
});

return Future.success(appConfig);
});
}

mapD2OptionSetToOptions(optionSet: D2OptionSet): Option[] {
return optionSet.options.map(
(option): Option => ({
id: option.code,
name: option.name,
})
);
}

getHazardTypes(hazardTypesByCode: Option[]): Option[] {
return _(hazardTypesByCode)
.compactMap(hazardType => {
const hazardTypeId = getHazardTypeByCode(hazardType.id);
if (hazardTypeId) {
return {
id: hazardTypeId,
name: hazardType.name,
};
}
})
.toArray();
}
}
Loading

0 comments on commit 9040305

Please sign in to comment.