Skip to content

Commit

Permalink
Merge pull request #121 from shikshalokam/master
Browse files Browse the repository at this point in the history
Program Join changes for Observation and survey (ED-99, ED-540)
  • Loading branch information
aks30 authored Apr 27, 2023
2 parents 32c5d1e + a2dcad5 commit 6e0c7d5
Show file tree
Hide file tree
Showing 12 changed files with 355 additions and 25 deletions.
43 changes: 41 additions & 2 deletions controllers/v1/observationsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const userExtensionHelper = require(MODULES_BASE_PATH + "/userExtension/helper")
const programsHelper = require(MODULES_BASE_PATH + "/programs/helper");
const userRolesHelper = require(MODULES_BASE_PATH + "/userRoles/helper");
const userProfileService = require(ROOT_PATH + "/generics/services/users");
const coreService = require(ROOT_PATH + "/generics/services/core");
const programUsersHelper = require(MODULES_BASE_PATH + "/programUsers/helper");

/**
* Observations
Expand Down Expand Up @@ -1065,7 +1067,8 @@ module.exports = class Observations extends Abstract {
return new Promise(async (resolve, reject) => {

try {

let appVersion = req.headers["x-app-ver"] ? req.headers["x-app-ver"] : req.headers.appversion ? req.headers.appversion : "";
let appName = req.headers["x-app-id"] ? req.headers["x-app-id"] : req.headers.appname ? req.headers.appname : "";
let response = {
message: messageConstants.apiResponses.ASSESSMENT_FETCHED,
result: {}
Expand All @@ -1079,13 +1082,14 @@ module.exports = class Observations extends Abstract {
}
let observationDocument = await observationsHelper.observationDocuments( queryObject )
observationDocument = observationDocument[0]

if (!observationDocument) {
return resolve({
status: httpStatusCode.bad_request.status,
message: messageConstants.apiResponses.OBSERVATION_NOT_FOUND
});
}

let filterData = {
"type" : observationDocument.entityType
};
Expand Down Expand Up @@ -1164,6 +1168,41 @@ module.exports = class Observations extends Abstract {
throw messageConstants.apiResponses.PROGRAM_NOT_FOUND;
}

//fetch programUsers data
let programUsers = await programUsersHelper.programUsersDocuments(
{
userId : req.userDetails.userId,
programId : observationDocument.programId
},
[
"_id",
"resourcesStarted"
]
);

if (!programUsers.length > 0 || ( programUsers.length > 0 && programUsers[0].resourcesStarted == false)) {
// join observation's program. PII data consent is given via this api call.
// no need to check if usr already joined the program or not it is managed in ml-core service.

let programJoinData = {};
programJoinData.userRoleInformation = req.body;
programJoinData.isResource = true;
let joinProgram = await coreService.joinProgram (
req.userDetails.userToken,
programJoinData,
observationDocument.programId,
appVersion,
appName
);

if ( !joinProgram.success ) {
return resolve({
status: httpStatusCode.bad_request.status,
message: messageConstants.apiResponses.PROGRAM_JOIN_FAILED
});
}
}

/*
<- Currently not required for bodh-2:10 as roles is not given in user
*/
Expand Down
23 changes: 23 additions & 0 deletions controllers/v1/programUsersController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* name : programUsers.js
* author : Vishnu
* created-date : 9-Jan-2023
* Description : PII data related controller.
*/

/**
* programUsers
* @class
*/
module.exports = class ProgramUsers extends Abstract {
constructor() {
super(programUsersSchema);
}

static get name() {
return "programUsers";
}

};


