Skip to content

Commit

Permalink
Save disease outbreak event without populate event id in the other pr…
Browse files Browse the repository at this point in the history
…ogram
  • Loading branch information
anagperal committed Aug 12, 2024
1 parent 2788c7e commit f6d2019
Show file tree
Hide file tree
Showing 17 changed files with 451 additions and 435 deletions.
7 changes: 1 addition & 6 deletions src/CompositionRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ function getCompositionRoot(repositories: Repositories) {
},
diseaseOutbreakEvent: {
get: new GetDiseaseOutbreakByIdUseCase(repositories),
getWithOptions: new GetDiseaseOutbreakWithOptionsUseCase(
repositories.diseaseOutbreakEventRepository,
repositories.optionsRepository,
repositories.teamMemberRepository,
repositories.orgUnitRepository
),
getWithOptions: new GetDiseaseOutbreakWithOptionsUseCase(repositories),
getAll: new GetAllDiseaseOutbreaksUseCase(repositories.diseaseOutbreakEventRepository),
save: new SaveDiseaseOutbreakUseCase(repositories.diseaseOutbreakEventRepository),
},
Expand Down
142 changes: 38 additions & 104 deletions src/data/repositories/OptionsD2Repository.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
import { D2OptionSetSchema, SelectedPick } from "@eyeseetea/d2-api/2.36";
import { CodedNamedRef } from "../../domain/entities/Ref";
import { D2Api } from "../../types/d2-api";
import { D2Api, MetadataPick } from "../../types/d2-api";
import { Code, Option } from "../../domain/entities/Ref";
import { apiToFuture, FutureData } from "../api-futures";
import { OptionsRepository } from "../../domain/repositories/OptionsRepository";
import { assertOrError } from "./utils/AssertOrError";
import { Future } from "../../domain/entities/generic/Future";

type D2OptionSet = SelectedPick<
D2OptionSetSchema,
{
id: true;
name: true;
code: true;
options: { id: true; name: true; code: true };
}
>;
import { getHazardTypeByCode } from "./consts/DiseaseOutbreakConstants";

export class OptionsD2Repository implements OptionsRepository {
constructor(private api: D2Api) {}
Expand All @@ -36,111 +24,57 @@ export class OptionsD2Repository implements OptionsRepository {
});
}

