Skip to content

Commit

Permalink
Update chart level visualization for suppliers and materials
Browse files Browse the repository at this point in the history
  • Loading branch information
yulia-bel committed Feb 28, 2023
1 parent 18d1a20 commit d526d80
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 26 deletions.
4 changes: 2 additions & 2 deletions api/src/modules/admin-regions/admin-regions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> {
Expand Down
25 changes: 6 additions & 19 deletions api/src/modules/impact/impact.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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;
},
Expand Down
2 changes: 0 additions & 2 deletions api/src/modules/materials/materials.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,6 @@ export class MaterialsService extends AppBaseService<

async getMaxDepthLevelWithMpath(ids: string[]): Promise<number> {
const depthLevels: number[] = [];
// try to use one single query
// use promise all

await Promise.all(
ids.map(async (id: string) => {
Expand Down
25 changes: 24 additions & 1 deletion api/src/modules/suppliers/suppliers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
HttpException,
Inject,
Injectable,
Logger,
NotFoundException,
} from '@nestjs/common';
import {
Expand Down Expand Up @@ -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<number> {
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);
}
}
41 changes: 39 additions & 2 deletions api/test/e2e/impact/impact-chart/chart-materials-levels.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ export async function createNestedMaterialsPreconditions(): Promise<{
levelTwoMaterial1,
levelTwoMaterial2,
levelTwoMaterial3,
levelTwoMaterial4,
levelTwoMaterial5,
],
};
}

0 comments on commit d526d80

Please sign in to comment.