From 44a349dd400368be34089b1d32795deec1687ad3 Mon Sep 17 00:00:00 2001 From: VISHNUDAS-tunerlabse Date: Mon, 18 Apr 2022 10:56:06 +0530 Subject: [PATCH 1/9] model change --- models/observations.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/models/observations.js b/models/observations.js index a105286e..0e9509a3 100644 --- a/models/observations.js +++ b/models/observations.js @@ -53,6 +53,7 @@ module.exports = { type: String, index: true }, - userRoleInformation : Object + userRoleInformation : Object, + userProfile : Object } }; \ No newline at end of file From bcec71bc1838b73f49152f9f4aac5a669fa22685 Mon Sep 17 00:00:00 2001 From: VISHNUDAS-tunerlabse Date: Mon, 18 Apr 2022 12:04:39 +0530 Subject: [PATCH 2/9] user profile data --- .../v1/observationSubmissionsController.js | 24 +++++++++++++ controllers/v1/surveysController.js | 3 +- generics/httpStatusCodes/index.js | 3 +- generics/messageConstants/endpoints.js | 3 +- generics/services/users.js | 36 ++++++++++--------- models/observationSubmissions.js | 3 +- models/surveySubmissions.js | 3 +- module/observations/helper.js | 21 +++++++++-- module/surveys/helper.js | 30 ++++++++++++---- 9 files changed, 95 insertions(+), 31 deletions(-) diff --git a/controllers/v1/observationSubmissionsController.js b/controllers/v1/observationSubmissionsController.js index 39b87c6b..d97db16b 100644 --- a/controllers/v1/observationSubmissionsController.js +++ b/controllers/v1/observationSubmissionsController.js @@ -15,6 +15,7 @@ const criteriaHelper = require(MODULES_BASE_PATH + "/criteria/helper") const questionsHelper = require(MODULES_BASE_PATH + "/questions/helper") const observationSubmissionsHelper = require(MODULES_BASE_PATH + "/observationSubmissions/helper") const scoringHelper = require(MODULES_BASE_PATH + "/scoring/helper") +const sunbirdUserProfile = require(ROOT_PATH + "/generics/services/users"); /** * ObservationSubmissions @@ -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. */ @@ -216,6 +218,28 @@ module.exports = class ObservationSubmissions extends Abstract { isRubricDriven: solutionDocument.isRubricDriven }; + //Fetch user profile information by calling sunbird's user read api. + let userProfileData = {}; + let userProfile = await sunbirdUserProfile.profile(req.userDetails.userToken, req.userDetails.userId); + if ( userProfile.success && + userProfile.data && + userProfile.data.response + ) { + userProfileData = userProfile.data.response; + if ( !observationDocument.userProfile || !Object.keys(observationDocument.userProfile).length > 0 ) { + let updateObservation = await observationsHelper.updateObservationDocument + ( + { _id: req.params._id }, + { + $set: { userProfile : userProfileData } + } + ) + } + } else if ( !Object.keys(userProfileData).length > 0 && observationDocument.userProfile && Object.keys(observationDocument.userProfile).length > 0 ) { + userProfileData = observationDocument.userProfile; + } + submissionDocument.userProfile = userProfileData; + 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/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..9bcd599d 100644 --- a/generics/services/users.js +++ b/generics/services/users.js @@ -8,47 +8,50 @@ //dependencies const request = require('request'); const userServiceUrl = process.env.USER_SERVICE_URL; +const serverTimeout = process.env.SUNBIRD_SERVER_TIMEOUT ? parseInt(process.env.SUNBIRD_SERVER_TIMEOUT) : 5000; 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 reject (result = { + success : false + }); + }, serverTimeout); } catch (error) { return reject(error); @@ -59,3 +62,4 @@ const profile = function ( token,userId = "" ) { module.exports = { profile : profile } +//https://staging.sunbirded.org/api/user/v5/read/ \ No newline at end of file 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/surveySubmissions.js b/models/surveySubmissions.js index b516c344..caad0657 100644 --- a/models/surveySubmissions.js +++ b/models/surveySubmissions.js @@ -45,7 +45,8 @@ module.exports = { }, surveyInformation: Object, appInformation : Object, - userRoleInformation : Object + userRoleInformation : Object, + userProfile : Object } }; diff --git a/module/observations/helper.js b/module/observations/helper.js index 2bbf419d..1a771bbc 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 sunbirdUserProfile = require(ROOT_PATH + "/generics/services/users"); /** * ObservationsHelper @@ -112,6 +113,17 @@ module.exports = class ObservationsHelper { } } + //Fetch user profile information by calling sunbird's user read api. + let userProfileData = {}; + let userProfile = await sunbirdUserProfile.profile(requestingUserAuthToken, userId); + + if ( userProfile.success && + userProfile.data && + userProfile.data.response + ) { + userProfileData = userProfile.data.response; + } + if( userRoleAndProfileInformation && Object.keys(userRoleAndProfileInformation).length > 0) { let solutionData = @@ -152,7 +164,8 @@ module.exports = class ObservationsHelper { data, userId, solutionData, - userRoleAndProfileInformation + userRoleAndProfileInformation, + userProfileData ); return resolve(_.pick(observationData, ["_id", "name", "description"])); @@ -172,10 +185,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 +217,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/surveys/helper.js b/module/surveys/helper.js index 470765d4..8e3cffde 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 sunbirdUserProfile = 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 sunbirdUserProfile.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) { From 7e0a97f68aa634a07a674953b67b96fc11569b87 Mon Sep 17 00:00:00 2001 From: VISHNUDAS-tunerlabse Date: Mon, 18 Apr 2022 12:06:00 +0530 Subject: [PATCH 3/9] comment removed --- generics/services/users.js | 1 - 1 file changed, 1 deletion(-) diff --git a/generics/services/users.js b/generics/services/users.js index 9bcd599d..4a5875d6 100644 --- a/generics/services/users.js +++ b/generics/services/users.js @@ -62,4 +62,3 @@ const profile = function ( token,userId = "" ) { module.exports = { profile : profile } -//https://staging.sunbirded.org/api/user/v5/read/ \ No newline at end of file From 9ccdd7bcbbb7f41bcd6b42cb01dcaa85a3bbf447 Mon Sep 17 00:00:00 2001 From: Priyanka Pradeep Date: Tue, 19 Apr 2022 14:28:09 +0530 Subject: [PATCH 4/9] report Information in solution --- generics/messageConstants/common.js | 3 +- models/solutions.js | 3 +- module/observations/helper.js | 107 +++++++++++++++++++++++++++- 3 files changed, 109 insertions(+), 4 deletions(-) diff --git a/generics/messageConstants/common.js b/generics/messageConstants/common.js index d43b2fb2..535d09cb 100644 --- a/generics/messageConstants/common.js +++ b/generics/messageConstants/common.js @@ -54,5 +54,6 @@ module.exports = { CREATED_BY_ME : "createdByMe", ALL_ROLES : "all", VERSION_3 : "v3", - DRAFT : "draft" + DRAFT : "draft", + DISTRICT : "district" } \ No newline at end of file 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/module/observations/helper.js b/module/observations/helper.js index 1a771bbc..ac6843e0 100644 --- a/module/observations/helper.js +++ b/module/observations/helper.js @@ -114,14 +114,16 @@ module.exports = class ObservationsHelper { } //Fetch user profile information by calling sunbird's user read api. + let addReportInfoToSolution = false; let userProfileData = {}; let userProfile = await sunbirdUserProfile.profile(requestingUserAuthToken, userId); - + if ( userProfile.success && userProfile.data && userProfile.data.response ) { userProfileData = userProfile.data.response; + addReportInfoToSolution = true; } if( userRoleAndProfileInformation && Object.keys(userRoleAndProfileInformation).length > 0) { @@ -168,6 +170,13 @@ module.exports = class ObservationsHelper { userProfileData ); + if ( addReportInfoToSolution && observationData.solutionId ) { + await _addReportInformationInSolution( + observationData.solutionId ? observationData.solutionId : "", + observationData.userProfile ? observationData.userProfile : {} + ); + } + return resolve(_.pick(observationData, ["_id", "name", "description"])); } catch (error) { @@ -1987,4 +1996,98 @@ module.exports = class ObservationsHelper { }); } -}; \ No newline at end of file +}; + +/** + * Update Report Information In Solutions. + * @method + * @name _addReportInformationInSolution + * @param {String} solutionId - solution id. + * @param {Object} userProfile - user read response + * @returns {Object} Solution information. +*/ + +function _addReportInformationInSolution(solutionId = "", userProfile = {}) { + return new Promise(async (resolve, reject) => { + try { + + if ( userProfile && solutionId ) { + + let district = {}; + let organisation = []; + + for (const location of userProfile["userLocations"]) { + if ( location.type == messageConstants.common.DISTRICT ) { + district["locationId"] = location.id; + district["name"] = location.name; + } + } + + for (const org of userProfile["organisations"]) { + let orgData = {}; + orgData.orgName = org.orgName; + orgData.organisationId = org.organisationId; + organisation.push(orgData); + } + + let solutionDocument = await solutionHelper.solutionDocuments({ + _id: solutionId + }, + [ + "_id", + "reportInformation" + ] + ); + + if( !solutionDocument.length > 0 ) { + throw { + message : messageConstants.apiResponses.SOLUTION_NOT_FOUND, + status : httpStatusCode["bad_request"].status + } + } + + solutionDocument = solutionDocument[0]; + + let reportInformation = solutionDocument.reportInformation ? solutionDocument.reportInformation : {}; + + if ( reportInformation ) { + reportInformation["districts"] = []; + reportInformation["districts"].push(district); + reportInformation["organisations"] = organisation; + } else { + + const checkDistrictExist = reportInformation["districts"].some(eachDistrict => { + if (eachDistrict.locationId == district["locationId"]) { + return true; + } + }); + + if ( !checkDistrictExist ) { + reportInformation["districts"].push(district); + } + + reportInformation["organisations"].concat(organisation); + } + + await solutionHelper.updateSolutionDocument + ( + { _id : ObjectId(solutionId) }, + { $set : { reportInformation: reportInformation } } + ) + } + + return resolve({ + success: true, + message: messageConstants.apiResponses.UPDATED_DOCUMENT_SUCCESSFULLY, + data: true + }); + + } catch (error) { + return resolve({ + success: false, + message: error.message, + data: false + }); + } + }) +} \ No newline at end of file From 2530075421afe1eaa19eee50575ae7dfdd67da7e Mon Sep 17 00:00:00 2001 From: VISHNUDAS-tunerlabse Date: Wed, 20 Apr 2022 10:15:10 +0530 Subject: [PATCH 5/9] PR review resolves --- .../v1/observationSubmissionsController.js | 26 +++---------------- generics/messageConstants/common.js | 3 ++- generics/services/users.js | 3 +-- module/observations/helper.js | 4 +-- module/surveys/helper.js | 4 +-- 5 files changed, 10 insertions(+), 30 deletions(-) diff --git a/controllers/v1/observationSubmissionsController.js b/controllers/v1/observationSubmissionsController.js index d97db16b..de5ee603 100644 --- a/controllers/v1/observationSubmissionsController.js +++ b/controllers/v1/observationSubmissionsController.js @@ -15,7 +15,7 @@ const criteriaHelper = require(MODULES_BASE_PATH + "/criteria/helper") const questionsHelper = require(MODULES_BASE_PATH + "/questions/helper") const observationSubmissionsHelper = require(MODULES_BASE_PATH + "/observationSubmissions/helper") const scoringHelper = require(MODULES_BASE_PATH + "/scoring/helper") -const sunbirdUserProfile = require(ROOT_PATH + "/generics/services/users"); + /** * ObservationSubmissions @@ -215,30 +215,10 @@ module.exports = class ObservationSubmissions extends Abstract { entityProfile: {}, status: "started", scoringSystem: solutionDocument.scoringSystem, - isRubricDriven: solutionDocument.isRubricDriven + isRubricDriven: solutionDocument.isRubricDriven, + userProfile : observationDocument.userProfile }; - //Fetch user profile information by calling sunbird's user read api. - let userProfileData = {}; - let userProfile = await sunbirdUserProfile.profile(req.userDetails.userToken, req.userDetails.userId); - if ( userProfile.success && - userProfile.data && - userProfile.data.response - ) { - userProfileData = userProfile.data.response; - if ( !observationDocument.userProfile || !Object.keys(observationDocument.userProfile).length > 0 ) { - let updateObservation = await observationsHelper.updateObservationDocument - ( - { _id: req.params._id }, - { - $set: { userProfile : userProfileData } - } - ) - } - } else if ( !Object.keys(userProfileData).length > 0 && observationDocument.userProfile && Object.keys(observationDocument.userProfile).length > 0 ) { - userProfileData = observationDocument.userProfile; - } - submissionDocument.userProfile = userProfileData; if( solutionDocument.hasOwnProperty("criteriaLevelReport") ) { submissionDocument["criteriaLevelReport"] = solutionDocument["criteriaLevelReport"]; diff --git a/generics/messageConstants/common.js b/generics/messageConstants/common.js index 535d09cb..cb0f65e6 100644 --- a/generics/messageConstants/common.js +++ b/generics/messageConstants/common.js @@ -55,5 +55,6 @@ module.exports = { ALL_ROLES : "all", VERSION_3 : "v3", DRAFT : "draft", - DISTRICT : "district" + DISTRICT : "district", + SERVER_TIME_OUT : 5000 } \ No newline at end of file diff --git a/generics/services/users.js b/generics/services/users.js index 4a5875d6..f713b012 100644 --- a/generics/services/users.js +++ b/generics/services/users.js @@ -8,7 +8,6 @@ //dependencies const request = require('request'); const userServiceUrl = process.env.USER_SERVICE_URL; -const serverTimeout = process.env.SUNBIRD_SERVER_TIMEOUT ? parseInt(process.env.SUNBIRD_SERVER_TIMEOUT) : 5000; const profile = function ( token,userId = "" ) { return new Promise(async (resolve, reject) => { @@ -51,7 +50,7 @@ const profile = function ( token,userId = "" ) { return reject (result = { success : false }); - }, serverTimeout); + }, messageConstants.common.SERVER_TIME_OUT); } catch (error) { return reject(error); diff --git a/module/observations/helper.js b/module/observations/helper.js index ac6843e0..c908f101 100644 --- a/module/observations/helper.js +++ b/module/observations/helper.js @@ -20,7 +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 sunbirdUserProfile = require(ROOT_PATH + "/generics/services/users"); +const userProfileService = require(ROOT_PATH + "/generics/services/users"); /** * ObservationsHelper @@ -116,7 +116,7 @@ module.exports = class ObservationsHelper { //Fetch user profile information by calling sunbird's user read api. let addReportInfoToSolution = false; let userProfileData = {}; - let userProfile = await sunbirdUserProfile.profile(requestingUserAuthToken, userId); + let userProfile = await userProfileService.profile(requestingUserAuthToken, userId); if ( userProfile.success && userProfile.data && diff --git a/module/surveys/helper.js b/module/surveys/helper.js index 8e3cffde..9f9fb23a 100644 --- a/module/surveys/helper.js +++ b/module/surveys/helper.js @@ -20,7 +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 sunbirdUserProfile = require(ROOT_PATH + "/generics/services/users"); +const userProfileService = require(ROOT_PATH + "/generics/services/users"); /** * SurveysHelper @@ -1055,7 +1055,7 @@ module.exports = class SurveysHelper { //Fetch user profile information by calling sunbird's user read api. let userProfileData = {}; - let userProfile = await sunbirdUserProfile.profile(userToken, userId); + let userProfile = await userProfileService.profile(userToken, userId); if ( userProfile.success && userProfile.data && userProfile.data.response From 73312ecebf6967ea17e64766253684ed77ae3f0e Mon Sep 17 00:00:00 2001 From: Priyanka Pradeep Date: Wed, 20 Apr 2022 13:34:42 +0530 Subject: [PATCH 6/9] PR changes --- generics/messageConstants/apiResponses.js | 3 +- module/observations/helper.js | 102 +--------------------- module/solutions/helper.js | 79 +++++++++++++++++ 3 files changed, 85 insertions(+), 99 deletions(-) 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/module/observations/helper.js b/module/observations/helper.js index c908f101..b8cd61da 100644 --- a/module/observations/helper.js +++ b/module/observations/helper.js @@ -171,9 +171,9 @@ module.exports = class ObservationsHelper { ); if ( addReportInfoToSolution && observationData.solutionId ) { - await _addReportInformationInSolution( - observationData.solutionId ? observationData.solutionId : "", - observationData.userProfile ? observationData.userProfile : {} + let updateSolution = await solutionHelper.addReportInformationInSolution( + observationData.solutionId, + observationData.userProfile ); } @@ -1996,98 +1996,4 @@ module.exports = class ObservationsHelper { }); } -}; - -/** - * Update Report Information In Solutions. - * @method - * @name _addReportInformationInSolution - * @param {String} solutionId - solution id. - * @param {Object} userProfile - user read response - * @returns {Object} Solution information. -*/ - -function _addReportInformationInSolution(solutionId = "", userProfile = {}) { - return new Promise(async (resolve, reject) => { - try { - - if ( userProfile && solutionId ) { - - let district = {}; - let organisation = []; - - for (const location of userProfile["userLocations"]) { - if ( location.type == messageConstants.common.DISTRICT ) { - district["locationId"] = location.id; - district["name"] = location.name; - } - } - - for (const org of userProfile["organisations"]) { - let orgData = {}; - orgData.orgName = org.orgName; - orgData.organisationId = org.organisationId; - organisation.push(orgData); - } - - let solutionDocument = await solutionHelper.solutionDocuments({ - _id: solutionId - }, - [ - "_id", - "reportInformation" - ] - ); - - if( !solutionDocument.length > 0 ) { - throw { - message : messageConstants.apiResponses.SOLUTION_NOT_FOUND, - status : httpStatusCode["bad_request"].status - } - } - - solutionDocument = solutionDocument[0]; - - let reportInformation = solutionDocument.reportInformation ? solutionDocument.reportInformation : {}; - - if ( reportInformation ) { - reportInformation["districts"] = []; - reportInformation["districts"].push(district); - reportInformation["organisations"] = organisation; - } else { - - const checkDistrictExist = reportInformation["districts"].some(eachDistrict => { - if (eachDistrict.locationId == district["locationId"]) { - return true; - } - }); - - if ( !checkDistrictExist ) { - reportInformation["districts"].push(district); - } - - reportInformation["organisations"].concat(organisation); - } - - await solutionHelper.updateSolutionDocument - ( - { _id : ObjectId(solutionId) }, - { $set : { reportInformation: reportInformation } } - ) - } - - return resolve({ - success: true, - message: messageConstants.apiResponses.UPDATED_DOCUMENT_SUCCESSFULLY, - data: true - }); - - } catch (error) { - return resolve({ - success: false, - message: error.message, - data: false - }); - } - }) -} \ No newline at end of file +}; \ No newline at end of file diff --git a/module/solutions/helper.js b/module/solutions/helper.js index fd80b424..8c3ba841 100644 --- a/module/solutions/helper.js +++ b/module/solutions/helper.js @@ -1970,6 +1970,85 @@ 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 && Object.keys(userProfile).length > 0 ) { + 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); + } + + //checking solution is exist + let solutionDocument = await this.solutionDocuments({ + _id: solutionId + }, + ["_id"]); + + if( !solutionDocument.length > 0 ) { + throw { + message : CONSTANTS.apiResponses.SOLUTION_NOT_FOUND, + status : HTTP_STATUS_CODE['bad_request'].status + } + } + + let updateQuery = {}; + updateQuery["$addToSet"] = {}; + updateQuery["$addToSet"]["reportInformation.organisations"] = { $each : organisation}; + updateQuery["$addToSet"]["reportInformation.districts"] = { $each : district}; + + //add user district and organisation in solution + await this.updateSolutionDocument + ( + { _id : solutionId }, + updateQuery + ) + }else{ + throw new Error(messageConstants.apiResponses.SOLUTION_ID_AND_USERPROFILE_REQUIRED); + } + + return resolve({ + success: true, + data: [] + }); + + } catch (error) { + return resolve({ + success : false, + message : error.message, + data: [] + }); + } + }); } }; From dfbb2af5f38274ade6b540f03736eca9acfe9ea2 Mon Sep 17 00:00:00 2001 From: VISHNUDAS-tunerlabse Date: Wed, 20 Apr 2022 14:54:16 +0530 Subject: [PATCH 7/9] timeout change --- generics/services/users.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generics/services/users.js b/generics/services/users.js index f713b012..03ccd998 100644 --- a/generics/services/users.js +++ b/generics/services/users.js @@ -47,7 +47,7 @@ const profile = function ( token,userId = "" ) { return resolve(result); } setTimeout(function () { - return reject (result = { + return resolve (result = { success : false }); }, messageConstants.common.SERVER_TIME_OUT); From ef17b667e01d423c01e2aaf6e9160eb36cd99188 Mon Sep 17 00:00:00 2001 From: Priyanka Pradeep Date: Thu, 21 Apr 2022 17:33:28 +0530 Subject: [PATCH 8/9] Pr changes --- module/solutions/helper.js | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/module/solutions/helper.js b/module/solutions/helper.js index 8c3ba841..4a5477b7 100644 --- a/module/solutions/helper.js +++ b/module/solutions/helper.js @@ -1986,7 +1986,12 @@ module.exports = class SolutionsHelper { try { //check solution & userProfile is exist - if ( solutionId && Object.keys(userProfile).length > 0 ) { + if ( + solutionId && userProfile && + userProfile["userLocations"] && + userProfile["organisations"] + ) { + let district = []; let organisation = []; @@ -2023,22 +2028,28 @@ module.exports = class SolutionsHelper { let updateQuery = {}; updateQuery["$addToSet"] = {}; - updateQuery["$addToSet"]["reportInformation.organisations"] = { $each : organisation}; - updateQuery["$addToSet"]["reportInformation.districts"] = { $each : district}; + 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 await this.updateSolutionDocument ( { _id : solutionId }, updateQuery ) - }else{ + } else { throw new Error(messageConstants.apiResponses.SOLUTION_ID_AND_USERPROFILE_REQUIRED); } return resolve({ - success: true, - data: [] + success: true + message: messageConstants.apiResponses.UPDATED_DOCUMENT_SUCCESSFULLY }); } catch (error) { From 8b2881e920712f158889769f6e7b35c21ef33729 Mon Sep 17 00:00:00 2001 From: Priyanka Pradeep Date: Fri, 22 Apr 2022 09:59:01 +0530 Subject: [PATCH 9/9] fixes --- module/solutions/helper.js | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/module/solutions/helper.js b/module/solutions/helper.js index 4a5477b7..dd45b4fe 100644 --- a/module/solutions/helper.js +++ b/module/solutions/helper.js @@ -2013,19 +2013,6 @@ module.exports = class SolutionsHelper { organisation.push(orgData); } - //checking solution is exist - let solutionDocument = await this.solutionDocuments({ - _id: solutionId - }, - ["_id"]); - - if( !solutionDocument.length > 0 ) { - throw { - message : CONSTANTS.apiResponses.SOLUTION_NOT_FOUND, - status : HTTP_STATUS_CODE['bad_request'].status - } - } - let updateQuery = {}; updateQuery["$addToSet"] = {}; @@ -2038,17 +2025,20 @@ module.exports = class SolutionsHelper { } //add user district and organisation in solution - await this.updateSolutionDocument - ( - { _id : solutionId }, - updateQuery - ) + 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 + success: true, message: messageConstants.apiResponses.UPDATED_DOCUMENT_SUCCESSFULLY });