getAllHazardTypes(): FutureData<CodedNamedRef[]> {
return apiToFuture(
this.api.metadata.get({
optionSets: {
fields: {
id: true,
name: true,
code: true,
options: { id: true, name: true, code: true },
},
filter: { code: { eq: "RTSL_ZEB_OS_HAZARD_TYPE" } },
},
})
).flatMap(response => {
if (!response.optionSets[0]) throw new Error("Hazard Types options not found");

return Future.success(this.mapD2OptionSetToCodedNamedRefs(response.optionSets[0]));
getAllHazardTypes(): FutureData<Option[]> {
return this.getOptionSetByCode("RTSL_ZEB_OS_HAZARD_TYPE").map(hazardTypes => {
return hazardTypes.map(hazardType => ({
id: getHazardTypeByCode(hazardType.id),
name: hazardType.name,
}));
});
}

getAllMainSyndromes(): FutureData<CodedNamedRef[]> {
return apiToFuture(
this.api.metadata.get({
optionSets: {
fields: {
id: true,
name: true,
code: true,
options: { id: true, name: true, code: true },
},
filter: { code: { eq: "RTSL_ZEB_OS_SYNDROME" } },
},
})
).flatMap(response => {
if (!response.optionSets[0]) throw new Error("Main Syndromes options not found");
return Future.success(this.mapD2OptionSetToCodedNamedRefs(response.optionSets[0]));
});
getAllMainSyndromes(): FutureData<Option[]> {
return this.getOptionSetByCode("RTSL_ZEB_OS_SYNDROME");
}

getAllSuspectedDiseases(): FutureData<CodedNamedRef[]> {
return apiToFuture(
this.api.metadata.get({
optionSets: {
fields: {
id: true,
name: true,
code: true,
options: { id: true, name: true, code: true },
},
filter: { code: { eq: "RTSL_ZEB_OS_DISEASE" } },
},
})
).flatMap(response => {
if (!response.optionSets[0]) throw new Error("Suspected Diseases options not found");
getAllSuspectedDiseases(): FutureData<Option[]> {
return this.getOptionSetByCode("RTSL_ZEB_OS_DISEASE");
}

return Future.success(this.mapD2OptionSetToCodedNamedRefs(response.optionSets[0]));
});
getAllNotificationSources(): FutureData<Option[]> {
return this.getOptionSetByCode("RTSL_ZEB_OS_SOURCE");
}

getAllNotificationSources(): FutureData<CodedNamedRef[]> {
return apiToFuture(
this.api.metadata.get({
optionSets: {
fields: {
id: true,
name: true,
code: true,
options: { id: true, name: true, code: true },
},
filter: { code: { eq: "RTSL_ZEB_OS_SOURCE" } },
},
})
).flatMap(response => {
if (!response.optionSets[0]) throw new Error("Notification Sources options not found");
return Future.success(this.mapD2OptionSetToCodedNamedRefs(response.optionSets[0]));
});
getAllIncidentStatus(): FutureData<Option[]> {
return this.getOptionSetByCode("RTSL_ZEB_OS_INCIDENT_STATUS");
}

getAllIncidentStatus(): FutureData<CodedNamedRef[]> {
private getOptionSetByCode(code: string): FutureData<Option[]> {
return apiToFuture(
this.api.metadata.get({
optionSets: {
fields: {
id: true,
name: true,
code: true,
options: { id: true, name: true, code: true },
},
filter: { code: { eq: "RTSL_ZEB_OS_INCIDENT_STATUS" } },
},
optionSets: { fields: optionSetsFields, filter: { code: { eq: code } } },
})
).flatMap(response => {
if (!response.optionSets[0]) throw new Error("Incident Status options not found");

return Future.success(this.mapD2OptionSetToCodedNamedRefs(response.optionSets[0]));
});
)
.flatMap(response => assertOrError(response.optionSets[0], `OptionSet ${code}`))
.map(d2Option => this.mapD2OptionSetToOptions(d2Option));
}

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

const optionSetsFields = {
name: true,
code: true,
options: { id: true, name: true, code: true },
};

type D2OptionSet = MetadataPick<{
optionSets: { fields: typeof optionSetsFields };
}>["optionSets"][number];
57 changes: 19 additions & 38 deletions src/data/repositories/OrgUnitD2Repository.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,17 @@
import { D2OrganisationUnitSchema, SelectedPick } from "@eyeseetea/d2-api/2.36";
import { D2Api } from "../../types/d2-api";
import { D2Api, MetadataPick } from "../../types/d2-api";
import { OrgUnit } from "../../domain/entities/OrgUnit";
import { Id } from "../../domain/entities/Ref";
import { OrgUnitRepository } from "../../domain/repositories/OrgUnitRepository";
import { apiToFuture, FutureData } from "../api-futures";

type D2OrgUnit = SelectedPick<
D2OrganisationUnitSchema,
{
id: true;
name: true;
code: true;
level: true;
}
>;

export class OrgUnitD2Repository implements OrgUnitRepository {
constructor(private api: D2Api) {}

getAll(): FutureData<OrgUnit[]> {
return apiToFuture(
this.api.metadata.get({
organisationUnits: {
fields: {
id: true,
name: true,
code: true,
level: true,
},
fields: d2OrgUnitFields,
},
})
).map(response => {
Expand All @@ -42,37 +26,34 @@ export class OrgUnitD2Repository implements OrgUnitRepository {
return apiToFuture(
this.api.metadata.get({
organisationUnits: {
fields: {
id: true,
name: true,
code: true,
level: true,
},
fields: d2OrgUnitFields,
filter: { id: { in: ids } },
},
})
).map(response => {
const orgUnits: OrgUnit[] = response.organisationUnits.map((ou): OrgUnit => {
return {
id: ou.id,
name: ou.name,
code: ou.code,
level: ou.level === 2 ? "Province" : "District",
};
});

return orgUnits;
return this.mapD2OrgUnitsToOrgUnits(response.organisationUnits);
});
}

private mapD2OrgUnitsToOrgUnits(d2OrgUnit: D2OrgUnit[]): OrgUnit[] {
return d2OrgUnit.map(ou => {
return {
return d2OrgUnit.map(
(ou): OrgUnit => ({
id: ou.id,
name: ou.name,
code: ou.code,
level: ou.level === 2 ? "Province" : "District",
};
});
})
);
}
}

const d2OrgUnitFields = {
id: true,
name: true,
code: true,
level: true,
};

type D2OrgUnit = MetadataPick<{
organisationUnits: { fields: typeof d2OrgUnitFields };
}>["organisationUnits"][number];
42 changes: 15 additions & 27 deletions src/data/repositories/TeamMemberD2Repository.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,18 @@
import { D2Api, D2UserSchema } from "../../types/d2-api";
import { D2Api, MetadataPick } from "../../types/d2-api";
import { TeamMember } from "../../domain/entities/incident-management-team/TeamMember";
import { Id } from "../../domain/entities/Ref";
import { TeamMemberRepository } from "../../domain/repositories/TeamMemberRepository";
import { apiToFuture, FutureData } from "../api-futures";
import { SelectedPick } from "@eyeseetea/d2-api/api";
import { assertOrError } from "./utils/AssertOrError";

type D2User = SelectedPick<
D2UserSchema,
{
id: true;
name: true;
username: true;
email: true;
phoneNumber: true;
}
>;

export class TeamMemberD2Repository implements TeamMemberRepository {
constructor(private api: D2Api) {}

getAll(): FutureData<TeamMember[]> {
return apiToFuture(
this.api.metadata.get({
users: {
fields: {
id: true,
name: true,
email: true,
phoneNumber: true,
username: true,
},
fields: d2UserFields,
},
})
).map(response => {
Expand All @@ -43,13 +25,7 @@ export class TeamMemberD2Repository implements TeamMemberRepository {
return apiToFuture(
this.api.metadata.get({
users: {
fields: {
id: true,
name: true,
email: true,
phoneNumber: true,
username: true,
},
fields: d2UserFields,
filter: { username: { eq: id } },
},
})
Expand All @@ -74,3 +50,15 @@ export class TeamMemberD2Repository implements TeamMemberRepository {
});
}
}

const d2UserFields = {
id: true,
name: true,
email: true,
phoneNumber: true,
username: true,
};

type _D2User = MetadataPick<{
users: { fields: typeof d2UserFields };
}>["users"][number];
Loading

0 comments on commit f6d2019

Please sign in to comment.