diff --git a/controllers/v1/observationSubmissionsController.js b/controllers/v1/observationSubmissionsController.js index 39b87c6b..de5ee603 100644 --- a/controllers/v1/observationSubmissionsController.js +++ b/controllers/v1/observationSubmissionsController.js @@ -16,6 +16,7 @@ const questionsHelper = require(MODULES_BASE_PATH + "/questions/helper") const observationSubmissionsHelper = require(MODULES_BASE_PATH + "/observationSubmissions/helper") const scoringHelper = require(MODULES_BASE_PATH + "/scoring/helper") + /** * ObservationSubmissions * @class @@ -90,6 +91,7 @@ module.exports = class ObservationSubmissions extends Abstract { * @param {String} req.params._id -observation solution id. * @param {String} req.query.entityId -entity id. * @param {String} req.userDetails.userId - logged in user id. + * @param {String} req.userDetails.userToken - logged in user token. * @returns {JSON} - observation submissions creation. */ @@ -213,9 +215,11 @@ module.exports = class ObservationSubmissions extends Abstract { entityProfile: {}, status: "started", scoringSystem: solutionDocument.scoringSystem, - isRubricDriven: solutionDocument.isRubricDriven + isRubricDriven: solutionDocument.isRubricDriven, + userProfile : observationDocument.userProfile }; + if( solutionDocument.hasOwnProperty("criteriaLevelReport") ) { submissionDocument["criteriaLevelReport"] = solutionDocument["criteriaLevelReport"]; } diff --git a/controllers/v1/surveysController.js b/controllers/v1/surveysController.js index e61ba48e..97a1ae4f 100644 --- a/controllers/v1/surveysController.js +++ b/controllers/v1/surveysController.js @@ -12,6 +12,7 @@ const surveysHelper = require(MODULES_BASE_PATH + "/surveys/helper"); const assessorsHelper = require(MODULES_BASE_PATH + "/entityAssessors/helper"); const solutionsHelper = require(MODULES_BASE_PATH + "/solutions/helper"); + /** * Surveys * @class @@ -638,7 +639,7 @@ module.exports = class Surveys extends Abstract { let surveyDetails = {}; if( validateSurveyId || req.query.solutionId ) { - + let surveyId = req.params._id ? req.params._id : ""; surveyDetails = await surveysHelper.detailsV3 diff --git a/generics/httpStatusCodes/index.js b/generics/httpStatusCodes/index.js index 82aa9978..3fcf5ca5 100644 --- a/generics/httpStatusCodes/index.js +++ b/generics/httpStatusCodes/index.js @@ -17,7 +17,8 @@ module.exports = { }, 'ok': { status: 200, - message: "Success" + message: "Success", + code: "OK" }, 'created': { status: 201, diff --git a/generics/messageConstants/apiResponses.js b/generics/messageConstants/apiResponses.js index 42c80f51..920a8d92 100644 --- a/generics/messageConstants/apiResponses.js +++ b/generics/messageConstants/apiResponses.js @@ -367,5 +367,6 @@ module.exports = { "OBSERVATION_OR_SOLUTION_CHECK" : "Required Observation Id or Solution Id ", "SOLUTION_NOT_FOUND_OR_NOT_A_TARGETED": "Solution is not targeted to the role", "OBSERVATION_SUBMISSION_FOUND" : "Observation submission fetched successfully", - "SURVEY_SUBMISSION_FOUND": "Survey submission fetched successfully" + "SURVEY_SUBMISSION_FOUND": "Survey submission fetched successfully", + "SOLUTION_ID_AND_USERPROFILE_REQUIRED": "Required solution Id and userProfile" } diff --git a/generics/messageConstants/common.js b/generics/messageConstants/common.js index d43b2fb2..cb0f65e6 100644 --- a/generics/messageConstants/common.js +++ b/generics/messageConstants/common.js @@ -54,5 +54,7 @@ module.exports = { CREATED_BY_ME : "createdByMe", ALL_ROLES : "all", VERSION_3 : "v3", - DRAFT : "draft" + DRAFT : "draft", + DISTRICT : "district", + SERVER_TIME_OUT : 5000 } \ No newline at end of file diff --git a/generics/messageConstants/endpoints.js b/generics/messageConstants/endpoints.js index 6beb5ce2..fc7bdb00 100644 --- a/generics/messageConstants/endpoints.js +++ b/generics/messageConstants/endpoints.js @@ -17,5 +17,6 @@ module.exports = { SOLUTION_DETAILS_BASED_ON_ROLE_LOCATION : "/v1/solutions/detailsBasedOnRoleAndLocation", GET_PROJECT_TEMPLATE_LISTS : "/v1/project/templates/listByIds", DOWNLOADABLE_FILE_URL: "/v1/cloud-services/files/getDownloadableUrl", - USER_READ : "/user/v1/read" + USER_READ : "/user/v1/read", + USER_READ_V5 : "/v5/user/read" } \ No newline at end of file diff --git a/generics/services/users.js b/generics/services/users.js index c7fa692e..03ccd998 100644 --- a/generics/services/users.js +++ b/generics/services/users.js @@ -13,42 +13,44 @@ const profile = function ( token,userId = "" ) { return new Promise(async (resolve, reject) => { try { - let url = userServiceUrl + messageConstants.endpoints.USER_READ; - + let url = userServiceUrl + messageConstants.endpoints.USER_READ_V5; + if( userId !== "" ) { - url = url + "/" + userId; + url = url + "/" + userId + "?" + "fields=organisations,roles,locations,declarations,externalIds" } - + const options = { headers : { "content-type": "application/json", "x-authenticated-user-token" : token } }; - - request.post(url,options,kendraCallback); - - function kendraCallback(err, data) { - - let result = { - success : true - }; - + + request.get(url,options,userReadCallback); + let result = { + success : true + }; + function userReadCallback(err, data) { + if (err) { result.success = false; } else { - let response = JSON.parse(data.body); - if( response.status === HTTP_STATUS_CODE['ok'].status ) { + if( response.responseCode === httpStatusCode['ok'].code ) { result["data"] = response.result; } else { result.success = false; } } - + return resolve(result); } + setTimeout(function () { + return resolve (result = { + success : false + }); + }, messageConstants.common.SERVER_TIME_OUT); } catch (error) { return reject(error); diff --git a/models/observationSubmissions.js b/models/observationSubmissions.js index 2a8d258a..fe851647 100644 --- a/models/observationSubmissions.js +++ b/models/observationSubmissions.js @@ -88,6 +88,7 @@ module.exports = { referenceFrom : String, appInformation : Object, userRoleInformation : Object, - criteriaLevelReport : Boolean + criteriaLevelReport : Boolean, + userProfile : Object } }; diff --git a/models/observations.js b/models/observations.js index 3acbc11b..45cb332c 100644 --- a/models/observations.js +++ b/models/observations.js @@ -60,6 +60,7 @@ module.exports = { index: true }, userRoleInformation : Object, + userProfile : Object, deleted : { type : Boolean, index: true diff --git a/models/solutions.js b/models/solutions.js index dced93dd..d084735f 100644 --- a/models/solutions.js +++ b/models/solutions.js @@ -132,6 +132,7 @@ module.exports = { minNoOfSubmissionsRequired: { type: Number, default: 1 - } + }, + reportInformation : Object } }; \ No newline at end of file diff --git a/models/surveySubmissions.js b/models/surveySubmissions.js index 6e204fb4..daa5de52 100644 --- a/models/surveySubmissions.js +++ b/models/surveySubmissions.js @@ -48,7 +48,8 @@ module.exports = { description: { type: String, index: true } }, appInformation : Object, - userRoleInformation : Object + userRoleInformation : Object, + userProfile : Object } }; diff --git a/module/observations/helper.js b/module/observations/helper.js index 2bbf419d..b8cd61da 100644 --- a/module/observations/helper.js +++ b/module/observations/helper.js @@ -20,6 +20,7 @@ const FileStream = require(ROOT_PATH + "/generics/fileStream"); const submissionsHelper = require(MODULES_BASE_PATH + "/submissions/helper"); const programsHelper = require(MODULES_BASE_PATH + "/programs/helper"); const solutionHelper = require(MODULES_BASE_PATH + "/solutions/helper"); +const userProfileService = require(ROOT_PATH + "/generics/services/users"); /** * ObservationsHelper @@ -112,6 +113,19 @@ module.exports = class ObservationsHelper { } } + //Fetch user profile information by calling sunbird's user read api. + let addReportInfoToSolution = false; + let userProfileData = {}; + let userProfile = await userProfileService.profile(requestingUserAuthToken, userId); + + if ( userProfile.success && + userProfile.data && + userProfile.data.response + ) { + userProfileData = userProfile.data.response; + addReportInfoToSolution = true; + } + if( userRoleAndProfileInformation && Object.keys(userRoleAndProfileInformation).length > 0) { let solutionData = @@ -152,9 +166,17 @@ module.exports = class ObservationsHelper { data, userId, solutionData, - userRoleAndProfileInformation + userRoleAndProfileInformation, + userProfileData ); + if ( addReportInfoToSolution && observationData.solutionId ) { + let updateSolution = await solutionHelper.addReportInformationInSolution( + observationData.solutionId, + observationData.userProfile + ); + } + return resolve(_.pick(observationData, ["_id", "name", "description"])); } catch (error) { @@ -172,10 +194,11 @@ module.exports = class ObservationsHelper { * @param {Object} solution - Solution detail data. * @param {Object} solution - Solution detail data. * @param {Object} userRoleInformation - user role and profile details. + * @param {Object} userProfileInformation - user profile information. * @returns {Object} observation creation data. */ - static createObservation(data,userId,solution,userRoleInformation="") { + static createObservation(data,userId,solution,userRoleInformation="",userProfileInformation = {}) { return new Promise(async (resolve, reject) => { try { if (data.entities) { @@ -203,7 +226,8 @@ module.exports = class ObservationsHelper { "updatedBy": userId, "createdBy": userId, "isAPrivateProgram" : solution.isAPrivateProgram, - "userRoleInformation" : userRoleInformation ? userRoleInformation : {} + "userRoleInformation" : userRoleInformation ? userRoleInformation : {}, + "userProfile" : userProfileInformation ? userProfileInformation : {} }) ); diff --git a/module/solutions/helper.js b/module/solutions/helper.js index fd80b424..dd45b4fe 100644 --- a/module/solutions/helper.js +++ b/module/solutions/helper.js @@ -1970,6 +1970,86 @@ module.exports = class SolutionsHelper { return reject(error); } }); + } + + /** + * Update User District and Organisation In Solutions For Reporting. + * @method + * @name addReportInformationInSolution + * @param {String} solutionId - solution id. + * @param {Object} userProfile - user profile details + * @returns {Object} Solution information. +*/ + + static addReportInformationInSolution(solutionId,userProfile) { + return new Promise(async (resolve, reject) => { + try { + + //check solution & userProfile is exist + if ( + solutionId && userProfile && + userProfile["userLocations"] && + userProfile["organisations"] + ) { + + let district = []; + let organisation = []; + + //get the districts from the userProfile + for (const location of userProfile["userLocations"]) { + if ( location.type == messageConstants.common.DISTRICT ) { + let distData = {} + distData["locationId"] = location.id; + distData["name"] = location.name; + district.push(distData); + } + } + + //get the organisations from the userProfile + for (const org of userProfile["organisations"]) { + let orgData = {}; + orgData.orgName = org.orgName; + orgData.organisationId = org.organisationId; + organisation.push(orgData); + } + + let updateQuery = {}; + updateQuery["$addToSet"] = {}; + + if ( organisation.length > 0 ) { + updateQuery["$addToSet"]["reportInformation.organisations"] = { $each : organisation}; + } + + if ( district.length > 0 ) { + updateQuery["$addToSet"]["reportInformation.districts"] = { $each : district}; + } + + //add user district and organisation in solution + if ( updateQuery["$addToSet"] && Object.keys(updateQuery["$addToSet"].length > 0)) { + await this.updateSolutionDocument + ( + { _id : solutionId }, + updateQuery + ) + } + + } else { + throw new Error(messageConstants.apiResponses.SOLUTION_ID_AND_USERPROFILE_REQUIRED); + } + + return resolve({ + success: true, + message: messageConstants.apiResponses.UPDATED_DOCUMENT_SUCCESSFULLY + }); + + } catch (error) { + return resolve({ + success : false, + message : error.message, + data: [] + }); + } + }); } }; diff --git a/module/surveys/helper.js b/module/surveys/helper.js index 470765d4..9f9fb23a 100644 --- a/module/surveys/helper.js +++ b/module/surveys/helper.js @@ -20,6 +20,7 @@ const surveySolutionTemplate = "-SURVEY-TEMPLATE"; const surveyAndFeedback = "SF"; const questionsHelper = require(MODULES_BASE_PATH + "/questions/helper"); const userRolesHelper = require(MODULES_BASE_PATH + "/userRoles/helper"); +const userProfileService = require(ROOT_PATH + "/generics/services/users"); /** * SurveysHelper @@ -785,7 +786,8 @@ module.exports = class SurveysHelper { surveyId, userId, validateSurvey.data.submissionId, - roleInformation + roleInformation, + token ) if (!surveyDetails.success) { @@ -816,13 +818,14 @@ module.exports = class SurveysHelper { * @name details * @param {String} surveyId - survey id. * @param {String} userId - userId + * @param {String} userToken - userToken. * @returns {JSON} - returns survey solution, program and questions. */ - static details(surveyId = "", userId= "", submissionId = "", roleInformation = {}) { + static details(surveyId = "", userId= "", submissionId = "", roleInformation = {}, userToken ="") { return new Promise(async (resolve, reject) => { try { - + if (surveyId == "") { throw new Error(messageConstants.apiResponses.SURVEY_ID_REQUIRED) } @@ -1050,6 +1053,17 @@ module.exports = class SurveysHelper { }; submissionDocument.surveyInformation.startDate = new Date(); + //Fetch user profile information by calling sunbird's user read api. + let userProfileData = {}; + let userProfile = await userProfileService.profile(userToken, userId); + if ( userProfile.success && + userProfile.data && + userProfile.data.response + ) { + userProfileData = userProfile.data.response; + } + submissionDocument.userProfile = userProfileData; + if (Object.keys(roleInformation).length > 0 && roleInformation.role) { //commented for multiple role // let roleDocument = await userRolesHelper.list @@ -1072,7 +1086,7 @@ module.exports = class SurveysHelper { let submissionDoc = await database.models.surveySubmissions.create( submissionDocument ); - + if (submissionDoc._id) { assessment.submissionId = submissionDoc._id; } @@ -1473,7 +1487,8 @@ module.exports = class SurveysHelper { surveyData.data, userId, validateSurvey.data.submissionId, - bodyData + bodyData, + token ); if (!surveyDetails.success) { @@ -1609,7 +1624,7 @@ module.exports = class SurveysHelper { userId, token ); - + if( !surveyData.success ) { return resolve(surveyData); } @@ -1630,7 +1645,8 @@ module.exports = class SurveysHelper { surveyData.data, userId, validateSurvey.data.submissionId, - bodyData + bodyData, + token ) if (!surveyDetails.success) {