diff --git a/api/src/modules/impact/comparison/actual-vs-scenario.service.ts b/api/src/modules/impact/comparison/actual-vs-scenario.service.ts index e4d47ab26..f306f1558 100644 --- a/api/src/modules/impact/comparison/actual-vs-scenario.service.ts +++ b/api/src/modules/impact/comparison/actual-vs-scenario.service.ts @@ -248,16 +248,21 @@ export class ActualVsScenarioImpactService { return rangeOfYears.map((year: number) => { const totalSumByYear: number = yearSumMap.get(year) || 0; const totalScenarioSumByYear: number = yearScenarioSumMap.get(year) || 0; + const absoluteDifference: number = + totalScenarioSumByYear - totalSumByYear; + const percentageDifference: number = + ((totalScenarioSumByYear - totalSumByYear) / + ((totalScenarioSumByYear + totalSumByYear) / 2)) * + 100; return { year, value: totalSumByYear, comparedScenarioValue: totalScenarioSumByYear, - absoluteDifference: totalScenarioSumByYear - totalSumByYear, - percentageDifference: - ((totalScenarioSumByYear - totalSumByYear) / - ((totalScenarioSumByYear + totalSumByYear) / 2)) * - 100, + absoluteDifference, + percentageDifference: isNaN(percentageDifference) + ? 0 + : percentageDifference, isProjected: year > lastYearWithData, }; }); @@ -319,13 +324,20 @@ export class ActualVsScenarioImpactService { valueToAggregate[valueIndex].isProjected || entityRowValue.isProjected; - entityRowValue.absoluteDifference = + const absoluteDifference: number = entityRowValue.comparedScenarioValue - entityRowValue.value; - entityRowValue.percentageDifference = + const percentageDifference: number = ((entityRowValue.comparedScenarioValue - entityRowValue.value) / ((entityRowValue.comparedScenarioValue + entityRowValue.value) / 2)) * 100; + + entityRowValue.absoluteDifference = isNaN(absoluteDifference) + ? 0 + : absoluteDifference; + entityRowValue.percentageDifference = isNaN(percentageDifference) + ? 0 + : percentageDifference; } } return entity.values; diff --git a/api/src/modules/impact/comparison/scenario-vs-scenario.service.ts b/api/src/modules/impact/comparison/scenario-vs-scenario.service.ts index dd4fb13b8..b50931fdd 100644 --- a/api/src/modules/impact/comparison/scenario-vs-scenario.service.ts +++ b/api/src/modules/impact/comparison/scenario-vs-scenario.service.ts @@ -238,16 +238,23 @@ export class ScenarioVsScenarioImpactService { valueToAggregate[valueIndex].isProjected || entityRowValue.isProjected; - entityRowValue.absoluteDifference = + const absoluteDifference: number = entityRowValue.comparedScenarioValue - entityRowValue.baseScenarioValue; - entityRowValue.percentageDifference = + const percentageDifference: number = ((entityRowValue.comparedScenarioValue - entityRowValue.baseScenarioValue) / ((entityRowValue.comparedScenarioValue + entityRowValue.baseScenarioValue) / 2)) * 100; + + entityRowValue.absoluteDifference = isNaN(absoluteDifference) + ? 0 + : absoluteDifference; + entityRowValue.percentageDifference = isNaN(percentageDifference) + ? 0 + : percentageDifference; } } return entity.values; @@ -423,19 +430,23 @@ export class ScenarioVsScenarioImpactService { const comparedScenarioTotalSumByYear: number = comparedScenarioYearSumMap.get(year) || 0; + const absoluteDifference: number = + comparedScenarioTotalSumByYear - (baseScenarioTotalSumByYear || 0); + const percentageDifference: number = + ((comparedScenarioTotalSumByYear - (baseScenarioTotalSumByYear || 0)) / + ((comparedScenarioTotalSumByYear + + (baseScenarioTotalSumByYear || 0)) / + 2)) * + 100; + return { year, baseScenarioValue: baseScenarioTotalSumByYear, comparedScenarioValue: comparedScenarioTotalSumByYear, - absoluteDifference: - comparedScenarioTotalSumByYear - (baseScenarioTotalSumByYear || 0), - percentageDifference: - ((comparedScenarioTotalSumByYear - - (baseScenarioTotalSumByYear || 0)) / - ((comparedScenarioTotalSumByYear + - (baseScenarioTotalSumByYear || 0)) / - 2)) * - 100, + absoluteDifference: isNaN(absoluteDifference) ? 0 : absoluteDifference, + percentageDifference: isNaN(percentageDifference) + ? 0 + : percentageDifference, isProjected: year > lastYearWithData, }; }); diff --git a/api/test/e2e/impact/impact-reports/impact-reports.spec.ts b/api/test/e2e/impact/impact-reports/impact-reports.spec.ts index 2b1081718..748270b53 100644 --- a/api/test/e2e/impact/impact-reports/impact-reports.spec.ts +++ b/api/test/e2e/impact/impact-reports/impact-reports.spec.ts @@ -59,7 +59,7 @@ describe('Impact Reports', () => { }); it('should create a scenario vs scenario impact report', async () => { const { baseScenario, comparedScenario, indicator } = - await fixtures.GivenTwoScenarios(); + await fixtures.GivenTwoScenarioWithInterventions(); const response = await fixtures.WhenIRequestAScenarioVsScenarioImpactReport( { app: testApplication, diff --git a/api/test/e2e/impact/impact-reports/impactReportFixtures.ts b/api/test/e2e/impact/impact-reports/impactReportFixtures.ts index 4737f5c38..a23e9dabe 100644 --- a/api/test/e2e/impact/impact-reports/impactReportFixtures.ts +++ b/api/test/e2e/impact/impact-reports/impactReportFixtures.ts @@ -106,7 +106,7 @@ export const impactReportFixtures = () => ({ scenarioIntervention, }; }, - GivenTwoScenarios: async (): Promise => { + GivenTwoScenarioWithInterventions: async (): Promise => { const { newScenarioChangeSupplier: baseScenario, newScenarioChangeMaterial: comparedScenario, diff --git a/api/test/e2e/impact/mocks/actual-vs-scenario-responses/mixed-interventions-scenario.response.ts b/api/test/e2e/impact/mocks/actual-vs-scenario-responses/mixed-interventions-scenario.response.ts index ca6aeea74..20a386ee9 100644 --- a/api/test/e2e/impact/mocks/actual-vs-scenario-responses/mixed-interventions-scenario.response.ts +++ b/api/test/e2e/impact/mocks/actual-vs-scenario-responses/mixed-interventions-scenario.response.ts @@ -216,7 +216,7 @@ export const mixedInterventionsScenarioTable2019 = { comparedScenarioValue: 0, isProjected: false, absoluteDifference: 0, - percentageDifference: null, + percentageDifference: 0, }, { year: 2020, @@ -262,7 +262,7 @@ export const mixedInterventionsScenarioTable2019 = { comparedScenarioValue: 0, isProjected: false, absoluteDifference: 0, - percentageDifference: null, + percentageDifference: 0, }, { year: 2020, @@ -308,7 +308,7 @@ export const mixedInterventionsScenarioTable2019 = { comparedScenarioValue: 0, isProjected: false, absoluteDifference: 0, - percentageDifference: null, + percentageDifference: 0, }, { year: 2020, @@ -352,7 +352,7 @@ export const mixedInterventionsScenarioTable2019 = { comparedScenarioValue: 0, isProjected: false, absoluteDifference: 0, - percentageDifference: null, + percentageDifference: 0, }, { year: 2020, @@ -395,7 +395,7 @@ export const mixedInterventionsScenarioTable2019 = { value: 0, comparedScenarioValue: 0, absoluteDifference: 0, - percentageDifference: null, + percentageDifference: 0, }, { year: 2020, diff --git a/api/test/e2e/impact/mocks/scenario-vs-scenario-responses/same-materials-scenarios.reponse.ts b/api/test/e2e/impact/mocks/scenario-vs-scenario-responses/same-materials-scenarios.reponse.ts index f8a5e4495..1263163b2 100644 --- a/api/test/e2e/impact/mocks/scenario-vs-scenario-responses/same-materials-scenarios.reponse.ts +++ b/api/test/e2e/impact/mocks/scenario-vs-scenario-responses/same-materials-scenarios.reponse.ts @@ -252,7 +252,7 @@ export function getComparisonResponseWithProjectedYears( comparedScenarioValue: 0, isProjected: false, absoluteDifference: 0, - percentageDifference: null, + percentageDifference: 0, }, { year: 2019, @@ -260,7 +260,7 @@ export function getComparisonResponseWithProjectedYears( comparedScenarioValue: 0, isProjected: false, absoluteDifference: 0, - percentageDifference: null, + percentageDifference: 0, }, { year: 2020, @@ -298,7 +298,7 @@ export function getComparisonResponseWithProjectedYears( comparedScenarioValue: 0, isProjected: false, absoluteDifference: 0, - percentageDifference: null, + percentageDifference: 0, }, { year: 2019, @@ -306,7 +306,7 @@ export function getComparisonResponseWithProjectedYears( comparedScenarioValue: 0, isProjected: false, absoluteDifference: 0, - percentageDifference: null, + percentageDifference: 0, }, { year: 2020, @@ -344,7 +344,7 @@ export function getComparisonResponseWithProjectedYears( comparedScenarioValue: 0, isProjected: false, absoluteDifference: 0, - percentageDifference: null, + percentageDifference: 0, }, { year: 2019, @@ -352,7 +352,7 @@ export function getComparisonResponseWithProjectedYears( comparedScenarioValue: 0, isProjected: false, absoluteDifference: 0, - percentageDifference: null, + percentageDifference: 0, }, { year: 2020, @@ -388,7 +388,7 @@ export function getComparisonResponseWithProjectedYears( comparedScenarioValue: 0, isProjected: false, absoluteDifference: 0, - percentageDifference: null, + percentageDifference: 0, }, { year: 2019, @@ -396,7 +396,7 @@ export function getComparisonResponseWithProjectedYears( comparedScenarioValue: 0, isProjected: false, absoluteDifference: 0, - percentageDifference: null, + percentageDifference: 0, }, { year: 2020, @@ -431,7 +431,7 @@ export function getComparisonResponseWithProjectedYears( baseScenarioValue: 0, comparedScenarioValue: 0, absoluteDifference: 0, - percentageDifference: null, + percentageDifference: 0, isProjected: false, }, { @@ -439,7 +439,7 @@ export function getComparisonResponseWithProjectedYears( baseScenarioValue: 0, comparedScenarioValue: 0, absoluteDifference: 0, - percentageDifference: null, + percentageDifference: 0, isProjected: false, }, {