diff --git a/api/src/modules/admin-regions/admin-regions.service.ts b/api/src/modules/admin-regions/admin-regions.service.ts index 452ceadde..a6635208c 100644 --- a/api/src/modules/admin-regions/admin-regions.service.ts +++ b/api/src/modules/admin-regions/admin-regions.service.ts @@ -268,8 +268,8 @@ export class AdminRegionsService extends AppBaseService< return adminRegions.map((adminRegion: AdminRegion) => adminRegion.id); } /** - * @description: Retrieving max admin region depth level of selected materials - * based on mpath column which represents the ascendants path to the material + * @description: Retrieving max admin region depth level of selected admin regions + * based on mpath column which represents the ascendants path to the admin region * @param adminRegionIds */ async getAdminRegionsMaxLevel(adminRegionIds: string[]): Promise { diff --git a/api/src/modules/impact/impact.service.ts b/api/src/modules/impact/impact.service.ts index a5f447b3e..38624305c 100644 --- a/api/src/modules/impact/impact.service.ts +++ b/api/src/modules/impact/impact.service.ts @@ -186,11 +186,10 @@ export class ImpactService extends BaseImpactService { depth = await this.adminRegionsService.getAdminRegionsMaxLevel( rankedImpactTableDto.originIds, ); - return depth; + return depth + 1; } else { return 0; } - break; case GROUP_BY_VALUES.MATERIAL: if (rankedImpactTableDto.materialIds) { @@ -201,32 +200,18 @@ export class ImpactService extends BaseImpactService { } else { return 0; } - break; case GROUP_BY_VALUES.SUPPLIER: - if (rankedImpactTableDto.materialIds) { - depth = await this.materialsService.getMaxDepthLevelWithMpath( - rankedImpactTableDto.materialIds, - ); - return depth + 1; - } else { - return 0; - } - break; - - case GROUP_BY_VALUES.BUSINESS_UNIT: - if (rankedImpactTableDto.materialIds) { - depth = await this.materialsService.getMaxDepthLevelWithMpath( - rankedImpactTableDto.materialIds, + if (rankedImpactTableDto.supplierIds) { + depth = await this.suppliersService.getMaxDepthLevelWithMpath( + rankedImpactTableDto.supplierIds, ); return depth + 1; } else { return 0; } - break; default: return 0; - break; } } @@ -308,6 +293,8 @@ export class ImpactService extends BaseImpactService { impactTableRows = impactTableRows.concat( this.getLevelOfImpactTableRows(row.children, depth - 1), ); + } else { + impactTableRows = impactTableRows.concat(row); } return impactTableRows; }, diff --git a/api/src/modules/materials/materials.service.ts b/api/src/modules/materials/materials.service.ts index 4f9b57668..82e371018 100644 --- a/api/src/modules/materials/materials.service.ts +++ b/api/src/modules/materials/materials.service.ts @@ -313,8 +313,6 @@ export class MaterialsService extends AppBaseService< async getMaxDepthLevelWithMpath(ids: string[]): Promise { const depthLevels: number[] = []; - // try to use one single query - // use promise all await Promise.all( ids.map(async (id: string) => { diff --git a/api/src/modules/suppliers/suppliers.service.ts b/api/src/modules/suppliers/suppliers.service.ts index 726fbdfce..ed5584735 100644 --- a/api/src/modules/suppliers/suppliers.service.ts +++ b/api/src/modules/suppliers/suppliers.service.ts @@ -3,7 +3,6 @@ import { HttpException, Inject, Injectable, - Logger, NotFoundException, } from '@nestjs/common'; import { @@ -202,4 +201,28 @@ export class SuppliersService extends AppBaseService< return suppliers.map((supplier: Supplier) => supplier.id); } + + /** + * @description: Retrieving max supplier depth level of selected suppliers + * based on mpath column which represents the ascendants path to the supplier + * @param ids + */ + async getMaxDepthLevelWithMpath(ids: string[]): Promise { + const depthLevels: number[] = []; + + await Promise.all( + ids.map(async (id: string) => { + const supplierMpath: any = await this.supplierRepository + .createQueryBuilder('supplier') + .select(['supplier.mpath as "mpath"']) + .where('supplier.id = :id', { id }) + .getRawOne(); + const supplierDepth: number = + supplierMpath.mpath.match(/\./g).length - 1; + depthLevels.push(supplierDepth); + }), + ); + + return Math.max(...depthLevels); + } } diff --git a/api/test/e2e/impact/impact-chart/chart-materials-levels.spec.ts b/api/test/e2e/impact/impact-chart/chart-materials-levels.spec.ts index 1d599b03e..acab567fd 100644 --- a/api/test/e2e/impact/impact-chart/chart-materials-levels.spec.ts +++ b/api/test/e2e/impact/impact-chart/chart-materials-levels.spec.ts @@ -101,7 +101,10 @@ describe('Impact Chart (Ranking) Test Suite (e2e) with requested levels for Mate .set('Authorization', `Bearer ${jwtToken}`) .query({ 'indicatorIds[]': [preconditions.indicator.id], - 'materialIds[]': [preconditions.materialsLevelOne[0].id], + 'materialIds[]': [ + preconditions.materialsLevelOne[0].id, + preconditions.materialsLevelOne[1].id, + ], endYear: 2020, startYear: 2020, groupBy: GROUP_BY_VALUES.MATERIAL, @@ -110,7 +113,41 @@ describe('Impact Chart (Ranking) Test Suite (e2e) with requested levels for Mate }) .expect(HttpStatus.OK); - expect(response.body.impactTable[0].rows.length).toBe(3); + expect(response.body.impactTable[0].rows.length).toBe(4); + expect(response.body.impactTable[0].rows[0].name).toBe( + 'Material Level Two 1', + ); + expect(response.body.impactTable[0].rows[0].values[0].value).toBe(1000); + expect( + response.body.impactTable[0].others.aggregatedValues[0].value, + ).toBe(1000); + }, + ); + + test( + 'When I query a Impact Chart grouped by material and filtered by level one and level two' + + 'And do not specify level' + + 'Then I should get response structure starting from the level two materials ', + async () => { + const response = await request(testApplication.getHttpServer()) + .get('/api/v1/impact/ranking') + .set('Authorization', `Bearer ${jwtToken}`) + .query({ + 'indicatorIds[]': [preconditions.indicator.id], + 'materialIds[]': [ + preconditions.materialsLevelTwo[4].id, + preconditions.materialsLevelTwo[3].id, + preconditions.materialsLevelOne[0].id, + ], + endYear: 2020, + startYear: 2020, + groupBy: GROUP_BY_VALUES.MATERIAL, + maxRankingEntities: 5, + sort: 'DES', + }) + .expect(HttpStatus.OK); + + expect(response.body.impactTable[0].rows.length).toBe(5); expect(response.body.impactTable[0].rows[0].name).toBe( 'Material Level Two 1', ); diff --git a/api/test/e2e/impact/mocks/chart-levels-preconditions/nested-materials.preconditions.ts b/api/test/e2e/impact/mocks/chart-levels-preconditions/nested-materials.preconditions.ts index dbf70afbe..febcc2d03 100644 --- a/api/test/e2e/impact/mocks/chart-levels-preconditions/nested-materials.preconditions.ts +++ b/api/test/e2e/impact/mocks/chart-levels-preconditions/nested-materials.preconditions.ts @@ -127,6 +127,8 @@ export async function createNestedMaterialsPreconditions(): Promise<{ levelTwoMaterial1, levelTwoMaterial2, levelTwoMaterial3, + levelTwoMaterial4, + levelTwoMaterial5, ], }; }