diff --git a/utilities/project-factory/src/server/api/genericApis.ts b/utilities/project-factory/src/server/api/genericApis.ts index 49c8427f2e9..902f5b5883f 100644 --- a/utilities/project-factory/src/server/api/genericApis.ts +++ b/utilities/project-factory/src/server/api/genericApis.ts @@ -545,26 +545,31 @@ async function getAutoGeneratedBoundaryCodes(boundaryList: any, childParentMap: const column = columnsData[i]; for (const element of column) { if (!findMapValue(elementCodesMap, element)) { - const parentCode = findMapValue(childParentMap, element) - if (parentCode !== undefined && parentCode !== null) { - countMap.set(parentCode, (findMapValue(countMap, parentCode) || 0) + 1); - let code; - const grandParentCode = findMapValue(childParentMap, parentCode); - if (grandParentCode != null && grandParentCode != undefined) { - const parentBoundaryCode = findMapValue(elementCodesMap, parentCode) - const lastUnderscoreIndex = parentBoundaryCode.lastIndexOf('_'); - const parentBoundaryCodeTrimmed = lastUnderscoreIndex !== -1 ? parentBoundaryCode.substring(0, lastUnderscoreIndex) : parentBoundaryCode; - code = generateElementCode(countMap.get(parentCode), parentBoundaryCodeTrimmed, element.value); - } else { - code = generateElementCode(countMap.get(parentCode), findMapValue(elementCodesMap, parentCode), element.value); - } + const parentElement = findMapValue(childParentMap, element); + if (parentElement !== undefined && parentElement !== null) { + const parentBoundaryCode = findMapValue(elementCodesMap, parentElement); + const currentCount = (findMapValue(countMap, parentElement) || 0) + 1; + countMap.set(parentElement, currentCount); + + const code = generateElementCode( + currentCount, + parentElement, + parentBoundaryCode, + element.value, + config.excludeBoundaryNameAtLastFromBoundaryCodes, + childParentMap, + elementCodesMap + ); + elementCodesMap.set(element, code); // Store the code of the element in the map } else { // Generate default code if parent code is not found - elementCodesMap.set(element, (request?.body?.ResourceDetails?.hierarchyType + "_").toUpperCase() + element.value.toString().substring(0, 2).toUpperCase()); + const prefix = config?.excludeHierarchyTypeFromBoundaryCodes + ? element.value.toString().substring(0, 2).toUpperCase() + : `${(request?.body?.ResourceDetails?.hierarchyType + "_").toUpperCase()}${element.value.toString().substring(0, 2).toUpperCase()}`; + + elementCodesMap.set(element, prefix); } - } else { - continue; } } } @@ -574,21 +579,33 @@ async function getAutoGeneratedBoundaryCodes(boundaryList: any, childParentMap: /** * Function to generate an element code based on sequence, parent code, and element. * @param sequence Sequence number - * @param parentCode Parent code + * @param parentElement Parent element + * @param parentBoundaryCode Parent boundary code * @param element Element + * @param excludeBoundaryNameAtLastFromBoundaryCodes Whether to exclude boundary name at last + * @param childParentMap Map of child to parent elements + * @param elementCodesMap Map of elements to their codes * @returns Generated element code */ -function generateElementCode(sequence: any, parentCode: any, element: any) { +function generateElementCode(sequence: any, parentElement: any, parentBoundaryCode: any, element: any, excludeBoundaryNameAtLastFromBoundaryCodes?: any, childParentMap?: any, elementCodesMap?: any) { // Pad single-digit numbers with leading zero - let paddedSequence = sequence.toString().padStart(2, "0"); - const code = parentCode.toUpperCase() + - "_" + - paddedSequence + - "_" + - element.toUpperCase(); - return ( - code.trim() - ); + const paddedSequence = sequence.toString().padStart(2, "0"); + let code; + + if (excludeBoundaryNameAtLastFromBoundaryCodes) { + code = `${parentBoundaryCode.toUpperCase()}_${paddedSequence}`; + } else { + const grandParentElement = findMapValue(childParentMap, parentElement); + if (grandParentElement != null && grandParentElement != undefined) { + const lastUnderscoreIndex = parentBoundaryCode ? parentBoundaryCode.lastIndexOf('_') : -1; + const parentBoundaryCodeTrimmed = lastUnderscoreIndex !== -1 ? parentBoundaryCode.substring(0, lastUnderscoreIndex) : parentBoundaryCode; + code = `${parentBoundaryCodeTrimmed.toUpperCase()}_${paddedSequence}_${element.toString().toUpperCase()}`; + } else { + code = `${parentBoundaryCode.toUpperCase()}_${paddedSequence}_${element.toString().toUpperCase()}`; + } + } + + return code.trim(); } /** @@ -637,24 +654,24 @@ async function getBoundarySheetData( headers ); } else { - let Filters :any= {}; + let Filters: any = {}; if (request?.body?.Filters && request?.body?.Filters.boundaries && Array.isArray(request?.body?.Filters.boundaries) && request?.body?.Filters.boundaries.length > 0) { Filters = { - Filters: { - boundaries: request.body.Filters.boundaries.map((boundary:any) => ({ - ...boundary, - boundaryType: boundary.type // Adding boundaryType field - })) - } + Filters: { + boundaries: request.body.Filters.boundaries.map((boundary: any) => ({ + ...boundary, + boundaryType: boundary.type // Adding boundaryType field + })) + } }; - } + } else { // logger.info("boundaryData for sheet " + JSON.stringify(boundaryData)) const responseFromCampaignSearch = await getCampaignSearchResponse(request); Filters = getFiltersFromCampaignSearchResponse(responseFromCampaignSearch) } - if (Filters?.Filters && Filters.Filters.boundaries && Array.isArray(Filters.Filters.boundaries) && Filters.Filters.boundaries.length > 0) { + if (Filters?.Filters && Filters.Filters.boundaries && Array.isArray(Filters.Filters.boundaries) && Filters.Filters.boundaries.length > 0) { const filteredBoundaryData = await generateFilteredBoundaryData( request, Filters diff --git a/utilities/project-factory/src/server/config/index.ts b/utilities/project-factory/src/server/config/index.ts index 015fe0711fb..127e6332f8f 100644 --- a/utilities/project-factory/src/server/config/index.ts +++ b/utilities/project-factory/src/server/config/index.ts @@ -16,6 +16,8 @@ const getDBSchemaName = (dbSchema = "") => { } // Configuration object containing various environment variables const config = { + excludeHierarchyTypeFromBoundaryCodes:false, + excludeBoundaryNameAtLastFromBoundaryCodes:false, masterNameForSchemaOfColumnHeaders: "adminSchema", boundary: { boundaryCode: process.env.BOUNDARY_CODE_HEADER_NAME || "HCM_ADMIN_CONSOLE_BOUNDARY_CODE",