Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/controllers/v1/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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({
Expand Down Expand Up @@ -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()
Expand All @@ -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({
Expand Down
21 changes: 12 additions & 9 deletions src/controllers/v1/entityTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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({
Expand Down Expand Up @@ -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()
Expand All @@ -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({
Expand Down
2 changes: 2 additions & 0 deletions src/generics/constants/api-responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
}
87 changes: 15 additions & 72 deletions src/module/entities/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand All @@ -1854,43 +1814,20 @@ 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
})
)
if (entityUploadedData.findIndex((entity) => entity === undefined) >= 0) {
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)
}
Expand All @@ -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) => {
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
22 changes: 16 additions & 6 deletions src/module/entityTypes/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -123,7 +125,10 @@ module.exports = class UserProjectsHelper {
})
)

return resolve(entityTypesUploadedData)
return resolve({
data: entityTypesUploadedData,
hasSuccess,
})
} catch (error) {
return reject(error)
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -358,7 +365,10 @@ module.exports = class UserProjectsHelper {
})
)

return resolve(entityTypesUploadedData)
return resolve({
data: entityTypesUploadedData,
hasSuccess,
})
} catch (error) {
return reject(error)
}
Expand Down
7 changes: 7 additions & 0 deletions src/release-notes/staging/release-3.4.1_RC_2.md
Original file line number Diff line number Diff line change
@@ -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.
.