From 2b6d0cfe82e508b39aa029a7af8d1646cfff5585 Mon Sep 17 00:00:00 2001 From: nmorasb Date: Thu, 23 May 2024 17:47:54 -0400 Subject: [PATCH] MAT-7238: refactor how stratification worksheets are created and added to only create one worksheet per group stratification --- src/services/export.service.ts | 44 +++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/src/services/export.service.ts b/src/services/export.service.ts index b96c3a7..e4ac85e 100644 --- a/src/services/export.service.ts +++ b/src/services/export.service.ts @@ -15,7 +15,6 @@ import { TestCaseExcelExportDto, PopulationDto, StratificationDto, - GroupedStratificationDto, } from '@madie/madie-models'; @Injectable() @@ -42,22 +41,36 @@ export class ExportService { index += 1; }); + // collect all the distinct stratifications by stratification name + const stratNames = new Set(); + const stratNamesToGroup = {}; testCaseExcelExportDtos.forEach((testCaseExcelExportDto) => { testCaseExcelExportDto.testCaseExecutionResults.forEach((result) => { result.stratifications?.forEach((stratification) => { - const stratificationWorksheet = workbook.addWorksheet( - `${index} - ${stratification.stratName}`, - ); - this.generateStratificationWorksheet( - stratificationWorksheet, - testCaseExcelExportDto, - stratification, - ); - index += 1; + stratNames.add(stratification.stratName); + stratNamesToGroup[stratification.stratName] = + testCaseExcelExportDto.groupId; }); }); }); + // create a worksheet for each stratification + for (const stratName of stratNames) { + const groupId = stratNamesToGroup[stratName]; + const stratificationWorksheet = workbook.addWorksheet( + `${index} - ${stratName}`, + ); + const testCaseExcelExportDto = testCaseExcelExportDtos.find( + (dto) => dto.groupId === groupId, + ); + this.generateStratificationWorksheet( + stratificationWorksheet, + stratName, + testCaseExcelExportDto, + ); + index++; + } + // Return final workbook return workbook.xlsx.writeBuffer() as Promise; } @@ -350,8 +363,8 @@ export class ExportService { public generateStratificationWorksheet( worksheet: ExcelJS.Worksheet, + stratName: string, testCaseGroupDto: TestCaseExcelExportDto, - groupedStratificationDto: GroupedStratificationDto, ) { let firstRow = []; let headerRow = []; @@ -366,6 +379,15 @@ export class ExportService { const firstRowData = []; const headerRowData = []; const testCaseData = []; + const groupedStratificationDto = result.stratifications?.find( + (s) => s.stratName === stratName, + ); + if ( + undefined === groupedStratificationDto || + null === groupedStratificationDto + ) { + return; + } const numValues: number = groupedStratificationDto.stratificationDtos?.length;