Skip to content

Commit

Permalink
improvements for periods and indicator report
Browse files Browse the repository at this point in the history
  • Loading branch information
eperedo committed Nov 13, 2024
1 parent 80060ef commit f27173f
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import React from "react";
import { Id } from "../../../domain/entities/Ref";
import { StepProps } from "../../../pages/project-wizard/ProjectWizard";
import { Filter } from "../data-elements/DataElementsFilters";
import DataElementsStep from "../data-elements/DataElementsStep";
import DataElementsStep, { DataElementsStepProps } from "../data-elements/DataElementsStep";

const initialFilters: Filter = { peopleOrBenefit: "people" };

const UniqueIndicatorsStep: React.FC<StepProps> = props => {
const { project } = props;
const getSelection = React.useCallback(
(sectorId: Id, dataElementIds: Id[]) => {
const getSelection = React.useCallback<DataElementsStepProps["onSelect"]>(
(sectorId, dataElementIds) => {
return project.updateUniqueBeneficiariesSelection(sectorId, dataElementIds);
},
[project]
Expand Down
12 changes: 4 additions & 8 deletions src/data/repositories/IndicatorReportD2Repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ export class IndicatorReportD2Repository implements IndicatorReportRepository {
) {
const currentDate = new Date().toISOString();
return reports.map((report): D2Response => {
const existingRecord = existingReports?.find(
item =>
item.startDate === report.period.startDateMonth &&
item.endDate === report.period.endDateMonth
const existingRecord = existingReports?.find(item =>
report.period.equalMonths(item.startDate, item.endDate)
);

return {
Expand Down Expand Up @@ -86,10 +84,8 @@ export class IndicatorReportD2Repository implements IndicatorReportRepository {
const groupedPeriods = UniqueBeneficiariesPeriod.uniquePeriodsByDates(periods);

return responses.map(response => {
const currentPeriod = groupedPeriods.find(
period =>
period.startDateMonth === response.startDate &&
period.endDateMonth === response.endDate
const currentPeriod = groupedPeriods.find(period =>
period.equalMonths(response.startDate, response.endDate)
);
if (!currentPeriod)
throw Error(`Period ${response.startDate}-${response.endDate} not found`);
Expand Down
8 changes: 1 addition & 7 deletions src/domain/entities/IndicatorCalculation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,6 @@ export class IndicatorCalculation extends Struct<IndicatorCalculationAttrs>() {
}

static calculateTotalValue(editable: Maybe<number>, returning: Maybe<number>): number {
if (!editable) {
return returning || 0;
} else if (!returning) {
return editable;
} else {
return editable + returning;
}
return (editable ?? 0) + (returning ?? 0);
}
}
4 changes: 4 additions & 0 deletions src/domain/entities/UniqueBeneficiariesPeriod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,8 @@ export class UniqueBeneficiariesPeriod extends Struct<UniqueBeneficiariesPeriods
.uniqBy(period => period.id)
.value();
}

public equalMonths(startDateMonth: number, endDateMonth: number): boolean {
return this.startDateMonth === startDateMonth && this.endDateMonth === endDateMonth;
}
}
15 changes: 5 additions & 10 deletions src/domain/usecases/GetProjectsByCountryUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ export class GetProjectsByCountryUseCase {
return uniquePeriods.map((period): IndicatorReport => {
const existingData = existingReports.find(
report =>
report.period.startDateMonth === period.startDateMonth &&
report.period.endDateMonth === period.endDateMonth &&
report.period.equalMonths(period.startDateMonth, period.endDateMonth) &&
report.countryId === countryId
);

Expand Down Expand Up @@ -90,19 +89,15 @@ export class GetProjectsByCountryUseCase {
return undefined;

const isCustomPeriod = period.type === "CUSTOM";
const periodExist = settingsProject?.periods.find(
item =>
item.startDateMonth === period.startDateMonth &&
item.endDateMonth === period.endDateMonth
const periodExist = settingsProject?.periods.find(item =>
period.equalMonths(item.startDateMonth, item.endDateMonth)
);

const notIndicatorsAvailable = isCustomPeriod && !periodExist;

const indicatorsCalculation = settingsProject?.indicatorsValidation
.find(
item =>
item.period.startDateMonth === period.startDateMonth &&
item.period.endDateMonth === period.endDateMonth
.find(item =>
period.equalMonths(item.period.startDateMonth, item.period.endDateMonth)
)
?.indicatorsCalculation.map((indicator): ProjectIndicatorRow => {
return {
Expand Down
18 changes: 6 additions & 12 deletions src/domain/usecases/SaveUniqueBeneficiariesSettingsUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,19 @@ export class SaveUniqueBeneficiariesSettingsUseCase {

if (this.isAnnualOrSemiAnnual(options.period)) {
throw new Error(i18n.t("Period is equal to the predefined annual/semi-annual period"));
}

if (!isValid) {
} else if (!isValid) {
throw new Error(errorMessage);
}

if (isPeriodProtected) {
} else if (isPeriodProtected) {
throw new Error(i18n.t("Cannot save a protected period"));
} else {
return this.saveSettings(settings, periodExist, options);
}

return this.saveSettings(settings, periodExist, options);
}

private isAnnualOrSemiAnnual(period: UniqueBeneficiariesPeriod): boolean {
const defaultPeriods = UniqueBeneficiariesPeriod.defaultPeriods();
return defaultPeriods.some(
defaultPeriod =>
defaultPeriod.startDateMonth === period.startDateMonth &&
defaultPeriod.endDateMonth === period.endDateMonth
return defaultPeriods.some(defaultPeriod =>
defaultPeriod.equalMonths(period.startDateMonth, period.endDateMonth)
);
}

Expand Down
5 changes: 2 additions & 3 deletions src/pages/country-indicator-report/CountryIndicatorReport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,8 @@ export const CountryIndicatorReport = React.memo(() => {

CountryIndicatorReport.displayName = "CountryIndicatorReport";

function getAllPeriods(settings: IndicatorReport[]): UniqueBeneficiariesPeriod[] {
const allPeriods = settings.flatMap(setting => setting.period);
return allPeriods;
function getAllPeriods(indicatorsReports: IndicatorReport[]): UniqueBeneficiariesPeriod[] {
return indicatorsReports.flatMap(setting => setting.period);
}

function mapItemsToDropdown(periods: UniqueBeneficiariesPeriod[]) {
Expand Down

0 comments on commit f27173f

Please sign in to comment.