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..487b2235 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, @@ -565,8 +566,8 @@ module.exports = class Users extends Abstract { message: constants.apiResponses.ENTITIES_NOT_ALLOWED_IN_ROLE }; } - - 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..8ef01fe7 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..0970a832 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; } @@ -1407,11 +1405,12 @@ 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 : ""; @@ -1420,7 +1419,7 @@ module.exports = class SolutionsHelper { if ( solutionType === constants.common.SURVEY ) { targetedSolution.isCreator = false; } - + mergedData.push(targetedSolution); delete targetedSolution.type; delete targetedSolution.externalId; @@ -1440,8 +1439,8 @@ module.exports = class SolutionsHelper { message: constants.apiResponses.TARGETED_OBSERVATION_FETCHED, data: { data: mergedData, - count: totalCount, - }, + count: totalCount + } }); } catch (error) { return reject({ @@ -1735,10 +1734,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); } @@ -1798,7 +1793,8 @@ module.exports = class SolutionsHelper { message: constants.apiResponses.LINK_VERIFIED, result: response, }); - } catch (error) { + } + catch (error) { return resolve({ success: false, status: error.status diff --git a/module/users/helper.js b/module/users/helper.js index 2497f8e3..7566a19f 100644 --- a/module/users/helper.js +++ b/module/users/helper.js @@ -599,7 +599,7 @@ module.exports = class UsersHelper { } const entitiesData = await entitiesHelper.entityDocuments(filterQuery, [ - "_id", + "_id", "childHierarchyPath" ]); if (!entitiesData.length > 0) { @@ -623,24 +623,33 @@ 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 - ); + for ( + let pointerToChildHierarchy = 0; + pointerToChildHierarchy < entityTypeIndex + 1; + pointerToChildHierarchy++ + ) { - entitiesMappingForm.result.forEach((entitiesMappingData) => { - entityTypes.push(entitiesMappingData.field); - }); + let entityType = entitiesData[0].childHierarchyPath[pointerToChildHierarchy]; + entityTypes.push(entityType); + } } return resolve({