Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Learners User profile data fetch SB-28582 #75

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions controllers/v1/observationSubmissionsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -97,13 +98,14 @@ module.exports = class ObservationSubmissions extends Abstract {
return new Promise(async (resolve, reject) => {

try {

let observationDocument = await observationsHelper.observationDocuments({
_id: req.params._id,
createdBy: req.userDetails.userId,
status: {$ne:"inactive"},
entities: ObjectId(req.query.entityId)
});

if (!observationDocument[0]) {
return resolve({
status: httpStatusCode.bad_request.status,
Expand All @@ -112,7 +114,7 @@ module.exports = class ObservationSubmissions extends Abstract {
}

observationDocument = observationDocument[0];

let entityDocument = await entitiesHelper.entityDocuments({
_id: req.query.entityId,
entityType: observationDocument.entityType
Expand Down Expand Up @@ -219,19 +221,34 @@ module.exports = class ObservationSubmissions extends Abstract {
if( solutionDocument.hasOwnProperty("criteriaLevelReport") ) {
submissionDocument["criteriaLevelReport"] = solutionDocument["criteriaLevelReport"];
}

if( observationDocument.userRoleInformation && Object.keys(observationDocument.userRoleInformation).length > 0 ){
submissionDocument.userRoleInformation = observationDocument.userRoleInformation;
} else if( req.body && req.body.role && !observationDocument.userRoleInformation ){
submissionDocument.userRoleInformation = req.body;
let updateObservation = await observationsHelper.updateObservationDocument
(
{ _id: req.params._id },
{
$set: { userRoleInformation : req.body }
}
)
} else {
let userRoleAndProfileInformation = {};
let userProfile = await sunbirdUserProfile.profile( req.userDetails.userToken, req.userDetails.userId );

if ( userProfile.success &&
userProfile.data &&
userProfile.data.response
) {
let userProfileData = userProfile.data.response;
let userProfileDetails = await gen.utils.formatProfileData( userProfileData );
userRoleAndProfileInformation = userProfileDetails;
} else if ( req.body && req.body.role ){
userRoleAndProfileInformation = req.body;
}

submissionDocument.userRoleInformation = userRoleAndProfileInformation;
let updateObservation = await observationsHelper.updateObservationDocument
(
{ _id: req.params._id },
{
$set: { userRoleInformation : userRoleAndProfileInformation }
}
)
}


if( solutionDocument.referenceFrom === messageConstants.common.PROJECT ) {
submissionDocument["referenceFrom"] = messageConstants.common.PROJECT;
Expand Down
1 change: 0 additions & 1 deletion controllers/v1/observationsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ module.exports = class Observations extends Abstract {
return new Promise(async (resolve, reject) => {

try {

let result = await observationsHelper.create(
req.query.solutionId,
req.body.data,
Expand Down
4 changes: 1 addition & 3 deletions controllers/v1/surveysController.js
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,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
Expand All @@ -651,9 +651,7 @@ module.exports = class Surveys extends Abstract {
);

} else {

let bodyData = req.body ? req.body : {};

surveyDetails = await surveysHelper.getDetailsByLink(
req.params._id,
req.userDetails.userId,
Expand Down
2 changes: 1 addition & 1 deletion envVariables.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ let enviromentVariables = {
"DISABLE_LEARNER_SERVICE_ON_OFF": {
"message" : "Disable learner service on/off",
"default": "ON"
}
},
}

let success = true;
Expand Down
39 changes: 38 additions & 1 deletion generics/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,42 @@ function checkIfValidUUID(value) {
const regexExp = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/gi;
return regexExp.test(value);
}

/**
* @method
* @name formatProfileData
* @param {Object} profileDoc - user profile informations.
* @return {Object} profileData - formated user profile data.
*/

function formatProfileData( profileDoc ) {
return new Promise(async (resolve, reject) => {
try{

const profileData = {};
for (const location of profileDoc["userLocations"]) {
profileData[location.type] = location.id;
}
for (const org of profileDoc["organisations"]) {
if (org.isSchool) {
profileData["school"] = org.externalId;
}
}
const roles = [];
for (const userRole of profileDoc['profileUserTypes']) {
userRole.subType ? roles.push(userRole.subType.toUpperCase()) : roles.push(userRole.type.toUpperCase());
}
profileData['role'] = roles.toString();

return resolve(profileData);

} catch(error) {
return reject(error)
}

});
}

module.exports = {
camelCaseToTitleCase : camelCaseToTitleCase,
lowerCase : lowerCase,
Expand All @@ -296,5 +332,6 @@ module.exports = {
md5Hash : md5Hash,
removeDuplicatesFromArray : removeDuplicatesFromArray,
convertStringToBoolean : convertStringToBoolean,
checkIfValidUUID : checkIfValidUUID
checkIfValidUUID : checkIfValidUUID,
formatProfileData : formatProfileData
};
3 changes: 2 additions & 1 deletion generics/httpStatusCodes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ module.exports = {
},
'ok': {
status: 200,
message: "Success"
message: "Success",
code : "OK"
},
'created': {
status: 201,
Expand Down
4 changes: 3 additions & 1 deletion generics/messageConstants/endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ 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"

}
32 changes: 18 additions & 14 deletions generics/services/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
//dependencies
const request = require('request');
const userServiceUrl = process.env.USER_SERVICE_URL;
VISHNUDAS-tunerlabs marked this conversation as resolved.
Show resolved Hide resolved
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 = {
Expand All @@ -25,21 +26,19 @@ const profile = function ( token,userId = "" ) {
"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 ) {
VISHNUDAS-tunerlabs marked this conversation as resolved.
Show resolved Hide resolved
if( response.responseCode === httpStatusCode['ok'].code ) {
result["data"] = response.result;
} else {
result.success = false;
Expand All @@ -49,6 +48,11 @@ const profile = function ( token,userId = "" ) {

return resolve(result);
}
setTimeout(function () {
return reject (result = {
success : false
});
}, serverTimeout);

} catch (error) {
return reject(error);
Expand Down
19 changes: 17 additions & 2 deletions module/observations/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
/**
* ObservationsHelper
* @class
Expand Down Expand Up @@ -112,6 +112,19 @@ module.exports = class ObservationsHelper {
}
}

let userProfile = await sunbirdUserProfile.profile(requestingUserAuthToken, userId);

if ( userProfile.success &&
userProfile.data &&
userProfile.data.response
) {
let userProfileData = userProfile.data.response;
let userProfileDetails = await gen.utils.formatProfileData( userProfileData );
userRoleAndProfileInformation = userProfileDetails;
} else {
userRoleAndProfileInformation = userRoleAndProfileInformation;
}

if( userRoleAndProfileInformation && Object.keys(userRoleAndProfileInformation).length > 0) {

let solutionData =
Expand Down Expand Up @@ -163,6 +176,8 @@ module.exports = class ObservationsHelper {
})
}



/**
* Create observation.
* @method
Expand Down Expand Up @@ -1971,5 +1986,5 @@ module.exports = class ObservationsHelper {
}
});
}

};
Loading