Skip to content

Commit

Permalink
Facility changes (#812)
Browse files Browse the repository at this point in the history
* Feat : changed facility Template

* Feat : locked target templates
  • Loading branch information
ashish-egov authored Jun 7, 2024
1 parent 1fbff63 commit 4129df8
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 133 deletions.
67 changes: 34 additions & 33 deletions utilities/project-factory/src/server/api/campaignApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { v4 as uuidv4 } from 'uuid';
import { httpRequest } from "../utils/request";
import { getFormattedStringForDebug, logger } from "../utils/logger";
import createAndSearch from '../config/createAndSearch';
import { getDataFromSheet, matchData, generateActivityMessage, throwError, translateSchema, replicateRequest } from "../utils/genericUtils";
import { getDataFromSheet, generateActivityMessage, throwError, translateSchema, replicateRequest } from "../utils/genericUtils";
import { immediateValidationForTargetSheet, validateSheetData, validateTargetSheetData } from '../validators/campaignValidators';
import { callMdmsSchema, getCampaignNumber } from "./genericApis";
import { callMdmsSchema, callMdmsTypeSchema, getCampaignNumber } from "./genericApis";
import { boundaryBulkUpload, convertToTypeData, generateHierarchy, generateProcessedFileAndPersist, getLocalizedName, reorderBoundariesOfDataAndValidate } from "../utils/campaignUtils";
const _ = require('lodash');
import { produceModifiedMessages } from "../kafka/Listener";
Expand Down Expand Up @@ -155,20 +155,20 @@ function changeBodyViaElements(elements: any, requestBody: any) {
}
}

function changeBodyViaSearchFromSheet(elements: any, request: any, dataFromSheet: any) {
if (!elements) {
return;
}
for (const element of elements) {
const arrayToSearch = []
for (const data of dataFromSheet) {
if (data[element.sheetColumnName]) {
arrayToSearch.push(data[element.sheetColumnName]);
}
}
_.set(request.body, element?.searchPath, arrayToSearch);
}
}
// function changeBodyViaSearchFromSheet(elements: any, request: any, dataFromSheet: any) {
// if (!elements) {
// return;
// }
// for (const element of elements) {
// const arrayToSearch = []
// for (const data of dataFromSheet) {
// if (data[element.sheetColumnName]) {
// arrayToSearch.push(data[element.sheetColumnName]);
// }
// }
// _.set(request.body, element?.searchPath, arrayToSearch);
// }
// }

