diff --git a/controllers/v1/solutions.js b/controllers/v1/solutions.js index a93357f2..2b518c0b 100644 --- a/controllers/v1/solutions.js +++ b/controllers/v1/solutions.js @@ -835,7 +835,7 @@ module.exports = class Solutions extends Abstract { * @method * @name verifyLink * @param {Object} req - requested data. - * @param {String} req.params._id - solution Id + * @param {String} req.params._id - solution link * @returns {Array} */ diff --git a/controllers/v1/users.js b/controllers/v1/users.js index c4e58b43..3ff4b930 100644 --- a/controllers/v1/users.js +++ b/controllers/v1/users.js @@ -426,10 +426,11 @@ module.exports = class Users extends Abstract { let currentMaximumCountOfRequiredEntities = 0; let requiredEntities = new Array; + let roleArray = req.query.role.split(","); // Calculate required entities for each of the role and send the output of the role which has maximum length. - for (let roleCount = 0; roleCount < req.query.role.split(",").length; roleCount++) { - const eachRole = req.query.role.split(",")[roleCount]; + for (let roleCount = 0; roleCount < roleArray.length; roleCount++) { + const eachRole = roleArray[roleCount]; const entitiesMappingData = await usersHelper.entityTypesByLocationAndRole( req.params._id, @@ -566,7 +567,7 @@ module.exports = class Users extends Abstract { }; } - targetedEntities.result = targetedEntity.data; + targetedEntities.result = targetedEntity.data[0]; } } diff --git a/models/programs.js b/models/programs.js index 02bcdea9..dcacd41b 100644 --- a/models/programs.js +++ b/models/programs.js @@ -2,8 +2,14 @@ module.exports = { name: "programs", schema: { externalId: String, - name: String, - description: String, + name: { + type : String, + index : true + }, + description: { + type : String, + index : true + }, owner: String, createdBy: String, updatedBy: String, @@ -20,7 +26,8 @@ module.exports = { components: ["json"], isAPrivateProgram : { default : false, - type : Boolean + type : Boolean, + index : true }, scope : { entityType : String, diff --git a/models/solutions.js b/models/solutions.js index ee2a69d6..8d02825b 100644 --- a/models/solutions.js +++ b/models/solutions.js @@ -3,8 +3,14 @@ module.exports = { schema: { externalId: String, isReusable: Boolean, - name: String, - description: String, + name: { + type : String, + index : true + }, + description: { + type : String, + index : true + }, author: String, parentSolutionId: "ObjectId", resourceType: Array, @@ -87,7 +93,10 @@ module.exports = { }, criteriaLevelReport : Boolean, license:Object, - link: String, + link: { + type : String, + index : true + }, minNoOfSubmissionsRequired: { type: Number, default: 1 diff --git a/models/user-roles.js b/models/user-roles.js index bf9dce95..455f3ca7 100644 --- a/models/user-roles.js +++ b/models/user-roles.js @@ -10,7 +10,9 @@ module.exports = { schema: { code: { type: String, - required: true + required: true, + index: true, + unique: true }, title: { type: String, diff --git a/module/programs/helper.js b/module/programs/helper.js index 962e12f5..8757375b 100644 --- a/module/programs/helper.js +++ b/module/programs/helper.js @@ -498,29 +498,36 @@ module.exports = class ProgramsHelper { if ( targetedPrograms.success && targetedPrograms.data && targetedPrograms.data.data.length > 0) { - for ( - let targetedProgram = 0; - targetedProgram < targetedPrograms.data.data.length; - targetedProgram ++ - ) { - - let currentTargetedProgram = targetedPrograms.data.data[targetedProgram]; - - if( currentTargetedProgram.components.length > 0 ) { - - let solutions = await solutionsHelper.solutionDocuments({ - _id : { $in : currentTargetedProgram.components }, - isDeleted : false, - status : constants.common.ACTIVE - },["_id"]); - - if( solutions && solutions.length > 0 ) { - currentTargetedProgram["solutions"] = solutions.length; - delete currentTargetedProgram.components; - } + let componentsIds = []; + targetedPrograms.data.data.forEach(targetedProgram => { + if( targetedProgram.components.length > 0 ) { + componentsIds = componentsIds.concat(targetedProgram.components); + } + }); + + let solutions = await solutionsHelper.solutionDocuments({ + _id : { $in : componentsIds }, + isDeleted : false, + status : constants.common.ACTIVE + },["_id"]); + const solutionsIds = [] + solutions.forEach(solution => solutionsIds.push(solution._id.toString())); + + targetedPrograms.data.data.forEach(targetedProgram => { + + if( targetedProgram.components.length > 0 ) { + + let countSolutions = 0; + targetedProgram.components.forEach(component => { + if (solutionsIds.includes(component.toString())) { + countSolutions++; + } + }); + targetedProgram.solutions = countSolutions; + delete targetedProgram.components; } - } + }); } return resolve({ diff --git a/module/solutions/helper.js b/module/solutions/helper.js index f9571fd5..fa71ae78 100644 --- a/module/solutions/helper.js +++ b/module/solutions/helper.js @@ -1379,15 +1379,13 @@ module.exports = class SolutionsHelper { } let targetedSolutions = { - success: false, + success: false }; let getTargetedSolution = true; if ( filter === constants.common.DISCOVERED_BY_ME ) { getTargetedSolution = false; - } else if ( solutionType === constants.common.COURSE ) { - getTargetedSolution = true; } else if ( gen.utils.convertStringToBoolean(surveyReportPage) === true ) { getTargetedSolution = false; } @@ -1405,28 +1403,25 @@ module.exports = class SolutionsHelper { ); } - if( targetedSolutions.success ) { - - if( targetedSolutions.data.data && targetedSolutions.data.data.length > 0 ) { - totalCount += targetedSolutions.data.count; - targetedSolutions.data.data.forEach(targetedSolution => { - targetedSolution.solutionId = targetedSolution._id; - targetedSolution._id = ""; + if( targetedSolutions.success && targetedSolutions.data.data && targetedSolutions.data.data.length > 0 ) { + totalCount += targetedSolutions.data.count; + targetedSolutions.data.data.forEach(targetedSolution => { + targetedSolution.solutionId = targetedSolution._id; + targetedSolution._id = ""; - if( solutionType !== constants.common.COURSE ) { - targetedSolution["creator"] = targetedSolution.creator ? targetedSolution.creator : ""; - } - - if ( solutionType === constants.common.SURVEY ) { - targetedSolution.isCreator = false; - } - - mergedData.push(targetedSolution); - delete targetedSolution.type; - delete targetedSolution.externalId; + if( solutionType !== constants.common.COURSE ) { + targetedSolution["creator"] = targetedSolution.creator ? targetedSolution.creator : ""; + } - }); - } + if ( solutionType === constants.common.SURVEY ) { + targetedSolution.isCreator = false; + } + + mergedData.push(targetedSolution); + delete targetedSolution.type; + delete targetedSolution.externalId; + + }); } if( mergedData.length > 0 ) { @@ -1440,7 +1435,7 @@ module.exports = class SolutionsHelper { message: constants.apiResponses.TARGETED_OBSERVATION_FETCHED, data: { data: mergedData, - count: totalCount, + count: totalCount }, }); } catch (error) { @@ -1681,8 +1676,8 @@ module.exports = class SolutionsHelper { //non targeted project exist let checkIfUserProjectExistsQuery = { createdBy: userId, - referenceFrom: constants.common.LINK, link: link, + referenceFrom: constants.common.LINK }; let checkForProjectExist = @@ -1735,10 +1730,6 @@ module.exports = class SolutionsHelper { verified: false, }; - if ( link == "" ) { - throw new Error(constants.apiResponses.LINK_REQUIRED_CHECK); - } - if ( userToken == "" ) { throw new Error(constants.apiResponses.REQUIRED_USER_AUTH_TOKEN); } diff --git a/module/users/helper.js b/module/users/helper.js index 021724de..f3302316 100644 --- a/module/users/helper.js +++ b/module/users/helper.js @@ -590,6 +590,7 @@ module.exports = class UsersHelper { static entityTypesByLocationAndRole(stateLocationId, role) { return new Promise(async (resolve, reject) => { try { + let filterQuery = { "registryDetails.code": stateLocationId, }; @@ -601,7 +602,7 @@ module.exports = class UsersHelper { } const entitiesData = await entitiesHelper.entityDocuments(filterQuery, [ - "_id", + "_id", "childHierarchyPath" ]); if (!entitiesData.length > 0) { @@ -625,24 +626,36 @@ module.exports = class UsersHelper { let entityTypes = []; let stateEntityExists = false; + let roleEntityType = ""; rolesDocument[0].entityTypes.forEach((roleDocument) => { if (roleDocument.entityType === constants.common.STATE_ENTITY_TYPE) { stateEntityExists = true; } + + if (entitiesData[0].childHierarchyPath.includes(roleDocument.entityType)) { + roleEntityType = roleDocument.entityType; + } + }); + + let entityTypeIndex = + entitiesData[0].childHierarchyPath.findIndex(path => path === roleEntityType); if (stateEntityExists) { entityTypes = [constants.common.STATE_ENTITY_TYPE]; } else { - let entitiesMappingForm = await this.entitiesMappingForm( - entitiesData[0]._id, - rolesDocument[0]._id - ); - entitiesMappingForm.result.forEach((entitiesMappingData) => { - entityTypes.push(entitiesMappingData.field); - }); + for ( + let pointerToChildHierarchy = 0; + pointerToChildHierarchy < entityTypeIndex + 1; + pointerToChildHierarchy++ + ) { + + let entityType = entitiesData[0].childHierarchyPath[pointerToChildHierarchy]; + entityTypes.push(entityType); + } + } return resolve({