From 8443bec32ca05cb002b6294f18264ec37f7322ad Mon Sep 17 00:00:00 2001 From: vishwanath1004 <50612405+vishwanath1004@users.noreply.github.com> Date: Tue, 5 May 2026 18:29:45 +0530 Subject: [PATCH 1/7] Adding the unique id into entity and entityType --- src/controllers/v1/entities.js | 13 ++- src/controllers/v1/entityTypes.js | 9 +- src/generics/helpers/utils.js | 13 +++ src/models/entities.js | 8 ++ src/models/entityTypes.js | 4 + src/module/entities/helper.js | 144 ++++++++++++++++++++++------ src/module/entities/validator/v1.js | 1 - src/module/entityTypes/helper.js | 44 ++++++++- 8 files changed, 197 insertions(+), 39 deletions(-) diff --git a/src/controllers/v1/entities.js b/src/controllers/v1/entities.js index 65b030c..789827f 100644 --- a/src/controllers/v1/entities.js +++ b/src/controllers/v1/entities.js @@ -150,10 +150,10 @@ module.exports = class Entities extends Abstract { 'metaInformation.administration', 'metaInformation.city', 'metaInformation.country', - 'entityTypeId', + 'entityTypeUniqueId', 'entityType', ] - let tenantId = req.userDetails.userInformation.tenantId + let tenantId = req.userDetails?.tenantAndOrgInfo?.tenantId || req.userDetails.userInformation.tenantId let entityDocument = await entitiesQueries.entityDocuments( { _id: req.params._id, tenantId: tenantId }, projection @@ -168,7 +168,8 @@ module.exports = class Entities extends Abstract { let relatedEntities = await entitiesHelper.relatedEntities( entityDocument[0]._id, - entityDocument[0].entityTypeId, + entityDocument[0].entityTypeUniqueId || + UTILS.generateEntityTypeUniqueId(entityDocument[0].entityType, tenantId), entityDocument[0].entityType, projection, tenantId @@ -622,6 +623,7 @@ module.exports = class Entities extends Abstract { // Prepare query parameters for adding the entity let queryParams = { type: req.query.type, + entityTypeUniqueId: req.query.entityTypeUniqueId, parentEntityId: req.query.parentEntityId, } // Call 'entitiesHelper.add' to perform the entity addition operation @@ -991,7 +993,10 @@ module.exports = class Entities extends Abstract { translationFile = JSON.parse(req.files.translationFile.data.toString()) } let newEntityData = await entitiesHelper.bulkCreate( - req.query.type, + { + type: req.query.type, + entityTypeUniqueId: req.query.entityTypeUniqueId, + }, null, null, req.userDetails, diff --git a/src/controllers/v1/entityTypes.js b/src/controllers/v1/entityTypes.js index e5509a0..6cd37c1 100644 --- a/src/controllers/v1/entityTypes.js +++ b/src/controllers/v1/entityTypes.js @@ -62,7 +62,6 @@ module.exports = class EntityTypes extends Abstract { try { let organizationId let query = {} - // create query to fetch assets query['tenantId'] = req.userDetails.tenantAndOrgInfo ? req.userDetails.tenantAndOrgInfo.tenantId @@ -73,7 +72,13 @@ module.exports = class EntityTypes extends Abstract { organizationId = req.userDetails.userInformation.organizationId query['orgId'] = { $in: [organizationId] } } - let result = await entityTypesHelper.list(query, ['name'], req.pageNo, req.pageSize, req.searchText) + let result = await entityTypesHelper.list( + query, + ['name', 'entityTypeUniqueId'], + req.pageNo, + req.pageSize, + req.searchText + ) return resolve(result) } catch (error) { diff --git a/src/generics/helpers/utils.js b/src/generics/helpers/utils.js index c5b78eb..c19d3a8 100644 --- a/src/generics/helpers/utils.js +++ b/src/generics/helpers/utils.js @@ -238,6 +238,18 @@ function generateUniqueId() { return uuidV4() } +/** + * Build a tenant-scoped unique identifier for entity types. + * @function + * @name generateEntityTypeUniqueId + * @param {String} name - entity type name. + * @param {String} tenantId - tenant id. + * @returns {String} - unique identifier derived from name and tenant. + */ +function generateEntityTypeUniqueId(name, tenantId) { + return `${String(name || '').trim()}_${String(tenantId || '').trim()}` +} + // Helper function to convert mongo ids to objectIds to facilitate proper query in aggregate function function convertMongoIds(query) { const keysToConvert = ['_id', 'entityTypeId'] // Add other fields if needed @@ -326,6 +338,7 @@ module.exports = { noOfElementsInArray: noOfElementsInArray, operatorValidation: operatorValidation, generateUniqueId: generateUniqueId, + generateEntityTypeUniqueId: generateEntityTypeUniqueId, convertMongoIds: convertMongoIds, stripOrgIds: stripOrgIds, convertOrgIdsToString: convertOrgIdsToString, diff --git a/src/models/entities.js b/src/models/entities.js index 8efbfc6..6f01ac7 100644 --- a/src/models/entities.js +++ b/src/models/entities.js @@ -9,10 +9,18 @@ module.exports = { name: 'entities', schema: { entityTypeId: 'ObjectId', + entityTypeUniqueId: { + type: String, + index: true, + }, entityType: { type: String, index: true, }, + entityUniqueId: { + type: String, + index: true, + }, groups: Object, metaInformation: { externalId: { type: String, index: true }, diff --git a/src/models/entityTypes.js b/src/models/entityTypes.js index 6a23dd8..50e12f3 100644 --- a/src/models/entityTypes.js +++ b/src/models/entityTypes.js @@ -11,6 +11,10 @@ module.exports = { name: { type: String, }, + entityTypeUniqueId: { + type: String, + index: true, + }, profileForm: Array, profileFields: Array, types: Array, diff --git a/src/module/entities/helper.js b/src/module/entities/helper.js index ba1c94b..af87526 100644 --- a/src/module/entities/helper.js +++ b/src/module/entities/helper.js @@ -15,6 +15,8 @@ const { Parser } = require('json2csv') const _ = require('lodash') +const buildEntityTypeUniqueId = (name, tenantId) => UTILS.generateEntityTypeUniqueId(name, tenantId) + /** * UserProjectsHelper * @class @@ -828,7 +830,8 @@ module.exports = class UserProjectsHelper { const projectedData = { _id: 1, entityType: 1, - entityTypeId: 1, + entityTypeUniqueId: 1, + tenantId: 1, childHierarchyPath: 1, } @@ -900,7 +903,8 @@ module.exports = class UserProjectsHelper { if (updateParentHierarchy) { const relatedEntities = await this.relatedEntities( parentEntity._id, - parentEntity.entityTypeId, + parentEntity.entityTypeUniqueId || + buildEntityTypeUniqueId(parentEntity.entityType, parentEntity.tenantId), parentEntity.entityType, ['_id'] ) @@ -966,14 +970,14 @@ module.exports = class UserProjectsHelper { * @method * @name relatedEntities * @param {String} entityId - entity id. - * @param {String} entityTypeId - entity type id. + * @param {String} entityTypeUniqueId - entity type unique id. * @param {String} entityType - entity type. * @param {Array} [projection = "all"] - total fields to be projected. * @param {String} tenantId - user's tenant id * @returns {Array} - returns an array of related entities data. */ - static relatedEntities(entityId, entityTypeId, entityType, projection = 'all', tenantId) { + static relatedEntities(entityId, entityTypeUniqueId, entityType, projection = 'all', tenantId) { return new Promise(async (resolve, reject) => { try { // if ( @@ -989,10 +993,10 @@ module.exports = class UserProjectsHelper { tenantId, } - if (entityTypeId && entityId && entityType) { + if (entityTypeUniqueId && entityId && entityType) { relatedEntitiesQuery[`groups.${entityType}`] = entityId - relatedEntitiesQuery['entityTypeId'] = {} - relatedEntitiesQuery['entityTypeId']['$ne'] = entityTypeId + relatedEntitiesQuery['entityTypeUniqueId'] = {} + relatedEntitiesQuery['entityTypeUniqueId']['$ne'] = entityTypeUniqueId } else { throw { status: HTTP_STATUS_CODE.bad_request.status, @@ -1136,6 +1140,7 @@ module.exports = class UserProjectsHelper { 'metaInformation', 'entityType', 'entityTypeId', + 'entityTypeUniqueId', 'registryDetails', ]) if (!entities.length > 0) { @@ -1418,14 +1423,27 @@ module.exports = class UserProjectsHelper { static add(queryParams, data, userDetails) { return new Promise(async (resolve, reject) => { try { - // Find the entities document based on the entityType in queryParams + console.log('queryParams -----> ', queryParams) + console.log('entityTypeQuery -----> ', queryParams.entityTypeUniqueId) let tenantId = userDetails.tenantAndOrgInfo.tenantId let orgId = userDetails.tenantAndOrgInfo.orgId[0] - let entityTypeDocument = await entityTypeQueries.findOne( - { name: queryParams.type, tenantId: tenantId }, - { _id: 1 } - ) + const entityTypeUniqueId = + queryParams.entityTypeUniqueId || buildEntityTypeUniqueId(queryParams.type, tenantId) + const entityTypeQuery = queryParams.entityTypeUniqueId + ? { + tenantId: tenantId, + entityTypeUniqueId: entityTypeUniqueId, + } + : { + tenantId: tenantId, + $or: [{ entityTypeUniqueId: entityTypeUniqueId }, { name: queryParams.type }], + } + let entityTypeDocument = await entityTypeQueries.findOne(entityTypeQuery, { + _id: 1, + entityTypeUniqueId: 1, + name: 1, + }) if (!entityTypeDocument) { throw { status: HTTP_STATUS_CODE.bad_request.status, @@ -1482,9 +1500,13 @@ module.exports = class UserProjectsHelper { : [] // Construct the entity document to be created let entityDoc = { - entityTypeId: entityTypeDocument._id, + entityTypeUniqueId: + entityTypeDocument.entityTypeUniqueId || + entityTypeUniqueId || + buildEntityTypeUniqueId(entityTypeDocument.name, tenantId), childHierarchyPath: childHierarchyPath, - entityType: queryParams.type, + entityType: entityTypeDocument.name || queryParams.type, + entityUniqueId: singleEntity.externalId, registryDetails: registryDetails, groups: {}, metaInformation: _.omit(singleEntity, ['locationId', 'code']), @@ -1720,18 +1742,67 @@ module.exports = class UserProjectsHelper { static bulkCreate(entityType, programId, solutionId, userDetails, entityCSVData, translationFile) { return new Promise(async (resolve, reject) => { try { - // Flag to track if at least one record is successfully processed - let hasSuccess = false + const entityTypeName = typeof entityType === 'object' ? entityType.type : entityType + const entityTypeUniqueIdInput = typeof entityType === 'object' ? entityType.entityTypeUniqueId : '' + + // let solutionsDocument = new Array() + // if (programId && solutionId) { + + // solutionsDocument = await database.models.entityTypes + // .find( + // { + // externalId: solutionId, + // programExternalId: programId, + // }, + // { + // programId: 1, + // externalId: 1, + // subType: 1, + // entityType: 1, + // entityTypeId: 1, + // } + // ) + // .lean() + // } + + // let solutionsData + + // if (solutionsDocument.length) { + // solutionsData = solutionsDocument.reduce( + // (ac, entities) => ({ + // ...ac, + // [entities.metaInformation.externalId]: { + // subType: entities.subType, + // solutionId: entities._id, + // programId: entities.programId, + // entityType: entities.entityType, + // entityTypeId: entities.entityTypeId, + // newEntities: new Array(), + // }, + // }), + // {} + // ) + // } + // Find the entity type document based on the provided entityType let tenantId = userDetails.tenantAndOrgInfo.tenantId let orgId = userDetails.tenantAndOrgInfo.orgId[0] - let entityTypeDocument = await entityTypeQueries.findOne( - { - name: entityType, - tenantId: tenantId, - }, - { _id: 1, tenantId: 1 } - ) + const entityTypeUniqueId = entityTypeUniqueIdInput || buildEntityTypeUniqueId(entityTypeName, tenantId) + const entityTypeQuery = entityTypeUniqueIdInput + ? { + tenantId: tenantId, + entityTypeUniqueId: entityTypeUniqueId, + } + : { + tenantId: tenantId, + $or: [{ entityTypeUniqueId: entityTypeUniqueId }, { name: entityTypeName }], + } + let entityTypeDocument = await entityTypeQueries.findOne(entityTypeQuery, { + _id: 1, + tenantId: 1, + entityTypeUniqueId: 1, + name: 1, + }) if (!entityTypeDocument) { throw { status: HTTP_STATUS_CODE.bad_request.status, @@ -1748,8 +1819,11 @@ module.exports = class UserProjectsHelper { ? userDetails.userInformation.userId : CONSTANTS.common.SYSTEM let entityCreation = { - entityTypeId: entityTypeDocument._id, - entityType: entityType, + entityTypeUniqueId: + entityTypeDocument.entityTypeUniqueId || + entityTypeUniqueId || + buildEntityTypeUniqueId(entityTypeDocument.name, tenantId), + entityType: entityTypeDocument.name || entityTypeName, registryDetails: {}, groups: {}, updatedBy: userId, @@ -1801,6 +1875,7 @@ module.exports = class UserProjectsHelper { code: externalId, locationId: externalId, } + entityCreation.entityUniqueId = externalId } if (translationFile) { @@ -1812,6 +1887,7 @@ module.exports = class UserProjectsHelper { } singleEntity['_SYSTEM_ID'] = newEntity._id.toString() + singleEntity.entityUniqueId = newEntity.entityUniqueId if (singleEntity._SYSTEM_ID) { singleEntity.status = CONSTANTS.apiResponses.SUCCESS @@ -2055,23 +2131,28 @@ module.exports = class UserProjectsHelper { static listEntitiesByType(req) { return new Promise(async (resolve, reject) => { try { + console.log('req.userDetails -----> ', req.userDetails) // Retrieve the schema meta information key let schemaMetaInformation = this.entitiesSchemaData().SCHEMA_METAINFORMATION - let tenantId = req.userDetails.userInformation.tenantId + let tenantId = req.userDetails?.tenantAndOrgInfo?.tenantId || req.userDetails?.userInformation?.tenantId // Define projection for entity document fields to retrieve let projection = [ schemaMetaInformation + '.externalId', schemaMetaInformation + '.name', 'registryDetails.locationId', + 'entityTypeUniqueId', ] // Calculate skipping value based on pagination parameters let skippingValue = req.pageSize * (req.pageNo - 1) - // Query entities based on entity type ID + // Query entities based on entity type unique id, with legacy entityTypeId fallback. + const entityTypeFilter = UTILS.strictObjectIdCheck(req.params._id) + ? { entityTypeId: ObjectId(req.params._id) } + : { entityTypeUniqueId: req.params._id } let entityDocuments = await entitiesQueries.entityDocuments( { - entityTypeId: ObjectId(req.params._id), + ...entityTypeFilter, tenantId: tenantId, }, projection, @@ -2210,7 +2291,7 @@ module.exports = class UserProjectsHelper { metaInformation: 1, groups: 1, entityType: 1, - entityTypeId: 1, + entityTypeUniqueId: 1, }, }, { @@ -2238,6 +2319,7 @@ module.exports = class UserProjectsHelper { entity.metaInformation.childrenCount = 0 entity.metaInformation.entityType = entity.entityType entity.metaInformation.entityTypeId = entity.entityTypeId + entity.metaInformation.entityTypeUniqueId = entity.entityTypeUniqueId entity.metaInformation.subEntityGroups = new Array() entity.groups && @@ -2351,12 +2433,14 @@ async function populateTargetedEntityTypesData(targetedEntityTypes, tenantId) { name: { $in: targetedEntityTypes }, tenantId: tenantId, }, - ['name', '_id'] + ['name', '_id', 'entityTypeUniqueId'] ) formattedTargetedEntityTypes.forEach((entityType) => { entityType['entityTypeId'] = entityType._id.toString() entityType['entityType'] = entityType.name + entityType['entityTypeUniqueId'] = + entityType.entityTypeUniqueId || buildEntityTypeUniqueId(entityType.name, tenantId) delete entityType._id delete entityType.name }) diff --git a/src/module/entities/validator/v1.js b/src/module/entities/validator/v1.js index 5946d04..43c5589 100644 --- a/src/module/entities/validator/v1.js +++ b/src/module/entities/validator/v1.js @@ -102,7 +102,6 @@ module.exports = (req) => { }, listByEntityType: function () { req.checkParams('_id').exists().withMessage('required Entity type') - req.checkParams('_id').exists().isMongoId().withMessage('Invalid Entity ID') }, subEntityListBasedOnRoleAndLocation: function () { req.checkParams('_id').exists().withMessage('required state location id') diff --git a/src/module/entityTypes/helper.js b/src/module/entityTypes/helper.js index a3ec997..07866f6 100644 --- a/src/module/entityTypes/helper.js +++ b/src/module/entityTypes/helper.js @@ -7,10 +7,13 @@ // Dependencies const entityTypeQueries = require(DB_QUERY_BASE_PATH + '/entityTypes') +const entitiesQueries = require(DB_QUERY_BASE_PATH + '/entities') // const entitiesHelper = require(MODULES_BASE_PATH + "/entities/helper") // const programUsersQueries = require(DB_QUERY_BASE_PATH + "/programUsers") +const buildEntityTypeUniqueId = (name, tenantId) => UTILS.generateEntityTypeUniqueId(name, tenantId) + /** * UserProjectsHelper * @class @@ -36,6 +39,10 @@ module.exports = class UserProjectsHelper { entityType = UTILS.valueParser(entityType) entityType['tenantId'] = userDetails.tenantAndOrgInfo.tenantId entityType['orgId'] = userDetails.tenantAndOrgInfo.orgId[0] + entityType['entityTypeUniqueId'] = buildEntityTypeUniqueId( + entityType.name, + entityType['tenantId'] + ) entityType.registryDetails = {} let removedKeys = [] @@ -184,6 +191,8 @@ module.exports = class UserProjectsHelper { ? userDetails.userInformation.userId : CONSTANTS.common.SYSTEM + entityType.entityTypeUniqueId = buildEntityTypeUniqueId(entityType.name, entityType.tenantId) + let newEntityType = await entityTypeQueries.create( _.merge( { @@ -230,6 +239,11 @@ module.exports = class UserProjectsHelper { delete bodyData.orgIds let tenantId = userDetails.tenantAndOrgInfo.tenantId + const existingEntityType = await entityTypeQueries.findOne( + { _id: ObjectId(entityTypeId), tenantId: tenantId }, + { name: 1, entityTypeUniqueId: 1 } + ) + bodyData.entityTypeUniqueId = buildEntityTypeUniqueId(bodyData.name, tenantId) // Find and update the entity type by ID with the provided bodyData let entityInformation = await entityTypeQueries.findOneAndUpdate( @@ -242,6 +256,18 @@ module.exports = class UserProjectsHelper { return reject({ status: 404, message: CONSTANTS.apiResponses.ENTITYTYPE_NOT_FOUND }) } + if (existingEntityType && existingEntityType.entityTypeUniqueId !== bodyData.entityTypeUniqueId) { + await entitiesQueries.updateMany( + { entityTypeId: ObjectId(entityTypeId), tenantId: tenantId }, + { + $set: { + entityType: bodyData.name, + entityTypeUniqueId: bodyData.entityTypeUniqueId, + }, + } + ) + } + resolve({ success: true, message: CONSTANTS.apiResponses.ENTITY_UPDATATED, @@ -321,10 +347,12 @@ module.exports = class UserProjectsHelper { // Get the userId from userDetails or default to SYSTEM const userId = - userDetails && userDetails.userInformation.id - ? userDetails && userDetails.userInformation.id + userDetails && userDetails.userInformation.userId + ? userDetails && userDetails.userInformation.userId : CONSTANTS.common.SYSTEM + entityType.entityTypeUniqueId = buildEntityTypeUniqueId(entityType.name, tenantId) + if (!entityType.name) { entityType['_SYSTEM_ID'] = '' entityType.status = CONSTANTS.apiResponses.ENTITY_TYPE_FAILED @@ -346,6 +374,18 @@ module.exports = class UserProjectsHelper { ) ) + if (updateEntityType && updateEntityType._id) { + await entitiesQueries.updateMany( + { entityTypeId: ObjectId(entityType._SYSTEM_ID), tenantId: tenantId }, + { + $set: { + entityType: entityType.name, + entityTypeUniqueId: entityType.entityTypeUniqueId, + }, + } + ) + } + delete entityType.registryDetails if (updateEntityType._id) { From f4ef412c5d983b6ceb0358f4cbf8059c32c308f2 Mon Sep 17 00:00:00 2001 From: vishwanath1004 <50612405+vishwanath1004@users.noreply.github.com> Date: Fri, 8 May 2026 09:14:42 +0530 Subject: [PATCH 2/7] PR comments resolving : changing from entityUniqueId to code --- src/models/entities.js | 2 +- src/module/entities/helper.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/models/entities.js b/src/models/entities.js index 6f01ac7..fe978af 100644 --- a/src/models/entities.js +++ b/src/models/entities.js @@ -17,7 +17,7 @@ module.exports = { type: String, index: true, }, - entityUniqueId: { + code: { type: String, index: true, }, diff --git a/src/module/entities/helper.js b/src/module/entities/helper.js index af87526..9e66804 100644 --- a/src/module/entities/helper.js +++ b/src/module/entities/helper.js @@ -1506,7 +1506,7 @@ module.exports = class UserProjectsHelper { buildEntityTypeUniqueId(entityTypeDocument.name, tenantId), childHierarchyPath: childHierarchyPath, entityType: entityTypeDocument.name || queryParams.type, - entityUniqueId: singleEntity.externalId, + code: singleEntity.externalId, registryDetails: registryDetails, groups: {}, metaInformation: _.omit(singleEntity, ['locationId', 'code']), @@ -1875,7 +1875,7 @@ module.exports = class UserProjectsHelper { code: externalId, locationId: externalId, } - entityCreation.entityUniqueId = externalId + entityCreation.code = externalId } if (translationFile) { @@ -1887,7 +1887,7 @@ module.exports = class UserProjectsHelper { } singleEntity['_SYSTEM_ID'] = newEntity._id.toString() - singleEntity.entityUniqueId = newEntity.entityUniqueId + singleEntity.code = newEntity.code if (singleEntity._SYSTEM_ID) { singleEntity.status = CONSTANTS.apiResponses.SUCCESS From f98be5ffcc6d6637baeddb25cdbca43b285e64a5 Mon Sep 17 00:00:00 2001 From: vishwanath1004 <50612405+vishwanath1004@users.noreply.github.com> Date: Sun, 10 May 2026 16:01:03 +0530 Subject: [PATCH 3/7] PR comments resolving : changing from entityUniqueId to code --- src/controllers/v1/entities.js | 12 ++--- src/controllers/v1/entityTypes.js | 2 +- src/generics/helpers/utils.js | 8 ++-- src/models/entities.js | 6 ++- src/models/entityTypes.js | 2 +- src/module/entities/helper.js | 77 ++++++++++++++++--------------- 6 files changed, 56 insertions(+), 51 deletions(-) diff --git a/src/controllers/v1/entities.js b/src/controllers/v1/entities.js index 789827f..9431e8f 100644 --- a/src/controllers/v1/entities.js +++ b/src/controllers/v1/entities.js @@ -150,10 +150,10 @@ module.exports = class Entities extends Abstract { 'metaInformation.administration', 'metaInformation.city', 'metaInformation.country', - 'entityTypeUniqueId', + 'entityTypeCode', 'entityType', ] - let tenantId = req.userDetails?.tenantAndOrgInfo?.tenantId || req.userDetails.userInformation.tenantId + let tenantId = req.userDetails.userInformation.tenantId let entityDocument = await entitiesQueries.entityDocuments( { _id: req.params._id, tenantId: tenantId }, projection @@ -168,8 +168,8 @@ module.exports = class Entities extends Abstract { let relatedEntities = await entitiesHelper.relatedEntities( entityDocument[0]._id, - entityDocument[0].entityTypeUniqueId || - UTILS.generateEntityTypeUniqueId(entityDocument[0].entityType, tenantId), + entityDocument[0].entityTypeCode || + UTILS.generateEntityTypeCode(entityDocument[0].entityType, tenantId), entityDocument[0].entityType, projection, tenantId @@ -623,7 +623,7 @@ module.exports = class Entities extends Abstract { // Prepare query parameters for adding the entity let queryParams = { type: req.query.type, - entityTypeUniqueId: req.query.entityTypeUniqueId, + entityTypeCode: req.query.entityTypeCode, parentEntityId: req.query.parentEntityId, } // Call 'entitiesHelper.add' to perform the entity addition operation @@ -995,7 +995,7 @@ module.exports = class Entities extends Abstract { let newEntityData = await entitiesHelper.bulkCreate( { type: req.query.type, - entityTypeUniqueId: req.query.entityTypeUniqueId, + entityTypeCode: req.query.entityTypeCode, }, null, null, diff --git a/src/controllers/v1/entityTypes.js b/src/controllers/v1/entityTypes.js index 6cd37c1..6d893c3 100644 --- a/src/controllers/v1/entityTypes.js +++ b/src/controllers/v1/entityTypes.js @@ -74,7 +74,7 @@ module.exports = class EntityTypes extends Abstract { } let result = await entityTypesHelper.list( query, - ['name', 'entityTypeUniqueId'], + ['name', 'entityTypeCode'], req.pageNo, req.pageSize, req.searchText diff --git a/src/generics/helpers/utils.js b/src/generics/helpers/utils.js index c19d3a8..43235d1 100644 --- a/src/generics/helpers/utils.js +++ b/src/generics/helpers/utils.js @@ -241,18 +241,18 @@ function generateUniqueId() { /** * Build a tenant-scoped unique identifier for entity types. * @function - * @name generateEntityTypeUniqueId + * @name generateEntityTypeCode * @param {String} name - entity type name. * @param {String} tenantId - tenant id. * @returns {String} - unique identifier derived from name and tenant. */ -function generateEntityTypeUniqueId(name, tenantId) { +function generateEntityTypeCode(name, tenantId) { return `${String(name || '').trim()}_${String(tenantId || '').trim()}` } // Helper function to convert mongo ids to objectIds to facilitate proper query in aggregate function function convertMongoIds(query) { - const keysToConvert = ['_id', 'entityTypeId'] // Add other fields if needed + const keysToConvert = ['_id', 'entityTypeCode'] // Add other fields if needed const convertValue = (value) => { if (Array.isArray(value)) { @@ -338,7 +338,7 @@ module.exports = { noOfElementsInArray: noOfElementsInArray, operatorValidation: operatorValidation, generateUniqueId: generateUniqueId, - generateEntityTypeUniqueId: generateEntityTypeUniqueId, + generateEntityTypeCode: generateEntityTypeCode, convertMongoIds: convertMongoIds, stripOrgIds: stripOrgIds, convertOrgIdsToString: convertOrgIdsToString, diff --git a/src/models/entities.js b/src/models/entities.js index fe978af..7a14056 100644 --- a/src/models/entities.js +++ b/src/models/entities.js @@ -9,7 +9,7 @@ module.exports = { name: 'entities', schema: { entityTypeId: 'ObjectId', - entityTypeUniqueId: { + entityTypeCode: { type: String, index: true, }, @@ -62,5 +62,9 @@ module.exports = { name: { 'metaInformation.externalId': 1, tenantId: 1 }, indexType: { unique: true }, }, + { + name: { code: 1, tenantId: 1 }, + indexType: { unique: true }, + }, ], } diff --git a/src/models/entityTypes.js b/src/models/entityTypes.js index 50e12f3..500c884 100644 --- a/src/models/entityTypes.js +++ b/src/models/entityTypes.js @@ -11,7 +11,7 @@ module.exports = { name: { type: String, }, - entityTypeUniqueId: { + entityTypeCode: { type: String, index: true, }, diff --git a/src/module/entities/helper.js b/src/module/entities/helper.js index 9e66804..7f00ca3 100644 --- a/src/module/entities/helper.js +++ b/src/module/entities/helper.js @@ -15,7 +15,7 @@ const { Parser } = require('json2csv') const _ = require('lodash') -const buildEntityTypeUniqueId = (name, tenantId) => UTILS.generateEntityTypeUniqueId(name, tenantId) +const buildEntityTypeUniqueCode = (name, tenantId) => UTILS.generateEntityTypeUniqueCode(name, tenantId) /** * UserProjectsHelper @@ -830,7 +830,7 @@ module.exports = class UserProjectsHelper { const projectedData = { _id: 1, entityType: 1, - entityTypeUniqueId: 1, + entityTypeUniqueCode: 1, tenantId: 1, childHierarchyPath: 1, } @@ -903,8 +903,8 @@ module.exports = class UserProjectsHelper { if (updateParentHierarchy) { const relatedEntities = await this.relatedEntities( parentEntity._id, - parentEntity.entityTypeUniqueId || - buildEntityTypeUniqueId(parentEntity.entityType, parentEntity.tenantId), + parentEntity.entityTypeUniqueCode || + buildentityTypeUniqueCode(parentEntity.entityType, parentEntity.tenantId), parentEntity.entityType, ['_id'] ) @@ -970,14 +970,14 @@ module.exports = class UserProjectsHelper { * @method * @name relatedEntities * @param {String} entityId - entity id. - * @param {String} entityTypeUniqueId - entity type unique id. + * @param {String} entityTypeUniqueCode - entity type unique id. * @param {String} entityType - entity type. * @param {Array} [projection = "all"] - total fields to be projected. * @param {String} tenantId - user's tenant id * @returns {Array} - returns an array of related entities data. */ - static relatedEntities(entityId, entityTypeUniqueId, entityType, projection = 'all', tenantId) { + static relatedEntities(entityId, entityTypeUniqueCode, entityType, projection = 'all', tenantId) { return new Promise(async (resolve, reject) => { try { // if ( @@ -993,10 +993,10 @@ module.exports = class UserProjectsHelper { tenantId, } - if (entityTypeUniqueId && entityId && entityType) { + if (entityTypeUniqueCode && entityId && entityType) { relatedEntitiesQuery[`groups.${entityType}`] = entityId - relatedEntitiesQuery['entityTypeUniqueId'] = {} - relatedEntitiesQuery['entityTypeUniqueId']['$ne'] = entityTypeUniqueId + relatedEntitiesQuery['entityTypeUniqueCode'] = {} + relatedEntitiesQuery['entityTypeUniqueCode']['$ne'] = entityTypeUniqueCode } else { throw { status: HTTP_STATUS_CODE.bad_request.status, @@ -1140,7 +1140,7 @@ module.exports = class UserProjectsHelper { 'metaInformation', 'entityType', 'entityTypeId', - 'entityTypeUniqueId', + 'entityTypeUniqueCode', 'registryDetails', ]) if (!entities.length > 0) { @@ -1424,24 +1424,24 @@ module.exports = class UserProjectsHelper { return new Promise(async (resolve, reject) => { try { console.log('queryParams -----> ', queryParams) - console.log('entityTypeQuery -----> ', queryParams.entityTypeUniqueId) + console.log('entityTypeQuery -----> ', queryParams.entityTypeUniqueCode) let tenantId = userDetails.tenantAndOrgInfo.tenantId let orgId = userDetails.tenantAndOrgInfo.orgId[0] - const entityTypeUniqueId = - queryParams.entityTypeUniqueId || buildEntityTypeUniqueId(queryParams.type, tenantId) - const entityTypeQuery = queryParams.entityTypeUniqueId + const entityTypeUniqueCode = + queryParams.entityTypeUniqueCode || buildentityTypeUniqueCode(queryParams.type, tenantId) + const entityTypeQuery = queryParams.entityTypeUniqueCode ? { tenantId: tenantId, - entityTypeUniqueId: entityTypeUniqueId, + entityTypeUniqueCode: entityTypeUniqueCode, } : { tenantId: tenantId, - $or: [{ entityTypeUniqueId: entityTypeUniqueId }, { name: queryParams.type }], + $or: [{ entityTypeUniqueCode: entityTypeUniqueCode }, { name: queryParams.type }], } let entityTypeDocument = await entityTypeQueries.findOne(entityTypeQuery, { _id: 1, - entityTypeUniqueId: 1, + entityTypeUniqueCode: 1, name: 1, }) if (!entityTypeDocument) { @@ -1500,10 +1500,10 @@ module.exports = class UserProjectsHelper { : [] // Construct the entity document to be created let entityDoc = { - entityTypeUniqueId: - entityTypeDocument.entityTypeUniqueId || - entityTypeUniqueId || - buildEntityTypeUniqueId(entityTypeDocument.name, tenantId), + entityTypeUniqueCode: + entityTypeDocument.entityTypeUniqueCode || + entityTypeUniqueCode || + buildentityTypeUniqueCode(entityTypeDocument.name, tenantId), childHierarchyPath: childHierarchyPath, entityType: entityTypeDocument.name || queryParams.type, code: singleEntity.externalId, @@ -1743,7 +1743,7 @@ module.exports = class UserProjectsHelper { return new Promise(async (resolve, reject) => { try { const entityTypeName = typeof entityType === 'object' ? entityType.type : entityType - const entityTypeUniqueIdInput = typeof entityType === 'object' ? entityType.entityTypeUniqueId : '' + const entityTypeUniqueCodeInput = typeof entityType === 'object' ? entityType.entityTypeUniqueCode : '' // let solutionsDocument = new Array() // if (programId && solutionId) { @@ -1787,20 +1787,21 @@ module.exports = class UserProjectsHelper { // Find the entity type document based on the provided entityType let tenantId = userDetails.tenantAndOrgInfo.tenantId let orgId = userDetails.tenantAndOrgInfo.orgId[0] - const entityTypeUniqueId = entityTypeUniqueIdInput || buildEntityTypeUniqueId(entityTypeName, tenantId) - const entityTypeQuery = entityTypeUniqueIdInput + const entityTypeUniqueCode = + entityTypeUniqueCodeInput || buildentityTypeUniqueCode(entityTypeName, tenantId) + const entityTypeQuery = entityTypeUniqueCodeInput ? { tenantId: tenantId, - entityTypeUniqueId: entityTypeUniqueId, + entityTypeUniqueCode: entityTypeUniqueCode, } : { tenantId: tenantId, - $or: [{ entityTypeUniqueId: entityTypeUniqueId }, { name: entityTypeName }], + $or: [{ entityTypeUniqueCode: entityTypeUniqueCode }, { name: entityTypeName }], } let entityTypeDocument = await entityTypeQueries.findOne(entityTypeQuery, { _id: 1, tenantId: 1, - entityTypeUniqueId: 1, + entityTypeUniqueCode: 1, name: 1, }) if (!entityTypeDocument) { @@ -1819,10 +1820,10 @@ module.exports = class UserProjectsHelper { ? userDetails.userInformation.userId : CONSTANTS.common.SYSTEM let entityCreation = { - entityTypeUniqueId: - entityTypeDocument.entityTypeUniqueId || - entityTypeUniqueId || - buildEntityTypeUniqueId(entityTypeDocument.name, tenantId), + entityTypeUniqueCode: + entityTypeDocument.entityTypeUniqueCode || + entityTypeUniqueCode || + buildentityTypeUniqueCode(entityTypeDocument.name, tenantId), entityType: entityTypeDocument.name || entityTypeName, registryDetails: {}, groups: {}, @@ -2140,7 +2141,7 @@ module.exports = class UserProjectsHelper { schemaMetaInformation + '.externalId', schemaMetaInformation + '.name', 'registryDetails.locationId', - 'entityTypeUniqueId', + 'entityTypeUniqueCode', ] // Calculate skipping value based on pagination parameters @@ -2149,7 +2150,7 @@ module.exports = class UserProjectsHelper { // Query entities based on entity type unique id, with legacy entityTypeId fallback. const entityTypeFilter = UTILS.strictObjectIdCheck(req.params._id) ? { entityTypeId: ObjectId(req.params._id) } - : { entityTypeUniqueId: req.params._id } + : { entityTypeUniqueCode: req.params._id } let entityDocuments = await entitiesQueries.entityDocuments( { ...entityTypeFilter, @@ -2291,7 +2292,7 @@ module.exports = class UserProjectsHelper { metaInformation: 1, groups: 1, entityType: 1, - entityTypeUniqueId: 1, + entityTypeUniqueCode: 1, }, }, { @@ -2319,7 +2320,7 @@ module.exports = class UserProjectsHelper { entity.metaInformation.childrenCount = 0 entity.metaInformation.entityType = entity.entityType entity.metaInformation.entityTypeId = entity.entityTypeId - entity.metaInformation.entityTypeUniqueId = entity.entityTypeUniqueId + entity.metaInformation.entityTypeUniqueCode = entity.entityTypeUniqueCode entity.metaInformation.subEntityGroups = new Array() entity.groups && @@ -2433,14 +2434,14 @@ async function populateTargetedEntityTypesData(targetedEntityTypes, tenantId) { name: { $in: targetedEntityTypes }, tenantId: tenantId, }, - ['name', '_id', 'entityTypeUniqueId'] + ['name', '_id', 'entityTypeUniqueCode'] ) formattedTargetedEntityTypes.forEach((entityType) => { entityType['entityTypeId'] = entityType._id.toString() entityType['entityType'] = entityType.name - entityType['entityTypeUniqueId'] = - entityType.entityTypeUniqueId || buildEntityTypeUniqueId(entityType.name, tenantId) + entityType['entityTypeUniqueCode'] = + entityType.entityTypeUniqueCode || buildentityTypeUniqueCode(entityType.name, tenantId) delete entityType._id delete entityType.name }) From f8d1dbbf0b67960f20d2e355e8ab0a07370f6842 Mon Sep 17 00:00:00 2001 From: vishwanath1004 <50612405+vishwanath1004@users.noreply.github.com> Date: Sun, 10 May 2026 16:13:31 +0530 Subject: [PATCH 4/7] PR comments resolving : a unique compoundIndex code+tenantId --- src/models/entityTypes.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/models/entityTypes.js b/src/models/entityTypes.js index 500c884..f27f839 100644 --- a/src/models/entityTypes.js +++ b/src/models/entityTypes.js @@ -52,5 +52,9 @@ module.exports = { name: { name: 1, tenantId: 1 }, indexType: { unique: true }, }, + { + name: { entityTypeCode: 1, tenantId: 1 }, + indexType: { unique: true }, + }, ], } From 64bbb72c6e99c009443a78b5d4aa1c4f493608f4 Mon Sep 17 00:00:00 2001 From: vishwanath1004 <50612405+vishwanath1004@users.noreply.github.com> Date: Wed, 13 May 2026 23:43:38 +0530 Subject: [PATCH 5/7] Entity code creation | EntityTypeCode fetch and save --- src/controllers/v1/entities.js | 2 - src/module/entities/helper.js | 192 +++++++++++++-------------------- 2 files changed, 73 insertions(+), 121 deletions(-) diff --git a/src/controllers/v1/entities.js b/src/controllers/v1/entities.js index 9431e8f..96a57ca 100644 --- a/src/controllers/v1/entities.js +++ b/src/controllers/v1/entities.js @@ -620,10 +620,8 @@ module.exports = class Entities extends Abstract { add(req) { return new Promise(async (resolve, reject) => { try { - // Prepare query parameters for adding the entity let queryParams = { type: req.query.type, - entityTypeCode: req.query.entityTypeCode, parentEntityId: req.query.parentEntityId, } // Call 'entitiesHelper.add' to perform the entity addition operation diff --git a/src/module/entities/helper.js b/src/module/entities/helper.js index 7f00ca3..5680910 100644 --- a/src/module/entities/helper.js +++ b/src/module/entities/helper.js @@ -15,7 +15,22 @@ const { Parser } = require('json2csv') const _ = require('lodash') -const buildEntityTypeUniqueCode = (name, tenantId) => UTILS.generateEntityTypeUniqueCode(name, tenantId) +const generateUniqueCode = (name, tenantId) => UTILS.generateUniqueCode(name, tenantId) + +const setEntityCodeAndRegistryDetails = (entityData, externalId, tenantId) => { + if (!externalId) { + return entityData + } + + entityData.code = generateUniqueCode(externalId, tenantId) + entityData.registryDetails = { + ...(entityData.registryDetails || {}), + code: externalId, + locationId: externalId, + } + + return entityData +} /** * UserProjectsHelper @@ -830,7 +845,7 @@ module.exports = class UserProjectsHelper { const projectedData = { _id: 1, entityType: 1, - entityTypeUniqueCode: 1, + entityTypeCode: 1, tenantId: 1, childHierarchyPath: 1, } @@ -903,8 +918,7 @@ module.exports = class UserProjectsHelper { if (updateParentHierarchy) { const relatedEntities = await this.relatedEntities( parentEntity._id, - parentEntity.entityTypeUniqueCode || - buildentityTypeUniqueCode(parentEntity.entityType, parentEntity.tenantId), + parentEntity.entityTypeCode || generateUniqueCode(parentEntity.entityType, parentEntity.tenantId), parentEntity.entityType, ['_id'] ) @@ -970,14 +984,14 @@ module.exports = class UserProjectsHelper { * @method * @name relatedEntities * @param {String} entityId - entity id. - * @param {String} entityTypeUniqueCode - entity type unique id. + * @param {String} entityTypeCode - entity type unique id. * @param {String} entityType - entity type. * @param {Array} [projection = "all"] - total fields to be projected. * @param {String} tenantId - user's tenant id * @returns {Array} - returns an array of related entities data. */ - static relatedEntities(entityId, entityTypeUniqueCode, entityType, projection = 'all', tenantId) { + static relatedEntities(entityId, entityTypeCode, entityType, projection = 'all', tenantId) { return new Promise(async (resolve, reject) => { try { // if ( @@ -993,10 +1007,10 @@ module.exports = class UserProjectsHelper { tenantId, } - if (entityTypeUniqueCode && entityId && entityType) { + if (entityTypeCode && entityId && entityType) { relatedEntitiesQuery[`groups.${entityType}`] = entityId - relatedEntitiesQuery['entityTypeUniqueCode'] = {} - relatedEntitiesQuery['entityTypeUniqueCode']['$ne'] = entityTypeUniqueCode + relatedEntitiesQuery['entityTypeCode'] = {} + relatedEntitiesQuery['entityTypeCode']['$ne'] = entityTypeCode } else { throw { status: HTTP_STATUS_CODE.bad_request.status, @@ -1140,7 +1154,7 @@ module.exports = class UserProjectsHelper { 'metaInformation', 'entityType', 'entityTypeId', - 'entityTypeUniqueCode', + 'entityTypeCode', 'registryDetails', ]) if (!entities.length > 0) { @@ -1423,27 +1437,19 @@ module.exports = class UserProjectsHelper { static add(queryParams, data, userDetails) { return new Promise(async (resolve, reject) => { try { - console.log('queryParams -----> ', queryParams) - console.log('entityTypeQuery -----> ', queryParams.entityTypeUniqueCode) - let tenantId = userDetails.tenantAndOrgInfo.tenantId let orgId = userDetails.tenantAndOrgInfo.orgId[0] - const entityTypeUniqueCode = - queryParams.entityTypeUniqueCode || buildentityTypeUniqueCode(queryParams.type, tenantId) - const entityTypeQuery = queryParams.entityTypeUniqueCode - ? { - tenantId: tenantId, - entityTypeUniqueCode: entityTypeUniqueCode, - } - : { - tenantId: tenantId, - $or: [{ entityTypeUniqueCode: entityTypeUniqueCode }, { name: queryParams.type }], - } - let entityTypeDocument = await entityTypeQueries.findOne(entityTypeQuery, { - _id: 1, - entityTypeUniqueCode: 1, - name: 1, - }) + let entityTypeDocument = await entityTypeQueries.findOne( + { + tenantId: tenantId, + name: queryParams.type, + }, + { + _id: 1, + entityTypeCode: 1, + name: 1, + } + ) if (!entityTypeDocument) { throw { status: HTTP_STATUS_CODE.bad_request.status, @@ -1452,61 +1458,51 @@ module.exports = class UserProjectsHelper { } let entityDocuments = [] let dataArray = Array.isArray(data) ? data : [data] - for (let pointer = 0; pointer < dataArray.length; pointer++) { let singleEntity = dataArray[pointer] if (singleEntity.createdByProgramId) { singleEntity.createdByProgramId = ObjectId(singleEntity.createdByProgramId) } - if (singleEntity.createdBySolutionId) { singleEntity.createdBySolutionId = ObjectId(singleEntity.solutionId) } - // Prepare registryDetails based on singleEntity data - let registryDetails = {} - registryDetails['locationId'] = singleEntity.externalId - registryDetails['code'] = singleEntity.externalId - registryDetails['lastUpdatedAt'] = new Date() + let registryDetails = { + locationId: singleEntity.externalId, + code: singleEntity.externalId, + lastUpdatedAt: new Date(), + } let childHierarchyPath = [] if (Array.isArray(singleEntity.childHierarchyPath)) { - // Fetch the valid childHierarchyPath from the entityType DB let validEntityType = await entityTypeQueries.entityTypesDocument( { - // Use the "$in" operator to check if any of the entityType names are present in the 'childHierarchyPath' array name: { $in: singleEntity.childHierarchyPath, }, tenantId: tenantId, }, - // Specify to return only the 'name' field of matching documents - { name: 1 } ) - // Create a mapping of names to their original index in childHierarchyPath const validatedChildHierarchy = singleEntity.childHierarchyPath.filter((name) => validEntityType.some((entityType) => entityType.name === name) ) - // Convert the names in 'validatedChildHierarchy' to strings and assign them to 'childHierarchyPath' + childHierarchyPath = validatedChildHierarchy.map(String) } + singleEntity.targetedEntityTypes = Array.isArray(singleEntity.targetedEntityTypes) ? await populateTargetedEntityTypesData( singleEntity.targetedEntityTypes.map((item) => item.trim()), tenantId ) : [] - // Construct the entity document to be created let entityDoc = { - entityTypeUniqueCode: - entityTypeDocument.entityTypeUniqueCode || - entityTypeUniqueCode || - buildentityTypeUniqueCode(entityTypeDocument.name, tenantId), + entityTypeCode: entityTypeDocument.entityTypeCode, childHierarchyPath: childHierarchyPath, - entityType: entityTypeDocument.name || queryParams.type, - code: singleEntity.externalId, + entityType: entityTypeDocument.name, + code: generateUniqueCode(singleEntity.externalId, tenantId), registryDetails: registryDetails, groups: {}, metaInformation: _.omit(singleEntity, ['locationId', 'code']), @@ -1541,8 +1537,6 @@ module.exports = class UserProjectsHelper { throw CONSTANTS.apiResponses.ENTITY_INFORMATION_NOT_INSERTED } - // await this.pushEntitiesToElasticSearch(entities); - return resolve(entityData) } catch (error) { return reject(error) @@ -1743,65 +1737,25 @@ module.exports = class UserProjectsHelper { return new Promise(async (resolve, reject) => { try { const entityTypeName = typeof entityType === 'object' ? entityType.type : entityType - const entityTypeUniqueCodeInput = typeof entityType === 'object' ? entityType.entityTypeUniqueCode : '' - - // let solutionsDocument = new Array() - // if (programId && solutionId) { - - // solutionsDocument = await database.models.entityTypes - // .find( - // { - // externalId: solutionId, - // programExternalId: programId, - // }, - // { - // programId: 1, - // externalId: 1, - // subType: 1, - // entityType: 1, - // entityTypeId: 1, - // } - // ) - // .lean() - // } - - // let solutionsData - - // if (solutionsDocument.length) { - // solutionsData = solutionsDocument.reduce( - // (ac, entities) => ({ - // ...ac, - // [entities.metaInformation.externalId]: { - // subType: entities.subType, - // solutionId: entities._id, - // programId: entities.programId, - // entityType: entities.entityType, - // entityTypeId: entities.entityTypeId, - // newEntities: new Array(), - // }, - // }), - // {} - // ) - // } + const entityTypeCodeInput = typeof entityType === 'object' ? entityType.entityTypeCode : '' // Find the entity type document based on the provided entityType let tenantId = userDetails.tenantAndOrgInfo.tenantId let orgId = userDetails.tenantAndOrgInfo.orgId[0] - const entityTypeUniqueCode = - entityTypeUniqueCodeInput || buildentityTypeUniqueCode(entityTypeName, tenantId) - const entityTypeQuery = entityTypeUniqueCodeInput + const entityTypeCode = entityTypeCodeInput || generateUniqueCode(entityTypeName, tenantId) + const entityTypeQuery = entityTypeCodeInput ? { tenantId: tenantId, - entityTypeUniqueCode: entityTypeUniqueCode, + entityTypeCode: entityTypeCode, } : { tenantId: tenantId, - $or: [{ entityTypeUniqueCode: entityTypeUniqueCode }, { name: entityTypeName }], + $or: [{ entityTypeCode: entityTypeCode }, { name: entityTypeName }], } let entityTypeDocument = await entityTypeQueries.findOne(entityTypeQuery, { _id: 1, tenantId: 1, - entityTypeUniqueCode: 1, + entityTypeCode: 1, name: 1, }) if (!entityTypeDocument) { @@ -1820,10 +1774,10 @@ module.exports = class UserProjectsHelper { ? userDetails.userInformation.userId : CONSTANTS.common.SYSTEM let entityCreation = { - entityTypeUniqueCode: - entityTypeDocument.entityTypeUniqueCode || - entityTypeUniqueCode || - buildentityTypeUniqueCode(entityTypeDocument.name, tenantId), + entityTypeCode: + entityTypeDocument.entityTypeCode || + entityTypeCode || + generateUniqueCode(entityTypeDocument.name, tenantId), entityType: entityTypeDocument.name || entityTypeName, registryDetails: {}, groups: {}, @@ -1872,11 +1826,7 @@ module.exports = class UserProjectsHelper { return entityCreation } - entityCreation.registryDetails = { - code: externalId, - locationId: externalId, - } - entityCreation.code = externalId + setEntityCodeAndRegistryDetails(entityCreation, externalId, tenantId) } if (translationFile) { @@ -1951,10 +1901,10 @@ module.exports = class UserProjectsHelper { }) if (updateData.registryDetails && Object.keys(updateData.registryDetails).length > 0) { - entityCreation.registryDetails['code'] = - entityCreation.registryDetails['code'] || entityCreation.externalId - entityCreation.registryDetails['locationId'] = - entityCreation.registryDetails['locationId'] || entityCreation.locationId + updateData.registryDetails['code'] = + updateData.registryDetails['code'] || singleEntity.externalId + updateData.registryDetails['locationId'] = + updateData.registryDetails['locationId'] || singleEntity.externalId updateData['registryDetails']['lastUpdatedAt'] = new Date() } @@ -1986,6 +1936,8 @@ module.exports = class UserProjectsHelper { return singleEntity } + setEntityCodeAndRegistryDetails(updateData, updateData['metaInformation.externalId'], tenantId) + if (translationFile) { updateData['translations'] = translationFile[updateData['metaInformation.name']] } @@ -2077,6 +2029,10 @@ module.exports = class UserProjectsHelper { ) delete bodyData.targetedEntityTypes } + + if (bodyData['metaInformation.externalId']) { + setEntityCodeAndRegistryDetails(bodyData, bodyData['metaInformation.externalId'], tenantId) + } // Update the entity using findOneAndUpdate let entityInformation = await entitiesQueries.findOneAndUpdate( { _id: ObjectId(entityId), tenantId: tenantId }, @@ -2132,16 +2088,15 @@ module.exports = class UserProjectsHelper { static listEntitiesByType(req) { return new Promise(async (resolve, reject) => { try { - console.log('req.userDetails -----> ', req.userDetails) // Retrieve the schema meta information key let schemaMetaInformation = this.entitiesSchemaData().SCHEMA_METAINFORMATION - let tenantId = req.userDetails?.tenantAndOrgInfo?.tenantId || req.userDetails?.userInformation?.tenantId + let tenantId = req.userDetails.userInformation.tenantId // Define projection for entity document fields to retrieve let projection = [ schemaMetaInformation + '.externalId', schemaMetaInformation + '.name', 'registryDetails.locationId', - 'entityTypeUniqueCode', + 'entityTypeCode', ] // Calculate skipping value based on pagination parameters @@ -2150,7 +2105,7 @@ module.exports = class UserProjectsHelper { // Query entities based on entity type unique id, with legacy entityTypeId fallback. const entityTypeFilter = UTILS.strictObjectIdCheck(req.params._id) ? { entityTypeId: ObjectId(req.params._id) } - : { entityTypeUniqueCode: req.params._id } + : { entityTypeCode: req.params._id } let entityDocuments = await entitiesQueries.entityDocuments( { ...entityTypeFilter, @@ -2292,7 +2247,7 @@ module.exports = class UserProjectsHelper { metaInformation: 1, groups: 1, entityType: 1, - entityTypeUniqueCode: 1, + entityTypeCode: 1, }, }, { @@ -2320,7 +2275,7 @@ module.exports = class UserProjectsHelper { entity.metaInformation.childrenCount = 0 entity.metaInformation.entityType = entity.entityType entity.metaInformation.entityTypeId = entity.entityTypeId - entity.metaInformation.entityTypeUniqueCode = entity.entityTypeUniqueCode + entity.metaInformation.entityTypeCode = entity.entityTypeCode entity.metaInformation.subEntityGroups = new Array() entity.groups && @@ -2434,14 +2389,13 @@ async function populateTargetedEntityTypesData(targetedEntityTypes, tenantId) { name: { $in: targetedEntityTypes }, tenantId: tenantId, }, - ['name', '_id', 'entityTypeUniqueCode'] + ['name', '_id', 'entityTypeCode'] ) formattedTargetedEntityTypes.forEach((entityType) => { entityType['entityTypeId'] = entityType._id.toString() entityType['entityType'] = entityType.name - entityType['entityTypeUniqueCode'] = - entityType.entityTypeUniqueCode || buildentityTypeUniqueCode(entityType.name, tenantId) + entityType['entityTypeCode'] = entityType.entityTypeCode || generateUniqueCode(entityType.name, tenantId) delete entityType._id delete entityType.name }) From 4a3fa070aaf00b8f62ccafaea56d43a9515c022d Mon Sep 17 00:00:00 2001 From: vishwanath1004 <50612405+vishwanath1004@users.noreply.github.com> Date: Wed, 13 May 2026 23:44:34 +0530 Subject: [PATCH 6/7] PR comments resolving : unique generate method name change --- src/generics/helpers/utils.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/generics/helpers/utils.js b/src/generics/helpers/utils.js index 43235d1..c18ba7f 100644 --- a/src/generics/helpers/utils.js +++ b/src/generics/helpers/utils.js @@ -246,7 +246,8 @@ function generateUniqueId() { * @param {String} tenantId - tenant id. * @returns {String} - unique identifier derived from name and tenant. */ -function generateEntityTypeCode(name, tenantId) { + +function generateUniqueCode(name, tenantId) { return `${String(name || '').trim()}_${String(tenantId || '').trim()}` } @@ -338,7 +339,7 @@ module.exports = { noOfElementsInArray: noOfElementsInArray, operatorValidation: operatorValidation, generateUniqueId: generateUniqueId, - generateEntityTypeCode: generateEntityTypeCode, + generateUniqueCode: generateUniqueCode, convertMongoIds: convertMongoIds, stripOrgIds: stripOrgIds, convertOrgIdsToString: convertOrgIdsToString, From 030d77cf2844b13fb1924c0b97be2515e9988bf2 Mon Sep 17 00:00:00 2001 From: vishwanath1004 <50612405+vishwanath1004@users.noreply.github.com> Date: Wed, 13 May 2026 23:45:05 +0530 Subject: [PATCH 7/7] PR comments resolving : EntityTypeCode change --- src/module/entityTypes/helper.js | 33 +++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/module/entityTypes/helper.js b/src/module/entityTypes/helper.js index 07866f6..37953c3 100644 --- a/src/module/entityTypes/helper.js +++ b/src/module/entityTypes/helper.js @@ -12,7 +12,7 @@ const entitiesQueries = require(DB_QUERY_BASE_PATH + '/entities') // const entitiesHelper = require(MODULES_BASE_PATH + "/entities/helper") // const programUsersQueries = require(DB_QUERY_BASE_PATH + "/programUsers") -const buildEntityTypeUniqueId = (name, tenantId) => UTILS.generateEntityTypeUniqueId(name, tenantId) +const generateUniqueCode = (name, tenantId) => UTILS.generateEntityTypeCode(name, tenantId) /** * UserProjectsHelper @@ -39,10 +39,7 @@ module.exports = class UserProjectsHelper { entityType = UTILS.valueParser(entityType) entityType['tenantId'] = userDetails.tenantAndOrgInfo.tenantId entityType['orgId'] = userDetails.tenantAndOrgInfo.orgId[0] - entityType['entityTypeUniqueId'] = buildEntityTypeUniqueId( - entityType.name, - entityType['tenantId'] - ) + entityType['entityTypeCode'] = generateUniqueCode(entityType.name, entityType['tenantId']) entityType.registryDetails = {} let removedKeys = [] @@ -191,7 +188,7 @@ module.exports = class UserProjectsHelper { ? userDetails.userInformation.userId : CONSTANTS.common.SYSTEM - entityType.entityTypeUniqueId = buildEntityTypeUniqueId(entityType.name, entityType.tenantId) + entityType.entityTypeCode = generateUniqueCode(entityType.name, entityType.tenantId) let newEntityType = await entityTypeQueries.create( _.merge( @@ -241,9 +238,9 @@ module.exports = class UserProjectsHelper { let tenantId = userDetails.tenantAndOrgInfo.tenantId const existingEntityType = await entityTypeQueries.findOne( { _id: ObjectId(entityTypeId), tenantId: tenantId }, - { name: 1, entityTypeUniqueId: 1 } + { name: 1, entityTypeCode: 1 } ) - bodyData.entityTypeUniqueId = buildEntityTypeUniqueId(bodyData.name, tenantId) + bodyData.entityTypeCode = generateUniqueCode(bodyData.name, tenantId) // Find and update the entity type by ID with the provided bodyData let entityInformation = await entityTypeQueries.findOneAndUpdate( @@ -256,13 +253,13 @@ module.exports = class UserProjectsHelper { return reject({ status: 404, message: CONSTANTS.apiResponses.ENTITYTYPE_NOT_FOUND }) } - if (existingEntityType && existingEntityType.entityTypeUniqueId !== bodyData.entityTypeUniqueId) { + if (existingEntityType && existingEntityType.entityTypeCode !== bodyData.entityTypeCode) { await entitiesQueries.updateMany( { entityTypeId: ObjectId(entityTypeId), tenantId: tenantId }, { $set: { entityType: bodyData.name, - entityTypeUniqueId: bodyData.entityTypeUniqueId, + entityTypeCode: bodyData.entityTypeCode, }, } ) @@ -351,7 +348,7 @@ module.exports = class UserProjectsHelper { ? userDetails && userDetails.userInformation.userId : CONSTANTS.common.SYSTEM - entityType.entityTypeUniqueId = buildEntityTypeUniqueId(entityType.name, tenantId) + entityType.entityTypeCode = generateUniqueCode(entityType.name, tenantId) if (!entityType.name) { entityType['_SYSTEM_ID'] = '' @@ -380,7 +377,7 @@ module.exports = class UserProjectsHelper { { $set: { entityType: entityType.name, - entityTypeUniqueId: entityType.entityTypeUniqueId, + entityTypeCode: entityType.entityTypeCode, }, } ) @@ -480,9 +477,19 @@ module.exports = class UserProjectsHelper { // Retrieve entity type data based on the provided query and projection const result = await entityTypeQueries.getAggregate(aggregateData) + const tenantId = bodyQuery.tenantId + const responseData = (result[0] && Array.isArray(result[0].data) ? result[0].data : []).map( + (entityType) => { + // Backfill entityTypeCode for older records that were created before the field existed. + if (!entityType.entityTypeCode && entityType.name && tenantId) { + entityType.entityTypeCode = generateUniqueCode(entityType.name, tenantId) + } + return entityType + } + ) return resolve({ message: CONSTANTS.apiResponses.ENTITY_TYPES_FETCHED, - result: result[0].data, + result: responseData, }) } catch (error) { return reject(error)