function updateErrorsForUser(newCreatedData: any[], newSearchedData: any[], errors: any[], createAndSearchConfig: any, userNameAndPassword: any[]) {
newCreatedData.forEach((createdElement: any) => {
Expand Down Expand Up @@ -199,10 +199,6 @@ function updateErrors(newCreatedData: any[], newSearchedData: any[], errors: any
for (const searchedElement of newSearchedData) {
let match = true;
for (const key in createdElement) {
if (createdElement.hasOwnProperty(key) && !searchedElement.hasOwnProperty(key) && key != '!row#number!') {
match = false;
break;
}
if (createdElement[key] !== searchedElement[key] && key != '!row#number!') {
match = false;
break;
Expand Down Expand Up @@ -406,13 +402,13 @@ function updateOffset(createAndSearchConfig: any, params: any, requestBody: any)


async function processSearchAndValidation(request: any, createAndSearchConfig: any, dataFromSheet: any[]) {
if (request?.body?.dataToSearch?.length > 0) {
const params: any = getParamsViaElements(createAndSearchConfig?.searchDetails?.searchElements, request);
changeBodyViaElements(createAndSearchConfig?.searchDetails?.searchElements, request)
changeBodyViaSearchFromSheet(createAndSearchConfig?.requiresToSearchFromSheet, request, dataFromSheet)
const arraysToMatch = await processSearch(createAndSearchConfig, request, params)
matchData(request, request.body.dataToSearch, arraysToMatch, createAndSearchConfig)
}
// if (request?.body?.dataToSearch?.length > 0) {
// const params: any = getParamsViaElements(createAndSearchConfig?.searchDetails?.searchElements, request);
// changeBodyViaElements(createAndSearchConfig?.searchDetails?.searchElements, request)
// changeBodyViaSearchFromSheet(createAndSearchConfig?.requiresToSearchFromSheet, request, dataFromSheet)
// const arraysToMatch = await processSearch(createAndSearchConfig, request, params)
// matchData(request, request.body.dataToSearch, arraysToMatch, createAndSearchConfig)
// }
if (request?.body?.ResourceDetails?.type == "user") {
await enrichEmployees(request?.body?.dataToCreate, request)
await matchUserValidation(request.body.dataToCreate, request)
Expand All @@ -426,8 +422,8 @@ async function getEmployeesBasedOnUuids(dataToCreate: any[], request: any) {

const tenantId = request?.body?.ResourceDetails?.tenantId;
const searchUrl = config.host.hrmsHost + config.paths.hrmsEmployeeSearch;
await new Promise(resolve => setTimeout(resolve, 10000));
logger.info(`Waiting for 10 seconds`);
await new Promise(resolve => setTimeout(resolve, 10000));
const chunkSize = 50;
let employeesSearched: any[] = [];

Expand Down Expand Up @@ -508,7 +504,7 @@ async function processValidate(request: any, localizationMap?: { [key: string]:
else {
let schema: any;
if (type == "facility" || type == "user") {
const mdmsResponse = await callMdmsSchema(request, config?.values?.moduleName, type, tenantId);
const mdmsResponse = await callMdmsTypeSchema(request, tenantId, type);
schema = mdmsResponse
}
const translatedSchema = await translateSchema(schema, localizationMap);
Expand Down Expand Up @@ -676,8 +672,8 @@ async function performAndSaveResourceActivity(request: any, createAndSearchConfi
logger.info(`Activity : ${createAndSearchConfig?.createBulkDetails?.url} status: ${responsePayload?.statusCode}`);
activities.push(activity);
}
await new Promise(resolve => setTimeout(resolve, 10000));
logger.info(`Waiting for 10 seconds`);
await new Promise(resolve => setTimeout(resolve, 10000));
await confirmCreation(createAndSearchConfig, request, dataToCreate, creationTime, activities);
}
await generateProcessedFileAndPersist(request, localizationMap);
Expand Down Expand Up @@ -713,8 +709,8 @@ async function handleResouceDetailsError(request: any, error: any) {
produceModifiedMessages(request?.body, config?.kafka?.KAFKA_UPDATE_RESOURCE_DETAILS_TOPIC);
}
if (request?.body?.Activities && Array.isArray(request?.body?.Activities && request?.body?.Activities.length > 0)) {
await new Promise(resolve => setTimeout(resolve, 2000));
logger.info("Waiting for 2 seconds");
await new Promise(resolve => setTimeout(resolve, 2000));
produceModifiedMessages(request?.body, config?.kafka?.KAFKA_CREATE_RESOURCE_ACTIVITY_TOPIC);
}
}
Expand Down Expand Up @@ -756,7 +752,12 @@ async function processCreate(request: any, localizationMap?: any) {
const createAndSearchConfig = createAndSearch[type]
const dataFromSheet = await getDataFromSheet(request, request?.body?.ResourceDetails?.fileStoreId, request?.body?.ResourceDetails?.tenantId, createAndSearchConfig, undefined, localizationMap)
let schema: any;
if (type == "facility" || type == "user") {
if (type == "facility") {
logger.info("Fetching schema to validate the created data for type: " + type);
const mdmsResponse = await callMdmsTypeSchema(request, tenantId, type);
schema = mdmsResponse
}
else if (type == "user") {
logger.info("Fetching schema to validate the created data for type: " + type);
const mdmsResponse = await callMdmsSchema(request, config?.values?.moduleName, type, tenantId);
schema = mdmsResponse
Expand Down Expand Up @@ -828,8 +829,8 @@ async function confirmProjectParentCreation(request: any, projectId: any) {
else {
logger.info("Project not found. Waiting for 1 seconds");
retry = retry - 1
await new Promise(resolve => setTimeout(resolve, 1000));
logger.info(`Waiting for ${retry} for 1 more second`);
await new Promise(resolve => setTimeout(resolve, 1000));
}
}
if (!projectFound) {
Expand Down
84 changes: 78 additions & 6 deletions utilities/project-factory/src/server/api/genericApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import { getExcelWorkbookFromFileURL } from "../utils/excelUtils";
//Function to get Workbook with different tabs (for type target)
const getTargetWorkbook = async (fileUrl: string, localizationMap?: any) => {
// Define headers for HTTP request
const workbook:any =await getExcelWorkbookFromFileURL(fileUrl,"");

const workbook: any = await getExcelWorkbookFromFileURL(fileUrl, "");

// Get the main sheet name (assuming it's the second sheet)
const mainSheetName = workbook.getWorksheet(1).name;
Expand Down Expand Up @@ -102,7 +102,7 @@ const getSheetData = async (
) => {
// Retrieve workbook using the getTargetWorkbook function
const localizedSheetName = getLocalizedName(sheetName, localizationMap);
const workbook:any =await getExcelWorkbookFromFileURL(fileUrl,localizedSheetName);
const workbook: any = await getExcelWorkbookFromFileURL(fileUrl, localizedSheetName);

const worksheet: any = workbook.getWorksheet(localizedSheetName);

Expand Down Expand Up @@ -563,8 +563,8 @@ async function getBoundarySheetData(
modifiedHierarchy,
localizationMap
);
const headerColumnsAfterHierarchy = await getConfigurableColumnHeadersBasedOnCampaignType(request,localizationMap);
const headers = [...localizedHeadersUptoHierarchy,...headerColumnsAfterHierarchy];
const headerColumnsAfterHierarchy = await getConfigurableColumnHeadersBasedOnCampaignType(request, localizationMap);
const headers = [...localizedHeadersUptoHierarchy, ...headerColumnsAfterHierarchy];
// create empty sheet if no boundary present in system
// const localizedBoundaryTab = getLocalizedName(
// getBoundaryTabName(),
Expand Down Expand Up @@ -1033,6 +1033,77 @@ async function callMdmsSchema(
return response?.SchemaDefinitions?.[0]?.definition;
}

function enrichSchema(data: any, properties: any, required: any, columns: any) {

// Sort columns based on orderNumber, using name as tie-breaker if orderNumbers are equal
columns.sort((a: any, b: any) => {
if (a.orderNumber === b.orderNumber) {
return a.name.localeCompare(b.name);
}
return a.orderNumber - b.orderNumber;
});

// Extract sorted property names
const sortedPropertyNames = columns.map((column: any) => column.name);

// Update data with new properties and required fields
data.properties = properties;
data.required = required;
data.columns = sortedPropertyNames;
}

function convertIntoSchema(data: any) {
const properties: any = {};
const required: any[] = [];
const columns: any[] = [];

for (const propType of ['enumProperties', 'numberProperties', 'stringProperties']) {
if (data.properties[propType] && Array.isArray(data.properties[propType]) && data.properties[propType]?.length > 0) {
for (const property of data.properties[propType]) {
properties[property?.name] = {
...property,
type: propType === 'stringProperties' ? 'string' : propType === 'numberProperties' ? 'number' : undefined
};

if (property?.isRequired && required.indexOf(property?.name) === -1) {
required.push(property?.name);
}

// If orderNumber is missing, default to a very high number
columns.push({ name: property?.name, orderNumber: property?.orderNumber || 9999999999 });
}
}
}
enrichSchema(data, properties, required, columns);
return data;
}



async function callMdmsTypeSchema(
request: any,
tenantId: string,
type: any
) {
const { RequestInfo = {} } = request?.body || {};
const requestBody = {
RequestInfo,
MdmsCriteria: {
tenantId: tenantId,
uniqueIdentifiers: [
type
],
schemaCode: "HCM-ADMIN-CONSOLE.typeSchemas"
}
};
const url = config.host.mdmsV2 + config.paths.mdms_v2_search;
const response = await httpRequest(url, requestBody);
if (!response?.mdms?.[0]?.data) {
throwError("COMMON", 500, "INTERNAL_SERVER_ERROR", "Error occured during schema search");
}
return convertIntoSchema(response?.mdms?.[0]?.data);
}

async function getMDMSV1Data(request: any, moduleName: string, masterName: string, tenantId: string) {
const resp: any = await callMdmsData(request, moduleName, masterName, tenantId);
return resp?.["MdmsRes"]?.[moduleName]?.[masterName];
Expand Down Expand Up @@ -1060,5 +1131,6 @@ export {
callMdmsData,
getMDMSV1Data,
callMdmsSchema,
callMdmsV2Data
callMdmsV2Data,
callMdmsTypeSchema
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const createAndSearch: any = {
},
uniqueIdentifier: "id",
uniqueIdentifierColumn: "A",
activeColumn: "G",
uniqueIdentifierColumnName: "HCM_ADMIN_CONSOLE_FACILITY_CODE",
matchEachKey: true,
parseArrayConfig: {
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 @@ -117,6 +117,8 @@ const config = {
// Values configuration
values: {
//module name
unfrozeTillRow: process.env.UNFROZE_TILL_ROW || "10000",
unfrozeTillColumn: process.env.UNFROZE_TILL_COLUMN || "50",
moduleName: process.env.MODULE_NAME || "HCM-ADMIN-CONSOLE",
readMeTab: "Read Me",
userMainBoundary: "mz",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@ async function enrichBoundaryCodes(resources: any[], messageObject: any, boundar
const code = data[uniqueCodeColumn];
// Extract boundary codes
const boundaryCode = data[getLocalizedName(createAndSearch?.[resource?.type]?.boundaryValidation?.column, localizationMap)];
if (boundaryCode) {
var active: any = "Active";
if (createAndSearch?.[resource?.type]?.activeColumn) {
var activeColumn = getLocalizedName(createAndSearch?.[resource?.type]?.activeColumn, localizationMap);
active = data[activeColumn];
}
if (boundaryCode && active == "Active") {
// Split boundary codes if they have comma separated values
const boundaryCodesArray = boundaryCode.split(',');
boundaryCodesArray.forEach((bc: string) => {
Expand All @@ -60,6 +65,7 @@ async function enrichBoundaryCodes(resources: any[], messageObject: any, boundar
boundaryCodes[resource?.type][trimmedBC] = [];
}
boundaryCodes[resource?.type][trimmedBC].push(code);
logger.info(`Boundary code ${trimmedBC} mapped to resource ${resource?.type} with code ${code}`)
});
}
}
Expand Down Expand Up @@ -212,8 +218,8 @@ async function processCampaignMapping(messageObject: any) {
break;
}
else {
await new Promise(resolve => setTimeout(resolve, 20000));
logger.info(`Waiting for 20 seconds for resource with id ${resourceDetailId} on retry ${retry}`);
await new Promise(resolve => setTimeout(resolve, 20000));
}
}
}
Expand Down
39 changes: 8 additions & 31 deletions utilities/project-factory/src/server/utils/campaignUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { campaignStatuses, headingMapping, resourceDataStatuses } from "../confi
import { getBoundaryColumnName, getBoundaryTabName } from "./boundaryUtils";
import { searchProjectTypeCampaignService } from "../service/campaignManageService";
import { validateBoundaryOfResouces } from "../validators/campaignValidators";
import { getExcelWorkbookFromFileURL, getNewExcelWorkbook } from "./excelUtils";
import { getExcelWorkbookFromFileURL, getNewExcelWorkbook, lockTargetFields } from "./excelUtils";
const _ = require('lodash');


Expand Down Expand Up @@ -158,6 +158,10 @@ function enrichErrors(errorData: any, worksheet: any, statusColumn: any, errorDe
if (error.isUniqueIdentifier) {
const uniqueIdentifierCell = worksheet.getCell(`${createAndSearchConfig.uniqueIdentifierColumn}${rowIndex}`);
uniqueIdentifierCell.value = error.uniqueIdentifier;
if (createAndSearchConfig?.activeColumn) {
const activeCell = worksheet.getCell(`${createAndSearchConfig.activeColumn}${rowIndex}`);
activeCell.value = "Active";
}
}
});
}
Expand Down Expand Up @@ -458,8 +462,8 @@ async function generateProcessedFileAndPersist(request: any, localizationMap?: {
if (request?.body?.Activities && Array.isArray(request?.body?.Activities && request?.body?.Activities.length > 0)) {
logger.info("Activities to persist : ")
logger.debug(getFormattedStringForDebug(request?.body?.Activities));
await new Promise(resolve => setTimeout(resolve, 2000));
logger.info(`Waiting for 2 seconds`);
await new Promise(resolve => setTimeout(resolve, 2000));
produceModifiedMessages(request?.body, config?.kafka?.KAFKA_CREATE_RESOURCE_ACTIVITY_TOPIC);
}
}
Expand Down Expand Up @@ -1307,33 +1311,6 @@ async function processBasedOnAction(request: any, actionInUrl: any) {
processAfterPersist(request, actionInUrl)
}

function lockTargetFields(newSheet: any, targetColumnNumber: any, boundaryCodeColumnIndex: any) {
// Make every cell locked by default
newSheet.eachRow((row: any) => {
row.eachCell((cell: any) => {
cell.protection = { locked: true };
});
});
// Unlock cells in the target column
if (targetColumnNumber > -1) {
newSheet.eachRow((row: any) => {
const cell = row.getCell(targetColumnNumber); // Excel columns are 1-based
cell.protection = { locked: false };
});
}

// Hide the boundary code column
if (boundaryCodeColumnIndex !== -1) {
newSheet.getColumn(boundaryCodeColumnIndex + 1).hidden = true;
}

// Protect the sheet with a password (optional)
newSheet.protect('passwordhere', {
selectLockedCells: true,
selectUnlockedCells: true
});
}


async function getLocalizedHierarchy(request: any, localizationMap: any) {
var hierarchy = await getHierarchy(request, request?.query?.tenantId, request?.query?.hierarchyType);
Expand Down Expand Up @@ -1362,7 +1339,7 @@ async function appendSheetsToWorkbook(request: any, boundaryData: any[], differe
const columnWidths = Array(12).fill(30);
mainSheet.columns = columnWidths.map(width => ({ width }));
// mainSheetData.forEach(row => mainSheet.addRow(row));
addDataToSheet(mainSheet, mainSheetData, 'F3842D', 30);
addDataToSheet(mainSheet, mainSheetData, 'F3842D', 30, false, true);
logger.info("appending different districts tab in the sheet started")
await appendDistricts(request, workbook, uniqueDistrictsForMainSheet, differentTabsBasedOnLevel, boundaryData, localizationMap, districtLevelRowBoundaryCodeMap, hierarchy);
logger.info("Sheet with different tabs generated successfully");
Expand All @@ -1379,7 +1356,7 @@ async function appendDistricts(request: any, workbook: any, uniqueDistrictsForMa
for (const uniqueData of uniqueDistrictsForMainSheet) {
const uniqueDataFromLevelForDifferentTabs = uniqueData.slice(uniqueData.lastIndexOf('_') + 1);
logger.info(`generating the boundary data for ${uniqueDataFromLevelForDifferentTabs} - ${differentTabsBasedOnLevel}`)
const districtDataFiltered = boundaryData.filter((boundary: any) => boundary[differentTabsBasedOnLevel] === uniqueDataFromLevelForDifferentTabs && boundary[hierarchy[hierarchy.length-1]]);
const districtDataFiltered = boundaryData.filter((boundary: any) => boundary[differentTabsBasedOnLevel] === uniqueDataFromLevelForDifferentTabs && boundary[hierarchy[hierarchy.length - 1]]);
const modifiedFilteredData = modifyFilteredData(districtDataFiltered, districtLevelRowBoundaryCodeMap.get(uniqueData), localizationMap);
if (modifiedFilteredData?.[0]) {
const newSheetData = [configurableColumnHeadersFromSchemaForTargetSheet];
Expand Down
Loading

0 comments on commit 4129df8

Please sign in to comment.