Skip to content

Commit

Permalink
Merge pull request #75 from ELEVATE-Project/validationForBulkCreate
Browse files Browse the repository at this point in the history
"added-validation-for-bulkCreate"
  • Loading branch information
VISHNUDAS-tunerlabs authored Dec 12, 2024
2 parents 78f4fcf + ef8ce22 commit 558f109
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/generics/constants/api-responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,9 @@ module.exports = {
USER_ROLE_UPDATED: 'USER_ROLE_UPDATED',
USER_ROLE_DELETED: 'USER_ROLE_DELETED',
ENTITY_UPDATED: 'ENTITY_UPDATED',
ENTITIES_FAILED: 'The creation of this entities was skipped due to missing required data.',
ENTITY_TYPE_FAILED: 'The creation of this entity type was skipped due to missing required data.',
FIELD_MISSING: 'Fields are missing',
ENTITY_TYPE_CREATION_FAILED: 'ENTITY TYPE CREATION FAILED',

}
23 changes: 20 additions & 3 deletions src/module/entities/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,11 @@ module.exports = class UserProjectsHelper {
return _.startsWith(key, '_')
})

if (!entityCreation.metaInformation.name || !entityCreation.metaInformation.externalId) {
entityCreation.status = CONSTANTS.apiResponses.ENTITIES_FAILED
entityCreation.message = CONSTANTS.apiResponses.FIELD_MISSING
return entityCreation
}
// if (solutionsData && singleEntity._solutionId && singleEntity._solutionId != '')
// singleEntity['createdByProgramId'] = solutionsData[singleEntity._solutionId]['programId']
let newEntity = await entitiesQueries.create(entityCreation)
Expand All @@ -1274,6 +1279,11 @@ module.exports = class UserProjectsHelper {

singleEntity['_SYSTEM_ID'] = newEntity._id.toString()

if (singleEntity._SYSTEM_ID) {
singleEntity.status = CONSTANTS.apiResponses.SUCCESS
singleEntity.message = CONSTANTS.apiResponses.SUCCESS
}

// if (
// solutionsData &&
// singleEntity._solutionId &&
Expand Down Expand Up @@ -1373,6 +1383,12 @@ module.exports = class UserProjectsHelper {
updateData[`metaInformation.${key}`] = columnsToUpdate[key]
})

if (!updateData['metaInformation.name'] || !updateData['metaInformation.externalId']) {
singleEntity.status = CONSTANTS.apiResponses.ENTITIES_FAILED
singleEntity.message = CONSTANTS.apiResponses.FIELD_MISSING
return singleEntity
}

if (Object.keys(updateData).length > 0) {
let updateEntity = await entitiesQueries.findOneAndUpdate(
{ _id: singleEntity['_SYSTEM_ID'] },
Expand All @@ -1381,12 +1397,13 @@ module.exports = class UserProjectsHelper {
)

if (!updateEntity || !updateEntity._id) {
singleEntity['UPDATE_STATUS'] = CONSTANTS.apiResponses.ENTITY_NOT_FOUND
singleEntity['status'] = CONSTANTS.apiResponses.ENTITY_NOT_FOUND
} else {
singleEntity['UPDATE_STATUS'] = CONSTANTS.apiResponses.SUCCESS
singleEntity['status'] = CONSTANTS.apiResponses.SUCCESS
singleEntity['message'] = CONSTANTS.apiResponses.SUCCESS
}
} else {
singleEntity['UPDATE_STATUS'] = CONSTANTS.apiResponses.NO_INFORMATION_TO_UPDATE
singleEntity['status'] = CONSTANTS.apiResponses.NO_INFORMATION_TO_UPDATE
}

return singleEntity
Expand Down
13 changes: 13 additions & 0 deletions src/module/entityTypes/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ module.exports = class UserProjectsHelper {
userDetails && userDetails.userInformation.id
? userDetails && userDetails.userInformation.id
: CONSTANTS.common.SYSTEM

if (!entityType.name) {
entityType['_SYSTEM_ID'] = ''
entityType.status = CONSTANTS.apiResponses.ENTITY_TYPE_FAILED
entityType.message = CONSTANTS.apiResponses.FIELD_MISSING
return entityType
}
let newEntityType = await entityTypeQueries.create(
_.merge(
{
Expand Down Expand Up @@ -296,6 +303,12 @@ module.exports = class UserProjectsHelper {
? userDetails && userDetails.userInformation.id
: CONSTANTS.common.SYSTEM

if (!entityType.name) {
entityType['_SYSTEM_ID'] = ''
entityType.status = CONSTANTS.apiResponses.ENTITY_TYPE_FAILED
entityType.message = CONSTANTS.apiResponses.FIELD_MISSING
return entityType
}
// Find and update the entityType by _SYSTEM_ID with merged data
let updateEntityType = await entityTypeQueries.findOneAndUpdate(
{
Expand Down

0 comments on commit 558f109

Please sign in to comment.