Skip to content

Commit

Permalink
Boundary code generated has now configurable hierarchy type and boun…
Browse files Browse the repository at this point in the history
…dary name at last based on config (#963)

* fixed target validate for different tabs not present

* made hierarchytype and bondary name configurable in auto generation of boundary code

* added toString() on elment for safety
  • Loading branch information
nitish-egov authored Jun 26, 2024
1 parent 0573b25 commit d382481
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 36 deletions.
89 changes: 53 additions & 36 deletions utilities/project-factory/src/server/api/genericApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions utilities/project-factory/src/server/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit d382481

Please sign in to comment.