12 changes: 9 additions & 3 deletions controllers/v1/surveysController.js
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,8 @@ module.exports = class Surveys extends Abstract {
async details(req) {
return new Promise(async (resolve, reject) => {
try {

let appVersion = req.headers["x-app-ver"] ? req.headers["x-app-ver"] : req.headers.appversion ? req.headers.appversion : "";
let appName = req.headers["x-app-id"] ? req.headers["x-app-id"] : req.headers.appname ? req.headers.appname : "";
let validateSurveyId = gen.utils.isValidMongoId(req.params._id);

let surveyDetails = {};
Expand All @@ -648,7 +649,9 @@ module.exports = class Surveys extends Abstract {
surveyId,
req.query.solutionId,
req.userDetails.userId,
req.userDetails.userToken
req.userDetails.userToken,
appVersion,
appName
);

} else {
Expand All @@ -659,7 +662,10 @@ module.exports = class Surveys extends Abstract {
req.params._id,
req.userDetails.userId,
req.userDetails.userToken,
bodyData
bodyData,
"",
appVersion,
appName
);
}

Expand Down
3 changes: 2 additions & 1 deletion generics/messageConstants/apiResponses.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,5 +370,6 @@ module.exports = {
"SOLUTION_ID_AND_USERPROFILE_REQUIRED": "Required solution Id and userProfile",
"USER_ROLES_NOT_FOUND" : "Could not found user roles",
"OBSERVATION_NOT_RELEVENT_FOR_USER" : "Dear User, this Observation is not relevant for your sub-role and location",
"SUBMISSION_NOT_FOUND_OR_SUBMISSION_STATUS_NOT_COMPLETE":"No submission found or Submission status is not completed"
"SUBMISSION_NOT_FOUND_OR_SUBMISSION_STATUS_NOT_COMPLETE":"No submission found or Submission status is not completed",
"PROGRAM_JOIN_FAILED" : "Failed to join program",
}
3 changes: 2 additions & 1 deletion generics/messageConstants/endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ module.exports = {
USER_READ_V5 : "/v5/user/read",
GET_LOCATION_DATA : "/v1/location/search",
GET_FORM_DATA : "/plugin/v1/form/read",
GET_SCHOOL_DATA : "/v1/org/search"
GET_SCHOOL_DATA : "/v1/org/search",
JOIN_PROGRAM : "/v1/programs/join"
}
68 changes: 67 additions & 1 deletion generics/services/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,71 @@ const downloadableUrls = function (bodyData) {
})

}

/**
* Join program.
* @function
* @name joinProgram
* @param {String} token - User token.
* @param {Object} bodyData - Requested body data.
* @param {String} programId - program id.
* @param {Number} appVersion - appVersion.
* @param {String} appName - appName.
* @returns {JSON} - Details of program join.
*/

const joinProgram = function ( token, bodyData, programId, appVersion = "", appName = "") {
return new Promise(async (resolve, reject) => {
try {

const url =
coreServiceBaseURL + messageConstants.endpoints.JOIN_PROGRAM + "/" + programId;

let options = {
headers : {
"content-type": "application/json",
"internal-access-token": process.env.INTERNAL_ACCESS_TOKEN,
"x-authenticated-user-token" : token
},
json : bodyData
};
if ( appVersion !== "" ) {
options.headers.appversion = appVersion;
}

if ( appName !== "" ) {
options.headers.appname = appName;
}

request.post(url,options,kendraCallback);

function kendraCallback(err, data) {

let result = {
success : true
};

if (err) {
result.success = false;
} else {

let response = data.body;

if( response.status === httpStatusCode['ok'].status ) {
result["data"] = response.result;
} else {
result.success = false;
}
}

return resolve(result);
}

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

module.exports = {
getDownloadableUrl : getDownloadableUrl,
Expand All @@ -369,5 +434,6 @@ module.exports = {
getUsersByEntityAndRole : getUsersByEntityAndRole,
solutionBasedOnRoleAndLocation : solutionBasedOnRoleAndLocation,
solutionDetailsBasedOnRoleAndLocation : solutionDetailsBasedOnRoleAndLocation,
downloadableUrls: downloadableUrls
downloadableUrls: downloadableUrls,
joinProgram: joinProgram
};
32 changes: 32 additions & 0 deletions models/programUsers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = {
name: "programUsers",
schema: {
programId: {
type : "ObjectId",
required: true,
index: true
},
userId: {
type: String,
required: true,
index: true
},
resourcesStarted: {
type: Boolean,
index: true,
default: false
},
userProfile: {
type : Object,
required: true
},
userRoleInformation: Object,
appInformation: Object
},
compoundIndex: [
{
"name" :{ userId: 1, programId: 1 },
"indexType" : { unique: true }
}
]
};
4 changes: 3 additions & 1 deletion models/programs.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ module.exports = {
index : true
}
}]
}
},
rootOrganisations : Array,
createdFor : Array
}
};
4 changes: 1 addition & 3 deletions module/observationSubmissions/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ const emailClient = require(ROOT_PATH + "/generics/helpers/emailCommunications")
const scoringHelper = require(MODULES_BASE_PATH + "/scoring/helper")
const criteriaHelper = require(MODULES_BASE_PATH + "/criteria/helper")
const questionsHelper = require(MODULES_BASE_PATH + "/questions/helper")
const entitiesHelper = require(MODULES_BASE_PATH + "/entities/helper")
const solutionHelper = require(MODULES_BASE_PATH + "/solutions/helper");
const criteriaQuestionHelper = require(MODULES_BASE_PATH + "/criteriaQuestions/helper");
const programsHelper = require(MODULES_BASE_PATH + "/programs/helper");
const userProfileService = require(ROOT_PATH + "/generics/services/users");

/**
* ObservationSubmissionsHelper
* @class
Expand Down Expand Up @@ -441,7 +439,7 @@ module.exports = class ObservationSubmissionsHelper {
"evidencesStatus.submissions",
"evidencesStatus.canBeNotApplicable",
"evidencesStatus.canBeNotAllowed",
"evidencesStatus.notApplicable",
"evidencesStatus.notApplicable"
];
let result = await this.observationSubmissionsDocument
(
Expand Down
25 changes: 21 additions & 4 deletions module/observations/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const solutionHelper = require(MODULES_BASE_PATH + "/solutions/helper");
const userProfileService = require(ROOT_PATH + "/generics/services/users");
const formService = require(ROOT_PATH + "/generics/services/form");
const userRolesHelper = require(MODULES_BASE_PATH + "/userRoles/helper");
const programUsersHelper = require(MODULES_BASE_PATH + "/programUsers/helper");

/**
* ObservationsHelper
Expand Down Expand Up @@ -1697,7 +1698,6 @@ module.exports = class ObservationsHelper {
static entities( userId,token,observationId,solutionId,bodyData) {
return new Promise(async (resolve, reject) => {
try {

if( observationId === "" ) {

let observationData = await this.observationDocuments({
Expand Down Expand Up @@ -1772,9 +1772,12 @@ module.exports = class ObservationsHelper {

let observationData = await this.observationDocuments({
_id : observationId,
},["_id","solutionId"]);
},["_id","solutionId","programId"]);

let solutionData;
let requestForPIIConsent;
let rootOrganisations;
let programJoined;
if(observationData[0]){

solutionData = await solutionHelper.solutionDocuments({
Expand All @@ -1784,9 +1787,20 @@ module.exports = class ObservationsHelper {
"allowMultipleAssessemts",
"license"
]);

//Check data present in programUsers collection.
//checkForUserJoinedProgram will check for data and if its present return true else false.
programJoined = await programUsersHelper.checkForUserJoinedProgram(observationData[0].programId,userId);

// get requestForPIIconsent value and rootOrganisations of program. rootOrganisations added by programs team
let programsData = await programsHelper.list({
_id : observationData[0].programId
},["requestForPIIConsent", "rootOrganisations"]);

requestForPIIConsent = ( programsData[0].requestForPIIConsent ) ? programsData[0].requestForPIIConsent : false;
rootOrganisations = ( programsData[0].rootOrganisations ) ? programsData[0].rootOrganisations : [];
}

return resolve({
success : true,
message : messageConstants.apiResponses.OBSERVATION_ENTITIES_FETCHED,
Expand All @@ -1795,7 +1809,10 @@ module.exports = class ObservationsHelper {
_id : observationId,
"entities" : entitiesList.data.entities,
entityType : entitiesList.data.entityType,
"license" : solutionData[0].license
"license" : solutionData[0].license,
programJoined : programJoined,
"rootOrganisations" : rootOrganisations,
"requestForPIIConsent" : requestForPIIConsent
}
});

Expand Down
Loading

0 comments on commit 6e0c7d5

Please sign in to comment.