diff --git a/.env.sample b/.env.sample index 81fa286c..f6307ace 100644 --- a/.env.sample +++ b/.env.sample @@ -27,4 +27,7 @@ KAFKA_GROUP_ID = "projects" # SUBMISSION TOPIC SUBMISSION_TOPIC = "dev.sl.projects.submissions" // Kafka topic name for pushing projects submissions -PROJECT_SUBMISSION_TOPIC = "dev.sl.projects.submissions" // project submission topic \ No newline at end of file +PROJECT_SUBMISSION_TOPIC = "dev.sl.projects.submissions" // project submission topic + +# SUNBIRD LOCATION AND USER READ +USER_SERVICE_URL = "http://user-service:3000" // service used for user profile read location search are using this base url \ No newline at end of file diff --git a/generics/constants/common.js b/generics/constants/common.js index d8440a78..0884d1e0 100644 --- a/generics/constants/common.js +++ b/generics/constants/common.js @@ -48,5 +48,6 @@ module.exports = { "DEFAULT_TASK_COMPLETED" : 0, "IMAGE_DATA_TYPE" : "image/jpeg", "DISTRICT": "district", - "SERVER_TIME_OUT" : 5000 + "SERVER_TIME_OUT" : 5000, + "OK" : "OK" }; diff --git a/generics/constants/endpoints.js b/generics/constants/endpoints.js index 385ffd15..11c43bb4 100644 --- a/generics/constants/endpoints.js +++ b/generics/constants/endpoints.js @@ -46,5 +46,6 @@ module.exports = { PROJECT_AND_TASK_REPORT : "/v1/improvement-project/projectAndTaskReport", FILES_DOWNLOADABLE_URL: "/v1/cloud-services/files/getDownloadableUrl", OBSERVATION_DETAILS : "/v1/observations/details", - USER_READ_V5 : "/v5/user/read" + USER_READ_V5 : "/v5/user/read", + GET_LOCATION_DATA : "/v1/location/search" }; diff --git a/generics/helpers/utils.js b/generics/helpers/utils.js index 1f1621b3..58d92e77 100644 --- a/generics/helpers/utils.js +++ b/generics/helpers/utils.js @@ -3,9 +3,10 @@ * author : Aman Karki * Date : 13-July-2020 * Description : All utility functions. - */ - - /** +*/ +// Dependencies +const {validate : uuidValidate,v4 : uuidV4} = require('uuid'); +/** * convert camel case to title case. * @function * @name camelCaseToTitleCase @@ -240,6 +241,29 @@ function revertStatusorNot( appVersion ) { } } + +/** + * check whether string is valid uuid. + * @function + * @name checkValidUUID + * @param {String} uuids + * @returns {Boolean} returns a Boolean value true/false +*/ + +function checkValidUUID(uuids) { + + var validateUUID = true; + if(Array.isArray(uuids)){ + for (var i = 0; uuids.length > i; i++) { + if(!uuidValidate(uuids[i])){ + validateUUID = false + } + } + }else { + validateUUID = uuidValidate(uuids); + } + return validateUUID; +} module.exports = { camelCaseToTitleCase : camelCaseToTitleCase, lowerCase : lowerCase, @@ -252,5 +276,6 @@ module.exports = { isValidMongoId : isValidMongoId, convertProjectStatus : convertProjectStatus, revertProjectStatus:revertProjectStatus, - revertStatusorNot:revertStatusorNot + revertStatusorNot:revertStatusorNot, + checkValidUUID : checkValidUUID }; diff --git a/generics/services/users.js b/generics/services/users.js index 17ceb69a..c5e5a04d 100644 --- a/generics/services/users.js +++ b/generics/services/users.js @@ -59,6 +59,89 @@ const profile = function ( token,userId = "" ) { }) } -module.exports = { - profile : profile + +/** + * + * @function + * @name locationSearch + * @param {object} filterData - location search filter object. + * @param {Boolean} formatResult - format result or not. + * @returns {Promise} returns a promise. +*/ + +const locationSearch = function ( filterData, formatResult = false ) { + return new Promise(async (resolve, reject) => { + try { + + let bodyData={}; + bodyData["request"] = {}; + bodyData["request"]["filters"] = filterData; + const url = + userServiceUrl + CONSTANTS.endpoints.GET_LOCATION_DATA; + const options = { + headers : { + "content-type": "application/json" + }, + json : bodyData + }; + + request.post(url,options,requestCallback); + + let result = { + success : true + }; + + function requestCallback(err, data) { + if (err) { + result.success = false; + } else { + let response = data.body; + + if( response.responseCode === CONSTANTS.common.OK && + response.result && + response.result.response && + response.result.response.length > 0 + ) { + if ( formatResult ) { + let entityResult =new Array; + response.result.response.map(entityData => { + let data = {}; + data._id = entityData.id; + data.entityType = entityData.type; + data.metaInformation = {}; + data.metaInformation.name = entityData.name; + data.metaInformation.externalId = entityData.code + data.registryDetails = {}; + data.registryDetails.locationId = entityData.id; + data.registryDetails.code = entityData.code; + entityResult.push(data); + }); + result["data"] = entityResult; + result["count"] = response.result.count; + } else { + result["data"] = response.result.response; + result["count"] = response.result.count; + } + + } else { + result.success = false; + } + } + return resolve(result); + } + + setTimeout(function () { + return resolve (result = { + success : false + }); + }, CONSTANTS.common.SERVER_TIME_OUT); + + } catch (error) { + return reject(error); + } + }) } +module.exports = { + profile : profile, + locationSearch : locationSearch +}; diff --git a/models/projects.js b/models/projects.js index 35a91116..c9c2a1a6 100644 --- a/models/projects.js +++ b/models/projects.js @@ -83,7 +83,7 @@ module.exports = { default : [] }, entityId : { - type : "ObjectId", + type : String, index : true }, programId : { diff --git a/module/reports/helper.js b/module/reports/helper.js index aec84087..a07f2ec5 100644 --- a/module/reports/helper.js +++ b/module/reports/helper.js @@ -38,12 +38,14 @@ module.exports = class ReportsHelper { return new Promise(async (resolve, reject) => { try { let query = { }; + if (entityId) { - query["entityId"] = ObjectId(entityId); + query["entityId"] = entityId; } else { query["userId"] = userId } + let dateRange = await _getDateRangeofReport(reportType); let endOf = dateRange.endOf; let startFrom = dateRange.startFrom; @@ -70,7 +72,7 @@ module.exports = class ReportsHelper { [] ); - + let tasksReport = { "total": 0, "overdue": 0 @@ -319,6 +321,7 @@ module.exports = class ReportsHelper { } } catch (error) { return resolve({ + success: false, message: error.message, data: false @@ -352,9 +355,9 @@ module.exports = class ReportsHelper { $exists : true } }; - - if(entityId != "" && UTILS.isValidMongoId(entityId)) { - query.entityId = ObjectId(entityId); + + if( entityId != "" ) { + query.entityId = entityId; } if (userRole != "") { @@ -484,11 +487,12 @@ module.exports = class ReportsHelper { }; if (entityId) { - query["entityId"] = ObjectId(entityId); + query["entityId"] = entityId; } else { query["userId"] = userId } + let chartObject = []; let dateRange = await _getDateRangeofReport(reportType); let endOf = dateRange.endOf; diff --git a/module/userProjects/helper.js b/module/userProjects/helper.js index 245ff30a..de6e4b76 100644 --- a/module/userProjects/helper.js +++ b/module/userProjects/helper.js @@ -178,7 +178,7 @@ module.exports = class UserProjectsHelper { let addOrUpdateEntityToProject = false; if (data.entityId) { - + // If entity is not present in project or new entity is updated. if ( !userProject[0].entityInformation || @@ -190,7 +190,7 @@ module.exports = class UserProjectsHelper { addOrUpdateEntityToProject = true; } } - + if (addOrUpdateEntityToProject) { let entityInformation = @@ -924,7 +924,6 @@ module.exports = class UserProjectsHelper { "projectTemplateId" ] ); - if (!project.length > 0) { throw { status: HTTP_STATUS_CODE['bad_request'].status, @@ -1436,7 +1435,6 @@ module.exports = class UserProjectsHelper { static add(data, userId, userToken, appName = "", appVersion = "") { return new Promise(async (resolve, reject) => { try { - const projectsModel = Object.keys(schemas["projects"].schema); let createProject = {}; @@ -1609,7 +1607,6 @@ module.exports = class UserProjectsHelper { hasAcceptedTAndC : userProject.hasAcceptedTAndC ? userProject.hasAcceptedTAndC : false } }); - } catch (error) { return resolve({ status: @@ -2680,27 +2677,48 @@ function _projectCategories(categories) { function _entitiesInformation(entityIds) { return new Promise(async (resolve, reject) => { try { + let locationIds = []; + let locationCodes = []; + let entityInformations = []; + entityIds.forEach(entity=>{ + if (UTILS.checkValidUUID(entity)) { + locationIds.push(entity); + } else { + locationCodes.push(entity); + } + }); - let entityData = - await coreService.entityDocuments( - entityIds, - ["metaInformation", "entityType", "entityTypeId", "registryDetails"] - ); + if ( locationIds.length > 0 ) { + let bodyData = { + "id" : locationIds + } + let entityData = await userProfileService.locationSearch( bodyData, formatResult = true); + if ( entityData.success ) { + entityInformations = entityData.data; + } + } - if (!entityData.success) { + if ( locationCodes.length > 0 ) { + let bodyData = { + "code" : locationCodes + } + let entityData = await userProfileService.locationSearch( bodyData , formatResult = true ); + if ( entityData.success ) { + entityInformations = entityInformations.concat(entityData.data); + } + } + + if ( !entityInformations.length > 0 ) { throw { status: HTTP_STATUS_CODE['bad_request'].status, message: CONSTANTS.apiResponses.ENTITY_NOT_FOUND } } - let entitiesData = []; - - if (entityData.success && entityData.data.length > 0) { - - entitiesData = _entitiesMetaInformation(entityData.data); + if ( entityInformations.length > 0 ) { + entitiesData = _entitiesMetaInformation(entityInformations); } - + return resolve({ success: true, data: entitiesData @@ -2967,15 +2985,12 @@ function _observationDetails(observationData, userRoleAndProfileInformation = {} */ function _entitiesMetaInformation(entitiesData) { - entitiesData = entitiesData.map(entity => { - entity.metaInformation._id = ObjectId(entity._id); + entity.metaInformation._id = entity._id; entity.metaInformation.entityType = entity.entityType; - entity.metaInformation.entityTypeId = ObjectId(entity.entityTypeId); entity.metaInformation.registryDetails = entity.registryDetails; return entity.metaInformation; - }); - + }); return entitiesData; } diff --git a/package.json b/package.json index a7afd220..ab919560 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,8 @@ "bunyan": "^1.8.12", "bunyan-format": "^0.2.1", "cache-manager": "^3.1.0", + "chai": "^4.2.0", + "chai-http": "^4.3.0", "cli-table": "^0.3.1", "cors": "^2.8.5", "csvtojson": "^2.0.10", @@ -49,21 +51,20 @@ "keycloak-auth-utils": "^3.3.0", "lodash": "^4.17.15", "log": "^1.4.0", + "mocha": "^6.2.2", "moment-timezone": "^0.5.31", "mongoose": "^5.9.4", "mongoose-autopopulate": "^0.12.0", "mongoose-delete": "^0.5.1", "mongoose-timestamp": "^0.6.0", "mongoose-ttl": "0.0.3", + "node-cache": "^5.1.2", "p-each-series": "^2.1.0", "path": "^0.12.7", "request": "^2.88.2", "require-all": "^3.0.0", "uuid": "^8.3.0", - "xml-js": "^1.6.11", - "chai": "^4.2.0", - "chai-http": "^4.3.0", - "mocha": "^6.2.2" + "xml-js": "^1.6.11" }, "devDependencies": { "grunt-apidoc": "^0.11.0",