diff --git a/src/controllers/v1/entities.js b/src/controllers/v1/entities.js index 08ae63d..65b030c 100644 --- a/src/controllers/v1/entities.js +++ b/src/controllers/v1/entities.js @@ -1000,7 +1000,7 @@ module.exports = class Entities extends Abstract { ) // Check if new entities were created successfully - if (newEntityData.length > 0) { + if (newEntityData.hasSuccess && newEntityData.data.length > 0) { const fileName = `Entity-Upload` let fileStream = new FileStream(fileName) let input = fileStream.initStream() @@ -1016,14 +1016,17 @@ module.exports = class Entities extends Abstract { // Push each new entity into the file stream for processing await Promise.all( - newEntityData.map(async (newEntity) => { + newEntityData.data.map(async (newEntity) => { input.push(newEntity) }) ) input.push(null) } else { - throw CONSTANTS.apiResponses.SOMETHING_WENT_WRONG + throw { + status: HTTP_STATUS_CODE.bad_request.status, + message: CONSTANTS.apiResponses.ALL_RECORDS_FAILED_TO_PROCESS, + } } } catch (error) { return reject({ @@ -1071,7 +1074,7 @@ module.exports = class Entities extends Abstract { let newEntityData = await entitiesHelper.bulkUpdate(entityCSVData, translationFile, req.userDetails) // Check if entities were updated successfully - if (newEntityData.length > 0) { + if (newEntityData.hasSuccess && newEntityData.data.length > 0) { const fileName = `Entity-Upload` let fileStream = new FileStream(fileName) let input = fileStream.initStream() @@ -1086,14 +1089,17 @@ module.exports = class Entities extends Abstract { })() await Promise.all( - newEntityData.map(async (newEntity) => { + newEntityData.data.map(async (newEntity) => { input.push(newEntity) }) ) input.push(null) } else { - throw new Error(CONSTANTS.apiResponses.SOMETHING_WENT_WRONG) + throw { + status: HTTP_STATUS_CODE.bad_request.status, + message: CONSTANTS.apiResponses.ALL_RECORDS_FAILED_TO_UPDATE, + } } } catch (error) { return reject({ diff --git a/src/controllers/v1/entityTypes.js b/src/controllers/v1/entityTypes.js index a81751f..e5509a0 100644 --- a/src/controllers/v1/entityTypes.js +++ b/src/controllers/v1/entityTypes.js @@ -278,8 +278,7 @@ module.exports = class EntityTypes extends Abstract { // Call 'entityTypesHelper.bulkCreate' to create multiple entity types from CSV data and user details const newEntityTypeData = await entityTypesHelper.bulkCreate(entityTypesCSVData, req.userDetails) - // Check if entity types were created successfully - if (newEntityTypeData.length > 0 && newEntityTypeData[0].status === CONSTANTS.apiResponses.SUCCESS) { + if (newEntityTypeData.hasSuccess && newEntityTypeData.data.length > 0) { const fileName = `EntityType-Upload` let fileStream = new FileStream(fileName) let input = fileStream.initStream() @@ -294,16 +293,17 @@ module.exports = class EntityTypes extends Abstract { })() await Promise.all( - newEntityTypeData.map(async (entityType) => { + newEntityTypeData.data.map(async (entityType) => { input.push(entityType) }) ) input.push(null) } else { - const error = new Error(CONSTANTS.apiResponses.ENTITY_TYPE_CREATION_FAILED) - error.status = HTTP_STATUS_CODE.bad_request.status - throw error + throw { + status: HTTP_STATUS_CODE.bad_request.status, + message: CONSTANTS.apiResponses.ALL_RECORDS_FAILED_TO_PROCESS, + } } } catch (error) { return reject({ @@ -343,7 +343,7 @@ module.exports = class EntityTypes extends Abstract { let newEntityTypeData = await entityTypesHelper.bulkUpdate(entityTypesCSVData, req.userDetails) // Check if entity types were updated successfully - if (newEntityTypeData.length > 0) { + if (newEntityTypeData.hasSuccess && newEntityTypeData.data.length > 0) { const fileName = `EntityType-Upload` let fileStream = new FileStream(fileName) let input = fileStream.initStream() @@ -358,14 +358,17 @@ module.exports = class EntityTypes extends Abstract { })() await Promise.all( - newEntityTypeData.map(async (entityType) => { + newEntityTypeData.data.map(async (entityType) => { input.push(entityType) }) ) input.push(null) } else { - throw CONSTANTS.apiResponses.ENTITY_TYPE_NOT_UPDATED + throw { + status: HTTP_STATUS_CODE.bad_request.status, + message: CONSTANTS.apiResponses.ALL_RECORDS_FAILED_TO_UPDATE, + } } } catch (error) { return reject({ diff --git a/src/generics/constants/api-responses.js b/src/generics/constants/api-responses.js index 2f76711..f3929fd 100644 --- a/src/generics/constants/api-responses.js +++ b/src/generics/constants/api-responses.js @@ -80,4 +80,6 @@ module.exports = { ENTITIES_DELETED_SUCCESSFULLY: 'ENTITIES_DELETED_SUCCESSFULLY', ADMIN_ROLE_REQUIRED: 'Access denied: Admin role required', NOT_A_VALID_MONGOID: 'externalId cannot be a Mongo ObjectId', + ALL_RECORDS_FAILED_TO_PROCESS: 'All records failed to process', + ALL_RECORDS_FAILED_TO_UPDATE: 'All records failed to update', } diff --git a/src/module/entities/helper.js b/src/module/entities/helper.js index 0353c8a..65ab808 100644 --- a/src/module/entities/helper.js +++ b/src/module/entities/helper.js @@ -1718,45 +1718,8 @@ module.exports = class UserProjectsHelper { static bulkCreate(entityType, programId, solutionId, userDetails, entityCSVData, translationFile) { return new Promise(async (resolve, reject) => { try { - // 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(), - // }, - // }), - // {} - // ) - // } - + // Flag to track if at least one record is successfully processed + let hasSuccess = false // Find the entity type document based on the provided entityType let tenantId = userDetails.tenantAndOrgInfo.tenantId let orgId = userDetails.tenantAndOrgInfo.orgId[0] @@ -1841,9 +1804,6 @@ module.exports = class UserProjectsHelper { if (translationFile) { entityCreation['translations'] = translationFile[entityCreation.metaInformation.name] } - - // if (solutionsData && singleEntity._solutionId && singleEntity._solutionId != '') - // singleEntity['createdByProgramId'] = solutionsData[singleEntity._solutionId]['programId'] let newEntity = await entitiesQueries.create(entityCreation) if (!newEntity._id) { return @@ -1854,19 +1814,8 @@ module.exports = class UserProjectsHelper { if (singleEntity._SYSTEM_ID) { singleEntity.status = CONSTANTS.apiResponses.SUCCESS singleEntity.message = CONSTANTS.apiResponses.SUCCESS + hasSuccess = true } - - // if ( - // solutionsData && - // singleEntity._solutionId && - // singleEntity._solutionId != '' && - // newEntity.entityType == solutionsData[singleEntity._solutionId]['entityType'] - // ) { - // solutionsData[singleEntity._solutionId].newEntities.push(newEntity._id) - // } - - // await this.pushEntitiesToElasticSearch([singleEntity["_SYSTEM_ID"]]); - return singleEntity }) ) @@ -1874,23 +1823,11 @@ module.exports = class UserProjectsHelper { throw CONSTANTS.apiResponses.SOMETHING_WRONG_INSERTED_UPDATED } - // solutionsData && - // (await Promise.all( - // Object.keys(solutionsData).map(async (solutionExternalId) => { - // if (solutionsData[solutionExternalId].newEntities.length > 0) { - // await database.models.solutions.updateOne( - // { _id: solutionsData[solutionExternalId].solutionId }, - // { - // $addToSet: { - // entities: { $each: solutionsData[solutionExternalId].newEntities }, - // }, - // } - // ) - // } - // }) - // )) - - return resolve(entityUploadedData) + // return resolve(entityUploadedData) + return resolve({ + data: entityUploadedData, + hasSuccess, + }) } catch (error) { return reject(error) } @@ -1910,6 +1847,8 @@ module.exports = class UserProjectsHelper { static bulkUpdate(entityCSVData, translationFile, userDetails) { return new Promise(async (resolve, reject) => { try { + // Flag to track if at least one record is successfully processed + let hasSuccess = false let tenantId = userDetails.tenantAndOrgInfo.tenantId const entityUploadedData = await Promise.all( entityCSVData.map(async (singleEntity) => { @@ -1993,6 +1932,7 @@ module.exports = class UserProjectsHelper { } else { singleEntity['status'] = CONSTANTS.apiResponses.SUCCESS singleEntity['message'] = CONSTANTS.apiResponses.SUCCESS + hasSuccess = true } } else { singleEntity['status'] = CONSTANTS.apiResponses.NO_INFORMATION_TO_UPDATE @@ -2007,7 +1947,10 @@ module.exports = class UserProjectsHelper { throw CONSTANTS.apiResponses.SOMETHING_WRONG_INSERTED_UPDATED } - return resolve(entityUploadedData) + return resolve({ + data: entityUploadedData, + hasSuccess, + }) } catch (error) { return reject(error) } diff --git a/src/module/entityTypes/helper.js b/src/module/entityTypes/helper.js index 58c8775..a3ec997 100644 --- a/src/module/entityTypes/helper.js +++ b/src/module/entityTypes/helper.js @@ -26,9 +26,10 @@ module.exports = class UserProjectsHelper { * @returns {JSON} - uploaded entity information. */ static bulkCreate(entityTypesCSVData, userDetails) { - console.log(userDetails, '<--userDetails in bulkCreate entityTypesCSVData') return new Promise(async (resolve, reject) => { try { + // Flag to track if at least one record is successfully processed + let hasSuccess = false const entityTypesUploadedData = await Promise.all( entityTypesCSVData.map(async (entityType) => { try { @@ -107,8 +108,9 @@ module.exports = class UserProjectsHelper { ) ) if (newEntityType._id) { - entityType['_SYSTEM_ID'] = newEntityType._id - entityType.status = CONSTANTS.apiResponses.SUCCESS + ;(entityType['_SYSTEM_ID'] = newEntityType._id), + (entityType.status = CONSTANTS.apiResponses.SUCCESS), + (hasSuccess = true) } else { entityType['_SYSTEM_ID'] = '' entityType.status = CONSTANTS.apiResponses.FAILURE @@ -123,7 +125,10 @@ module.exports = class UserProjectsHelper { }) ) - return resolve(entityTypesUploadedData) + return resolve({ + data: entityTypesUploadedData, + hasSuccess, + }) } catch (error) { return reject(error) } @@ -260,6 +265,8 @@ module.exports = class UserProjectsHelper { static bulkUpdate(entityTypesCSVData, userDetails) { return new Promise(async (resolve, reject) => { try { + // Flag to track if at least one record is successfully processed + let hasSuccess = false let tenantId = userDetails.tenantAndOrgInfo.tenantId // Process each entity type in the provided array asynchronously const entityTypesUploadedData = await Promise.all( @@ -343,7 +350,7 @@ module.exports = class UserProjectsHelper { if (updateEntityType._id) { entityType['_SYSTEM_ID'] = updateEntityType._id - entityType.status = CONSTANTS.common.SUCCESS + ;(entityType.status = CONSTANTS.common.SUCCESS), (hasSuccess = true) } else { entityType['_SYSTEM_ID'] = '' entityType.status = CONSTANTS.common.FAILURE @@ -358,7 +365,10 @@ module.exports = class UserProjectsHelper { }) ) - return resolve(entityTypesUploadedData) + return resolve({ + data: entityTypesUploadedData, + hasSuccess, + }) } catch (error) { return reject(error) } diff --git a/src/release-notes/staging/release-3.4.1_RC_2.md b/src/release-notes/staging/release-3.4.1_RC_2.md new file mode 100644 index 0000000..7abf3bf --- /dev/null +++ b/src/release-notes/staging/release-3.4.1_RC_2.md @@ -0,0 +1,7 @@ +# 🚀 Entity-Service Release 4.0.1 + +## 🐞 Bug Fixes + +- **4721** – When there is miss-matching external id's in csv file.createMappingCsv API gives success message. +- **4750** - API returns 200 success response even when mandatory fields are missing during entity bulk creation. + .