diff --git a/src/.env.sample b/src/.env.sample index 990c872..b729489 100644 --- a/src/.env.sample +++ b/src/.env.sample @@ -14,3 +14,6 @@ SERVICE_NAME = entity-management #api doc endpoint API_DOC_URL = "/entity-management/api-doc" +#Indicate If auth token is bearer or not +IS_AUTH_TOKEN_BEARER=false + diff --git a/src/api-doc/Entity Management API's.postman_collection.json b/src/api-doc/Entity Management API's.postman_collection.json index 5f4f4e9..2c42615 100644 --- a/src/api-doc/Entity Management API's.postman_collection.json +++ b/src/api-doc/Entity Management API's.postman_collection.json @@ -41,15 +41,8 @@ }, "url": { "raw": "{{baseUrl}}/entity-management/v1/entityTypes/find", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entityTypes", - "find" - ] + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entityTypes", "find"] } }, "response": [] @@ -86,16 +79,8 @@ }, "url": { "raw": "{{baseUrl}}/entity-management/v1/entityTypes/update/663335e2a766490014aead0a", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entityTypes", - "update", - "663335e2a766490014aead0a" - ] + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entityTypes", "update", "663335e2a766490014aead0a"] } }, "response": [] @@ -132,15 +117,8 @@ }, "url": { "raw": "{{baseUrl}}/entity-management/v1/entityTypes/create", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entityTypes", - "create" - ] + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entityTypes", "create"] } }, "response": [] @@ -178,15 +156,8 @@ }, "url": { "raw": "{{baseUrl}}/entity-management/v1/entityTypes/bulkUpdate", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entityTypes", - "bulkUpdate" - ] + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entityTypes", "bulkUpdate"] } }, "response": [] @@ -214,15 +185,8 @@ ], "url": { "raw": "{{baseUrl}}/entity-management/v1/entityTypes/list", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entityTypes", - "list" - ] + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entityTypes", "list"] } }, "response": [] @@ -266,15 +230,8 @@ }, "url": { "raw": "{{baseUrl}}/entity-management/v1/entityTypes/bulkCreate", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entityTypes", - "bulkCreate" - ] + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entityTypes", "bulkCreate"] } }, "response": [] @@ -307,16 +264,8 @@ ], "url": { "raw": "{{baseUrl}}/entity-management/v1/entities/listByEntityType/5f32d8228e0dc8312404056e?page=1", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entities", - "listByEntityType", - "5f32d8228e0dc8312404056e" - ], + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entities", "listByEntityType", "5f32d8228e0dc8312404056e"], "query": [ { "key": "page", @@ -359,16 +308,8 @@ }, "url": { "raw": "{{baseUrl}}/entity-management/v1/entities/update/663364443c990eaa179e289e", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entities", - "update", - "663364443c990eaa179e289e" - ] + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entities", "update", "663364443c990eaa179e289e"] } }, "response": [] @@ -412,15 +353,8 @@ }, "url": { "raw": "{{baseUrl}}/entity-management/v1/entities/bulkCreate?type=school", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entities", - "bulkCreate" - ], + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entities", "bulkCreate"], "query": [ { "key": "type", @@ -464,15 +398,8 @@ }, "url": { "raw": "{{baseUrl}}/entity-management/v1/entities/bulkUpdate", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entities", - "bulkUpdate" - ] + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entities", "bulkUpdate"] } }, "response": [] @@ -510,15 +437,8 @@ }, "url": { "raw": "{{baseUrl}}/entity-management/v1/entities/mappingUpload", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entities", - "mappingUpload" - ] + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entities", "mappingUpload"] } }, "response": [] @@ -546,16 +466,8 @@ ], "url": { "raw": "{{baseUrl}}/entity-management/v1/entities/list/5f33c3d85f637784791cd830?page=1&limit=100&type=school", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entities", - "list", - "5f33c3d85f637784791cd830" - ], + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entities", "list", "5f33c3d85f637784791cd830"], "query": [ { "key": "page", @@ -609,16 +521,8 @@ }, "url": { "raw": "{{baseUrl}}/entity-management/v1/entities/relatedEntities/6153158e5bec1d28882358ab", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entities", - "relatedEntities", - "6153158e5bec1d28882358ab" - ] + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entities", "relatedEntities", "6153158e5bec1d28882358ab"] } }, "response": [] @@ -650,15 +554,8 @@ }, "url": { "raw": "{{baseUrl}}/entity-management/v1/entities/find", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entities", - "find" - ] + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entities", "find"] } }, "response": [] @@ -686,16 +583,8 @@ ], "url": { "raw": "{{baseUrl}}/entity-management/v1/entities/details/663339bc0cb19f01c459853b", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entities", - "details", - "663339bc0cb19f01c459853b" - ] + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entities", "details", "663339bc0cb19f01c459853b"] } }, "response": [] @@ -706,7 +595,7 @@ "disableBodyPruning": true }, "request": { - "method": "GET", + "method": "POST", "header": [ { "key": "x-auth-token", @@ -735,15 +624,8 @@ }, "url": { "raw": "{{baseUrl}}/entity-management/v1/entities/listByLocationIds", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entities", - "listByLocationIds" - ] + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entities", "listByLocationIds"] } }, "response": [] @@ -780,15 +662,8 @@ }, "url": { "raw": "{{baseUrl}}/entity-management/v1/entities/add?type=state", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entities", - "add" - ], + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entities", "add"], "query": [ { "key": "type", @@ -817,16 +692,8 @@ ], "url": { "raw": "{{baseUrl}}/entity-management/v1/entities/subEntityListBasedOnRoleAndLocation/entity123", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entities", - "subEntityListBasedOnRoleAndLocation", - "entity123" - ] + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entities", "subEntityListBasedOnRoleAndLocation", "entity123"] } }, "response": [] @@ -849,16 +716,8 @@ ], "url": { "raw": "{{baseUrl}}/entity-management/v1/entities/subEntityList/663354e56109810671aec49f?type=school&search=&page=1&limit=100", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entities", - "subEntityList", - "663354e56109810671aec49f" - ], + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entities", "subEntityList", "663354e56109810671aec49f"], "query": [ { "key": "type", @@ -917,15 +776,8 @@ }, "url": { "raw": "{{baseUrl}}/entity-management/v1/entities/listByIds", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entities", - "listByIds" - ] + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entities", "listByIds"] } }, "response": [] @@ -952,15 +804,8 @@ ], "url": { "raw": "{{baseUrl}}/entity-management/v1/entities/entityListBasedOnEntityType?entityType=state", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entities", - "entityListBasedOnEntityType" - ], + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entities", "entityListBasedOnEntityType"], "query": [ { "key": "entityType", @@ -987,16 +832,8 @@ ], "url": { "raw": "{{baseUrl}}/entity/v1/entities/targetedRoles/5f33c3d85f637784791cd831", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "entities", - "targetedRoles", - "5f33c3d85f637784791cd831" - ] + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "entities", "targetedRoles", "5f33c3d85f637784791cd831"] } }, "response": [] @@ -1035,15 +872,8 @@ }, "url": { "raw": "{{baseUrl}}/entity/v1/userRoleExtension/create", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "userRoleExtension", - "create" - ] + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "userRoleExtension", "create"] } }, "response": [] @@ -1072,16 +902,8 @@ }, "url": { "raw": "{{baseUrl}}/entity/v1/userRoleExtension/update/66a94c06fff8abb34f9d800f", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "userRoleExtension", - "update", - "66a94c06fff8abb34f9d800f" - ] + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "userRoleExtension", "update", "66a94c06fff8abb34f9d800f"] } }, "response": [] @@ -1106,15 +928,8 @@ }, "url": { "raw": "{{baseUrl}}/entity/v1/userRoleExtension/find", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "userRoleExtension", - "find" - ] + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "userRoleExtension", "find"] } }, "response": [] @@ -1135,16 +950,8 @@ ], "url": { "raw": "{{baseUrl}}/entity/v1/userRoleExtension/delete/66a94fc1fdf2d5dda4b689dc", - "host": [ - "{{baseUrl}}" - ], - "path": [ - "entity", - "v1", - "userRoleExtension", - "delete", - "66a94fc1fdf2d5dda4b689dc" - ] + "host": ["{{baseUrl}}"], + "path": ["entity", "v1", "userRoleExtension", "delete", "66a94fc1fdf2d5dda4b689dc"] } }, "response": [] @@ -1152,4 +959,4 @@ ] } ] -} \ No newline at end of file +} diff --git a/src/api-doc/api-doc.yaml b/src/api-doc/api-doc.yaml index 99c9cd9..35c7bac 100644 --- a/src/api-doc/api-doc.yaml +++ b/src/api-doc/api-doc.yaml @@ -9,7 +9,7 @@ info: contact: email: tech-infra@shikshalokam.org servers: - - url: http://{{baseurl}}/entity-management + - url: http://{{baseurl}}/entity-management description: local server - url: https://dev.elevate-apis.shikshalokam.org description: dev server @@ -98,7 +98,7 @@ paths: param: entityTypes msg: EntityTypes file is required parameters: [] - + /v1/entityTypes/bulkUpdate: post: operationId: '' @@ -777,7 +777,7 @@ paths: schema: type: string required: true - + responses: '200': description: ok @@ -977,6 +977,8 @@ paths: properties: externalId: type: string + name: + type: string entityType: type: string childHierarchyPath: @@ -991,6 +993,7 @@ paths: sampleBodyData: value: externalId: entity123 + name: entityName childHierarchyPath: - district - beat @@ -1764,7 +1767,7 @@ paths: param: _id msg: required Entity id /v1/entities/listByLocationIds: - get: + POST: summary: List the entities based on location id tags: - entities-API's @@ -2638,7 +2641,7 @@ paths: examples: SuccessResponse: value: - message: USER_ROLE_UPDATATED + message: USER_ROLE_UPDATED status: 200 result: status: ACTIVE diff --git a/src/controllers/v1/entityTypes.js b/src/controllers/v1/entityTypes.js index 5d5d2a8..21bea33 100644 --- a/src/controllers/v1/entityTypes.js +++ b/src/controllers/v1/entityTypes.js @@ -241,7 +241,7 @@ module.exports = class EntityTypes extends Abstract { const newEntityTypeData = await entityTypesHelper.bulkCreate(entityTypesCSVData, req.userDetails) // Check if entity types were created successfully - if (newEntityTypeData.length > 0) { + if (newEntityTypeData.length > 0 && newEntityTypeData[0].status === CONSTANTS.apiResponses.SUCCESS) { const fileName = `EntityType-Upload` let fileStream = new FileStream(fileName) let input = fileStream.initStream() @@ -263,7 +263,9 @@ module.exports = class EntityTypes extends Abstract { input.push(null) } else { - throw CONSTANTS.apiResponses.PROJECT_FAILED + const error = new Error(CONSTANTS.apiResponses.ENTITY_TYPE_CREATION_FAILED) + error.status = HTTP_STATUS_CODE.bad_request.status + throw error } } catch (error) { return reject({ diff --git a/src/controllers/v1/userRoleExtension.js b/src/controllers/v1/userRoleExtension.js index d11ddf6..c153d81 100644 --- a/src/controllers/v1/userRoleExtension.js +++ b/src/controllers/v1/userRoleExtension.js @@ -108,7 +108,7 @@ module.exports = class userRoleExtension extends Abstract { * * { - "message": "USER_ROLE_UPDATATED", + "message": "USER_ROLE_UPDATED", "status": 200, "result": { "status": "ACTIVE", diff --git a/src/databaseQueries/entities.js b/src/databaseQueries/entities.js index 4d0816d..5cb058d 100644 --- a/src/databaseQueries/entities.js +++ b/src/databaseQueries/entities.js @@ -15,8 +15,8 @@ module.exports = class entities { return resolve(entityData) } catch (error) { return reject({ - status: error.status || HTTP_STATUS_CODE.internal_server_error.status, - message: error.message || HTTP_STATUS_CODE.internal_server_error.message, + status: error.status || HTTP_STATUS_CODE.bad_request.status, + message: error.message || HTTP_STATUS_CODE.bad_request.message, errorObject: error, }) } @@ -40,8 +40,8 @@ module.exports = class entities { return resolve(entityData) } catch (error) { return reject({ - status: error.status || HTTP_STATUS_CODE.internal_server_error.status, - message: error.message || HTTP_STATUS_CODE.internal_server_error.message, + status: error.status || HTTP_STATUS_CODE.bad_request.status, + message: error.message || HTTP_STATUS_CODE.bad_request.message, errorObject: error, }) } @@ -64,8 +64,8 @@ module.exports = class entities { return resolve(entityData) } catch (error) { return reject({ - status: error.status || HTTP_STATUS_CODE.internal_server_error.status, - message: error.message || HTTP_STATUS_CODE.internal_server_error.message, + status: error.status || HTTP_STATUS_CODE.bad_request.status, + message: error.message || HTTP_STATUS_CODE.bad_request.message, errorObject: error, }) } @@ -90,8 +90,8 @@ module.exports = class entities { return resolve(updatedCategories) } catch (error) { return reject({ - status: error.status || HTTP_STATUS_CODE.internal_server_error.status, - message: error.message || HTTP_STATUS_CODE.internal_server_error.message, + status: error.status || HTTP_STATUS_CODE.bad_request.status, + message: error.message || HTTP_STATUS_CODE.bad_request.message, errorObject: error, }) } @@ -138,8 +138,8 @@ module.exports = class entities { return resolve(document) } catch (error) { return reject({ - status: error.status || HTTP_STATUS_CODE.internal_server_error.status, - message: error.message || HTTP_STATUS_CODE.internal_server_error.message, + status: error.status || HTTP_STATUS_CODE.bad_request.status, + message: error.message || HTTP_STATUS_CODE.bad_request.message, errorObject: error, }) } @@ -198,8 +198,8 @@ module.exports = class entities { return resolve(entitiesDocuments) } catch (error) { return reject({ - status: error.status || HTTP_STATUS_CODE.internal_server_error.status, - message: error.message || HTTP_STATUS_CODE.internal_server_error.message, + status: error.status || HTTP_STATUS_CODE.bad_request.status, + message: error.message || HTTP_STATUS_CODE.bad_request.message, errorObject: error, }) } diff --git a/src/databaseQueries/entityTypes.js b/src/databaseQueries/entityTypes.js index 83a0284..96ffb48 100644 --- a/src/databaseQueries/entityTypes.js +++ b/src/databaseQueries/entityTypes.js @@ -53,7 +53,11 @@ module.exports = class EntityTypes { return resolve(entityTypesDoc) } catch (error) { - return reject(error) + return reject({ + status: error.status || HTTP_STATUS_CODE.bad_request.status, + message: error.message || HTTP_STATUS_CODE.bad_request.message, + errorObject: error, + }) } }) } @@ -74,8 +78,8 @@ module.exports = class EntityTypes { return resolve(entityData) } catch (error) { return reject({ - status: error.status || HTTP_STATUS_CODE.internal_server_error.status, - message: error.message || HTTP_STATUS_CODE.internal_server_error.message, + status: error.status || HTTP_STATUS_CODE.bad_request.status, + message: error.message || HTTP_STATUS_CODE.bad_request.message, errorObject: error, }) } @@ -122,8 +126,8 @@ module.exports = class EntityTypes { return resolve(document) } catch (error) { return reject({ - status: error.status || HTTP_STATUS_CODE.internal_server_error.status, - message: error.message || HTTP_STATUS_CODE.internal_server_error.message, + status: error.status || HTTP_STATUS_CODE.bad_request.status, + message: error.message || HTTP_STATUS_CODE.bad_request.message, errorObject: error, }) } @@ -152,8 +156,8 @@ module.exports = class EntityTypes { return resolve(entityTypeData) } catch (error) { return reject({ - status: error.status || HTTP_STATUS_CODE.internal_server_error.status, - message: error.message || HTTP_STATUS_CODE.internal_server_error.message, + status: error.status || HTTP_STATUS_CODE.bad_request.status, + message: error.message || HTTP_STATUS_CODE.bad_request.message, errorObject: error, }) } @@ -196,8 +200,8 @@ module.exports = class EntityTypes { }) } catch (error) { return resolve({ - success: false, - message: error.message, + status: error.status || HTTP_STATUS_CODE.bad_request.status, + message: error.message || HTTP_STATUS_CODE.bad_request.message, data: false, }) } diff --git a/src/databaseQueries/userRoleExtension.js b/src/databaseQueries/userRoleExtension.js index 09376c0..f9bc6e9 100644 --- a/src/databaseQueries/userRoleExtension.js +++ b/src/databaseQueries/userRoleExtension.js @@ -18,8 +18,8 @@ module.exports = class userRoleExtension { return resolve(userRoleData) } catch (error) { return reject({ - status: error.status || HTTP_STATUS_CODE.internal_server_error.status, - message: error.message || HTTP_STATUS_CODE.internal_server_error.message, + status: error.status || HTTP_STATUS_CODE.bad_request.status, + message: error.message || HTTP_STATUS_CODE.bad_request.message, errorObject: error, }) } @@ -44,8 +44,8 @@ module.exports = class userRoleExtension { return resolve(userData) } catch (error) { return reject({ - status: error.status || HTTP_STATUS_CODE.internal_server_error.status, - message: error.message || HTTP_STATUS_CODE.internal_server_error.message, + status: error.status || HTTP_STATUS_CODE.bad_request.status, + message: error.message || HTTP_STATUS_CODE.bad_request.message, errorObject: error, }) } @@ -100,8 +100,8 @@ module.exports = class userRoleExtension { return resolve(userDocuments) } catch (error) { return reject({ - status: error.status || HTTP_STATUS_CODE.internal_server_error.status, - message: error.message || HTTP_STATUS_CODE.internal_server_error.message, + status: error.status || HTTP_STATUS_CODE.bad_request.status, + message: error.message || HTTP_STATUS_CODE.bad_request.message, errorObject: error, }) } @@ -121,8 +121,8 @@ module.exports = class userRoleExtension { return resolve(userData) } catch (error) { return reject({ - status: error.status || HTTP_STATUS_CODE.internal_server_error.status, - message: error.message || HTTP_STATUS_CODE.internal_server_error.message, + status: error.status || HTTP_STATUS_CODE.bad_request.status, + message: error.message || HTTP_STATUS_CODE.bad_request.message, errorObject: error, }) } diff --git a/src/envVariables.js b/src/envVariables.js index 9a8cf28..f7f3a0b 100644 --- a/src/envVariables.js +++ b/src/envVariables.js @@ -32,6 +32,11 @@ let enviromentVariables = { message: 'Required api doc url', optional: false, }, + IS_AUTH_TOKEN_BEARER: { + message: 'Required specification: If auth token is bearer or not', + optional: true, + default: false, + }, } let success = true @@ -101,13 +106,13 @@ module.exports = function () { } if ( - (!process.env[eachEnvironmentVariable] || process.env[eachEnvironmentVariable] == '') && - enviromentVariables[eachEnvironmentVariable].default && - enviromentVariables[eachEnvironmentVariable].default != '' + (!process.env[eachEnvironmentVariable] || process.env[eachEnvironmentVariable].trim() === '') && + enviromentVariables[eachEnvironmentVariable]?.optional === true && + enviromentVariables[eachEnvironmentVariable]?.default !== undefined ) { process.env[eachEnvironmentVariable] = enviromentVariables[eachEnvironmentVariable].default - success = true keyCheckPass = true + success = true } if (!keyCheckPass) { diff --git a/src/generics/constants/api-responses.js b/src/generics/constants/api-responses.js index 4d9c9b9..7708986 100644 --- a/src/generics/constants/api-responses.js +++ b/src/generics/constants/api-responses.js @@ -20,7 +20,6 @@ module.exports = { INVALID_ENTITY_TYPE: 'INVALID_ENTITY_TYPE', ENTITY_TYPES_FETCHED: 'ENTITY_TYPES_FETCHED', PROJECT_NOT_CREATED: 'PROJECT_NOT_CREATED', - PROJECT_FAILED: 'PROJECT_FAILED', INVALID_OR_MISSING_SYSTEM_ID: 'INVALID_OR_MISSING_SYSTEM_ID', TOKEN_MISSING_CODE: 'TOKEN_MISSING_CODE', TOKEN_MISSING_MESSAGE: 'TOKEN_MISSING_MESSAGE', @@ -53,7 +52,12 @@ module.exports = { ROLES_NOT_FOUND: 'ROLES_NOT_FOUND', ENTITY_TYPE_DETAILS_NOT_FOUND: 'ENTITY_TYPE_DETAILS_NOT_FOUND', USER_ROLE_INFORMATION_CREATED: 'USER_ROLE_INFORMATION_CREATED', - USER_ROLE_UPDATATED: 'USER_ROLE_UPDATATED', + 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', + } diff --git a/src/generics/middleware/authenticator.js b/src/generics/middleware/authenticator.js index 4bc5dcd..4e06341 100644 --- a/src/generics/middleware/authenticator.js +++ b/src/generics/middleware/authenticator.js @@ -7,7 +7,7 @@ // dependencies const jwt = require('jsonwebtoken') - +const isBearerRequired = process.env.IS_AUTH_TOKEN_BEARER === 'true' var respUtil = function (resp) { return { status: resp.errCode, @@ -37,10 +37,11 @@ module.exports = async function (req, res, next, token = '') { delete req.headers[e] }) - var token = req.headers['x-auth-token'] if (!req.rspObj) req.rspObj = {} var rspObj = req.rspObj + token = req.headers['x-auth-token'] + let internalAccessApiPaths = CONSTANTS.common.INTERNAL_ACCESS_URLS let performInternalAccessTokenCheck = false await Promise.all( @@ -71,6 +72,20 @@ module.exports = async function (req, res, next, token = '') { return res.status(HTTP_STATUS_CODE['unauthorized'].status).send(respUtil(rspObj)) } + // Check if a Bearer token is required for authentication + if (isBearerRequired) { + const [authType, extractedToken] = token.split(' ') + if (authType.toLowerCase() !== 'bearer') { + rspObj.errCode = CONSTANTS.apiResponses.TOKEN_INVALID_CODE + rspObj.errMsg = CONSTANTS.apiResponses.TOKEN_INVALID_MESSAGE + rspObj.responseCode = HTTP_STATUS_CODE['unauthorized'].status + return res.status(HTTP_STATUS_CODE['unauthorized'].status).send(respUtil(rspObj)) + } + token = extractedToken?.trim() + } else { + token = token?.trim() + } + rspObj.errCode = CONSTANTS.apiResponses.TOKEN_INVALID_CODE rspObj.errMsg = CONSTANTS.apiResponses.TOKEN_INVALID_MESSAGE rspObj.responseCode = HTTP_STATUS_CODE['unauthorized'].status diff --git a/src/module/entities/helper.js b/src/module/entities/helper.js index 7b741b8..cb1d13a 100644 --- a/src/module/entities/helper.js +++ b/src/module/entities/helper.js @@ -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) @@ -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 && @@ -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'] }, @@ -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 diff --git a/src/module/entities/validator/v1.js b/src/module/entities/validator/v1.js index 9b33f3b..d025a4b 100644 --- a/src/module/entities/validator/v1.js +++ b/src/module/entities/validator/v1.js @@ -19,18 +19,32 @@ module.exports = (req) => { }, update: function () { req.checkParams('_id').exists().withMessage('required _id') - if (req.body.metaInformation) { + req.checkParams('_id').exists().isMongoId().withMessage('Invalid Entity ID') + if (req.body['metaInformation.name']) { + req.checkBody('metaInformation.name') + .exists() + .withMessage('The name field is required.') + .trim() + .notEmpty() + .withMessage('The name field cannot be empty.') + } + if (req.body['metaInformation.externalId']) { req.checkBody('metaInformation.externalId') .exists() - .withMessage('Metainformation must contain externalId.') + .withMessage('The name field is required.') + .trim() + .notEmpty() + .withMessage('The name field cannot be empty.') } }, subEntityList: function () { req.checkQuery('type').exists().withMessage('required type') req.checkParams('_id').exists().withMessage('required _id') + req.checkParams('_id').exists().isMongoId().withMessage('Invalid Entity ID') }, targetedRoles: function () { req.checkParams('_id').exists().withMessage('The entity ID (_id) is required.') + req.checkParams('_id').exists().isMongoId().withMessage('Invalid Entity ID') }, entityListBasedOnEntityType: function () { req.checkQuery('entityType').exists().withMessage('required entityType') @@ -44,19 +58,23 @@ 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') }, details: function () { - req.checkParams('_id').exists().withMessage('required state location id') + req.checkParams('_id').exists().withMessage('required id') + req.checkParams('_id').exists().isMongoId().withMessage('Invalid Entity ID') }, list: function () { req.checkQuery('type').exists().withMessage('required type') req.checkParams('_id').exists().withMessage('required entity id') + req.checkParams('_id').exists().isMongoId().withMessage('Invalid Entity ID') }, relatedEntities: function () { req.checkParams('_id').exists().withMessage('required Entity id') + req.checkParams('_id').exists().isMongoId().withMessage('Invalid Entity ID') }, bulkCreate: function () { if (!req.files || !req.files.entities) { diff --git a/src/module/entityTypes/helper.js b/src/module/entityTypes/helper.js index fea596a..09eba26 100644 --- a/src/module/entityTypes/helper.js +++ b/src/module/entityTypes/helper.js @@ -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( { @@ -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( { diff --git a/src/module/entityTypes/validator/v1.js b/src/module/entityTypes/validator/v1.js index 7cde3d4..5ca69df 100644 --- a/src/module/entityTypes/validator/v1.js +++ b/src/module/entityTypes/validator/v1.js @@ -20,6 +20,7 @@ module.exports = (req, res) => { }, update: function () { req.checkParams('_id').exists().withMessage('required _id') + req.checkParams('_id').exists().isMongoId().withMessage('Invalid EntityType ID') req.checkBody('name').exists().withMessage('required name') }, create: function () { diff --git a/src/module/userRoleExtension/helper.js b/src/module/userRoleExtension/helper.js index ad94c20..3b7875f 100644 --- a/src/module/userRoleExtension/helper.js +++ b/src/module/userRoleExtension/helper.js @@ -74,7 +74,7 @@ module.exports = class userRoleExtensionHelper { resolve({ success: true, - message: CONSTANTS.apiResponses.USER_ROLE_UPDATATED, + message: CONSTANTS.apiResponses.USER_ROLE_UPDATED, result: userInformation, }) } catch (error) { diff --git a/src/module/userRoleExtension/validator/v1.js b/src/module/userRoleExtension/validator/v1.js index a45d4bf..2c65c58 100644 --- a/src/module/userRoleExtension/validator/v1.js +++ b/src/module/userRoleExtension/validator/v1.js @@ -27,12 +27,14 @@ module.exports = (req) => { update: function () { req.checkBody('title').exists().withMessage('required title') req.checkParams('_id').exists().withMessage('required _id') + req.checkParams('_id').exists().isMongoId().withMessage('Invalid userRoleExtension ID') }, find: function () { req.checkBody('query').exists().withMessage('required query') }, delete: function () { req.checkParams('_id').exists().withMessage('required id') + req.checkParams('_id').exists().isMongoId().withMessage('Invalid userRoleExtension ID') }, }