Skip to content

Commit

Permalink
Merge pull request #94 from shikshalokam/5.0-entity-generalization
Browse files Browse the repository at this point in the history
5.0 entity generalization
  • Loading branch information
aks30 authored Aug 24, 2022
2 parents be945f8 + 0b2478c commit 6ced1e1
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 42 deletions.
5 changes: 4 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
3 changes: 2 additions & 1 deletion generics/constants/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};
3 changes: 2 additions & 1 deletion generics/constants/endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
};
33 changes: 29 additions & 4 deletions generics/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -252,5 +276,6 @@ module.exports = {
isValidMongoId : isValidMongoId,
convertProjectStatus : convertProjectStatus,
revertProjectStatus:revertProjectStatus,
revertStatusorNot:revertStatusorNot
revertStatusorNot:revertStatusorNot,
checkValidUUID : checkValidUUID
};
87 changes: 85 additions & 2 deletions generics/services/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
2 changes: 1 addition & 1 deletion models/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module.exports = {
default : []
},
entityId : {
type : "ObjectId",
type : String,
index : true
},
programId : {
Expand Down
16 changes: 10 additions & 6 deletions module/reports/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -70,7 +72,7 @@ module.exports = class ReportsHelper {
[]
);


let tasksReport = {
"total": 0,
"overdue": 0
Expand Down Expand Up @@ -319,6 +321,7 @@ module.exports = class ReportsHelper {
}
} catch (error) {
return resolve({

success: false,
message: error.message,
data: false
Expand Down Expand Up @@ -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 != "") {
Expand Down Expand Up @@ -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;
Expand Down
59 changes: 37 additions & 22 deletions module/userProjects/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand All @@ -190,7 +190,7 @@ module.exports = class UserProjectsHelper {
addOrUpdateEntityToProject = true;
}
}

if (addOrUpdateEntityToProject) {

let entityInformation =
Expand Down Expand Up @@ -924,7 +924,6 @@ module.exports = class UserProjectsHelper {
"projectTemplateId"
]
);

if (!project.length > 0) {
throw {
status: HTTP_STATUS_CODE['bad_request'].status,
Expand Down Expand Up @@ -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 = {};

Expand Down Expand Up @@ -1609,7 +1607,6 @@ module.exports = class UserProjectsHelper {
hasAcceptedTAndC : userProject.hasAcceptedTAndC ? userProject.hasAcceptedTAndC : false
}
});

} catch (error) {
return resolve({
status:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down

0 comments on commit 6ced1e1

Please sign in to comment.