diff --git a/api/src/modules/impact/reports/impact.report.ts b/api/src/modules/impact/reports/impact.report.ts index 04b880fd3f..80ae0b219e 100644 --- a/api/src/modules/impact/reports/impact.report.ts +++ b/api/src/modules/impact/reports/impact.report.ts @@ -78,13 +78,13 @@ export class ImpactReportService { yearKey = yearKey.concat(' (projected)'); } if ('baseScenarioValue' in nodeValue) { - resultObject[`${yearKey} (base Scenario)`] = + resultObject[`${yearKey} (Base Scenario)`] = nodeValue.baseScenarioValue; - resultObject[`${yearKey} (compared Scenario)`] = + resultObject[`${yearKey} (Compared Scenario)`] = nodeValue.comparedScenarioValue; } else if ('comparedScenarioValue' in nodeValue) { resultObject[yearKey] = nodeValue.value; - resultObject[`${yearKey} (compared Scenario)`] = + resultObject[`${yearKey} (Compared Scenario)`] = nodeValue.comparedScenarioValue; } else { resultObject[yearKey] = nodeValue.value; @@ -111,20 +111,25 @@ export class ImpactReportService { const indicatorKey: string[] = []; const groupByKey: string[] = []; const yearKeys: string[] = []; - const differenceKeys: string[] = []; + const comparisonKeys: string[] = []; Object.keys(dataSample).forEach((key: string) => { if (key.includes(IndicatorColumnKey)) { groupByKey.push(key); } else if (key.includes(GroupByColumnKey)) { groupByKey.push(key); } else if (key.includes(AbsoluteDifferenceColumnKey)) { - differenceKeys.push(key); + comparisonKeys.push(key); } else if (key.includes(PercentageDifferenceColumnKey)) { - differenceKeys.push(key); + comparisonKeys.push(key); } else { yearKeys.push(key); } }); - return [...indicatorKey, ...groupByKey, ...yearKeys, ...differenceKeys]; + return [ + ...indicatorKey, + ...groupByKey, + ...yearKeys.sort(), + ...comparisonKeys, + ]; } } 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 a5f199340a..e332a74702 100644 --- a/api/test/e2e/impact/impact-reports/impact-reports.spec.ts +++ b/api/test/e2e/impact/impact-reports/impact-reports.spec.ts @@ -45,4 +45,20 @@ describe('Impact Reports', () => { materials, }); }); + it('should create an actual vs scenario impact report', async () => { + const { indicator, scenarioIntervention } = + await fixtures.GivenAScenarioIntervention(); + const response = await fixtures.WhenIRequestAnActualVsScenarioImpactReport({ + app: testApplication, + jwtToken, + indicatorIds: [indicator.id], + comparedScenarioId: scenarioIntervention.scenarioId, + }); + + await fixtures.ThenIShouldGetAnImpactReportAboutProvidedFilters(response, { + indicators: [indicator], + isActualVsScenario: true, + }); + }); + it('should create a scenario vs scenario impact report', async () => {}); }); diff --git a/api/test/e2e/impact/impact-reports/impactReportFixtures.ts b/api/test/e2e/impact/impact-reports/impactReportFixtures.ts index aa2e95d51e..d58b0d1eb9 100644 --- a/api/test/e2e/impact/impact-reports/impactReportFixtures.ts +++ b/api/test/e2e/impact/impact-reports/impactReportFixtures.ts @@ -19,6 +19,9 @@ import * as request from 'supertest'; import { GROUP_BY_VALUES } from '../../../../src/modules/impact/dto/impact-table.dto'; import { range } from 'lodash'; import { Material } from '../../../../src/modules/materials/material.entity'; +import { Scenario } from 'modules/scenarios/scenario.entity'; +import { createNewCoefficientsInterventionPreconditions } from '../mocks/actual-vs-scenario-preconditions/new-coefficients-intervention.preconditions'; +import { ScenarioIntervention } from '../../../../src/modules/scenario-interventions/scenario-intervention.entity'; export const impactReportFixtures = () => ({ GivenSourcingLocationWithImpact: async () => { @@ -89,6 +92,20 @@ export const impactReportFixtures = () => ({ sourcingRecords, }; }, + GivenAScenarioIntervention: async ( + customScenario?: Scenario, + ): Promise<{ + indicator: Indicator; + scenarioIntervention: ScenarioIntervention; + }> => { + const { indicator, scenarioIntervention } = + await createNewCoefficientsInterventionPreconditions(customScenario); + return { + indicator, + scenarioIntervention, + }; + }, + WhenIRequestAnImpactReport: (options: { app: TestApplication; jwtToken: string; @@ -104,12 +121,32 @@ export const impactReportFixtures = () => ({ groupBy: 'material', }); }, + WhenIRequestAnActualVsScenarioImpactReport: (options: { + app: TestApplication; + jwtToken: string; + indicatorIds: string[]; + comparedScenarioId: string; + }): Promise => { + return request(options.app.getHttpServer()) + .get('/api/v1/impact/compare/scenario/vs/actual/report') + .set('Authorization', `Bearer ${options.jwtToken}`) + .query({ + 'indicatorIds[]': [...options.indicatorIds], + startYear: 2010, + endYear: 2027, + groupBy: 'material', + comparedScenarioId: options.comparedScenarioId, + }); + }, + ThenIShouldGetAnImpactReportAboutProvidedFilters: ( response: request.Response, filters?: { groupBy?: GROUP_BY_VALUES; indicators?: Indicator[]; materials?: Material[]; + isActualVsScenario?: boolean; + isScenarioVsScenario?: boolean; }, ) => { expect(response.status).toBe(200); @@ -126,6 +163,17 @@ export const impactReportFixtures = () => ({ expect(response.text).toContain(material.name); } } + if (filters?.isActualVsScenario) { + expect(response.text).toContain('Compared Scenario'); + expect(response.text).toContain('Absolute Difference'); + expect(response.text).toContain('Percentage Difference'); + } + if (filters?.isScenarioVsScenario) { + expect(response.text).toContain('Base Scenario'); + expect(response.text).toContain('Compared Scenario'); + expect(response.text).toContain('Absolute Difference'); + expect(response.text).toContain('Percentage Difference'); + } for (const year of range(2010, 2027)) { expect(response.text).toContain(year.toString()); } diff --git a/api/test/e2e/impact/mocks/actual-vs-scenario-preconditions/new-coefficients-intervention.preconditions.ts b/api/test/e2e/impact/mocks/actual-vs-scenario-preconditions/new-coefficients-intervention.preconditions.ts index 43307a6a68..e9c929a76a 100644 --- a/api/test/e2e/impact/mocks/actual-vs-scenario-preconditions/new-coefficients-intervention.preconditions.ts +++ b/api/test/e2e/impact/mocks/actual-vs-scenario-preconditions/new-coefficients-intervention.preconditions.ts @@ -28,6 +28,8 @@ import { import { INDICATOR_NAME_CODES } from 'modules/indicators/indicator.entity'; import { Scenario } from 'modules/scenarios/scenario.entity'; +// TODO: Pending to start refactoring all precondition creation mess to follow the fixture approach + export async function createNewCoefficientsInterventionPreconditions( customScenario?: Scenario, ): Promise<{ @@ -48,6 +50,7 @@ export async function createNewCoefficientsInterventionPreconditions( name: 'Deforestation', unit, nameCode: INDICATOR_NAME_CODES.DF_SLUC, + shortName: 'Deforestation', }); // Creating Materials