Skip to content

Commit

Permalink
add logic for step 7
Browse files Browse the repository at this point in the history
  • Loading branch information
eperedo committed Mar 14, 2024
1 parent f5190c5 commit 2811c2b
Show file tree
Hide file tree
Showing 10 changed files with 584 additions and 127 deletions.
14 changes: 14 additions & 0 deletions src/CompositionRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ import { DataValueTestRepository } from "./data/repositories/DataValueTestReposi
import { GetDisaggregationsUseCase } from "$/domain/usecases/GetDisaggregationsUseCase";
import { GetMissingDisaggregatesUseCase } from "./domain/usecases/GetMissingDisaggregatesUseCase";
import { GetCategoryDesaggregationsUseCase } from "$/domain/usecases/GetCategoryDesaggregationsUseCase";
import { ValidateMidwiferyAndPersonnelUseCase } from "./domain/usecases/ValidateMidwiferyAndPersonnelUseCase";
import { GetMidwiferyPersonnelDisaggregationsUseCase } from "$/domain/usecases/GetMidwiferyPersonnelDisaggregationsUseCase";

export type CompositionRoot = ReturnType<typeof getCompositionRoot>;

Expand Down Expand Up @@ -122,6 +124,18 @@ function getCompositionRoot(repositories: Repositories, metadata: MetadataItem)
},
issues: { save: new SaveIssueUseCase(repositories.qualityAnalysisRepository, metadata) },
settings: { get: new GetSettingsUseCase(repositories.settingsRepository) },
nursingMidwifery: {
getDisaggregations: new GetMidwiferyPersonnelDisaggregationsUseCase(
repositories.settingsRepository
),
validate: new ValidateMidwiferyAndPersonnelUseCase(
repositories.qualityAnalysisRepository,
repositories.issueRepository,
repositories.dataValueRepository,
repositories.moduleRepository,
repositories.settingsRepository
),
},
};
}

Expand Down
42 changes: 42 additions & 0 deletions src/domain/entities/MidwiferyPersonnel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Maybe } from "$/utils/ts-utils";
import { NamedRef } from "@eyeseetea/d2-logger/domain/entities/Base";
import { convertToNumberOrZero } from "../usecases/common/utils";
import { DataElement } from "./DataElement";
import { DataValue } from "./DataValue";
import { Struct } from "./generic/Struct";
import { Id, Period } from "./Ref";

type MidwiferyNursingAttrs = {
midwifery: MidwiferyNursingValue;
personnel: MidwiferyNursingValue;
isMidwiferyGreater: boolean;
period: Period;
countryId: Id;
combination: NamedRef;
};

type MidwiferyNursingValue = {
dataElement: DataElement;
dataValue: Maybe<DataValue>;
};

export class MidwiferyNursing extends Struct<MidwiferyNursingAttrs>() {
static build(data: MidwiferyNursingAttrs): MidwiferyNursing {
return this.create({
...data,
isMidwiferyGreater: this.checkIfMidwiferyIsGreater(data.midwifery, data.personnel),
});
}

private static checkIfMidwiferyIsGreater(
midwifery: MidwiferyNursingValue,
nursing: MidwiferyNursingValue
): boolean {
if (!midwifery.dataValue && !nursing.dataValue) return false;

const midwiferyValue = convertToNumberOrZero(midwifery.dataValue?.value);
const nursingValue = convertToNumberOrZero(nursing.dataValue?.value);

return midwiferyValue > nursingValue;
}
}
4 changes: 3 additions & 1 deletion src/domain/entities/Settings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Maybe } from "$/utils/ts-utils";
import { Either } from "./generic/Either";
import { ValidationError } from "./generic/Errors";
import { Struct } from "./generic/Struct";
Expand All @@ -22,8 +23,9 @@ export type SectionDisaggregation = {
id: Id;
disaggregationId: Id;
name: string;
type: "combos";
type: "combos" | "key_occupations" | "edu_occupations";
combinations: string[];
nursingMidwifery: Maybe<string[][]>;
};

export class Settings extends Struct<SettingsAttrs>() {
Expand Down
22 changes: 22 additions & 0 deletions src/domain/usecases/GetMidwiferyPersonnelDisaggregationsUseCase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Id } from "$/domain/entities/Ref";
import { SectionDisaggregation } from "$/domain/entities/Settings";
import { SettingsRepository } from "$/domain/repositories/SettingsRepository";
import { Future } from "$/domain/entities/generic/Future";
import _ from "$/domain/entities/generic/Collection";
import { FutureData } from "$/data/api-futures";

export class GetMidwiferyPersonnelDisaggregationsUseCase {
constructor(private settingsRepository: SettingsRepository) {}

execute(sectionId: Id): FutureData<SectionDisaggregation[]> {
return this.settingsRepository.get().flatMap(settings => {
const section = settings.sections.find(section => section.id === sectionId);
if (!section)
return Future.error(new Error(`Cannot found section settings: ${sectionId}`));
const disaggregationsSorted = _(section.disaggregations)
.sortBy(disaggregation => disaggregation.name)
.value();
return Future.success(disaggregationsSorted);
});
}
}
Loading

0 comments on commit 2811c2b

Please sign in to comment.