From e610982f78daf936f6a6e231d6d635c52ad90782 Mon Sep 17 00:00:00 2001 From: Priyanka-eGov <74049060+Priyanka-eGov@users.noreply.github.com> Date: Mon, 29 Jul 2024 14:14:37 +0530 Subject: [PATCH] HCMPRE-6407 download template changes (#1128) * HLM-6407 adding changes for facility template * HLM-6407 adding changes for facility template * HLM-6407 adding changes for facility template * HLM-6407 codde review comment * HCMPRE-91 code review comments * HLM-6407 using same mdms schema for microplan * HLM-6407 adding changes for facility template * HLM-6407 adding changes for facility template * HLM-6407 adding changes for facility template * HLM-6407 codde review comment * HCMPRE-91 code review comments * HLM-6407 using same mdms schema for microplan * HLM-6407 code review comments * HLM-6407 code review comments --- .../src/server/api/campaignApis.ts | 33 ++++- .../src/server/api/genericApis.ts | 6 +- .../src/server/config/createAndSearch.ts | 132 ++++++++++++++++++ .../config/models/createRequestSchema.ts | 2 +- .../src/server/utils/campaignUtils.ts | 4 +- .../src/server/utils/genericUtils.ts | 83 ++++++++++- 6 files changed, 243 insertions(+), 17 deletions(-) diff --git a/utilities/project-factory/src/server/api/campaignApis.ts b/utilities/project-factory/src/server/api/campaignApis.ts index 616b836d5b3..0edd9b151ce 100644 --- a/utilities/project-factory/src/server/api/campaignApis.ts +++ b/utilities/project-factory/src/server/api/campaignApis.ts @@ -3,10 +3,10 @@ import { v4 as uuidv4 } from 'uuid'; import { httpRequest } from "../utils/request"; import { getFormattedStringForDebug, logger } from "../utils/logger"; import createAndSearch from '../config/createAndSearch'; -import { getDataFromSheet, generateActivityMessage, throwError, translateSchema, replicateRequest } from "../utils/genericUtils"; +import { getDataFromSheet, generateActivityMessage, throwError, translateSchema, replicateRequest, appendProjectTypeToCapacity } from "../utils/genericUtils"; import { immediateValidationForTargetSheet, validateSheetData, validateTargetSheetData } from '../validators/campaignValidators'; import { callMdmsTypeSchema, getCampaignNumber } from "./genericApis"; -import { boundaryBulkUpload, convertToTypeData, generateHierarchy, generateProcessedFileAndPersist, getBoundaryOnWhichWeSplit, getLocalizedName, reorderBoundariesOfDataAndValidate } from "../utils/campaignUtils"; +import { boundaryBulkUpload, convertToTypeData, generateHierarchy, generateProcessedFileAndPersist, getBoundaryOnWhichWeSplit, getLocalizedName, reorderBoundariesOfDataAndValidate, checkIfSourceIsMicroplan } from "../utils/campaignUtils"; const _ = require('lodash'); import { produceModifiedMessages } from "../kafka/Listener"; import { createDataService } from "../service/dataManageService"; @@ -730,7 +730,7 @@ async function performAndSaveResourceActivity(request: any, createAndSearchConfi } _.set(newRequestBody, createAndSearchConfig?.createBulkDetails?.createPath, chunkData); creationTime = Date.now(); - if (type == "facility") { + if (type == "facility" || type == "facilityMicroplan") { await handeFacilityProcess(request, createAndSearchConfig, params, activities, newRequestBody); } else if (type == "user") { @@ -843,14 +843,39 @@ async function processCreate(request: any, localizationMap?: any) { boundaryBulkUpload(request, localizationMap); } else { - const createAndSearchConfig = createAndSearch[type] + // console.log(`Source is MICROPLAN -->`, source); + let createAndSearchConfig: any; + createAndSearchConfig = createAndSearch[type]; + const responseFromCampaignSearch = await getCampaignSearchResponse(request); + const campaignType = responseFromCampaignSearch?.CampaignDetails[0]?.projectType; + if (checkIfSourceIsMicroplan(request?.body?.ResourceDetails)) { + logger.info(`Data create Source is MICROPLAN`); + if (createAndSearchConfig?.parseArrayConfig?.parseLogic) { + createAndSearchConfig.parseArrayConfig.parseLogic = createAndSearchConfig.parseArrayConfig.parseLogic.map( + (item: any) => { + if (item.sheetColumn === "E") { + item.sheetColumnName += `_${campaignType}`; + } + return item; + } + ); + } + } + const dataFromSheet = await getDataFromSheet(request, request?.body?.ResourceDetails?.fileStoreId, request?.body?.ResourceDetails?.tenantId, createAndSearchConfig, undefined, localizationMap) let schema: any; + 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 == "facilityMicroplan") { + const mdmsResponse = await callMdmsTypeSchema(request, tenantId, "facility", "microplan"); + schema = mdmsResponse + logger.info("Appending project type to capacity for microplan " + campaignType); + schema = await appendProjectTypeToCapacity(schema, campaignType); + } else if (type == "user") { logger.info("Fetching schema to validate the created data for type: " + type); const mdmsResponse = await callMdmsTypeSchema(request, tenantId, type); diff --git a/utilities/project-factory/src/server/api/genericApis.ts b/utilities/project-factory/src/server/api/genericApis.ts index 36e370dc937..c7fa9ced8e0 100644 --- a/utilities/project-factory/src/server/api/genericApis.ts +++ b/utilities/project-factory/src/server/api/genericApis.ts @@ -61,11 +61,7 @@ function getJsonData(sheetData: any, getRow = false, getSheetName = false, sheet } function validateFirstRowColumn(createAndSearchConfig: any, worksheet: any, localizationMap: any) { - if ( - createAndSearchConfig && - createAndSearchConfig.parseArrayConfig && - createAndSearchConfig.parseArrayConfig.parseLogic - ) { + if (createAndSearchConfig?.parseArrayConfig?.parseLogic) { const parseLogic = createAndSearchConfig.parseArrayConfig.parseLogic; // Iterate over each column configuration for (const columnConfig of parseLogic) { diff --git a/utilities/project-factory/src/server/config/createAndSearch.ts b/utilities/project-factory/src/server/config/createAndSearch.ts index d89c45b7606..8fb63b1be9f 100644 --- a/utilities/project-factory/src/server/config/createAndSearch.ts +++ b/utilities/project-factory/src/server/config/createAndSearch.ts @@ -132,6 +132,138 @@ const createAndSearch: any = { searchPath: "Facilities" } }, + "facilityMicroplan": { + requiresToSearchFromSheet: [ + { + sheetColumnName: "HCM_ADMIN_CONSOLE_FACILITY_CODE", + searchPath: "Facility.id" + } + ], + boundaryValidation: { + column: "HCM_ADMIN_CONSOLE_BOUNDARY_CODE_MANDATORY" + }, + sheetSchema: { + "$schema": "http://json-schema.org/draft-07/schema#", + "title": "FacilityTemplateSchema", + "type": "object", + "properties": { + "Facility Name": { + "type": "string", + "maxLength": 2000, + "minLength": 1 + }, + "Facility Type": { + // "type": "string", + "enum": ["Warehouse", "Health Facility", "Storing Resource"] + }, + "Facility Status": { + // "type": "string", + "enum": ["Temporary", "Permanent"] + }, + "Capacity": { + "type": "number", + "minimum": 1, + "maximum": 100000000 + } + }, + "required": [ + "Facility Name", + "Facility Type", + "Facility Status", + "Capacity" + ], + "unique": [ + "Facility Name" + ] + }, + uniqueIdentifier: "id", + uniqueIdentifierColumn: "A", + activeColumn: "F", + activeColumnName: "HCM_ADMIN_CONSOLE_FACILITY_USAGE_MICROPLAN", + uniqueIdentifierColumnName: "HCM_ADMIN_CONSOLE_FACILITY_CODE", + matchEachKey: true, + parseArrayConfig: { + sheetName: "HCM_ADMIN_CONSOLE_FACILITIES", + parseLogic: [ + { + sheetColumn: "A", + sheetColumnName: "HCM_ADMIN_CONSOLE_FACILITY_CODE", + resultantPath: "id", + type: "string" + }, + { + sheetColumn: "B", + sheetColumnName: "HCM_ADMIN_CONSOLE_FACILITY_NAME_MICROPLAN", + resultantPath: "name", + type: "string" + }, + { + sheetColumn: "C", + sheetColumnName: "HCM_ADMIN_CONSOLE_FACILITY_TYPE_MICROPLAN", + resultantPath: "usage", + type: "string" + }, + { + sheetColumn: "D", + sheetColumnName: "HCM_ADMIN_CONSOLE_FACILITY_STATUS_MICROPLAN", + resultantPath: "isPermanent", + type: "boolean", + conversionCondition: { + "Permanent": "true", + "Temporary": "" + } + }, + { + sheetColumn: "E", + sheetColumnName: "HCM_ADMIN_CONSOLE_FACILITY_CAPACITY_MICROPLAN", + resultantPath: "storageCapacity", + type: "number" + }, + { + sheetColumn: "J", + sheetColumnName: "HCM_ADMIN_CONSOLE_RESIDING_BOUNDARY_CODE_MICROPLAN" + } + ], + tenantId: { + getValueViaPath: "ResourceDetails.tenantId", + resultantPath: "tenantId" + } + }, + createBulkDetails: { + limit: 50, + createPath: "Facilities", + url: config.host.facilityHost + "facility/v1/bulk/_create" + }, + searchDetails: { + searchElements: [ + { + keyPath: "tenantId", + getValueViaPath: "ResourceDetails.tenantId", + isInParams: true, + isInBody: false, + }, + { + keyPath: "Facility", + isInParams: false, + isInBody: true, + } + ], + searchLimit: { + keyPath: "limit", + value: "200", + isInParams: true, + isInBody: false, + }, + searchOffset: { + keyPath: "offset", + value: "0", + isInParams: true, + isInBody: false, + }, + url: config.host.facilityHost + "facility/v1/_search", + searchPath: "Facilities" + } + }, "boundary": { parseArrayConfig: { sheetName: "HCM_ADMIN_CONSOLE_BOUNDARY_CODE", diff --git a/utilities/project-factory/src/server/config/models/createRequestSchema.ts b/utilities/project-factory/src/server/config/models/createRequestSchema.ts index ed56e706d16..18a96101fbd 100644 --- a/utilities/project-factory/src/server/config/models/createRequestSchema.ts +++ b/utilities/project-factory/src/server/config/models/createRequestSchema.ts @@ -4,7 +4,7 @@ export const createRequestSchema = { "properties": { "type": { "type": "string", - "enum": ["boundary", "facility", "user", "boundaryWithTarget"] + "enum": ["boundary", "facility", "user", "boundaryWithTarget","facilityMicroplan"] }, "tenantId": { "type": "string", diff --git a/utilities/project-factory/src/server/utils/campaignUtils.ts b/utilities/project-factory/src/server/utils/campaignUtils.ts index f89eaf98703..2a329538a0e 100644 --- a/utilities/project-factory/src/server/utils/campaignUtils.ts +++ b/utilities/project-factory/src/server/utils/campaignUtils.ts @@ -1843,8 +1843,8 @@ async function getBoundaryOnWhichWeSplit(request: any) { } -function checkIfSourceIsMicroplan(campaignObject: any): boolean { - return campaignObject?.additionalDetails?.source === 'microplan'; +function checkIfSourceIsMicroplan(objectWithAdditionalDetails: any): boolean { + return objectWithAdditionalDetails?.additionalDetails?.source === 'microplan'; } diff --git a/utilities/project-factory/src/server/utils/genericUtils.ts b/utilities/project-factory/src/server/utils/genericUtils.ts index 1e3f6166c85..ce3709a0774 100644 --- a/utilities/project-factory/src/server/utils/genericUtils.ts +++ b/utilities/project-factory/src/server/utils/genericUtils.ts @@ -466,17 +466,23 @@ async function createFacilitySheet(request: any, allFacilities: any[], localizat const tenantId = request?.query?.tenantId; const responseFromCampaignSearch = await getCampaignSearchResponse(request); const isSourceMicroplan = checkIfSourceIsMicroplan(responseFromCampaignSearch?.CampaignDetails?.[0]); - let schema: any; + let schema; if (isSourceMicroplan) { schema = await callMdmsTypeSchema(request, tenantId, "facility", "microplan"); } else { - schema = await callMdmsTypeSchema(request, tenantId, "facility"); + schema = await callMdmsTypeSchema(request, tenantId, "facility", "all"); } const keys = schema?.columns; setDropdownFromSchema(request, schema, localizationMap); const headers = ["HCM_ADMIN_CONSOLE_FACILITY_CODE", ...keys] - const localizedHeaders = getLocalizedHeaders(headers, localizationMap); + let localizedHeaders; + if (isSourceMicroplan) { + localizedHeaders = getLocalizedHeadersForMicroplan(responseFromCampaignSearch, headers, localizationMap); + } + else { + localizedHeaders = getLocalizedHeaders(headers, localizationMap); + } const facilities = allFacilities.map((facility: any) => { return [ facility?.id, @@ -577,6 +583,21 @@ function getLocalizedHeaders(headers: any, localizationMap?: { [key: string]: st return messages; } +function getLocalizedHeadersForMicroplan(responseFromCampaignSearch: any, headers: any, localizationMap?: { [key: string]: string }) { + + const projectType = responseFromCampaignSearch?.CampaignDetails?.[0]?.projectType; + + headers = headers.map((header: string) => { + if (header === 'HCM_ADMIN_CONSOLE_FACILITY_CAPACITY_MICROPLAN') { + return `${header}_${projectType}`; + } + return header; + }); + + const messages = headers.map((header: any) => (localizationMap ? localizationMap[header] || header : header)); + return messages; +} + function modifyRequestForLocalisation(request: any, tenantId: string) { @@ -1086,7 +1107,15 @@ function getDifferentDistrictTabs(boundaryData: any, differentTabsBasedOnLevel: async function getConfigurableColumnHeadersFromSchemaForTargetSheet(request: any, hierarchy: any, boundaryData: any, differentTabsBasedOnLevel: any, campaignObject: any, localizationMap?: any) { const districtIndex = hierarchy.indexOf(differentTabsBasedOnLevel); - var headers = getLocalizedHeaders(hierarchy.slice(districtIndex), localizationMap); + let headers: any; + const isSourceMicroplan = checkIfSourceIsMicroplan(campaignObject); + if (isSourceMicroplan) { + logger.info(`Source is Microplan.`); + headers = getLocalizedHeaders(hierarchy, localizationMap); + } + else { + headers = getLocalizedHeaders(hierarchy.slice(districtIndex), localizationMap); + } const headerColumnsAfterHierarchy = await generateDynamicTargetHeaders(request, campaignObject, localizationMap); const localizedHeadersAfterHierarchy = getLocalizedHeaders(headerColumnsAfterHierarchy, localizationMap); headers = [...headers, getLocalizedName(config?.boundary?.boundaryCode, localizationMap), ...localizedHeadersAfterHierarchy] @@ -1105,6 +1134,49 @@ async function getMdmsDataBasedOnCampaignType(request: any, localizationMap?: an } +function appendProjectTypeToCapacity(schema: any, projectType: string): any { + const updatedSchema = JSON.parse(JSON.stringify(schema)); // Deep clone the schema + + const capacityKey = 'HCM_ADMIN_CONSOLE_FACILITY_CAPACITY_MICROPLAN'; + const newCapacityKey = `${capacityKey}_${projectType}`; + + // Update properties + if (updatedSchema.properties[capacityKey]) { + updatedSchema.properties[newCapacityKey] = { + ...updatedSchema.properties[capacityKey], + name: `${updatedSchema.properties[capacityKey].name}_${projectType}` + }; + delete updatedSchema.properties[capacityKey]; + } + + // Update required + updatedSchema.required = updatedSchema.required.map((item: string) => + item === capacityKey ? newCapacityKey : item + ); + + // Update columns + updatedSchema.columns = updatedSchema.columns.map((item: string) => + item === capacityKey ? newCapacityKey : item + ); + + // Update unique + updatedSchema.unique = updatedSchema.unique.map((item: string) => + item === capacityKey ? newCapacityKey : item + ); + + // Update errorMessage + if (updatedSchema.errorMessage[capacityKey]) { + updatedSchema.errorMessage[newCapacityKey] = updatedSchema.errorMessage[capacityKey]; + delete updatedSchema.errorMessage[capacityKey]; + } + + // Update columnsNotToBeFreezed + updatedSchema.columnsNotToBeFreezed = updatedSchema.columnsNotToBeFreezed.map((item: string) => + item === capacityKey ? newCapacityKey : item + ); + + return updatedSchema; +} export { @@ -1152,7 +1224,8 @@ export { getConfigurableColumnHeadersFromSchemaForTargetSheet, createBoundaryDataMainSheet, getMdmsDataBasedOnCampaignType, - shutdownGracefully + shutdownGracefully, + appendProjectTypeToCapacity };