From ac160ab0a451366cee683972969b0c3dc09bb27e Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Mon, 4 Jul 2022 15:14:46 +0530 Subject: [PATCH 01/36] added migration script --- script/README.md | Bin 0 -> 1326 bytes script/api-list/headers.js | 85 +++++ script/api-list/program.js | 136 +++++++ script/api-list/question.js | 110 ++++++ script/api-list/user.js | 41 +++ script/constant/config.js | 63 ++++ script/db/dbConfig.js | 42 +++ script/db/index.js | 46 +++ script/package.json | 23 ++ script/server.js | 54 +++ script/template/config/question.js | 300 +++++++++++++++ script/template/config/questionSet.js | 77 ++++ script/template/generate/gProgram.js | 193 ++++++++++ script/template/generate/gQuestion.js | 261 +++++++++++++ script/template/generate/gQuestionSet.js | 365 +++++++++++++++++++ script/template/helpers/hierarchyHelper.js | 263 +++++++++++++ script/template/helpers/questionsetHelper.js | 116 ++++++ 17 files changed, 2175 insertions(+) create mode 100644 script/README.md create mode 100644 script/api-list/headers.js create mode 100644 script/api-list/program.js create mode 100644 script/api-list/question.js create mode 100644 script/api-list/user.js create mode 100644 script/constant/config.js create mode 100644 script/db/dbConfig.js create mode 100644 script/db/index.js create mode 100644 script/package.json create mode 100644 script/server.js create mode 100644 script/template/config/question.js create mode 100644 script/template/config/questionSet.js create mode 100644 script/template/generate/gProgram.js create mode 100644 script/template/generate/gQuestion.js create mode 100644 script/template/generate/gQuestionSet.js create mode 100644 script/template/helpers/hierarchyHelper.js create mode 100644 script/template/helpers/questionsetHelper.js diff --git a/script/README.md b/script/README.md new file mode 100644 index 0000000000000000000000000000000000000000..31a17fe8e95d724870402d75fc6d40a062dc6fdc GIT binary patch literal 1326 zcma)+NlyYn5QX1W6aPae96Y(k#Dv6yf>Gl}a5;EU#1V-y;;GVub zP1UPcOV{sDNlur#;JM*XZMBrCp}yMsREq3IO(bn~k*#TUvP-h%RO4^L`wZ=R7kndREzc{D5oI*W!(n)urtDZNqaP4db?q zuPYrA_c6L5$_^21fM*#U64l5ftx%8uYfx4H!17DKF} zS$Fx{V|^1}Muul=g>JUWKC(wx&BJRFne|-IZO=t4>KRkJ9|`D|p=p-Jv`YT&|J7|; zvr0cybiRaMKs(eO-DAB_=k7gFr(53Fx*BICKood8gPs3e&{V$z?E4h$}>A+0SQ{`x7C44VTK4j(_XkOTz`+0j literal 0 HcmV?d00001 diff --git a/script/api-list/headers.js b/script/api-list/headers.js new file mode 100644 index 00000000..cbe592a1 --- /dev/null +++ b/script/api-list/headers.js @@ -0,0 +1,85 @@ +const { default: axios } = require("axios"); +const { CONFIG } = require("../constant/config"); +const querystring = require("querystring"); +const jwt = require("jsonwebtoken"); + +const genToken = async (url, body, type) => { + const isValid = await validateToken(type); + + const headers = { + "Content-Type": "application/x-www-form-urlencoded", + }; + + if (!isValid) { + const res = await axios.post(url, body, headers).catch((err) => { + console.log("Error while generateToken", err.response.data); + return err; + }); + return res ? res.data.access_token : ""; + } else { + const token = type === "dev" ? this.dev_token : this.dock_token; + + return token; + } +}; + +const validateToken = (type) => { + const token = type === "dev" ? this.dev_token : this.dock_token; + + try { + jwt.verify(token, "shhhhh"); + return true; + } catch (err) { + return false; + } +}; + +const generateToken = async (type) => { + let url = ""; + let body = {}; + + switch (type) { + case "dev": + url = CONFIG.SUNBIRD.HOST.dev + CONFIG.SUNBIRD.APIS.token; + body = querystring.stringify({ ...CONFIG.SUNBIRD.config.dev.query }); + this.dev_token = await genToken(url, body, "dev"); + return this.dev_token; + case "dock": + url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.token; + body = querystring.stringify({ ...CONFIG.SUNBIRD.config.dock.query }); + this.dock_token = await genToken(url, body, "dock"); + return this.dock_token; + } +}; + +const getHeaders = async (isTokenReq, type) => { + let headers = {}; + + switch (type) { + case "dev": + headers = { + "Content-Type": "application/json", + Authorization: CONFIG.SUNBIRD.config.dev.authorization, + }; + if (isTokenReq) { + headers["x-authenticated-user-token"] = await generateToken("dev"); + } + break; + + case "dock": + headers = { + "Content-Type": "application/json", + Authorization: CONFIG.SUNBIRD.config.dock.authorization, + }; + if (isTokenReq) { + headers["x-authenticated-user-token"] = await generateToken("dock"); + } + break; + } + + return headers; +}; + +module.exports = { + getHeaders, +}; diff --git a/script/api-list/program.js b/script/api-list/program.js new file mode 100644 index 00000000..f5ddb499 --- /dev/null +++ b/script/api-list/program.js @@ -0,0 +1,136 @@ +const { default: axios } = require("axios"); +const { CONFIG } = require("../constant/config"); +const { getHeaders } = require("./headers"); + +const nominateProgram = async (program_id, author) => { + const url = + CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.add_program_nomination; + const data = { + request: { + program_id: program_id, + status: "Pending", + collection_ids: [], + targetprimarycategories: [ + { + name: "Observation", + identifier: "obj-cat:observation_questionset_all", + targetObjectType: "QuestionSet", + }, + { + name: "Survey", + identifier: "obj-cat:survey_questionset_all", + targetObjectType: "QuestionSet", + } + ], + content_types: [], + organisation_id: "937dd865-b256-4c1a-9830-a9b5b89f0913", + user_id: "bb551fff-121e-4a18-b969-984ac62bd572", + }, + }; + const config = { + method: "post", + url: url, + headers: await getHeaders(true, "dock"), + data: data, + }; + + + const res = await axios(config).catch((err) => { + console.log("Error while nominating Program", err.response.data); + }); + return res.data; +}; + +const updateContributorToProgram = async (reqData) => { + + const url = + CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.update_program_nomination; + const data = { + request: { + ...reqData + }, + }; + + const config = { + method: "post", + url: url, + headers: await getHeaders(true, "dock"), + data: data, + }; + + + const res = await axios(config).catch((err) => { + console.log( + "Error while updating contributor to the Program", + err.response.data + ); + }); + return res.data; +}; + +const createProgram = async (templateData) => { + const url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.create_program; + const data = { + request: { + ...templateData, + }, + }; + const config = { + method: "post", + url: url, + headers: await getHeaders(false, "dock"), + data: data, + }; + + const res = await axios(config).catch((err) => { + console.log("Error while creating the Program", err.response.data); + }); + return res.data.result.program_id; +}; + +const updateProgram = async (templateData) => { + const url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.update_program; + const data = { + request: { + ...templateData, + }, + }; + const config = { + method: "post", + url: url, + headers: await getHeaders(false, "dock"), + data: data, + }; + const res = await axios(config).catch((err) => { + console.log("Error while updateProgram", err.response.data); + }); + + return res.data; +}; + +const publishProgram = async (templateData) => { + const url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.publish_program; + const data = { + request: { + ...templateData, + }, + }; + const config = { + method: "post", + url: url, + headers: await getHeaders(false, "dock"), + data: data, + }; + const res = await axios(config).catch((err) => { + console.log("Error while publishProgram", err.response.data); + }); + return res.data; +}; + +module.exports = { + createProgram, + updateProgram, + publishProgram, + nominateProgram, + updateContributorToProgram, +}; diff --git a/script/api-list/question.js b/script/api-list/question.js new file mode 100644 index 00000000..413f5009 --- /dev/null +++ b/script/api-list/question.js @@ -0,0 +1,110 @@ +const { default: axios } = require("axios"); +const { CONFIG } = require("../constant/config"); +const { getHeaders } = require("./headers"); + + +// Questionset + +const createQuestionSet = async (templateData) => { + const url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.create_questionset; + const data = { + request: { + questionset: { ...templateData }, + }, + }; + const config = { + method: "post", + url: url, + headers: await getHeaders(true, "dock"), + data: data, + }; + const res = await axios(config).catch((err) => { + console.log("Error while creating Questionset", err.response.data); + }); + return res.data.result.identifier; +}; + +const updateQuestionSetHierarchy = async (templateData) => { + const url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.update_hierarchy; + + const config = { + method: "patch", + url: url, + headers: await getHeaders(true, "dock"), + data: templateData, + }; + + const res = await axios(config).catch((err) => { + console.log("Error while updating the questionset", err.response.data); + }); + return res.data.result.identifiers; +}; + +const publishQuestionSet = async (questionsetId) => { + const url = + CONFIG.SUNBIRD.HOST.dock + + CONFIG.SUNBIRD.APIS.publish_questionset + + "/" + + questionsetId; + const config = { + method: "post", + url: url, + headers: await getHeaders(true, "dock"), + data: {}, + }; + + const res = await axios(config).catch((err) => { + console.log("Error while publishing the questionset", err.response.data); + }); +}; + +// Questions +const createQuestions = async (templateData) => { + const url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.create_question; + const data = { + request: { + question: { ...templateData }, + }, + }; + const config = { + method: "post", + url: url, + headers: await getHeaders(true, "dock"), + data: data, + }; + const res = await axios(config).catch((err) => { + console.log("Error while creating the question", err.response.data); + }); + return res.data.result.identifier; +}; + +const publishQuestion = async (questionId) => { + const url = + CONFIG.SUNBIRD.HOST.dock + + CONFIG.SUNBIRD.APIS.publish_question + + "/" + + questionId; + const config = { + method: "post", + url: url, + headers: await getHeaders(true, "dock"), + data: {}, + }; + + const res = await axios(config).catch((err) => { + console.log( + "Error while publishing the question", + questionId, + err.response.data + ); + }); + return res.data.result.identifier; +}; + +module.exports = { + createQuestionSet, + updateQuestionSetHierarchy, + publishQuestionSet, + createQuestions, + publishQuestion +}; diff --git a/script/api-list/user.js b/script/api-list/user.js new file mode 100644 index 00000000..41aaa173 --- /dev/null +++ b/script/api-list/user.js @@ -0,0 +1,41 @@ +const { default: axios } = require("axios"); +const { CONFIG } = require("../constant/config"); +const { getHeaders } = require("./headers"); + +const readUser = async (userId) => { + const params = "organisations,roles,locations,declarations,externalIds"; + const url = + CONFIG.SUNBIRD.HOST.dev + + CONFIG.SUNBIRD.APIS.read_user + + userId + + "?fields=" + + params; + + const res = await axios + .get(url, await getHeaders(true, "dev")) + .catch((err) => { + console.log("Error while reading User", err.response.data); + }); +}; + +const searchUser = async (userId) => { + const url = CONFIG.SUNBIRD.HOST.dev + CONFIG.SUNBIRD.APIS.search_user; + const config = { + method: "post", + url: url, + headers: await getHeaders(true, "dev"), + data: { + request: { filters: { id: userId } }, + }, + }; + + const res = await axios(config).catch((err) => { + console.log("Error while searching User", err.response.data); + }); + return res.data?.result?.response?.content; +}; + +module.exports = { + readUser, + searchUser, +}; diff --git a/script/constant/config.js b/script/constant/config.js new file mode 100644 index 00000000..9567f2da --- /dev/null +++ b/script/constant/config.js @@ -0,0 +1,63 @@ +require("dotenv").config(); + +const CONFIG = { + HOST: process.env.HOST, + PORT: process.env.PORT, + DB: { + DB_HOST: process.env.MONGODB_URL, + DB_NAME: process.env.DB_NAME, + TABLES: { + solutions: "solutions", + criteriaQuestions: "criteriaQuestions", + questions: "questions", + }, + }, + SUNBIRD: { + HOST: { + dev: "https://dev.sunbirded.org/", + dock: "https://dock.sunbirded.org/", + }, + APIS: { + token: "auth/realms/sunbird/protocol/openid-connect/token", + read_user: "api/user/v5/read/", + search_user: "api/user/v3/search", + create_questionset: "api/questionset/v1/create", + update_hierarchy: "api/questionset/v1/hierarchy/update", + publish_questionset: "api/questionset/v1/publish", + create_question: "api/question/v1/create", + publish_question: "api/question/v1/publish", + create_program: "api/program/v1/create", + update_program: "api/program/v1/update", + add_program_nomination: "api/program/v1/nomination/add", + update_program_nomination: "api/program/v1/nomination/update", + publish_program: "api/program/v1/publish", + }, + config: { + dev: { + query: { + username: process.env.dev_username, + password: process.env.dev_password, + grant_type: process.env.dev_grant_type, + client_id: process.env.dev_client_id, + client_secret: process.env.dev_client_secret, + }, + authorization: process.env.DEV_AUTHORIZATION, + }, + dock: { + query: { + username: process.env.dev_username, + password: process.env.dev_password, + grant_type: process.env.dev_grant_type, + client_id: process.env.dev_client_id, + client_secret: process.env.dev_client_secret, + }, + nominate_user: process.env.nominate_user, + authorization: process.env.DOCK_AUTHORIZATION, + }, + }, + }, +}; + +module.exports = { + CONFIG, +}; diff --git a/script/db/dbConfig.js b/script/db/dbConfig.js new file mode 100644 index 00000000..8fdfff18 --- /dev/null +++ b/script/db/dbConfig.js @@ -0,0 +1,42 @@ +const { CONFIG } = require("../constant/config"); + +const mongoose = require("mongoose"); + + +const connect = async () => { + try { + // const db = await mongoose.createConnection( + // // CONFIG.DB.DB_HOST + // CONFIG.DB.DB_HOST, + // { + // useNewUrlParser: true, + // } + // ); + + const Conn = mongoose.createConnection(); + + // connect to database + this.database = await Conn.openUri(CONFIG.DB.DB_HOST); + // Conn.on("error", console.error.bind(console, "connection error:")); + // Conn.once("open", function () { + // console.log("Connected to DB"); + // }); + // this.database = db; + // .db(CONFIG.DB.DB_NAME); + } catch (err) { + console.log("Error While connecting to DB", err); + } +}; + +const createDBInstance = async () => { + await connect(); +}; + +const getDBInstance = () => { + return this.database; +}; + +module.exports = { + createDBInstance, + getDBInstance, +}; diff --git a/script/db/index.js b/script/db/index.js new file mode 100644 index 00000000..43431c79 --- /dev/null +++ b/script/db/index.js @@ -0,0 +1,46 @@ +const { getDBInstance } = require("./dbConfig"); +const { ObjectId } = require("mongodb"); + + + +const findAll = async (clName, query) => { + try { + const db = await getDBInstance(); + return await db + .collection(clName) + .find({...query}) + .toArray(); + } catch(err) { + console.log('findAll Error: ', err); + } +}; + +const findOne = async (clName, query) => { + try { + const db = await getDBInstance(); + return await db + .collection(clName) + .findOne({...query}) + } catch(err) { + console.log('findOne Error:', err); + } +}; + +const updateById = async (clName, id, query) => { + try { + const db = await getDBInstance(); + const res = await db + .collection(clName) + .updateOne({ _id: ObjectId(id) }, { $set: { ...query } }, { upsert: true }); + } catch (err) { + console.log("updateById = ", id, "Error: " , err); + } +}; + + + +module.exports = { + findAll, + updateById, + findOne +}; diff --git a/script/package.json b/script/package.json new file mode 100644 index 00000000..1517088c --- /dev/null +++ b/script/package.json @@ -0,0 +1,23 @@ +{ + "name": "ml-survey", + "version": "1.0.0", + "description": "ml-survey-datas", + "main": "server.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "nodemon server.js" + }, + "author": "KenPath Technologies", + "license": "ISC", + "dependencies": { + "axios": "^0.26.1", + "dotenv": "^16.0.0", + "express": "^4.17.3", + "jsonwebtoken": "^8.5.1", + "lodash": "^4.17.21", + "mongodb": "^4.4.1", + "mongoose": "^6.2.8", + "nodemon": "^2.0.15", + "querystring": "^0.2.1" + } +} diff --git a/script/server.js b/script/server.js new file mode 100644 index 00000000..e81dcb5e --- /dev/null +++ b/script/server.js @@ -0,0 +1,54 @@ +const { CONFIG } = require("./constant/config"); +const { createDBInstance } = require("./db/dbConfig"); +const { findAll } = require("./db"); + +const express = require("express"); +const { + getQuestionSetTemplates, +} = require("./template/generate/gQuestionSet.js"); + +const { ObjectId } = require("mongodb"); + +const app = express(); +const PORT = 4000; + + +const migrateData = async (req, res) => { + try { + const db = await createDBInstance(); + // req.query.questionsetID || + // const id = "5f35044f19377eecddb06922"; + const data = await findAll("solutions", { + // _id: ObjectId(id), + // $or: [{migratedId: {$eq: null}}, {migratedId:{$exists: false}}], + programId: { $exists: true }, + type: {$in: ["observation", "survey"]} + }); + const template = await getQuestionSetTemplates(data); + // return template; + process.exit(); + } catch (err) { + console.log("Error while reading questionSet", err); + throw new Error("Error occured", err); + + } +}; + +migrateData(); + + + +// app.post("/questionset", async (req, res) => { +// try { +// const data = await migrateData(req); + +// res.status(200).json(data); +// } catch (err) { +// console.log("Error", err); +// res.send(err); +// } +// }); + +// app.listen(PORT, () => { +// console.log("app is running at port", PORT); +// }); diff --git a/script/template/config/question.js b/script/template/config/question.js new file mode 100644 index 00000000..34c089c9 --- /dev/null +++ b/script/template/config/question.js @@ -0,0 +1,300 @@ +const questionTemplate = { + date: { + name: "Migrated Question", + code: "externalId", + mimeType: "application/vnd.sunbird.question", + primaryCategory: "responseType", + interactionTypes: "responseType", + showRemarks: "showRemarks", + instructions: { + en: [null], + }, + body: "question", + editorState: { + question: "question", + }, + responseDeclaration: { + response1: { + type: "string", + }, + }, + interactions: { + validation: "validation", + response1: { + validation: { + pattern: "dateFormat", + }, + autoCapture: "autoCapture", + }, + }, + hints: "hint", + }, + slider: { + name: "Migrated Question-Slider Type", + code: "externalId", + mimeType: "application/vnd.sunbird.question", + primaryCategory: "responseType", + interactionTypes: "responseType", + showRemarks: "showRemarks", + instructions: { + en: [null], + }, + body: "question", + editorState: { + question: "question", + }, + responseDeclaration: { + response1: { + type: "integer", + maxScore: 1, + }, + }, + interactions: { + validation: { + required: "validation.required", + }, + response1: { + validation: { + range: { + min: "validation.min", + max: "validation.max", + }, + }, + step: "1", + }, + }, + hints: "hint", + evidence: { + mimeType: "file.type", + }, + }, + multiselect: { + name: "Migrated Question", + code: "externalId", + mimeType: "application/vnd.sunbird.question", + primaryCategory: "Multiselect Multiple Choice Question", + interactionTypes: ["choice"], + showRemarks: "showRemarks", + body: "question", + hints: "hint", + interactions: { + response1: { + type: "choice", + options: [ + { + label: "options[0]", + value: 0, + }, + { + label: "options[1]", + value: 1, + }, + { + label: "options[2]", + value: 2, + }, + { + label: "options[3]", + value: 3, + }, + { + label: "options[4]", + value: 4, + }, + ], + }, + validation: "validation", + }, + editorState: { + options: [ + { + answer: false, + value: { + body: "options[0]", + value: 0, + }, + }, + { + answer: false, + value: { + body: "options[1]", + value: 1, + }, + }, + { + answer: false, + value: { + body: "options[2]", + value: 2, + }, + }, + { + answer: false, + value: { + body: "options[3]", + value: 3, + }, + }, + { + answer: false, + value: { + body: "options[4]", + value: 4, + }, + }, + ], + question: "question", + }, + instructions: { + en: [null], + }, + evidence: { + mimeType: "file.type", + }, + responseDeclaration: { + response1: { + maxScore: 1, + cardinality: "multiple", + type: "integer", + correctResponse: { + outcomes: { + SCORE: 1, + }, + }, + mapping: [], + }, + }, + }, + mcq: { + name: "Migrated Question", + code: "externalId", + description: "", + mimeType: "application/vnd.sunbird.question", + primaryCategory: "Multiselect Multiple Choice Question", + interactionTypes: ["choice"], + body: "question", + interactions: { + validation: "validation", + response1: { + type: "choice", + options: [ + { + value: 0, + label: "options[0]", + }, + { + value: 1, + label: "options[1]", + }, + ], + }, + }, + editorState: { + options: [ + { + answer: false, + value: { + body: "options[0]", + value: 0, + }, + }, + { + answer: false, + value: { + body: "options[1]", + value: 1, + }, + }, + ], + question: "question", + }, + responseDeclaration: { + response1: { + maxScore: 0, + cardinality: "single", + type: "integer", + correctResponse: { + outcomes: { + SCORE: 0, + }, + }, + }, + }, + instructions: { + default: "tip", + }, + hints: "hint", + }, + text: { + name: "Migrated Question", + code: "externalId", + mimeType: "application/vnd.sunbird.question", + primaryCategory: "Text", + interactionTypes: ["text"], + showRemarks: "showRemarks", + body: "question", + instructions: { + en: [null], + }, + editorState: { + question: "question", + }, + responseDeclaration: { + response1: { + type: "string", + maxScore: 1, + }, + }, + interactions: { + validation: { + required: "validation.required", + }, + response1: { + validation: { + limit: { + maxLength: "100", + }, + }, + type: { + number: "validation.isNumber", + }, + }, + }, + hints: "hint", + }, + +}; + +const questionStatic = { + date: ["name", "mimeType", "instructions", "responseDeclaration"], + slider: ["name", "mimeType", "instructions", "responseDeclaration"], + multiselect: [ + "name", + "mimeType", + "instructions", + "primaryCategory", + "interactionTypes", + "responseDeclaration", + ], + mcq: [ + "name", + "mimeType", + "primaryCategory", + "interactionTypes", + "responseDeclaration", + ], + text: [ + "name", + "mimeType", + "instructions", + "primaryCategory", + "interactionTypes", + "responseDeclaration", + "instructions" + ] +}; + +module.exports = { + questionTemplate, + questionStatic, +}; diff --git a/script/template/config/questionSet.js b/script/template/config/questionSet.js new file mode 100644 index 00000000..3218e134 --- /dev/null +++ b/script/template/config/questionSet.js @@ -0,0 +1,77 @@ +const questionSetTemplate = { + name: "name", + description: "description", + code: "externalId", + mimeType: "application/vnd.sunbird.questionset", + primaryCategory: "type", + entityType: "entityType", + language: "language", + keywords: "keywords", + startDate: "startDate", + endDate: "endDate", + createdBy: "4e397c42-495e-4fdb-8558-f98176230916", + organisationId: "937dd865-b256-4c1a-9830-a9b5b89f0913", + creator: "check1@yopmail.com", + createdFor: ["01309282781705830427"], + channel: "01309282781705830427", + programId: "546b3ca0-cb7c-11ec-a165-33909bc91f74", + author: "check1@yopmail.com", + framework: "nit_tpd", +}; + +const questionSetTemplateStatic = [ + "mimeType", + "createdBy", + "creator", + "programId", + "author", + "organisationId", + "framework", + "channel", + "createdFor" +]; + +const program = { + name: `{solutionName} sourcing project`, + description: `{solutionName} sourcing project description`, + nomination_enddate: "2022-05-26", + shortlisting_enddate: "2022-05-26", + enddate: "2022-05-26", + content_submission_enddate: "2022-05-26", + rewards: null, + content_types: [], + target_collection_category: [], + sourcing_org_name: "NIT", + type: "public", + target_type: "searchCriteria", + rootorg_id: "01285019302823526477", + createdby: "5a587cc1-e018-4859-a0a8-e842650b9d64", + createdOn: "2022-05-26", + startdate: "2022-05-26", + slug: "sunbird", + status: "Draft", + program_id: "", + config: { + defaultContributeOrgReview: false, + }, +}; +const programTemplateStatic = [ + "rewards", + "content_types", + "target_collection_category", + "type", + "target_type", + "rootorg_id", + "createdby", + "slug", + "status", + "program_id", + "config", +]; + +module.exports = { + questionSetTemplate, + questionSetTemplateStatic, + program, + programTemplateStatic, +}; diff --git a/script/template/generate/gProgram.js b/script/template/generate/gProgram.js new file mode 100644 index 00000000..e377a7ab --- /dev/null +++ b/script/template/generate/gProgram.js @@ -0,0 +1,193 @@ +const { isEmpty } = require("lodash"); +const { + createProgram, + updateProgram, + publishProgram, + nominateProgram, + updateContributorToProgram, +} = require("../../api-list/program"); +const { searchUser } = require("../../api-list/user"); +const { updateSolutionById } = require("../helpers/questionsetHelper"); + +const getDate = (increment) => { + const d = new Date( + new Date().setDate(new Date().getDate() + increment) + ).toISOString(); + + return d; +}; + +const createProgramTemplate = async (solution, program_id) => { + const userData = await searchUser(solution.author); + + const userId = + userData?.length > 0 + ? solution.author + : "5a587cc1-e018-4859-a0a8-e842650b9d64"; + const rootOrgId = + userData?.length > 0 ? userData[0]?.rootOrgId : "01309282781705830427"; + + const template = { + name: `Migrated ${solution?.name} sourcing project`, + description: `${solution?.name} sourcing project description`, + nomination_enddate: `${getDate(1)}`, + rewards: null, + shortlisting_enddate: `${getDate(1)}`, + enddate: `${getDate(2)}`, + content_submission_enddate: `${getDate(1)}`, + type: "public", + target_type: "searchCriteria", + content_types: [], + target_collection_category: [], + sourcing_org_name: "NIT", + rootorg_id: rootOrgId, + createdby: userId, + createdOn: `${getDate(0)}`, + startdate: `${getDate(1)}`, + slug: "sunbird", + status: "Draft", + program_id: "", + config: { + defaultContributeOrgReview: false, + roles: [ + { + id: 1, + name: "CONTRIBUTOR", + tabs: [1], + default: true, + defaultTab: 1, + }, + { + id: 2, + name: "REVIEWER", + tabs: [2], + defaultTab: 2, + }, + ], + }, + }; + + let programId = solution.sourcingProgramId; + + let query = {}; + + if (!program_id) { + programId = await createProgram(template); + query = { ...query, sourcingProgramId: programId }; + } + + if (!solution.isSrcProgramUpdated) { + await updateProgramTemplate(programId, solution); + query = { + ...query, + isSrcProgramUpdated: true, + }; + } + + if (!solution.isSrcProgramPublished) { + const pub_res = await publishProgramTemplate(programId); + query = { + ...query, + isSrcProgramPublished: true, + }; + } + + if (!solution.isNominated) { + const res = await nominateProgram(programId, userId); + query = { ...query, isNominated: true }; + } + + if (!solution.isContributorAdded) { + const add_contri = { + program_id: programId, + user_id: "bb551fff-121e-4a18-b969-984ac62bd572", + rolemapping: { + CONTRIBUTOR: ["4e397c42-495e-4fdb-8558-f98176230916"], + }, + }; + + const update_nom = await updateContributorToProgram(add_contri); + query = { ...query, isContributorAdded: true }; + } + + if (!solution.isContributorAccepted) { + const accept_contri = { + program_id: programId, + user_id: "bb551fff-121e-4a18-b969-984ac62bd572", + status: "Approved", + updatedby: userId, + }; + const update_nom = await updateContributorToProgram(accept_contri); + query = { ...query, isContributorAccepted: true }; + } + + if (!isEmpty(query)) { + await updateSolutionById({ + id: solution._id.toString(), + query: { ...query }, + }); + } + + return programId; +}; + +const updateProgramTemplate = async (program_id, solution) => { + const template = { + config: { + defaultContributeOrgReview: false, + roles: [ + { + id: 1, + name: "CONTRIBUTOR", + tabs: [1], + default: true, + defaultTab: 1, + }, + { + id: 2, + name: "REVIEWER", + tabs: [2], + defaultTab: 2, + }, + ], + framework: ["nit_tpd"], + frameworkObj: { + code: "nit_tpd", + name: "nit_tpd", + type: "TPD", + identifier: "nit_tpd", + }, + sharedContext: [], + }, + targetprimarycategories: [ + { + identifier: "obj-cat:observation_questionset_all", + name: "Observation", + targetObjectType: "QuestionSet", + }, + { + identifier: "obj-cat:survey_questionset_all", + name: "Survey", + targetObjectType: "QuestionSet", + }, + ], + targetprimarycategorynames: ["Observation", "Survey"], + program_id: program_id, + }; + + await updateProgram(template); +}; + +const publishProgramTemplate = async (program_id) => { + const template = { + channel: "sunbird", + program_id: program_id, + }; + + const res = await publishProgram(template); +}; + +module.exports = { + createProgramTemplate, + publishProgramTemplate, +}; diff --git a/script/template/generate/gQuestion.js b/script/template/generate/gQuestion.js new file mode 100644 index 00000000..60d82090 --- /dev/null +++ b/script/template/generate/gQuestion.js @@ -0,0 +1,261 @@ +const { questionTemplate, questionStatic } = require("../config/question"); + +const getQBodyParagraph = (questionData) => { + return questionData + .map((data) => { + return `

${data}

`; + }) + .join(""); +}; + +const getQBodyDiv = (questionData) => { + const divs = questionData + .map((data) => { + return `

${data} 

`; + }) + .join(""); + const div = `
${divs}
`; + return div; +}; + +const getDateTemplate = (question) => { + const template = {}; + console.log("getDate"); + + for (let key in questionTemplate.date) { + const keyL = key.toLowerCase(); + if (questionStatic.date.includes(key)) { + template[key] = questionTemplate.date[key]; + } else if (keyL === "interactiontypes") { + template[key] = [question[questionTemplate.date[key]]]; + } else if (keyL === "body") { + template[key] = getQBodyParagraph(question[questionTemplate.date[key]]); + } else if (keyL === "editorstate") { + template[key] = { + question: getQBodyParagraph(question[questionTemplate.date["body"]]), + }; + } else if (keyL === "interactions") { + template[key] = { + validation: question["validation"], + response1: { + validation: { + pattern: question["dateFormat"], + }, + autoCapture: question["autoCapture"], + }, + }; + } else if (keyL === 'showremarks') { + template[key] = question[questionTemplate.date[key]] === true ? 'Yes' : 'No'; + } + else { + template[key] = question[questionTemplate.date[key]] || ""; + } + } + + return template; +}; + +const getSliderTemplate = (question) => { + const template = {}; + console.log("getSlider"); + + for (let key in questionTemplate.slider) { + const keyL = key.toLowerCase(); + if (questionStatic.slider.includes(key)) { + template[key] = questionTemplate.slider[key]; + } else if (keyL === "interactiontypes") { + template[key] = [question[questionTemplate.slider[key]]]; + } else if (keyL === "body") { + template[key] = getQBodyParagraph(question[questionTemplate.slider[key]]); + } else if (keyL === "editorstate") { + template[key] = { + question: getQBodyParagraph(question[questionTemplate.slider["body"]]), + }; + } else if (keyL === "interactions") { + template[key] = { + validation: { + required: question["validation"]["required"], + }, + response1: { + validation: { + range: { + min: question["validation"]["min"], + max: question["validation"]["max"], + }, + }, + step: "1", + }, + }; + } else if (keyL === "evidence") { + template[key] = question["file"] + ? { mimeType: question["file"]["type"] } + : { mimeType: [] }; + }else if (keyL === 'showremarks') { + template[key] = question[questionTemplate.date[key]] === true ? 'Yes' : 'No'; + } else { + template[key] = question[questionTemplate.slider[key]] || ""; + } + } + return template; +}; + +const getOptions = (options) => { + options.map((values, index) => { + values.value = index; + values.label = `${values.label}`; + }); + return options; +}; + +const getEditorOptions = (options) => { + const data = options.map((values, index) => { + return { + answer: false, + value: { + body: `

${values.label}

`, + value: index, + }, + }; + }); + return data; +}; + +const getMSMCQTemplate = (question) => { + const template = {}; + + console.log("getMSMCQ"); + + + for (let key in questionTemplate.multiselect) { + const keyL = key.toLowerCase(); + if (questionStatic.multiselect.includes(key)) { + template[key] = questionTemplate.multiselect[key]; + } else if (keyL === "interactiontypes") { + template[key] = [question[questionTemplate.multiselect[key]]]; + } else if (keyL === "body") { + template[key] = getQBodyDiv(question[questionTemplate.multiselect[key]]); + } else if (keyL === "editorstate") { + template[key] = { + question: getQBodyDiv(question[questionTemplate.multiselect["body"]]), + options: getEditorOptions(question["options"]), + }; + } else if (keyL === "interactions") { + template[key] = { + validation: question["validation"], + response1: { + type: "choice", + options: getOptions(question["options"]), + }, + }; + } else if (keyL === "evidence") { + template[key] = question["file"] + ? { mimeType: question["file"]["type"] } + : { mimeType: [] }; + }else if (keyL === 'showremarks') { + template[key] = question[questionTemplate.date[key]] === true ? 'Yes' : 'No'; + } else { + template[key] = question[questionTemplate.multiselect[key]] || ""; + } + } + + return template; +}; + +const getMCQTemplate = (question) => { + const template = {}; + console.log("getMcq"); + + + for (let key in questionTemplate.mcq) { + const keyL = key.toLowerCase(); + if (questionStatic.mcq.includes(key)) { + template[key] = questionTemplate.mcq[key]; + } else if (keyL === "interactiontypes") { + template[key] = [question[questionTemplate.mcq[key]]]; + } else if (keyL === "body") { + template[key] = getQBodyDiv(question[questionTemplate.mcq[key]]); + } else if (keyL === "editorstate") { + template[key] = { + question: getQBodyDiv(question[questionTemplate.mcq["body"]]), + options: getEditorOptions(question["options"]), + }; + } else if (keyL === "interactions") { + template[key] = { + validation: question["validation"], + response1: { + type: "choice", + options: getOptions(question["options"]), + }, + }; + } else if (keyL === "evidence") { + template[key] = question["file"] + ? { mimeType: question["file"]["type"] } + : { mimeType: [] }; + } else if (keyL === "instructions") { + template[key] = { default: question["tip"] }; + } else if (keyL === 'showremarks') { + template[key] = question[questionTemplate.date[key]] === true ? 'Yes' : 'No'; + }else { + template[key] = question[questionTemplate.mcq[key]] || ""; + } + } + + return template; +}; + +const getTextTemplate = (question, type) => { + const template = {}; + console.log("getText"); + + + for (let key in questionTemplate.text) { + const keyL = key.toLowerCase(); + if (questionStatic.text.includes(key)) { + template[key] = questionTemplate.text[key]; + } else if (keyL === "interactiontypes") { + template[key] = [question[questionTemplate.text[key]]]; + } else if (keyL === "body") { + template[key] = getQBodyParagraph(question[questionTemplate.text[key]]); + } else if (keyL === "editorstate") { + template[key] = { + question: getQBodyParagraph(question[questionTemplate.text["body"]]), + }; + } else if (keyL === "interactions") { + template[key] = { + validation: { + required: question["validation"]["required"], + }, + response1: { + validation: { + limit: { + maxLength: "100", + }, + }, + type: { + number: + type === "text" + ? "No" + : question["validation"]["IsNumber"] + ? "Yes" + : "No", + }, + }, + }; + } else if (keyL === 'showremarks') { + template[key] = question[questionTemplate.date[key]] === true ? 'Yes' : 'No'; + }else { + template[key] = question[questionTemplate.text[key]] || ""; + } + } + + return template; +}; + + +module.exports = { + getDateTemplate, + getSliderTemplate, + getMSMCQTemplate, + getTextTemplate, + getMCQTemplate, +}; diff --git a/script/template/generate/gQuestionSet.js b/script/template/generate/gQuestionSet.js new file mode 100644 index 00000000..f94816bc --- /dev/null +++ b/script/template/generate/gQuestionSet.js @@ -0,0 +1,365 @@ +const { isEmpty, uniq, get } = require("lodash"); +const { ObjectId } = require("mongodb"); +const { createQuestionSet } = require("../../api-list/question"); + +const { CONFIG } = require("../../constant/config"); +const { findAll, updateById } = require("../../db"); +const { + updateHierarchyChildren, + getPrecondition, + updateHierarchyTemplate, +} = require("../helpers/hierarchyHelper"); +const { + isVisibleIfPresent, + isChildrenPresent, + setQuestionSetTemplate, + getQuestionFromDB, + createQuestionTemplate, +} = require("../helpers/questionsetHelper"); +const { createProgramTemplate } = require("./gProgram"); + +const getQuestionSetTemplates = async (solutions) => { + const data = Promise.all( + solutions.map(async (solution) => { + + let programId = solution.sourcingProgramId; + programId = await createProgramTemplate(solution, programId); + console.log(); + console.log("------------------------------------------------------------------"); + console.log(); + console.log("ProgramId", programId); + return getQuestionSetTemplate(solution, programId); + }) + ); + return data; +}; + +const getQuestionSetTemplate = async (solution, programId) => { + let templateData = setQuestionSetTemplate(solution, programId); + + const questionsetid = solution._id.toString(); + + let hierarchy = { + questionsetDbId: questionsetid, + isHierarchyUpdated: solution.isHierarchyUpdated || false, + sourcingProgramId: programId, + criterias: [], + }; + + let questionSetMigratedId = solution.migratedId; + + if (!questionSetMigratedId) { + questionSetMigratedId = await createQuestionSet(templateData); + + await updateById(CONFIG.DB.TABLES.solutions, questionsetid, { + migratedId: questionSetMigratedId, + sourcingProgramId: programId, + }); + hierarchy = { + ...hierarchy, + questionset: questionSetMigratedId, + }; + } else { + hierarchy = { + ...hierarchy, + questionset: questionSetMigratedId, + }; + } + if (solution.themes) { + for (let i = 0; i < solution.themes.length; i++) { + const theme = solution.themes[i]; + let criteriaArrayId = theme.criteria; + for (let j = 0; j < criteriaArrayId.length; j++) { + const criteria = criteriaArrayId[j]; + const criteriaId = criteria.criteriaId.toString(); + const criResult = await findAll(CONFIG.DB.TABLES.criteriaQuestions, { + _id: ObjectId(criteriaId), + }); + + const questions = criResult + ? criResult[0].evidences + ? criResult[0].evidences[0].sections[0].questions + : [] + : []; + + console.log(); + console.log( + "criResultcriResult", + criResult[0].name, + "questions.length", + questions.length + ); + console.log(); + + hierarchy.criterias.push({ + questions: [], + }); + const questionTemplates = await getQuestionTemplate( + questions, + hierarchy, + (index = j), + criResult[0], + solution.type, + criteriaId, + (criteriaMigratedId = criResult[0].migratedId || "") + ); + hierarchy = questionTemplates.hierarchy; + } + } + } + console.log(); + console.log("hierarchyStructure", JSON.stringify(hierarchy)); + console.log(); + await updateHierarchyTemplate(hierarchy, solution, programId); + return hierarchy; +}; + +const getQuestionTemplate = async ( + questions, + hierarchy, + index, + criteria, + type, + criteriaId, + criteriaMigratedId +) => { + hierarchy.criterias[index] = { + migratedId: criteriaMigratedId, + criDbId: criteriaId, + code: criteria.externalId, + name: criteria.name, + description: criteria.description, + mimeType: "application/vnd.sunbird.questionset", + primaryCategory: type, + questions: [], + branchingLogic: {}, + allowMultipleInstances: "", + instances: {}, + }; + for (let i = 0; i < questions.length; i++) { + const question = questions[i]; + const questionId = question._id.toString(); + const templates = await getChildren(questionId, hierarchy, index); + } + + hierarchy.criterias[index].questions = uniq( + hierarchy.criterias[index].questions + ); + + return { questions, hierarchy }; +}; + +const getChildren = async (questionId, hierarchy, index) => { + const question = await getQuestionFromDB(questionId); + const migratedId = await createQuestionTemplate(question); + const parentId = migratedId; + hierarchy = updateHierarchyChildren(hierarchy, migratedId, index); + + if (question.responseType !== "matrix") { + if (!isChildrenPresent(question) && !isVisibleIfPresent(question)) { + hierarchy = updateHierarchyChildren(hierarchy, parentId, index); + return hierarchy; + } + + if (isChildrenPresent(question) && !isVisibleIfPresent(question)) { + const data = await IfChildrenAndNoVisibleIf( + question, + hierarchy, + index, + parentId + ); + } + + if (!isChildrenPresent(question) && isVisibleIfPresent(question)) { + return await IfNoChildrenAndVisibleIfAndInnerChildren( + question, + hierarchy, + index + ); + } + } else if (question.responseType === "matrix") { + const instanceQuestions = question?.instanceQuestions; + if (hierarchy.criterias[index]) { + hierarchy.criterias[index] = { + ...hierarchy.criterias[index], + allowMultipleInstances: "Yes", + instances: { label: question.instanceIdentifier }, + }; + } + + const visible = !isEmpty(get(question, "visibleIf")) + ? question?.visibleIf[0] + : {}; + + let matrixParentId = ""; + let matrixParentQuestion = {}; + let matrixParentIdMigratedId = ""; + if (!isEmpty(visible)) { + matrixParentId = visible._id; + matrixParentQuestion = await getQuestionFromDB(matrixParentId); + matrixParentIdMigratedId = matrixParentQuestion?.migratedId || ""; + if (!matrixParentIdMigratedId) { + matrixParentIdMigratedId = await createQuestionTemplate(question); + } + hierarchy = updateHierarchyChildren( + hierarchy, + matrixParentIdMigratedId, + index + ); + } + + for (let i = 0; i < instanceQuestions.length; i++) { + const instanceQuestionId = instanceQuestions[i]; + const insQuestion = await getQuestionFromDB(instanceQuestionId); + const insMigratedId = await createQuestionTemplate(insQuestion); + + hierarchy = updateHierarchyChildren(hierarchy, insMigratedId, index); + + if (!isEmpty(visible)) { + if ( + hierarchy.criterias[index].branchingLogic.hasOwnProperty( + matrixParentIdMigratedId + ) + ) { + hierarchy.criterias[index].branchingLogic[ + matrixParentIdMigratedId + ].target.push(insMigratedId); + + if (insMigratedId) { + hierarchy.criterias[index].branchingLogic[insMigratedId] = { + target: [], + preCondition: getPrecondition( + visible, + matrixParentIdMigratedId, + matrixParentQuestion + ), + source: [matrixParentIdMigratedId], + }; + } + } else { + { + hierarchy.criterias[index].branchingLogic[ + matrixParentIdMigratedId + ] = { + target: [insMigratedId], + preCondition: {}, + source: [], + }; + + if (insMigratedId) { + hierarchy.criterias[index].branchingLogic[insMigratedId] = { + target: [], + preCondition: getPrecondition( + visible, + matrixParentIdMigratedId, + matrixParentQuestion + ), + source: [matrixParentIdMigratedId], + }; + } + } + } + } + } + } +}; + +const IfChildrenAndNoVisibleIf = async ( + question, + hierarchy, + index, + parentId +) => { + let branching = { + [parentId]: { + target: [], + preCondition: {}, + source: [], + }, + }; + + if (question.children && question.children.length && !question.visibleIf) { + const children = question.children; + for (let i = 0; i < children.length; i++) { + const childId = children[i].toString(); + const childQuestion = await getQuestionFromDB(childId); + const migratedId = await createQuestionTemplate(childQuestion); + // hierarchy.criterias[index].questions.push(migratedId); + hierarchy = updateHierarchyChildren(hierarchy, migratedId, index); + + if (migratedId) { + branching[parentId].target.push(migratedId); + } + + if ( + !isChildrenPresent(childQuestion) && + isVisibleIfPresent(childQuestion) + ) { + const childBranching = await IfNoChildrenAndVisibleIf( + question, + parentId, + childQuestion + ); + + branching = { + ...branching, + ...childBranching, + }; + } + } + } + + hierarchy.criterias[index].branchingLogic = branching; + return hierarchy; +}; + +const IfNoChildrenAndVisibleIf = async (parentQuestion, parentId, question) => { + const visibleIf = question.visibleIf || []; + let childBranching = {}; + for (let i = 0; i < visibleIf.length; i++) { + const visible = visibleIf[i]; + const childId = question._id.toString(); + const chiQuestion = await getQuestionFromDB(childId); + const migratedId = await createQuestionTemplate(chiQuestion); + if (migratedId) { + childBranching = { + [migratedId]: { + target: [], + preCondition: getPrecondition(visible, parentId, parentQuestion), + source: [parentId], + }, + }; + } + } + return childBranching; +}; + +const IfNoChildrenAndVisibleIfAndInnerChildren = async ( + question, + hierarchy, + index +) => { + const visibleIf = question.visibleIf || []; + for (let i = 0; i < visibleIf.length; i++) { + const visible = visibleIf[i]; + const parentId = visible._id.toString(); + const parentQuestion = await getQuestionFromDB(parentId); + const migratedId = await createQuestionTemplate(parentQuestion); + hierarchy.criterias[index].questions.push(migratedId); + if ( + isChildrenPresent(parentQuestion) && + isVisibleIfPresent(parentQuestion) + ) { + return await IfChildrenAndNoVisibleIf( + parentQuestion, + hierarchy, + index, + migratedId + ); + } + } +}; + +module.exports = { + getQuestionSetTemplates, +}; diff --git a/script/template/helpers/hierarchyHelper.js b/script/template/helpers/hierarchyHelper.js new file mode 100644 index 00000000..b4c3a553 --- /dev/null +++ b/script/template/helpers/hierarchyHelper.js @@ -0,0 +1,263 @@ +const { + pick, + findIndex, + get, + compact, +} = require("lodash"); +const { publishQuestionSet, updateQuestionSetHierarchy } = require("../../api-list/question"); +const { CONFIG } = require("../../constant/config"); +const { updateById } = require("../../db"); + +const updateHierarchyChildren = (hierarchy, migratedId, index) => { + if (!hierarchy.criterias[index].questions.includes(migratedId)) { + hierarchy.criterias[index].questions.push(migratedId); + } + return hierarchy; +}; + + +const getOperator = (visibleIf) => { + const operator = + visibleIf.operator === "===" + ? "eq" + : visibleIf.operator === "!==" || visibleIf.operator === "!=" + ? "ne" + : ""; + + return operator; +}; + +const getPrecondition = (visible, parentId, parentQuestion) => { + return { + and: [ + { + [getOperator(visible)]: [ + { + var: `${parentId}.response1.value`, + type: "responseDeclaration", + }, + findIndex(parentQuestion.options, { + value: visible.value, + }), + ], + }, + ], + } +} + + +const updateHierarchyTemplate = async (hierarchy, solution, programId) => { + const updateHierarchyData = { + request: { + data: { + nodesModified: {}, + hierarchy: { + [hierarchy.questionset]: { + children: [], + root: true, + }, + }, + }, + }, + }; + + for (let i = 0; i < hierarchy.criterias.length; i++) { + const criteria = hierarchy.criterias[i]; + const metadata = pick(criteria, [ + "code", + "name", + "description", + "mimeType", + "primaryCategory", + "allowMultipleInstances", + "instances", + ]); + updateHierarchyData.request.data.nodesModified[criteria.name] = { + metadata: { + ...metadata, + }, + objectType: "QuestionSet", + root: false, + isNew: true, + }; + updateHierarchyData.request.data.hierarchy[ + hierarchy.questionset + ].children.push(criteria.name); + + updateHierarchyData.request.data.hierarchy[criteria.name] = { + children: compact(criteria.questions), + root: false, + }; + } + + console.log("updateHierarchydata", JSON.stringify(updateHierarchyData)); + console.log(); + const questionsetId = hierarchy.questionsetDbId; + + if (!hierarchy.isHierarchyUpdated) { + const result = await updateQuestionSetHierarchy(updateHierarchyData); + + await updateById(CONFIG.DB.TABLES.solutions, questionsetId, { + isHierarchyUpdated: true, + isBranchingUpdated: false, + sourcingProgramId: programId, + }); + for (let i = 0; i < hierarchy.criterias.length; i++) { + const criterias = hierarchy.criterias[i]; + hierarchy.criterias[i].migratedId = result[criterias.name]; + await updateById(CONFIG.DB.TABLES.criteriaQuestions, criterias.criDbId, { + migratedId: result[criterias.name], + }); + } + } + + if (!hierarchy.isBranchingUpdated) { + const branchinghierarchy = branchingQuestionSetHierarchy(hierarchy); + console.log("branchinghierarchy", JSON.stringify(branchinghierarchy)); + + const result = await updateQuestionSetHierarchy(branchinghierarchy); + await updateById(CONFIG.DB.TABLES.solutions, questionsetId, { + isBranchingUpdated: true, + sourcingProgramId: programId, + }); + } + await publishQuestionSet(hierarchy.questionset); +}; + +const branchingQuestionSetHierarchy = (hierarchy) => { + const updateHierarchyData = { + request: { + data: { + nodesModified: {}, + hierarchy: { + [hierarchy.questionset]: { + children: [], + root: true, + }, + }, + }, + }, + }; + for (let i = 0; i < hierarchy.criterias.length; i++) { + const criteria = hierarchy.criterias[i]; + const metadata = pick(criteria, [ + "code", + "name", + "description", + "mimeType", + "primaryCategory", + "allowMultipleInstances", + "instances", + ]); + updateHierarchyData.request.data.nodesModified[criteria.migratedId] = { + metadata: { + ...metadata, + allowBranching: "Yes", + branchingLogic: get(criteria, "branchingLogic") || {}, + }, + objectType: "QuestionSet", + root: false, + isNew: false, + }; + updateHierarchyData.request.data.hierarchy[ + hierarchy.questionset + ].children.push(criteria.migratedId); + + updateHierarchyData.request.data.hierarchy[criteria.migratedId] = { + children: compact(criteria.questions), + root: false, + }; + } + return updateHierarchyData; +}; + +module.exports = { + updateHierarchyChildren, + updateHierarchyTemplate, + branchingQuestionSetHierarchy, + getOperator, + getPrecondition +}; + + + +// const updateBranchingObject = (data, question, childId) => { +// const { parentId } = data; +// const matrixId = get(data, "matrix") ? data?.matrix?._id.toString() : ""; + +// if ( +// !data.hierarchy.criterias[data.index].branchingLogic[ +// parentId +// ].target.includes(question?.migratedId) +// ) { +// data.hierarchy.criterias[data.index].branchingLogic[parentId].target.push( +// question?.migratedId +// ); +// } + +// const ids = data.hierarchy.criterias[data.index].branchingLogic[ +// parentId +// ].target.filter((ele) => ele !== childId && ele !== matrixId); + +// data.hierarchy.criterias[data.index].branchingLogic[parentId].target = +// compact(ids); + +// delete data.hierarchy.criterias[data.index].branchingLogic[childId]; +// delete data.hierarchy.criterias[data.index].branchingLogic[matrixId]; + +// const visible = question?.visibleIf ? question?.visibleIf[0] : {}; +// if (question?.migratedId) { +// data.hierarchy.criterias[data.index].branchingLogic[question?.migratedId] = +// { +// target: [], +// preCondition: { +// and: [ +// { +// [getOperator(visible)]: [ +// { +// var: `${parentId}.response1.value`, +// type: "responseDeclaration", +// }, +// `${findIndex(data?.parent?.options, { +// value: visible.value, +// })}`, +// ], +// }, +// ], +// }, +// source: [parentId], +// }; +// } + +// return data; +// }; + +// const updateChildBranching = (data, question, children) => { +// const { parentId } = data; + +// for (let i = 0; i < children.length; i++) { +// const childId = children[i].toString(); + +// if (childId !== question._id.toString()) { +// if ( +// !data.hierarchy.criterias[data.index].branchingLogic[ +// parentId +// ].target.includes(childId) +// ) { +// data.hierarchy.criterias[data.index].branchingLogic[ +// parentId +// ].target.push(childId); +// } + +// data.hierarchy.criterias[data.index].branchingLogic[childId] = { +// target: [], +// preCondition: {}, +// source: [], +// }; +// } else { +// data = { ...updateBranchingObject(data, question) }; +// } +// } + +// return data; +// }; diff --git a/script/template/helpers/questionsetHelper.js b/script/template/helpers/questionsetHelper.js new file mode 100644 index 00000000..2c9c99e1 --- /dev/null +++ b/script/template/helpers/questionsetHelper.js @@ -0,0 +1,116 @@ +const { capitalize, isEmpty, get } = require("lodash"); +const { ObjectId } = require("mongodb"); +const { createQuestions, publishQuestion } = require("../../api-list/question"); +const { CONFIG } = require("../../constant/config"); +const { updateById, findAll } = require("../../db"); +const { + questionSetTemplate, + questionSetTemplateStatic, +} = require("../config/questionSet"); +const { getDateTemplate, getSliderTemplate, getMSMCQTemplate, getMCQTemplate, getTextTemplate } = require("../generate/gQuestion"); + +const setQuestionSetTemplate = (solution, programId) => { + let templateData = {}; + for (let key in questionSetTemplate) { + if (key === "programId") { + templateData[key] = programId; + } else if (questionSetTemplateStatic.includes(key)) { + templateData[key] = questionSetTemplate[key]; + } else if (key === "organisationId") { + if ( + solution[questionSetTemplate[key]] && + solution[questionSetTemplate[key]].length >= 1 + ) { + templateData[key] = solution[questionSetTemplate[key]][0]; + } else { + templateData[key] = solution[questionSetTemplate[key]]; + } + } else if (key.toLowerCase() === "entitytype") { + templateData[key] = capitalize(solution[questionSetTemplate[key]]) || ""; + } else { + templateData[key] = solution[questionSetTemplate[key]] || ""; + } + } + return templateData; +}; + +const createQuestionTemplate = async (question) => { + const type = question.responseType; + let migratedId = question.migratedId; + let query = {}; + let questionToMigrate = {}; + + if (!migratedId) { + if (type.toLowerCase() === "date") { + questionToMigrate = getDateTemplate(question); + } + if (type.toLowerCase() === "slider") { + questionToMigrate = getSliderTemplate(question); + } + if (type.toLowerCase() === "multiselect") { + questionToMigrate = getMSMCQTemplate(question); + } + if (type.toLowerCase() === "radio") { + questionToMigrate = getMCQTemplate(question); + } + if (type.toLowerCase() === "text" || type.toLowerCase() === "number") { + questionToMigrate = getTextTemplate(question, type); + } + + if (!isEmpty(questionToMigrate)) { + migratedId = await createQuestions(questionToMigrate); + query = { + ...query, + migratedId, + }; + } + } + + if (!question.isPublished && migratedId) { + await publishQuestion(migratedId); + query = { + ...query, + isPublished: true, + }; + } + + if (!isEmpty(query)) { + await updateById(CONFIG.DB.TABLES.questions, question._id, { + ...query, + }); + } + return migratedId; +}; + +const updateSolutionById = async ({ id, query }) => { + return await updateById(CONFIG.DB.TABLES.solutions, id, query); +}; + +const getQuestionFromDB = async (questionId) => { + const readQuestion = await findAll(CONFIG.DB.TABLES.questions, { + _id: ObjectId(questionId), + }); + return readQuestion[0]; +}; + +const isVisibleIfPresent = (question) => { + return !isEmpty(get(question, 'visibleIf')); +}; + +const isChildrenPresent = (question) => { + return !isEmpty(get(question, 'children')) +}; + +const isInstanceQuestionsPresent = (question) => { + return !isEmpty(get(question, 'instanceQuestions')) +}; + +module.exports = { + setQuestionSetTemplate, + createQuestionTemplate, + updateSolutionById, + getQuestionFromDB, + isVisibleIfPresent, + isChildrenPresent, + isInstanceQuestionsPresent, +}; From 102a9a61aff9efd5ea7e2cc84ece79e877df7b2c Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Mon, 4 Jul 2022 15:17:46 +0530 Subject: [PATCH 02/36] updated readme --- script/README.md | Bin 1326 -> 1110 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/script/README.md b/script/README.md index 31a17fe8e95d724870402d75fc6d40a062dc6fdc..93b13e9c16e2af9b2afb271ac8594ed5eebe205c 100644 GIT binary patch delta 48 zcmZ3-b&X?#4WqO#gFk~GgFBFPVQ^xIX9#5oV(?*5V6bISn0%Dcbg~hX(B?9x9ZUcN C*b2Y^ delta 283 zcmcb{v5sql4P(6rgFizsLkNQcgDnuNGZ-=$F_<&xF&IFxAy7<#A%Q_32vZnR8PXUM z8A=&)7)pR*ybN3n0YLphaQ!AgeITfSSC0#W6GJ=$*k&%cR`to@jHdM<%ZSnJ$Kc4| o%is!B3vqTX&`G)s#XzSOF_ZzFSqXMFCj&N!$2*%BGHzu80KsY~IsgCw From 774f0cadecd3b63005a209a9f0ff860204991705 Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Mon, 4 Jul 2022 15:18:44 +0530 Subject: [PATCH 03/36] updated readme --- script/README.md | Bin 1110 -> 1134 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/script/README.md b/script/README.md index 93b13e9c16e2af9b2afb271ac8594ed5eebe205c..da15b022cc538c2b54a713d9c9f8fad3d40964fe 100644 GIT binary patch delta 36 pcmcb{@s4A|JVtQ^1`teONMT52NMlH3C}qfDC}Bw0JeP4N69BJi2}}S0 delta 12 UcmaFIagAfcJjTu27 Date: Tue, 5 Jul 2022 15:22:28 +0530 Subject: [PATCH 04/36] removed package.json and updated default env --- script/api-list/user.js | 1 + script/constant/config.js | 24 +++++++++++------------- script/package.json | 23 ----------------------- script/server.js | 36 ++++++++---------------------------- 4 files changed, 20 insertions(+), 64 deletions(-) delete mode 100644 script/package.json diff --git a/script/api-list/user.js b/script/api-list/user.js index 41aaa173..575e4b16 100644 --- a/script/api-list/user.js +++ b/script/api-list/user.js @@ -19,6 +19,7 @@ const readUser = async (userId) => { }; const searchUser = async (userId) => { + console.log("mongooooo", CONFIG) const url = CONFIG.SUNBIRD.HOST.dev + CONFIG.SUNBIRD.APIS.search_user; const config = { method: "post", diff --git a/script/constant/config.js b/script/constant/config.js index 9567f2da..c15dc528 100644 --- a/script/constant/config.js +++ b/script/constant/config.js @@ -1,8 +1,6 @@ -require("dotenv").config(); +require("dotenv").config({path: "./../../.env"}); const CONFIG = { - HOST: process.env.HOST, - PORT: process.env.PORT, DB: { DB_HOST: process.env.MONGODB_URL, DB_NAME: process.env.DB_NAME, @@ -35,21 +33,21 @@ const CONFIG = { config: { dev: { query: { - username: process.env.dev_username, - password: process.env.dev_password, - grant_type: process.env.dev_grant_type, - client_id: process.env.dev_client_id, - client_secret: process.env.dev_client_secret, + username: process.env.username, + password: process.env.password, + grant_type: process.env.grant_type, + client_id: process.env.client_id, + client_secret: process.env.client_secret, }, authorization: process.env.DEV_AUTHORIZATION, }, dock: { query: { - username: process.env.dev_username, - password: process.env.dev_password, - grant_type: process.env.dev_grant_type, - client_id: process.env.dev_client_id, - client_secret: process.env.dev_client_secret, + username: process.env.username, + password: process.env.password, + grant_type: process.env.grant_type, + client_id: process.env.client_id, + client_secret: process.env.client_secret, }, nominate_user: process.env.nominate_user, authorization: process.env.DOCK_AUTHORIZATION, diff --git a/script/package.json b/script/package.json deleted file mode 100644 index 1517088c..00000000 --- a/script/package.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "name": "ml-survey", - "version": "1.0.0", - "description": "ml-survey-datas", - "main": "server.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1", - "start": "nodemon server.js" - }, - "author": "KenPath Technologies", - "license": "ISC", - "dependencies": { - "axios": "^0.26.1", - "dotenv": "^16.0.0", - "express": "^4.17.3", - "jsonwebtoken": "^8.5.1", - "lodash": "^4.17.21", - "mongodb": "^4.4.1", - "mongoose": "^6.2.8", - "nodemon": "^2.0.15", - "querystring": "^0.2.1" - } -} diff --git a/script/server.js b/script/server.js index e81dcb5e..23167499 100644 --- a/script/server.js +++ b/script/server.js @@ -1,31 +1,28 @@ +require("dotenv").config({path: "./../.env"}); const { CONFIG } = require("./constant/config"); const { createDBInstance } = require("./db/dbConfig"); const { findAll } = require("./db"); - -const express = require("express"); const { getQuestionSetTemplates, } = require("./template/generate/gQuestionSet.js"); const { ObjectId } = require("mongodb"); -const app = express(); -const PORT = 4000; -const migrateData = async (req, res) => { + +const migrateData = async () => { try { + console.log("Environment: " + process.env.APPLICATION_ENV ); + const db = await createDBInstance(); - // req.query.questionsetID || - // const id = "5f35044f19377eecddb06922"; + const id = "5f35044f19377eecddb06922"; const data = await findAll("solutions", { - // _id: ObjectId(id), - // $or: [{migratedId: {$eq: null}}, {migratedId:{$exists: false}}], + _id: ObjectId(id), programId: { $exists: true }, type: {$in: ["observation", "survey"]} }); const template = await getQuestionSetTemplates(data); - // return template; process.exit(); } catch (err) { console.log("Error while reading questionSet", err); @@ -34,21 +31,4 @@ const migrateData = async (req, res) => { } }; -migrateData(); - - - -// app.post("/questionset", async (req, res) => { -// try { -// const data = await migrateData(req); - -// res.status(200).json(data); -// } catch (err) { -// console.log("Error", err); -// res.send(err); -// } -// }); - -// app.listen(PORT, () => { -// console.log("app is running at port", PORT); -// }); +migrateData(); \ No newline at end of file From 6671a2debe4baff3fda2d6643ca9c2616ccf89b6 Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Tue, 5 Jul 2022 15:23:46 +0530 Subject: [PATCH 05/36] removed package.json and updated default env --- .env.sample | 17 ++++++++++++++++- package.json | 5 ++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/.env.sample b/.env.sample index 417f406a..f9c36de5 100644 --- a/.env.sample +++ b/.env.sample @@ -26,4 +26,19 @@ ML_PROJECT_SERVICE_URL = "http://ml-project-service:3000" KEYCLOAK_PUBLIC_KEY_PATH = "keycloak-public-keys" // Keycloak public keys path -DISABLE_LEARNER_SERVICE_ON_OFF = "ON" // Disable learner service check \ No newline at end of file +DISABLE_LEARNER_SERVICE_ON_OFF = "ON" // Disable learner service check + + + +# ML Script + +username = string +password = string +grant_type = string +client_id = string +client_secret= string + +DEV_HOST = string +DOCK_HOST = string +DEV_AUTHORIZATION = 'Bearer token' +DOCK_AUTHORIZATION= 'Bearer token' \ No newline at end of file diff --git a/package.json b/package.json index 29170d0a..a26bf3df 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,10 @@ "request": "^2.88.0", "require-all": "^2.2.0", "uuid": "^3.3.2", - "yaml-js": "^0.1.3" + "yaml-js": "^0.1.3", + "axios": "^0.26.1", + "lodash": "^4.17.21", + "querystring": "^0.2.1" }, "devDependencies": {} } From 1b68f9ff06a4e16beb5086f7058b1c4d7b3b2703 Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Wed, 6 Jul 2022 18:20:54 +0530 Subject: [PATCH 06/36] removed default solutionId --- script/api-list/program.js | 9 +- script/api-list/question.js | 44 +++++-- script/api-list/user.js | 1 - script/constant/config.js | 1 + script/server.js | 4 +- script/template/generate/gQuestionSet.js | 37 +++++- script/template/helpers/hierarchyHelper.js | 141 ++++++--------------- 7 files changed, 111 insertions(+), 126 deletions(-) diff --git a/script/api-list/program.js b/script/api-list/program.js index f5ddb499..6480385d 100644 --- a/script/api-list/program.js +++ b/script/api-list/program.js @@ -82,10 +82,8 @@ const createProgram = async (templateData) => { data: data, }; - const res = await axios(config).catch((err) => { - console.log("Error while creating the Program", err.response.data); - }); - return res.data.result.program_id; + const res = await axios(config) + return res?.data?.result?.program_id; }; const updateProgram = async (templateData) => { @@ -121,6 +119,9 @@ const publishProgram = async (templateData) => { headers: await getHeaders(false, "dock"), data: data, }; + + console.log("publish program config", data) + const res = await axios(config).catch((err) => { console.log("Error while publishProgram", err.response.data); }); diff --git a/script/api-list/question.js b/script/api-list/question.js index 413f5009..3e9693c1 100644 --- a/script/api-list/question.js +++ b/script/api-list/question.js @@ -18,10 +18,9 @@ const createQuestionSet = async (templateData) => { headers: await getHeaders(true, "dock"), data: data, }; - const res = await axios(config).catch((err) => { - console.log("Error while creating Questionset", err.response.data); - }); - return res.data.result.identifier; + const res = await axios(config); + + return res?.data?.result?.identifier; }; const updateQuestionSetHierarchy = async (templateData) => { @@ -34,10 +33,8 @@ const updateQuestionSetHierarchy = async (templateData) => { data: templateData, }; - const res = await axios(config).catch((err) => { - console.log("Error while updating the questionset", err.response.data); - }); - return res.data.result.identifiers; + const res = await axios(config); + return res?.data?.result?.identifiers; }; const publishQuestionSet = async (questionsetId) => { @@ -58,6 +55,26 @@ const publishQuestionSet = async (questionsetId) => { }); }; +const readQuestionSetHierarchy = async (questionSetId) => { + const url = + CONFIG.SUNBIRD.HOST.dock + + CONFIG.SUNBIRD.APIS.read_questionset + + questionSetId + + "?mode=edit"; + + const config = { + method: "get", + url: url, + headers: await getHeaders(true, "dock"), + // data: templateData, + }; + + const res = await axios(config); + // console.log("ressss", res?.data?.result?.questionSet); + return res?.data?.result?.questionSet; + +}; + // Questions const createQuestions = async (templateData) => { const url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.create_question; @@ -102,9 +119,10 @@ const publishQuestion = async (questionId) => { }; module.exports = { - createQuestionSet, - updateQuestionSetHierarchy, - publishQuestionSet, - createQuestions, - publishQuestion + createQuestionSet, + updateQuestionSetHierarchy, + publishQuestionSet, + createQuestions, + publishQuestion, + readQuestionSetHierarchy, }; diff --git a/script/api-list/user.js b/script/api-list/user.js index 575e4b16..41aaa173 100644 --- a/script/api-list/user.js +++ b/script/api-list/user.js @@ -19,7 +19,6 @@ const readUser = async (userId) => { }; const searchUser = async (userId) => { - console.log("mongooooo", CONFIG) const url = CONFIG.SUNBIRD.HOST.dev + CONFIG.SUNBIRD.APIS.search_user; const config = { method: "post", diff --git a/script/constant/config.js b/script/constant/config.js index c15dc528..0d897954 100644 --- a/script/constant/config.js +++ b/script/constant/config.js @@ -22,6 +22,7 @@ const CONFIG = { create_questionset: "api/questionset/v1/create", update_hierarchy: "api/questionset/v1/hierarchy/update", publish_questionset: "api/questionset/v1/publish", + read_questionset: "api/questionset/v1/hierarchy/", create_question: "api/question/v1/create", publish_question: "api/question/v1/publish", create_program: "api/program/v1/create", diff --git a/script/server.js b/script/server.js index 23167499..5a123340 100644 --- a/script/server.js +++ b/script/server.js @@ -16,9 +16,9 @@ const migrateData = async () => { console.log("Environment: " + process.env.APPLICATION_ENV ); const db = await createDBInstance(); - const id = "5f35044f19377eecddb06922"; + // const id = "5f34ec17585244939f89f90d"; const data = await findAll("solutions", { - _id: ObjectId(id), + // _id: ObjectId(id), programId: { $exists: true }, type: {$in: ["observation", "survey"]} }); diff --git a/script/template/generate/gQuestionSet.js b/script/template/generate/gQuestionSet.js index f94816bc..d55b7235 100644 --- a/script/template/generate/gQuestionSet.js +++ b/script/template/generate/gQuestionSet.js @@ -21,16 +21,34 @@ const { createProgramTemplate } = require("./gProgram"); const getQuestionSetTemplates = async (solutions) => { const data = Promise.all( solutions.map(async (solution) => { - + console.log(); + console.log( + "---------------------------solution---------------------------------------" + ); + console.log(); let programId = solution.sourcingProgramId; - programId = await createProgramTemplate(solution, programId); + programId = await createProgramTemplate(solution, programId).catch( + (err) => { + console.log("Error while creating the Program", err.response.data); + } + ); console.log(); - console.log("------------------------------------------------------------------"); + console.log( + "-----------------------------program-------------------------------------" + ); console.log(); console.log("ProgramId", programId); + + if (!programId) { + console.log("programId empty", programId) + return; + } + console.log("programId Present", programId) + console.log(); return getQuestionSetTemplate(solution, programId); }) ); + return data; }; @@ -49,8 +67,14 @@ const getQuestionSetTemplate = async (solution, programId) => { let questionSetMigratedId = solution.migratedId; if (!questionSetMigratedId) { - questionSetMigratedId = await createQuestionSet(templateData); - + questionSetMigratedId = await createQuestionSet(templateData).catch( + (err) => { + console.log("Error while creating Questionset", err.response.data); + } + ); + if (!questionSetMigratedId) { + return; + } await updateById(CONFIG.DB.TABLES.solutions, questionsetid, { migratedId: questionSetMigratedId, sourcingProgramId: programId, @@ -65,6 +89,7 @@ const getQuestionSetTemplate = async (solution, programId) => { questionset: questionSetMigratedId, }; } + if (solution.themes) { for (let i = 0; i < solution.themes.length; i++) { const theme = solution.themes[i]; @@ -348,7 +373,7 @@ const IfNoChildrenAndVisibleIfAndInnerChildren = async ( hierarchy.criterias[index].questions.push(migratedId); if ( isChildrenPresent(parentQuestion) && - isVisibleIfPresent(parentQuestion) + !isVisibleIfPresent(parentQuestion) ) { return await IfChildrenAndNoVisibleIf( parentQuestion, diff --git a/script/template/helpers/hierarchyHelper.js b/script/template/helpers/hierarchyHelper.js index b4c3a553..3904ea6e 100644 --- a/script/template/helpers/hierarchyHelper.js +++ b/script/template/helpers/hierarchyHelper.js @@ -1,15 +1,17 @@ +const { pick, findIndex, get, compact, find, omit } = require("lodash"); const { - pick, - findIndex, - get, - compact, -} = require("lodash"); -const { publishQuestionSet, updateQuestionSetHierarchy } = require("../../api-list/question"); + publishQuestionSet, + updateQuestionSetHierarchy, + readQuestionSetHierarchy, +} = require("../../api-list/question"); const { CONFIG } = require("../../constant/config"); const { updateById } = require("../../db"); const updateHierarchyChildren = (hierarchy, migratedId, index) => { - if (!hierarchy.criterias[index].questions.includes(migratedId)) { + if ( + migratedId && + !hierarchy.criterias[index].questions.includes(migratedId) + ) { hierarchy.criterias[index].questions.push(migratedId); } return hierarchy; @@ -105,26 +107,39 @@ const updateHierarchyTemplate = async (hierarchy, solution, programId) => { for (let i = 0; i < hierarchy.criterias.length; i++) { const criterias = hierarchy.criterias[i]; hierarchy.criterias[i].migratedId = result[criterias.name]; - await updateById(CONFIG.DB.TABLES.criteriaQuestions, criterias.criDbId, { - migratedId: result[criterias.name], - }); } } if (!hierarchy.isBranchingUpdated) { - const branchinghierarchy = branchingQuestionSetHierarchy(hierarchy); + const branchinghierarchy = await branchingQuestionSetHierarchy(hierarchy); console.log("branchinghierarchy", JSON.stringify(branchinghierarchy)); - const result = await updateQuestionSetHierarchy(branchinghierarchy); - await updateById(CONFIG.DB.TABLES.solutions, questionsetId, { - isBranchingUpdated: true, - sourcingProgramId: programId, - }); + const result = await updateQuestionSetHierarchy(branchinghierarchy).catch( + (err) => { + console.log( + "Error while updating the questionset branching", + err.response.data + ); + } + ); + if (result) { + await updateById(CONFIG.DB.TABLES.solutions, questionsetId, { + isBranchingUpdated: true, + sourcingProgramId: programId, + }); + await publishQuestionSet(hierarchy.questionset); + } } - await publishQuestionSet(hierarchy.questionset); }; -const branchingQuestionSetHierarchy = (hierarchy) => { +const branchingQuestionSetHierarchy = async (hierarchy) => { + let questionSetHierarchy = {}; + if (hierarchy.questionset && hierarchy.isHierarchyUpdated) { + questionSetHierarchy = await readQuestionSetHierarchy( + hierarchy.questionset + ); + } + const updateHierarchyData = { request: { data: { @@ -140,6 +155,14 @@ const branchingQuestionSetHierarchy = (hierarchy) => { }; for (let i = 0; i < hierarchy.criterias.length; i++) { const criteria = hierarchy.criterias[i]; + const hierarchyData = find(questionSetHierarchy.children, { + name: criteria?.name, + }); + criteria.migratedId = + hierarchy.questionset && hierarchy.isHierarchyUpdated + ? hierarchyData?.identifier + : criteria.migratedId; + const metadata = pick(criteria, [ "code", "name", @@ -179,85 +202,3 @@ module.exports = { getPrecondition }; - - -// const updateBranchingObject = (data, question, childId) => { -// const { parentId } = data; -// const matrixId = get(data, "matrix") ? data?.matrix?._id.toString() : ""; - -// if ( -// !data.hierarchy.criterias[data.index].branchingLogic[ -// parentId -// ].target.includes(question?.migratedId) -// ) { -// data.hierarchy.criterias[data.index].branchingLogic[parentId].target.push( -// question?.migratedId -// ); -// } - -// const ids = data.hierarchy.criterias[data.index].branchingLogic[ -// parentId -// ].target.filter((ele) => ele !== childId && ele !== matrixId); - -// data.hierarchy.criterias[data.index].branchingLogic[parentId].target = -// compact(ids); - -// delete data.hierarchy.criterias[data.index].branchingLogic[childId]; -// delete data.hierarchy.criterias[data.index].branchingLogic[matrixId]; - -// const visible = question?.visibleIf ? question?.visibleIf[0] : {}; -// if (question?.migratedId) { -// data.hierarchy.criterias[data.index].branchingLogic[question?.migratedId] = -// { -// target: [], -// preCondition: { -// and: [ -// { -// [getOperator(visible)]: [ -// { -// var: `${parentId}.response1.value`, -// type: "responseDeclaration", -// }, -// `${findIndex(data?.parent?.options, { -// value: visible.value, -// })}`, -// ], -// }, -// ], -// }, -// source: [parentId], -// }; -// } - -// return data; -// }; - -// const updateChildBranching = (data, question, children) => { -// const { parentId } = data; - -// for (let i = 0; i < children.length; i++) { -// const childId = children[i].toString(); - -// if (childId !== question._id.toString()) { -// if ( -// !data.hierarchy.criterias[data.index].branchingLogic[ -// parentId -// ].target.includes(childId) -// ) { -// data.hierarchy.criterias[data.index].branchingLogic[ -// parentId -// ].target.push(childId); -// } - -// data.hierarchy.criterias[data.index].branchingLogic[childId] = { -// target: [], -// preCondition: {}, -// source: [], -// }; -// } else { -// data = { ...updateBranchingObject(data, question) }; -// } -// } - -// return data; -// }; From 471e61527268104bde7d2251ec239b64d0985815 Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Thu, 7 Jul 2022 13:26:48 +0530 Subject: [PATCH 07/36] added rolemapping --- script/template/generate/gProgram.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/script/template/generate/gProgram.js b/script/template/generate/gProgram.js index e377a7ab..26936f30 100644 --- a/script/template/generate/gProgram.js +++ b/script/template/generate/gProgram.js @@ -47,6 +47,7 @@ const createProgramTemplate = async (solution, program_id) => { slug: "sunbird", status: "Draft", program_id: "", + rolemapping: [], config: { defaultContributeOrgReview: false, roles: [ @@ -77,7 +78,7 @@ const createProgramTemplate = async (solution, program_id) => { } if (!solution.isSrcProgramUpdated) { - await updateProgramTemplate(programId, solution); + const update_res = await updateProgramTemplate(programId, solution); query = { ...query, isSrcProgramUpdated: true, @@ -175,7 +176,7 @@ const updateProgramTemplate = async (program_id, solution) => { program_id: program_id, }; - await updateProgram(template); + return await updateProgram(template); }; const publishProgramTemplate = async (program_id) => { @@ -184,7 +185,7 @@ const publishProgramTemplate = async (program_id) => { program_id: program_id, }; - const res = await publishProgram(template); + return await publishProgram(template); }; module.exports = { From d891915112e2065b4a98ed7ad4be31e3fc4c44d0 Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Mon, 11 Jul 2022 20:02:14 +0530 Subject: [PATCH 08/36] added logs and migration count --- script/README.md | Bin 1134 -> 1168 bytes script/api-list/headers.js | 39 ++-- script/api-list/program.js | 112 +++++------ script/api-list/question.js | 53 +++-- script/api-list/user.js | 13 +- script/constant/config.js | 35 ++-- script/db/dbConfig.js | 20 +- script/logger.js | 42 ++++ script/server.js | 85 +++++++- script/template/generate/gProgram.js | 178 +++++++++++++++-- script/template/generate/gQuestionSet.js | 125 ++++++++---- script/template/helpers/hierarchyHelper.js | 197 ++++++++++++++----- script/template/helpers/questionsetHelper.js | 42 +++- 13 files changed, 671 insertions(+), 270 deletions(-) create mode 100644 script/logger.js diff --git a/script/README.md b/script/README.md index da15b022cc538c2b54a713d9c9f8fad3d40964fe..f2d0d26369f248545c28c5a6873226dcbe506545 100644 GIT binary patch delta 294 zcmaFIF@bZ#8Ai=whEj$+h9rheh9ZU(23`g(hG2$J20sQT22X|{1{a2SATOA~l_6+y zER(o2x?BK5I8coOgDr!?A+tDt0gzn`gyjtRK$#Sf zm~c9fl?YT-!VnL{l?(+CwSvhEISiRV6Cg5~5Y^_Wq97ZRf%;Q{I&B#gfF_p!d6_^x z>0md2+~LCD$`CgBEt4X<3xhv{GlMsX%?c6iWtN?6!K?1E!?8w@lf73c~D215p` p$<{2ECIvwED*#Oena0VW3uNa3 { const isValid = await validateToken(type); @@ -12,19 +13,19 @@ const genToken = async (url, body, type) => { if (!isValid) { const res = await axios.post(url, body, headers).catch((err) => { - console.log("Error while generateToken", err.response.data); + logger.error(`Error while generateToken Error: ${JSON.stringify(err?.response?.data)}`) return err; }); return res ? res.data.access_token : ""; } else { - const token = type === "dev" ? this.dev_token : this.dock_token; + const token = type === "sunbird" ? this.sunbird_token : this.vdn_token; return token; } }; const validateToken = (type) => { - const token = type === "dev" ? this.dev_token : this.dock_token; + const token = type === "sunbird" ? this.sunbird_token : this.vdn_token; try { jwt.verify(token, "shhhhh"); @@ -39,16 +40,16 @@ const generateToken = async (type) => { let body = {}; switch (type) { - case "dev": - url = CONFIG.SUNBIRD.HOST.dev + CONFIG.SUNBIRD.APIS.token; - body = querystring.stringify({ ...CONFIG.SUNBIRD.config.dev.query }); - this.dev_token = await genToken(url, body, "dev"); - return this.dev_token; - case "dock": - url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.token; - body = querystring.stringify({ ...CONFIG.SUNBIRD.config.dock.query }); - this.dock_token = await genToken(url, body, "dock"); - return this.dock_token; + case "sunbird": + url = CONFIG.SUNBIRD.HOST.sunbird + CONFIG.SUNBIRD.APIS.token; + body = querystring.stringify({ ...CONFIG.SUNBIRD.config.sunbird.query }); + this.sunbird_token = await genToken(url, body, "sunbird"); + return this.sunbird_token; + case "vdn": + url = CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.token; + body = querystring.stringify({ ...CONFIG.SUNBIRD.config.vdn.query }); + this.vdn_token = await genToken(url, body, "vdn"); + return this.vdn_token; } }; @@ -56,23 +57,23 @@ const getHeaders = async (isTokenReq, type) => { let headers = {}; switch (type) { - case "dev": + case "sunbird": headers = { "Content-Type": "application/json", - Authorization: CONFIG.SUNBIRD.config.dev.authorization, + Authorization: CONFIG.SUNBIRD.config.sunbird.authorization, }; if (isTokenReq) { - headers["x-authenticated-user-token"] = await generateToken("dev"); + headers["x-authenticated-user-token"] = await generateToken("sunbird"); } break; - case "dock": + case "vdn": headers = { "Content-Type": "application/json", - Authorization: CONFIG.SUNBIRD.config.dock.authorization, + Authorization: CONFIG.SUNBIRD.config.vdn.authorization, }; if (isTokenReq) { - headers["x-authenticated-user-token"] = await generateToken("dock"); + headers["x-authenticated-user-token"] = await generateToken("vdn"); } break; } diff --git a/script/api-list/program.js b/script/api-list/program.js index 6480385d..7a286e6a 100644 --- a/script/api-list/program.js +++ b/script/api-list/program.js @@ -2,74 +2,45 @@ const { default: axios } = require("axios"); const { CONFIG } = require("../constant/config"); const { getHeaders } = require("./headers"); -const nominateProgram = async (program_id, author) => { - const url = - CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.add_program_nomination; + + +const createProgram = async (templateData) => { + const url = CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.create_program; const data = { request: { - program_id: program_id, - status: "Pending", - collection_ids: [], - targetprimarycategories: [ - { - name: "Observation", - identifier: "obj-cat:observation_questionset_all", - targetObjectType: "QuestionSet", - }, - { - name: "Survey", - identifier: "obj-cat:survey_questionset_all", - targetObjectType: "QuestionSet", - } - ], - content_types: [], - organisation_id: "937dd865-b256-4c1a-9830-a9b5b89f0913", - user_id: "bb551fff-121e-4a18-b969-984ac62bd572", + ...templateData, }, }; const config = { method: "post", url: url, - headers: await getHeaders(true, "dock"), + headers: await getHeaders(false, "vdn"), data: data, }; - - const res = await axios(config).catch((err) => { - console.log("Error while nominating Program", err.response.data); - }); - return res.data; + const res = await axios(config); + return res?.data?.result?.program_id; }; -const updateContributorToProgram = async (reqData) => { - - const url = - CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.update_program_nomination; +const updateProgram = async (templateData) => { + const url = CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.update_program; const data = { request: { - ...reqData + ...templateData, }, }; - const config = { method: "post", url: url, - headers: await getHeaders(true, "dock"), + headers: await getHeaders(false, "vdn"), data: data, }; - - - const res = await axios(config).catch((err) => { - console.log( - "Error while updating contributor to the Program", - err.response.data - ); - }); + const res = await axios(config); return res.data; }; -const createProgram = async (templateData) => { - const url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.create_program; +const publishProgram = async (templateData) => { + const url = CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.publish_program; const data = { request: { ...templateData, @@ -78,53 +49,66 @@ const createProgram = async (templateData) => { const config = { method: "post", url: url, - headers: await getHeaders(false, "dock"), + headers: await getHeaders(false, "vdn"), data: data, }; - const res = await axios(config) - return res?.data?.result?.program_id; + const res = await axios(config); + return res.data; }; - -const updateProgram = async (templateData) => { - const url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.update_program; +const nominateProgram = async (program_id, author) => { + const url = + CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.add_program_nomination; const data = { request: { - ...templateData, + program_id: program_id, + status: "Pending", + collection_ids: [], + targetprimarycategories: [ + { + name: "Observation", + identifier: "obj-cat:observation_questionset_all", + targetObjectType: "QuestionSet", + }, + { + name: "Survey", + identifier: "obj-cat:survey_questionset_all", + targetObjectType: "QuestionSet", + }, + ], + content_types: [], + organisation_id: "937dd865-b256-4c1a-9830-a9b5b89f0913", + user_id: "bb551fff-121e-4a18-b969-984ac62bd572", }, }; const config = { method: "post", url: url, - headers: await getHeaders(false, "dock"), + headers: await getHeaders(true, "vdn"), data: data, }; - const res = await axios(config).catch((err) => { - console.log("Error while updateProgram", err.response.data); - }); + const res = await axios(config); return res.data; }; -const publishProgram = async (templateData) => { - const url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.publish_program; +const updateContributorToProgram = async (reqData) => { + const url = + CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.update_program_nomination; const data = { request: { - ...templateData, + ...reqData, }, }; + const config = { method: "post", url: url, - headers: await getHeaders(false, "dock"), + headers: await getHeaders(true, "vdn"), data: data, }; - console.log("publish program config", data) - - const res = await axios(config).catch((err) => { - console.log("Error while publishProgram", err.response.data); - }); + const res = await axios(config); return res.data; }; diff --git a/script/api-list/question.js b/script/api-list/question.js index 3e9693c1..4720547e 100644 --- a/script/api-list/question.js +++ b/script/api-list/question.js @@ -1,12 +1,12 @@ const { default: axios } = require("axios"); const { CONFIG } = require("../constant/config"); +const logger = require("../logger"); const { getHeaders } = require("./headers"); - // Questionset const createQuestionSet = async (templateData) => { - const url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.create_questionset; + const url = CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.create_questionset; const data = { request: { questionset: { ...templateData }, @@ -15,7 +15,7 @@ const createQuestionSet = async (templateData) => { const config = { method: "post", url: url, - headers: await getHeaders(true, "dock"), + headers: await getHeaders(true, "vdn"), data: data, }; const res = await axios(config); @@ -24,12 +24,12 @@ const createQuestionSet = async (templateData) => { }; const updateQuestionSetHierarchy = async (templateData) => { - const url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.update_hierarchy; + const url = CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.update_hierarchy; const config = { method: "patch", url: url, - headers: await getHeaders(true, "dock"), + headers: await getHeaders(true, "vdn"), data: templateData, }; @@ -39,25 +39,24 @@ const updateQuestionSetHierarchy = async (templateData) => { const publishQuestionSet = async (questionsetId) => { const url = - CONFIG.SUNBIRD.HOST.dock + + CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.publish_questionset + "/" + questionsetId; const config = { method: "post", url: url, - headers: await getHeaders(true, "dock"), + headers: await getHeaders(true, "vdn"), data: {}, }; - const res = await axios(config).catch((err) => { - console.log("Error while publishing the questionset", err.response.data); - }); + const res = await axios(config); + return res?.data?.result?.identifier; }; const readQuestionSetHierarchy = async (questionSetId) => { const url = - CONFIG.SUNBIRD.HOST.dock + + CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.read_questionset + questionSetId + "?mode=edit"; @@ -65,19 +64,16 @@ const readQuestionSetHierarchy = async (questionSetId) => { const config = { method: "get", url: url, - headers: await getHeaders(true, "dock"), - // data: templateData, + headers: await getHeaders(true, "vdn"), }; const res = await axios(config); - // console.log("ressss", res?.data?.result?.questionSet); return res?.data?.result?.questionSet; - }; // Questions -const createQuestions = async (templateData) => { - const url = CONFIG.SUNBIRD.HOST.dock + CONFIG.SUNBIRD.APIS.create_question; +const createQuestions = async (templateData, questionId) => { + const url = CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.create_question; const data = { request: { question: { ...templateData }, @@ -86,36 +82,31 @@ const createQuestions = async (templateData) => { const config = { method: "post", url: url, - headers: await getHeaders(true, "dock"), + headers: await getHeaders(true, "vdn"), data: data, }; const res = await axios(config).catch((err) => { - console.log("Error while creating the question", err.response.data); + console.log(`Error while creating the question for questionid: ${questionId} Error:`,err?.response?.data) + logger.error(`Error while creating the question for questionid: ${questionId} Error: + ${JSON.stringify(err.response.data)}`); }); - return res.data.result.identifier; + return res?.data?.result?.identifier; }; const publishQuestion = async (questionId) => { const url = - CONFIG.SUNBIRD.HOST.dock + + CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.publish_question + "/" + questionId; const config = { method: "post", url: url, - headers: await getHeaders(true, "dock"), - data: {}, + headers: await getHeaders(true, "vdn") }; - const res = await axios(config).catch((err) => { - console.log( - "Error while publishing the question", - questionId, - err.response.data - ); - }); - return res.data.result.identifier; + const res = await axios(config) + return res?.data?.result?.identifier; }; module.exports = { diff --git a/script/api-list/user.js b/script/api-list/user.js index 41aaa173..381a59a1 100644 --- a/script/api-list/user.js +++ b/script/api-list/user.js @@ -1,36 +1,37 @@ const { default: axios } = require("axios"); const { CONFIG } = require("../constant/config"); +const logger = require("../logger"); const { getHeaders } = require("./headers"); const readUser = async (userId) => { const params = "organisations,roles,locations,declarations,externalIds"; const url = - CONFIG.SUNBIRD.HOST.dev + + CONFIG.SUNBIRD.HOST.sunbird + CONFIG.SUNBIRD.APIS.read_user + userId + "?fields=" + params; const res = await axios - .get(url, await getHeaders(true, "dev")) + .get(url, await getHeaders(true, "sunbird")) .catch((err) => { - console.log("Error while reading User", err.response.data); + logger.error(`Error while reading User: ${JSON.stringify(err?.response?.data)}`) }); }; const searchUser = async (userId) => { - const url = CONFIG.SUNBIRD.HOST.dev + CONFIG.SUNBIRD.APIS.search_user; + const url = CONFIG.SUNBIRD.HOST.sunbird + CONFIG.SUNBIRD.APIS.search_user; const config = { method: "post", url: url, - headers: await getHeaders(true, "dev"), + headers: await getHeaders(true, "sunbird"), data: { request: { filters: { id: userId } }, }, }; const res = await axios(config).catch((err) => { - console.log("Error while searching User", err.response.data); + logger.error(`Error while searching User: ${JSON.stringify(err?.response?.data)}`) }); return res.data?.result?.response?.content; }; diff --git a/script/constant/config.js b/script/constant/config.js index 0d897954..55b7790e 100644 --- a/script/constant/config.js +++ b/script/constant/config.js @@ -1,4 +1,4 @@ -require("dotenv").config({path: "./../../.env"}); +require("dotenv").config(); const CONFIG = { DB: { @@ -12,8 +12,8 @@ const CONFIG = { }, SUNBIRD: { HOST: { - dev: "https://dev.sunbirded.org/", - dock: "https://dock.sunbirded.org/", + sunbird: process.env.SUNBIRD_HOST, + vdn: process.env.VDN_HOST }, APIS: { token: "auth/realms/sunbird/protocol/openid-connect/token", @@ -32,26 +32,25 @@ const CONFIG = { publish_program: "api/program/v1/publish", }, config: { - dev: { + sunbird: { query: { - username: process.env.username, - password: process.env.password, - grant_type: process.env.grant_type, - client_id: process.env.client_id, - client_secret: process.env.client_secret, + username: process.env.SUNBIRD_USER, + password: process.env.SUNBIRD_PWD, + grant_type: process.env.SUNBIRD_GRANT, + client_id: process.env.SUNBIRD_CLIENT, + client_secret: process.env.SUNBIRD_CLIENT_SECRET, }, - authorization: process.env.DEV_AUTHORIZATION, + authorization: process.env.SUNBIRD_AUTHORIZATION, }, - dock: { + vdn: { query: { - username: process.env.username, - password: process.env.password, - grant_type: process.env.grant_type, - client_id: process.env.client_id, - client_secret: process.env.client_secret, + username: process.env.SUNBIRD_USER, + password: process.env.SUNBIRD_PWD, + grant_type: process.env.SUNBIRD_GRANT, + client_id: process.env.SUNBIRD_CLIENT, + client_secret: process.env.SUNBIRD_CLIENT_SECRET, }, - nominate_user: process.env.nominate_user, - authorization: process.env.DOCK_AUTHORIZATION, + authorization: process.env.VDN_AUTHORIZATION, }, }, }, diff --git a/script/db/dbConfig.js b/script/db/dbConfig.js index 8fdfff18..dca169f3 100644 --- a/script/db/dbConfig.js +++ b/script/db/dbConfig.js @@ -1,30 +1,20 @@ const { CONFIG } = require("../constant/config"); const mongoose = require("mongoose"); +const logger = require("../logger"); const connect = async () => { try { - // const db = await mongoose.createConnection( - // // CONFIG.DB.DB_HOST - // CONFIG.DB.DB_HOST, - // { - // useNewUrlParser: true, - // } - // ); - const Conn = mongoose.createConnection(); - // connect to database + console.log("MONGODB_URL", CONFIG.DB.DB_HOST) this.database = await Conn.openUri(CONFIG.DB.DB_HOST); - // Conn.on("error", console.error.bind(console, "connection error:")); - // Conn.once("open", function () { - // console.log("Connected to DB"); - // }); - // this.database = db; - // .db(CONFIG.DB.DB_NAME); + Conn.on("error", console.error.bind(console, "connection error:")); } catch (err) { console.log("Error While connecting to DB", err); + logger.error(`Error While connecting to DB`, err); + process.exit(); } }; diff --git a/script/logger.js b/script/logger.js new file mode 100644 index 00000000..790c495b --- /dev/null +++ b/script/logger.js @@ -0,0 +1,42 @@ +var path = require('path') +const { createLogger, format, transports } = require('winston'); +const { combine, timestamp, label, printf } = format; +const myFormat = printf(({ level, message, timestamp }) => { + return `${timestamp} ${level}: ${message}`; +}); + +const logLevels = { + error: 0, + warn: 1, + info: 2, + debug: 3, + } +const date = new Date().getDate()+'-'+new Date().getMonth()+'-'+new Date().getFullYear() +const logger = createLogger({ + level: "logLevels", + format: combine( + timestamp(), + myFormat + ), + transports: [ + new transports.File({ filename: path.join(__dirname, `/logs/${date}-error.log`), level: 'error' }), + new transports.File({ filename: path.join(__dirname, `/logs/${date}-info.log`), level: 'info' }), + new transports.File({ filename: path.join(__dirname, `/logs/${date}-debug.log`), level: 'debug' }), + ] +}); + + +error = async(msg) =>{ + logger.error(msg) +} + +debug = async(msg) =>{ + logger.debug(msg) +} +info = async(msg) =>{ + logger.info(msg) +} + +module.exports = { + info, error, debug +} \ No newline at end of file diff --git a/script/server.js b/script/server.js index 5a123340..c0863637 100644 --- a/script/server.js +++ b/script/server.js @@ -7,27 +7,98 @@ const { } = require("./template/generate/gQuestionSet.js"); const { ObjectId } = require("mongodb"); +const logger = require("./logger"); -const migrateData = async () => { + +const migrateData = async (req, res) => { try { - console.log("Environment: " + process.env.APPLICATION_ENV ); + const programMigration = { + migrated: 0, + updated: 0, + published: 0, + nominated: 0, + contributor: 0, + accepted: 0, + }; + const questionSetMigration = { + migrated: 0, + hierarchy: 0, + branching: 0, + published: 0, + }; + + const migratedCount = { + totalCount: 0, + success: { + program: { + existing: { + ...programMigration, + }, + current: { + ...programMigration, + }, + }, + questionSet: { + existing: { + ...questionSetMigration, + }, + current: { + ...questionSetMigration, + }, + }, + }, + failed: { + program: { + migrated: { count: 0, ids: [] }, + updated: { count: 0, ids: [] }, + published: { count: 0, ids: [] }, + nominated: { count: 0, ids: [] }, + contributor: { count: 0, ids: [] }, + accepted: { count: 0, ids: [] }, + }, + questionSet: { + migrated: { count: 0, ids: [] }, + hierarchy: { count: 0, ids: [] }, + branching: { count: 0, ids: [] }, + published: { count: 0, ids: [] }, + }, + question: { count: 0, ids: [] } + }, + }; const db = await createDBInstance(); - // const id = "5f34ec17585244939f89f90d"; + // req.query.questionsetID || + // const id = "5f3bc15416fdc4ed008171b1"; + const data = await findAll("solutions", { // _id: ObjectId(id), + // $or: [{migratedId: {$eq: null}}, {migratedId:{$exists: false}}], programId: { $exists: true }, - type: {$in: ["observation", "survey"]} + type: { $in: ["observation", "survey"] }, }); - const template = await getQuestionSetTemplates(data); + + migratedCount.totalCount = data.length; + + const template = await getQuestionSetTemplates( + data, + migratedCount + ); + + console.log(); + console.log("migratedCounttt", JSON.stringify(migratedCount)); + console.log(); + // logger.in + logger.info(`\n migratedCount ${JSON.stringify(migratedCount)}`); + + // return template; process.exit(); } catch (err) { - console.log("Error while reading questionSet", err); + logger.error(`Error while migrating : ${err}`) + throw new Error("Error occured", err); - } }; diff --git a/script/template/generate/gProgram.js b/script/template/generate/gProgram.js index 26936f30..91e33393 100644 --- a/script/template/generate/gProgram.js +++ b/script/template/generate/gProgram.js @@ -7,6 +7,7 @@ const { updateContributorToProgram, } = require("../../api-list/program"); const { searchUser } = require("../../api-list/user"); +const logger = require("../../logger"); const { updateSolutionById } = require("../helpers/questionsetHelper"); const getDate = (increment) => { @@ -17,7 +18,7 @@ const getDate = (increment) => { return d; }; -const createProgramTemplate = async (solution, program_id) => { +const createProgramTemplate = async (solution, program_id, migratedCount) => { const userData = await searchUser(solution.author); const userId = @@ -73,29 +74,97 @@ const createProgramTemplate = async (solution, program_id) => { let query = {}; if (!program_id) { - programId = await createProgram(template); + programId = await createProgram(template).catch((err) => { + logger.error(`Error while creating program for solution_id: ${ + solution?._id + } Error: + ${JSON.stringify(err.response.data)}`); + migratedCount.failed.program.migrated.count++; + if (!migratedCount.failed.program.migrated.ids.includes(id)) { + migratedCount.failed.program.migrated.ids.push(id); + } + // updateFailedCount(migratedCount, "migrated", solution?._id); + }); + if (!programId) { + return; + } + logger.info( + `Sourcing Program created for solution_id: ${solution?._id} === ${programId}` + ); query = { ...query, sourcingProgramId: programId }; + } else { + migratedCount.success.program.existing.migrated++; } if (!solution.isSrcProgramUpdated) { const update_res = await updateProgramTemplate(programId, solution); + if (!update_res) { + migratedCount.failed.program.updated.count++; + if (!migratedCount.failed.program.updated.ids.includes(id)) { + migratedCount.failed.program.updated.ids.push(id); + } + await updateSolutionDb(query, solution, migratedCount); + return; + } + logger.info( + `Sourcing Program updated for solution_id: ${solution?._id} === ${programId}` + ); + query = { ...query, isSrcProgramUpdated: true, }; + } else { + migratedCount.success.program.existing.updated++; } if (!solution.isSrcProgramPublished) { - const pub_res = await publishProgramTemplate(programId); + const pub_res = await publishProgramTemplate(programId, solution?._id); + + if (!pub_res) { + migratedCount.failed.program.published.count++; + if (!migratedCount.failed.program.published.ids.includes(id)) { + migratedCount.failed.program.published.ids.push(id); + } + // updateFailedCount(migratedCount, "published", solution?._id); + await updateSolutionDb(query, solution, migratedCount); + return; + } + logger.info( + `Sourcing Program published for solution_id: ${solution?._id} === ${programId}` + ); + query = { ...query, isSrcProgramPublished: true, }; + } else { + migratedCount.success.program.existing.published++; } if (!solution.isNominated) { - const res = await nominateProgram(programId, userId); + const res = await nominateProgram(programId, userId).catch((err) => { + // updateFailedCount(migratedCount, "nominated", solution?._id); + migratedCount.failed.program.nominated.count++; + if (!migratedCount.failed.program.nominated.ids.includes(id)) { + migratedCount.failed.program.nominated.ids.push(id); + } + logger.error(`Error while nominating program for solution_id: ${ + solution?._id + } Error: + ${JSON.stringify(err.response.data)}`); + }); + if (!res) { + await updateSolutionDb(query, solution, migratedCount); + return; + } + logger.info( + `Sourcing Program nominated for solution_id: ${solution?._id} === ${programId}` + ); + query = { ...query, isNominated: true }; + } else { + migratedCount.success.program.existing.nominated++; } if (!solution.isContributorAdded) { @@ -107,8 +176,31 @@ const createProgramTemplate = async (solution, program_id) => { }, }; - const update_nom = await updateContributorToProgram(add_contri); + const update_nom = await updateContributorToProgram(add_contri).catch( + (err) => { + migratedCount.failed.program.contributor.count++; + if (!migratedCount.failed.program.contributor.ids.includes(id)) { + migratedCount.failed.program.contributor.ids.push(id); + } + // updateFailedCount(migratedCount, "contributor", solution?._id); + logger.error(`Error while adding contributor program for solution_id: ${ + solution?._id + } Error: + ${JSON.stringify(err.response.data)}`); + } + ); + + if (!update_nom) { + await updateSolutionDb(query, solution, migratedCount); + return; + } + logger.info( + `Sourcing Program added contributor for solution_id: ${solution?._id} === ${programId}` + ); + query = { ...query, isContributorAdded: true }; + } else { + migratedCount.success.program.existing.contributor++; } if (!solution.isContributorAccepted) { @@ -118,20 +210,71 @@ const createProgramTemplate = async (solution, program_id) => { status: "Approved", updatedby: userId, }; - const update_nom = await updateContributorToProgram(accept_contri); + const update_nom = await updateContributorToProgram(accept_contri).catch( + (err) => { + logger.error(`Error while accepting nomination to the program for solution_id: ${ + solution?._id + } Error: + ${JSON.stringify(err.response.data)}`); + migratedCount.failed.program.accepted.count++; + if (!migratedCount.failed.program.accepted.ids.includes(id)) { + migratedCount.failed.program.accepted.ids.push(id); + } + // updateFailedCount(migratedCount, "accepted", solution?._id); + } + ); + + if (!update_nom) { + await updateSolutionDb(query, solution, migratedCount); + return; + } + logger.info( + `Sourcing Program accepted nomination for solution_id: ${solution?._id} === ${programId}` + ); + query = { ...query, isContributorAccepted: true }; + } else { + migratedCount.success.program.existing.accepted++; } if (!isEmpty(query)) { - await updateSolutionById({ - id: solution._id.toString(), - query: { ...query }, - }); + await updateSolutionDb(query, solution, migratedCount); } return programId; }; +const updateSolutionDb = async (query, solution, migratedCount) => { + const res = await updateSolutionById({ + id: solution._id.toString(), + query: { ...query }, + }).catch((err) => { + logger.error( + `Error while updating program in solutions collection: ${solution?._id} Error: ${err}` + ); + }); + + if (query.hasOwnProperty("sourcingProgramId")) { + migratedCount.success.program.current.migrated++; + } + if (query.hasOwnProperty("isSrcProgramUpdated")) { + migratedCount.success.program.current.updated++; + } + if (query.hasOwnProperty("isSrcProgramPublished")) { + migratedCount.success.program.current.published++; + } + if (query.hasOwnProperty("isNominated")) { + migratedCount.success.program.current.nominated++; + } + if (query.hasOwnProperty("isContributorAdded")) { + migratedCount.success.program.current.contributor++; + } + if (query.hasOwnProperty("isContributorAccepted")) { + migratedCount.success.program.current.accepted++; + } +}; + + const updateProgramTemplate = async (program_id, solution) => { const template = { config: { @@ -176,16 +319,25 @@ const updateProgramTemplate = async (program_id, solution) => { program_id: program_id, }; - return await updateProgram(template); + const upd_res = await updateProgram(template).catch((err) => { + logger.error(`Error while updating program for solution_id: ${ + solution?._id + } Error: + ${JSON.stringify(err.response.data)}`); + }); + return upd_res; }; -const publishProgramTemplate = async (program_id) => { +const publishProgramTemplate = async (program_id, id) => { const template = { channel: "sunbird", program_id: program_id, }; - return await publishProgram(template); + return await publishProgram(template).catch((err) => { + logger.error(`Error while publishing program for solution_id: ${id} Error: + ${JSON.stringify(err.response.data)}`); + }); }; module.exports = { diff --git a/script/template/generate/gQuestionSet.js b/script/template/generate/gQuestionSet.js index d55b7235..679f1cfc 100644 --- a/script/template/generate/gQuestionSet.js +++ b/script/template/generate/gQuestionSet.js @@ -4,6 +4,7 @@ const { createQuestionSet } = require("../../api-list/question"); const { CONFIG } = require("../../constant/config"); const { findAll, updateById } = require("../../db"); +const logger = require("../../logger"); const { updateHierarchyChildren, getPrecondition, @@ -18,19 +19,20 @@ const { } = require("../helpers/questionsetHelper"); const { createProgramTemplate } = require("./gProgram"); -const getQuestionSetTemplates = async (solutions) => { +const getQuestionSetTemplates = async (solutions, migratedCount) => { const data = Promise.all( solutions.map(async (solution) => { + let programId = solution.sourcingProgramId; console.log(); console.log( - "---------------------------solution---------------------------------------" + "-----------------------sourcingProgramId----------------------", + programId ); console.log(); - let programId = solution.sourcingProgramId; - programId = await createProgramTemplate(solution, programId).catch( - (err) => { - console.log("Error while creating the Program", err.response.data); - } + programId = await createProgramTemplate( + solution, + programId, + migratedCount ); console.log(); console.log( @@ -38,21 +40,18 @@ const getQuestionSetTemplates = async (solutions) => { ); console.log(); console.log("ProgramId", programId); - + if (!programId) { - console.log("programId empty", programId) return; } - console.log("programId Present", programId) - console.log(); - return getQuestionSetTemplate(solution, programId); + + return getQuestionSetTemplate(solution, programId, migratedCount); }) ); - return data; }; -const getQuestionSetTemplate = async (solution, programId) => { +const getQuestionSetTemplate = async (solution, programId, migratedCount) => { let templateData = setQuestionSetTemplate(solution, programId); const questionsetid = solution._id.toString(); @@ -60,7 +59,14 @@ const getQuestionSetTemplate = async (solution, programId) => { let hierarchy = { questionsetDbId: questionsetid, isHierarchyUpdated: solution.isHierarchyUpdated || false, + isBranchingUpdated: solution.isBranchingUpdated || false, + isPublished: solution.isPublished || false, sourcingProgramId: programId, + isSrcProgramUpdated: solution.isSrcProgramUpdated || false, + isSrcProgramPublished: solution.isSrcProgramPublished || false, + isNominated: solution.isNominated || false, + isContributorAdded: solution.isContributorAdded || false, + isContributorAccepted: solution.isContributorAccepted || false, criterias: [], }; @@ -69,21 +75,33 @@ const getQuestionSetTemplate = async (solution, programId) => { if (!questionSetMigratedId) { questionSetMigratedId = await createQuestionSet(templateData).catch( (err) => { - console.log("Error while creating Questionset", err.response.data); + console.log(`Error while creating Questionset for solution_id: ${questionsetid} Error:`,err?.response?.data) + logger.error(`Error while creating Questionset for solution_id: ${questionsetid} Error: + ${JSON.stringify(err?.response?.data)}`); + + migratedCount.failed.questionSet.migrated.count++; + if (!migratedCount.failed.questionSet.migrated.ids.includes(id)) { + migratedCount.failed.questionSet.migrated.ids.push(id); + } } ); if (!questionSetMigratedId) { return; } + await updateById(CONFIG.DB.TABLES.solutions, questionsetid, { migratedId: questionSetMigratedId, - sourcingProgramId: programId, }); + + migratedCount.success.questionSet.current.migrated++; + hierarchy = { ...hierarchy, questionset: questionSetMigratedId, }; } else { + migratedCount.success.questionSet.existing.migrated++; + hierarchy = { ...hierarchy, questionset: questionSetMigratedId, @@ -116,6 +134,8 @@ const getQuestionSetTemplate = async (solution, programId) => { ); console.log(); + logger.info(`CriResult name:${criResult[0].name} criteria questions = ${questions.length}`) + hierarchy.criterias.push({ questions: [], }); @@ -126,16 +146,15 @@ const getQuestionSetTemplate = async (solution, programId) => { criResult[0], solution.type, criteriaId, - (criteriaMigratedId = criResult[0].migratedId || "") + (criteriaMigratedId = criResult[0].migratedId || ""), + migratedCount ); hierarchy = questionTemplates.hierarchy; } } } - console.log(); - console.log("hierarchyStructure", JSON.stringify(hierarchy)); - console.log(); - await updateHierarchyTemplate(hierarchy, solution, programId); + + await updateHierarchyTemplate(hierarchy, solution, programId, migratedCount); return hierarchy; }; @@ -146,7 +165,8 @@ const getQuestionTemplate = async ( criteria, type, criteriaId, - criteriaMigratedId + criteriaMigratedId, + migratedCount ) => { hierarchy.criterias[index] = { migratedId: criteriaMigratedId, @@ -164,7 +184,12 @@ const getQuestionTemplate = async ( for (let i = 0; i < questions.length; i++) { const question = questions[i]; const questionId = question._id.toString(); - const templates = await getChildren(questionId, hierarchy, index); + const templates = await getChildren( + questionId, + hierarchy, + index, + migratedCount + ); } hierarchy.criterias[index].questions = uniq( @@ -174,12 +199,14 @@ const getQuestionTemplate = async ( return { questions, hierarchy }; }; -const getChildren = async (questionId, hierarchy, index) => { +const getChildren = async (questionId, hierarchy, index, migratedCount) => { const question = await getQuestionFromDB(questionId); - const migratedId = await createQuestionTemplate(question); + const migratedId = await createQuestionTemplate(question, migratedCount); const parentId = migratedId; hierarchy = updateHierarchyChildren(hierarchy, migratedId, index); + logger.info(`getChildren of questionId: ${questionId} and questiontype: ${question?.responseType}`) + if (question.responseType !== "matrix") { if (!isChildrenPresent(question) && !isVisibleIfPresent(question)) { hierarchy = updateHierarchyChildren(hierarchy, parentId, index); @@ -191,7 +218,8 @@ const getChildren = async (questionId, hierarchy, index) => { question, hierarchy, index, - parentId + parentId, + migratedCount ); } @@ -199,7 +227,8 @@ const getChildren = async (questionId, hierarchy, index) => { return await IfNoChildrenAndVisibleIfAndInnerChildren( question, hierarchy, - index + index, + migratedCount ); } } else if (question.responseType === "matrix") { @@ -224,7 +253,10 @@ const getChildren = async (questionId, hierarchy, index) => { matrixParentQuestion = await getQuestionFromDB(matrixParentId); matrixParentIdMigratedId = matrixParentQuestion?.migratedId || ""; if (!matrixParentIdMigratedId) { - matrixParentIdMigratedId = await createQuestionTemplate(question); + matrixParentIdMigratedId = await createQuestionTemplate( + question, + migratedCount + ); } hierarchy = updateHierarchyChildren( hierarchy, @@ -236,7 +268,10 @@ const getChildren = async (questionId, hierarchy, index) => { for (let i = 0; i < instanceQuestions.length; i++) { const instanceQuestionId = instanceQuestions[i]; const insQuestion = await getQuestionFromDB(instanceQuestionId); - const insMigratedId = await createQuestionTemplate(insQuestion); + const insMigratedId = await createQuestionTemplate( + insQuestion, + migratedCount + ); hierarchy = updateHierarchyChildren(hierarchy, insMigratedId, index); @@ -293,7 +328,8 @@ const IfChildrenAndNoVisibleIf = async ( question, hierarchy, index, - parentId + parentId, + migratedCount ) => { let branching = { [parentId]: { @@ -308,8 +344,10 @@ const IfChildrenAndNoVisibleIf = async ( for (let i = 0; i < children.length; i++) { const childId = children[i].toString(); const childQuestion = await getQuestionFromDB(childId); - const migratedId = await createQuestionTemplate(childQuestion); - // hierarchy.criterias[index].questions.push(migratedId); + const migratedId = await createQuestionTemplate( + childQuestion, + migratedCount + ); hierarchy = updateHierarchyChildren(hierarchy, migratedId, index); if (migratedId) { @@ -323,7 +361,8 @@ const IfChildrenAndNoVisibleIf = async ( const childBranching = await IfNoChildrenAndVisibleIf( question, parentId, - childQuestion + childQuestion, + migratedCount ); branching = { @@ -338,14 +377,19 @@ const IfChildrenAndNoVisibleIf = async ( return hierarchy; }; -const IfNoChildrenAndVisibleIf = async (parentQuestion, parentId, question) => { +const IfNoChildrenAndVisibleIf = async ( + parentQuestion, + parentId, + question, + migratedCount +) => { const visibleIf = question.visibleIf || []; let childBranching = {}; for (let i = 0; i < visibleIf.length; i++) { const visible = visibleIf[i]; const childId = question._id.toString(); const chiQuestion = await getQuestionFromDB(childId); - const migratedId = await createQuestionTemplate(chiQuestion); + const migratedId = await createQuestionTemplate(chiQuestion, migratedCount); if (migratedId) { childBranching = { [migratedId]: { @@ -362,14 +406,18 @@ const IfNoChildrenAndVisibleIf = async (parentQuestion, parentId, question) => { const IfNoChildrenAndVisibleIfAndInnerChildren = async ( question, hierarchy, - index + index, + migratedCount ) => { const visibleIf = question.visibleIf || []; for (let i = 0; i < visibleIf.length; i++) { const visible = visibleIf[i]; const parentId = visible._id.toString(); const parentQuestion = await getQuestionFromDB(parentId); - const migratedId = await createQuestionTemplate(parentQuestion); + const migratedId = await createQuestionTemplate( + parentQuestion, + migratedCount + ); hierarchy.criterias[index].questions.push(migratedId); if ( isChildrenPresent(parentQuestion) && @@ -379,7 +427,8 @@ const IfNoChildrenAndVisibleIfAndInnerChildren = async ( parentQuestion, hierarchy, index, - migratedId + migratedId, + migratedCount ); } } diff --git a/script/template/helpers/hierarchyHelper.js b/script/template/helpers/hierarchyHelper.js index 3904ea6e..399585f6 100644 --- a/script/template/helpers/hierarchyHelper.js +++ b/script/template/helpers/hierarchyHelper.js @@ -6,6 +6,8 @@ const { } = require("../../api-list/question"); const { CONFIG } = require("../../constant/config"); const { updateById } = require("../../db"); +const logger = require("../../logger"); +const { updateQuestionSetFailedCount } = require("./questionsetHelper"); const updateHierarchyChildren = (hierarchy, migratedId, index) => { if ( @@ -17,7 +19,6 @@ const updateHierarchyChildren = (hierarchy, migratedId, index) => { return hierarchy; }; - const getOperator = (visibleIf) => { const operator = visibleIf.operator === "===" @@ -44,11 +45,15 @@ const getPrecondition = (visible, parentId, parentQuestion) => { ], }, ], - } -} - + }; +}; -const updateHierarchyTemplate = async (hierarchy, solution, programId) => { +const updateHierarchyTemplate = async ( + hierarchy, + solution, + programId, + migratedCount +) => { const updateHierarchyData = { request: { data: { @@ -95,19 +100,42 @@ const updateHierarchyTemplate = async (hierarchy, solution, programId) => { console.log("updateHierarchydata", JSON.stringify(updateHierarchyData)); console.log(); const questionsetId = hierarchy.questionsetDbId; - + let query = {}; if (!hierarchy.isHierarchyUpdated) { - const result = await updateQuestionSetHierarchy(updateHierarchyData); + const result = await updateQuestionSetHierarchy(updateHierarchyData).catch( + (err) => { + logger.error(`Error while updating the questionset for solution_id: ${questionsetId} Error: + ${JSON.stringify(err.response.data)}`); + + migratedCount.failed.questionSet.hierarchy.count++; + if ( + !migratedCount.failed.questionSet.hierarchy.ids.includes( + hierarchy?.questionset + ) + ) { + migratedCount.failed.questionSet.hierarchy.ids.push( + hierarchy?.questionset + ); + } + // updateQuestionSetFailedCount(migratedCount, "hierarchy", questionsetId); + } + ); - await updateById(CONFIG.DB.TABLES.solutions, questionsetId, { + if (!result) { + await updateSolutionsDb(query, questionsetId, migratedCount); + return; + } + query = { + ...query, isHierarchyUpdated: true, - isBranchingUpdated: false, - sourcingProgramId: programId, - }); + }; + for (let i = 0; i < hierarchy.criterias.length; i++) { const criterias = hierarchy.criterias[i]; hierarchy.criterias[i].migratedId = result[criterias.name]; } + } else { + migratedCount.success.questionSet.existing.hierarchy++; } if (!hierarchy.isBranchingUpdated) { @@ -116,26 +144,71 @@ const updateHierarchyTemplate = async (hierarchy, solution, programId) => { const result = await updateQuestionSetHierarchy(branchinghierarchy).catch( (err) => { - console.log( - "Error while updating the questionset branching", - err.response.data - ); + logger.error(`Error while updating the questionset branching for solution_id: ${questionsetId} Error: + ${JSON.stringify(err.response.data)}`); + migratedCount.failed.questionSet.branching.count++; + + if ( + !migratedCount.failed.questionSet.branching.ids.includes( + hierarchy?.questionset + ) + ) { + migratedCount.failed.questionSet.branching.ids.push( + hierarchy?.questionset + ); + } + // updateQuestionSetFailedCount(migratedCount, "branching", questionsetId); } ); - if (result) { - await updateById(CONFIG.DB.TABLES.solutions, questionsetId, { - isBranchingUpdated: true, - sourcingProgramId: programId, - }); - await publishQuestionSet(hierarchy.questionset); + if (!result) { + await updateSolutionsDb(query, questionsetId, migratedCount); + return; + } + query = { + ...query, + isBranchingUpdated: true, + }; + } else { + migratedCount.success.questionSet.existing.branching++; + } + + if (!hierarchy.isPublished) { + const res = await publishQuestionSet(hierarchy.questionset).catch((err) => { + logger.error(`Error while publishing the questionset for solution_id: ${questionsetId} === ${ + hierarchy?.questionset + } Error: + ${JSON.stringify(err.response.data)}`); + migratedCount.failed.questionSet.published.count++; + + if ( + !migratedCount.failed.questionSet.published.ids.includes( + hierarchy?.questionset + ) + ) { + migratedCount.failed.questionSet.published.ids.push( + hierarchy?.questionset + ); + } + // updateQuestionSetFailedCount(migratedCount, "published", hierarchy?.questionset); + }); + if (!res) { + await updateSolutionsDb(query, questionsetId, migratedCount); + return; } + query = { + ...query, + isPublished: true, + }; + } else if (hierarchy.isPublished) { + migratedCount.success.questionSet.existing.published++; } + const res = await updateSolutionsDb(query, questionsetId, migratedCount); }; const branchingQuestionSetHierarchy = async (hierarchy) => { let questionSetHierarchy = {}; if (hierarchy.questionset && hierarchy.isHierarchyUpdated) { - questionSetHierarchy = await readQuestionSetHierarchy( + questionSetHierarchy = await readQuestionSetHierarchy( hierarchy.questionset ); } @@ -162,43 +235,65 @@ const branchingQuestionSetHierarchy = async (hierarchy) => { hierarchy.questionset && hierarchy.isHierarchyUpdated ? hierarchyData?.identifier : criteria.migratedId; + if (criteria?.migratedId) { + const metadata = pick(criteria, [ + "code", + "name", + "description", + "mimeType", + "primaryCategory", + "allowMultipleInstances", + "instances", + ]); + updateHierarchyData.request.data.nodesModified[criteria.migratedId] = { + metadata: { + ...metadata, + allowBranching: "Yes", + branchingLogic: get(criteria, "branchingLogic") || {}, + }, + objectType: "QuestionSet", + root: false, + isNew: false, + }; + updateHierarchyData.request.data.hierarchy[ + hierarchy.questionset + ].children.push(criteria.migratedId); - const metadata = pick(criteria, [ - "code", - "name", - "description", - "mimeType", - "primaryCategory", - "allowMultipleInstances", - "instances", - ]); - updateHierarchyData.request.data.nodesModified[criteria.migratedId] = { - metadata: { - ...metadata, - allowBranching: "Yes", - branchingLogic: get(criteria, "branchingLogic") || {}, - }, - objectType: "QuestionSet", - root: false, - isNew: false, - }; - updateHierarchyData.request.data.hierarchy[ - hierarchy.questionset - ].children.push(criteria.migratedId); - - updateHierarchyData.request.data.hierarchy[criteria.migratedId] = { - children: compact(criteria.questions), - root: false, - }; + updateHierarchyData.request.data.hierarchy[criteria.migratedId] = { + children: compact(criteria.questions), + root: false, + }; + } } return updateHierarchyData; }; +const updateSolutionsDb = async (query, questionsetId, migratedCount) => { + const res = await updateById( + CONFIG.DB.TABLES.solutions, + questionsetId, + query + ).catch((err) => { + logger.error( + `Error while updating questionset in solutions collection: ${solution?._id}` + ); + }); + + if (query.hasOwnProperty("isHierarchyUpdated")) { + migratedCount.success.questionSet.current.hierarchy++; + } + if (query.hasOwnProperty("isBranchingUpdated")) { + migratedCount.success.questionSet.current.branching++; + } + if (query.hasOwnProperty("isPublished")) { + migratedCount.success.questionSet.current.published++; + } +}; + module.exports = { updateHierarchyChildren, updateHierarchyTemplate, branchingQuestionSetHierarchy, getOperator, - getPrecondition + getPrecondition, }; - diff --git a/script/template/helpers/questionsetHelper.js b/script/template/helpers/questionsetHelper.js index 2c9c99e1..8129a00f 100644 --- a/script/template/helpers/questionsetHelper.js +++ b/script/template/helpers/questionsetHelper.js @@ -3,11 +3,18 @@ const { ObjectId } = require("mongodb"); const { createQuestions, publishQuestion } = require("../../api-list/question"); const { CONFIG } = require("../../constant/config"); const { updateById, findAll } = require("../../db"); +const logger = require("../../logger"); const { questionSetTemplate, questionSetTemplateStatic, } = require("../config/questionSet"); -const { getDateTemplate, getSliderTemplate, getMSMCQTemplate, getMCQTemplate, getTextTemplate } = require("../generate/gQuestion"); +const { + getDateTemplate, + getSliderTemplate, + getMSMCQTemplate, + getMCQTemplate, + getTextTemplate, +} = require("../generate/gQuestion"); const setQuestionSetTemplate = (solution, programId) => { let templateData = {}; @@ -34,7 +41,7 @@ const setQuestionSetTemplate = (solution, programId) => { return templateData; }; -const createQuestionTemplate = async (question) => { +const createQuestionTemplate = async (question, migratedCount) => { const type = question.responseType; let migratedId = question.migratedId; let query = {}; @@ -58,7 +65,7 @@ const createQuestionTemplate = async (question) => { } if (!isEmpty(questionToMigrate)) { - migratedId = await createQuestions(questionToMigrate); + migratedId = await createQuestions(questionToMigrate, question._id); query = { ...query, migratedId, @@ -66,8 +73,27 @@ const createQuestionTemplate = async (question) => { } } - if (!question.isPublished && migratedId) { - await publishQuestion(migratedId); + if (!!migratedId && !question.isPublished) { + const res = await publishQuestion(migratedId).catch((err) => { + migratedCount.failed.question.count++; + if (!migratedCount.failed.question.ids.includes(migratedId)) { + migratedCount.failed.question.ids.push(migratedId); + } + console.log(`Error while publishing the question for questionid: ${migratedId} questionId: ${question?._id} Error: + ${err.response.data}`); + logger.error(`Error while publishing the question for migratedId: ${migratedId} Error: + ${JSON.stringify(err.response.data)}`); + }); + + console.log("createQuestion Template publish response",res , "migratedId", migratedId, "questionId", question?._id) + logger.info(`createQuestion Template publish response: ${res} , "migratedId" ${migratedId} questionId, ${question?._id}`); + + if (!res) { + return migratedId; + } + + console.log("createQuestion Template published", migratedId) + logger.info(`createQuestion Template published: ${migratedId}`); query = { ...query, isPublished: true, @@ -94,15 +120,15 @@ const getQuestionFromDB = async (questionId) => { }; const isVisibleIfPresent = (question) => { - return !isEmpty(get(question, 'visibleIf')); + return !isEmpty(get(question, "visibleIf")); }; const isChildrenPresent = (question) => { - return !isEmpty(get(question, 'children')) + return !isEmpty(get(question, "children")); }; const isInstanceQuestionsPresent = (question) => { - return !isEmpty(get(question, 'instanceQuestions')) + return !isEmpty(get(question, "instanceQuestions")); }; module.exports = { From 164ba1a951120b17787475ffa576452fb492298e Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Mon, 11 Jul 2022 20:04:44 +0530 Subject: [PATCH 09/36] updated package.json --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index a26bf3df..76c0823b 100644 --- a/package.json +++ b/package.json @@ -65,7 +65,8 @@ "yaml-js": "^0.1.3", "axios": "^0.26.1", "lodash": "^4.17.21", - "querystring": "^0.2.1" + "querystring": "^0.2.1", + "winston": "^3.8.1" }, "devDependencies": {} } From eb063252e71f9e6cea89123fa1172e9ec6699c62 Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Wed, 13 Jul 2022 12:21:46 +0530 Subject: [PATCH 10/36] updated env --- .env.sample | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/.env.sample b/.env.sample index f9c36de5..a5e65b43 100644 --- a/.env.sample +++ b/.env.sample @@ -32,13 +32,14 @@ DISABLE_LEARNER_SERVICE_ON_OFF = "ON" / # ML Script -username = string -password = string -grant_type = string -client_id = string -client_secret= string - -DEV_HOST = string -DOCK_HOST = string -DEV_AUTHORIZATION = 'Bearer token' -DOCK_AUTHORIZATION= 'Bearer token' \ No newline at end of file +SUNBIRD_USER = string +SUNBIRD_PWD = string +SUNBIRD_GRANT = string +SUNBIRD_CLIENT = string +SUNBIRD_CLIENT_SECRET = string + +SUNBIRD_HOST = string +SUNBIRD_AUTHORIZATION = 'Bearer token' + +VDN_HOST = string +VDN_AUTHORIZATION= 'Bearer token' \ No newline at end of file From 8e8b680d11e673501b770bdfcc9c7b1029a70b17 Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Thu, 14 Jul 2022 19:03:26 +0530 Subject: [PATCH 11/36] updated question --- script/template/generate/gQuestion.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/template/generate/gQuestion.js b/script/template/generate/gQuestion.js index 60d82090..cb5b071c 100644 --- a/script/template/generate/gQuestion.js +++ b/script/template/generate/gQuestion.js @@ -101,7 +101,7 @@ const getSliderTemplate = (question) => { const getOptions = (options) => { options.map((values, index) => { - values.value = index; + values.value = `${values?.value}`; values.label = `${values.label}`; }); return options; From 635d87a4cd580b0135dba5d74f7d9454899af491 Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Thu, 14 Jul 2022 19:19:27 +0530 Subject: [PATCH 12/36] updated question --- script/template/helpers/hierarchyHelper.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/script/template/helpers/hierarchyHelper.js b/script/template/helpers/hierarchyHelper.js index 399585f6..a8db6263 100644 --- a/script/template/helpers/hierarchyHelper.js +++ b/script/template/helpers/hierarchyHelper.js @@ -37,7 +37,7 @@ const getPrecondition = (visible, parentId, parentQuestion) => { [getOperator(visible)]: [ { var: `${parentId}.response1.value`, - type: "responseDeclaration", + type: "interactions", }, findIndex(parentQuestion.options, { value: visible.value, From 5f32ed02a0a33371b5c2410efe25c17290a5d35a Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Fri, 22 Jul 2022 11:07:19 +0530 Subject: [PATCH 13/36] added matrix and updated logs --- script/server.js | 3 +- script/template/config/question.js | 24 +- script/template/generate/gQuestion.js | 27 +- script/template/generate/gQuestionSet.js | 526 +++++++------------ script/template/generate/template.js | 41 ++ script/template/helpers/hierarchyHelper.js | 41 +- script/template/helpers/questionsetHelper.js | 71 ++- script/template/migrate/common.js | 228 ++++++++ script/template/migrate/matrix.js | 453 ++++++++++++++++ script/template/migrate/nonmatrix.js | 358 +++++++++++++ 10 files changed, 1385 insertions(+), 387 deletions(-) create mode 100644 script/template/generate/template.js create mode 100644 script/template/migrate/common.js create mode 100644 script/template/migrate/matrix.js create mode 100644 script/template/migrate/nonmatrix.js diff --git a/script/server.js b/script/server.js index c0863637..62e77734 100644 --- a/script/server.js +++ b/script/server.js @@ -71,11 +71,10 @@ const migrateData = async (req, res) => { }; const db = await createDBInstance(); // req.query.questionsetID || - // const id = "5f3bc15416fdc4ed008171b1"; + // const id = "61a8b0a519c32c0007df55a8"; const data = await findAll("solutions", { // _id: ObjectId(id), - // $or: [{migratedId: {$eq: null}}, {migratedId:{$exists: false}}], programId: { $exists: true }, type: { $in: ["observation", "survey"] }, }); diff --git a/script/template/config/question.js b/script/template/config/question.js index 34c089c9..25564843 100644 --- a/script/template/config/question.js +++ b/script/template/config/question.js @@ -7,7 +7,7 @@ const questionTemplate = { interactionTypes: "responseType", showRemarks: "showRemarks", instructions: { - en: [null], + default: "tip", }, body: "question", editorState: { @@ -28,6 +28,9 @@ const questionTemplate = { }, }, hints: "hint", + evidence: { + mimeType: "file.type", + }, }, slider: { name: "Migrated Question-Slider Type", @@ -37,7 +40,7 @@ const questionTemplate = { interactionTypes: "responseType", showRemarks: "showRemarks", instructions: { - en: [null], + default: "tip", }, body: "question", editorState: { @@ -146,7 +149,7 @@ const questionTemplate = { question: "question", }, instructions: { - en: [null], + default: "tip", }, evidence: { mimeType: "file.type", @@ -224,6 +227,9 @@ const questionTemplate = { default: "tip", }, hints: "hint", + evidence: { + mimeType: "file.type", + }, }, text: { name: "Migrated Question", @@ -234,7 +240,7 @@ const questionTemplate = { showRemarks: "showRemarks", body: "question", instructions: { - en: [null], + default: "tip", }, editorState: { question: "question", @@ -261,17 +267,19 @@ const questionTemplate = { }, }, hints: "hint", + evidence: { + mimeType: "file.type", + }, }, }; const questionStatic = { - date: ["name", "mimeType", "instructions", "responseDeclaration"], - slider: ["name", "mimeType", "instructions", "responseDeclaration"], + date: ["name", "mimeType", "responseDeclaration"], + slider: ["name", "mimeType", "responseDeclaration"], multiselect: [ "name", "mimeType", - "instructions", "primaryCategory", "interactionTypes", "responseDeclaration", @@ -286,11 +294,9 @@ const questionStatic = { text: [ "name", "mimeType", - "instructions", "primaryCategory", "interactionTypes", "responseDeclaration", - "instructions" ] }; diff --git a/script/template/generate/gQuestion.js b/script/template/generate/gQuestion.js index cb5b071c..9d51b893 100644 --- a/script/template/generate/gQuestion.js +++ b/script/template/generate/gQuestion.js @@ -44,6 +44,12 @@ const getDateTemplate = (question) => { autoCapture: question["autoCapture"], }, }; + } else if (keyL === "evidence") { + template[key] = question["file"] + ? { ...question["file"], mimeType: question["file"]["type"] } + : { mimeType: [] }; + } else if (keyL === "instructions") { + template[key] = { default: question["tip"] }; } else if (keyL === 'showremarks') { template[key] = question[questionTemplate.date[key]] === true ? 'Yes' : 'No'; } @@ -88,9 +94,11 @@ const getSliderTemplate = (question) => { }; } else if (keyL === "evidence") { template[key] = question["file"] - ? { mimeType: question["file"]["type"] } + ? {...question["file"], mimeType: question["file"]["type"] } : { mimeType: [] }; - }else if (keyL === 'showremarks') { + } else if (keyL === "instructions") { + template[key] = { default: question["tip"] }; + } else if (keyL === 'showremarks') { template[key] = question[questionTemplate.date[key]] === true ? 'Yes' : 'No'; } else { template[key] = question[questionTemplate.slider[key]] || ""; @@ -101,6 +109,7 @@ const getSliderTemplate = (question) => { const getOptions = (options) => { options.map((values, index) => { + values.value = `${values?.value}`; values.label = `${values.label}`; }); @@ -149,9 +158,11 @@ const getMSMCQTemplate = (question) => { }; } else if (keyL === "evidence") { template[key] = question["file"] - ? { mimeType: question["file"]["type"] } + ? { ...question["file"], mimeType: question["file"]["type"] } : { mimeType: [] }; - }else if (keyL === 'showremarks') { + } else if (keyL === "instructions") { + template[key] = { default: question["tip"] }; + } else if (keyL === 'showremarks') { template[key] = question[questionTemplate.date[key]] === true ? 'Yes' : 'No'; } else { template[key] = question[questionTemplate.multiselect[key]] || ""; @@ -189,7 +200,7 @@ const getMCQTemplate = (question) => { }; } else if (keyL === "evidence") { template[key] = question["file"] - ? { mimeType: question["file"]["type"] } + ? { ...question["file"], mimeType: question["file"]["type"] } : { mimeType: [] }; } else if (keyL === "instructions") { template[key] = { default: question["tip"] }; @@ -241,6 +252,12 @@ const getTextTemplate = (question, type) => { }, }, }; + } else if (keyL === "evidence") { + template[key] = question["file"] + ? { ...question["file"], mimeType: question["file"]["type"] } + : { mimeType: [] }; + } else if (keyL === "instructions") { + template[key] = { default: question["tip"] }; } else if (keyL === 'showremarks') { template[key] = question[questionTemplate.date[key]] === true ? 'Yes' : 'No'; }else { diff --git a/script/template/generate/gQuestionSet.js b/script/template/generate/gQuestionSet.js index 679f1cfc..7ded652b 100644 --- a/script/template/generate/gQuestionSet.js +++ b/script/template/generate/gQuestionSet.js @@ -1,23 +1,15 @@ -const { isEmpty, uniq, get } = require("lodash"); const { ObjectId } = require("mongodb"); const { createQuestionSet } = require("../../api-list/question"); const { CONFIG } = require("../../constant/config"); const { findAll, updateById } = require("../../db"); const logger = require("../../logger"); -const { - updateHierarchyChildren, - getPrecondition, - updateHierarchyTemplate, -} = require("../helpers/hierarchyHelper"); -const { - isVisibleIfPresent, - isChildrenPresent, - setQuestionSetTemplate, - getQuestionFromDB, - createQuestionTemplate, -} = require("../helpers/questionsetHelper"); +const { updateHierarchyTemplate } = require("../helpers/hierarchyHelper"); +const { setQuestionSetTemplate } = require("../helpers/questionsetHelper"); const { createProgramTemplate } = require("./gProgram"); +const { getCriteriaData, initHierarchy } = require("../migrate/common"); +const { createSection } = require("./../migrate/matrix"); +const { getNonMatrixQuestions } = require("./../migrate/nonmatrix"); const getQuestionSetTemplates = async (solutions, migratedCount) => { const data = Promise.all( @@ -40,398 +32,252 @@ const getQuestionSetTemplates = async (solutions, migratedCount) => { ); console.log(); console.log("ProgramId", programId); + logger.debug( + `-----------------------sourcingProgramId---------------------- + ${programId}` + ); if (!programId) { return; } - return getQuestionSetTemplate(solution, programId, migratedCount); + return await migrateQuestionset(solution, programId, migratedCount); }) ); return data; }; -const getQuestionSetTemplate = async (solution, programId, migratedCount) => { +const migrateQuestionset = async (solution, programId, migratedCount) => { + logger.debug( + `-----------------------migrateQuestionset---------------------- + ${programId}` + ); let templateData = setQuestionSetTemplate(solution, programId); - - const questionsetid = solution._id.toString(); - - let hierarchy = { - questionsetDbId: questionsetid, - isHierarchyUpdated: solution.isHierarchyUpdated || false, - isBranchingUpdated: solution.isBranchingUpdated || false, - isPublished: solution.isPublished || false, - sourcingProgramId: programId, - isSrcProgramUpdated: solution.isSrcProgramUpdated || false, - isSrcProgramPublished: solution.isSrcProgramPublished || false, - isNominated: solution.isNominated || false, - isContributorAdded: solution.isContributorAdded || false, - isContributorAccepted: solution.isContributorAccepted || false, - criterias: [], - }; + const questionSetId = solution?._id.toString(); let questionSetMigratedId = solution.migratedId; - if (!questionSetMigratedId) { + if (questionSetMigratedId) { + migratedCount.success.questionSet.existing.migrated++; + } else { questionSetMigratedId = await createQuestionSet(templateData).catch( (err) => { - console.log(`Error while creating Questionset for solution_id: ${questionsetid} Error:`,err?.response?.data) - logger.error(`Error while creating Questionset for solution_id: ${questionsetid} Error: - ${JSON.stringify(err?.response?.data)}`); - - migratedCount.failed.questionSet.migrated.count++; + logger.error(`migrateQuestionset: Error while creating Questionset for solution_id: ${questionsetid} Error: + ${JSON.stringify(err?.response?.data)}`); if (!migratedCount.failed.questionSet.migrated.ids.includes(id)) { + migratedCount.failed.questionSet.migrated.count++; migratedCount.failed.questionSet.migrated.ids.push(id); } } ); + + logger.info( + `migrateQuestionset: questionSetMigratedId: + ${questionSetMigratedId}` + ); + + console.log(`migrateQuestionset: questionSetMigratedId: + ${questionSetMigratedId}`); + if (!questionSetMigratedId) { return; } - await updateById(CONFIG.DB.TABLES.solutions, questionsetid, { + await updateById(CONFIG.DB.TABLES.solutions, questionSetId, { migratedId: questionSetMigratedId, + }).catch((err) => { + logger.error( + `migrateQuestionset: Error while updating solution migratedId: + ${err}` + ); + console.log(`migrateQuestionset: Error while updating question: + ${err}`); }); migratedCount.success.questionSet.current.migrated++; + } - hierarchy = { - ...hierarchy, - questionset: questionSetMigratedId, - }; - } else { - migratedCount.success.questionSet.existing.migrated++; + let hierarchy = initHierarchy( + questionSetId, + solution, + programId, + questionSetMigratedId + ); + let matrixHierarchy = { criterias: [] }; - hierarchy = { - ...hierarchy, - questionset: questionSetMigratedId, - }; - } + let data = await migrateCriteriaQuestions( + solution, + hierarchy, + matrixHierarchy, + migratedCount + ); - if (solution.themes) { - for (let i = 0; i < solution.themes.length; i++) { - const theme = solution.themes[i]; - let criteriaArrayId = theme.criteria; - for (let j = 0; j < criteriaArrayId.length; j++) { - const criteria = criteriaArrayId[j]; - const criteriaId = criteria.criteriaId.toString(); - const criResult = await findAll(CONFIG.DB.TABLES.criteriaQuestions, { - _id: ObjectId(criteriaId), - }); - - const questions = criResult - ? criResult[0].evidences - ? criResult[0].evidences[0].sections[0].questions - : [] - : []; - - console.log(); - console.log( - "criResultcriResult", - criResult[0].name, - "questions.length", - questions.length - ); - console.log(); - - logger.info(`CriResult name:${criResult[0].name} criteria questions = ${questions.length}`) - - hierarchy.criterias.push({ - questions: [], - }); - const questionTemplates = await getQuestionTemplate( - questions, - hierarchy, - (index = j), - criResult[0], - solution.type, - criteriaId, - (criteriaMigratedId = criResult[0].migratedId || ""), - migratedCount - ); - hierarchy = questionTemplates.hierarchy; - } - } + for (let i = 0; i < data.matrixHierarchy.criterias.length; i++) { + const cri = data.matrixHierarchy.criterias[i]; + data.hierarchy.criterias.push(cri); } - await updateHierarchyTemplate(hierarchy, solution, programId, migratedCount); - return hierarchy; + console.log(); + console.log("migrateQuestionset", JSON.stringify(data.hierarchy)); + console.log(); + await updateHierarchyTemplate( + data.hierarchy, + solution, + programId, + migratedCount + ); + + return data.hierarchy; }; -const getQuestionTemplate = async ( - questions, +const migrateCriteriaQuestions = async ( + solution, hierarchy, - index, - criteria, - type, - criteriaId, - criteriaMigratedId, + matrixHierarchy, migratedCount ) => { - hierarchy.criterias[index] = { - migratedId: criteriaMigratedId, - criDbId: criteriaId, - code: criteria.externalId, - name: criteria.name, - description: criteria.description, - mimeType: "application/vnd.sunbird.questionset", - primaryCategory: type, - questions: [], - branchingLogic: {}, - allowMultipleInstances: "", - instances: {}, - }; - for (let i = 0; i < questions.length; i++) { - const question = questions[i]; - const questionId = question._id.toString(); - const templates = await getChildren( - questionId, - hierarchy, - index, - migratedCount - ); - } - - hierarchy.criterias[index].questions = uniq( - hierarchy.criterias[index].questions + logger.debug( + `migrateCriteriaQuestions: ${solution?._id}` ); - return { questions, hierarchy }; -}; + let criteriaIds = solution?.themes[0]?.criteria || []; + criteriaIds = criteriaIds.map((criteria) => ObjectId(criteria?.criteriaId)); + const criterias = await findAll("criteriaQuestions", { + _id: { $in: criteriaIds }, + }).catch((err) => {}); -const getChildren = async (questionId, hierarchy, index, migratedCount) => { - const question = await getQuestionFromDB(questionId); - const migratedId = await createQuestionTemplate(question, migratedCount); - const parentId = migratedId; - hierarchy = updateHierarchyChildren(hierarchy, migratedId, index); + for (let i = 0; i < criterias.length; i++) { + const criteria = criterias[i]; + let questionIds = criteria?.evidences[0].sections[0]?.questions || []; + questionIds = questionIds.map((question) => question?._id); - logger.info(`getChildren of questionId: ${questionId} and questiontype: ${question?.responseType}`) + const criteriaQuestions = await findAll("questions", { + _id: { $in: questionIds }, + }).catch((err) => {}); - if (question.responseType !== "matrix") { - if (!isChildrenPresent(question) && !isVisibleIfPresent(question)) { - hierarchy = updateHierarchyChildren(hierarchy, parentId, index); - return hierarchy; - } + hierarchy.criterias[i] = getCriteriaData(criteria, solution?.type); - if (isChildrenPresent(question) && !isVisibleIfPresent(question)) { - const data = await IfChildrenAndNoVisibleIf( - question, - hierarchy, - index, - parentId, - migratedCount - ); - } - - if (!isChildrenPresent(question) && isVisibleIfPresent(question)) { - return await IfNoChildrenAndVisibleIfAndInnerChildren( - question, - hierarchy, - index, - migratedCount - ); - } - } else if (question.responseType === "matrix") { - const instanceQuestions = question?.instanceQuestions; - if (hierarchy.criterias[index]) { - hierarchy.criterias[index] = { - ...hierarchy.criterias[index], - allowMultipleInstances: "Yes", - instances: { label: question.instanceIdentifier }, - }; - } + console.log(); + console.log( + "--------------------criteria----------------------", + criteria?.name + ); + console.log(); - const visible = !isEmpty(get(question, "visibleIf")) - ? question?.visibleIf[0] - : {}; - - let matrixParentId = ""; - let matrixParentQuestion = {}; - let matrixParentIdMigratedId = ""; - if (!isEmpty(visible)) { - matrixParentId = visible._id; - matrixParentQuestion = await getQuestionFromDB(matrixParentId); - matrixParentIdMigratedId = matrixParentQuestion?.migratedId || ""; - if (!matrixParentIdMigratedId) { - matrixParentIdMigratedId = await createQuestionTemplate( - question, - migratedCount - ); - } - hierarchy = updateHierarchyChildren( - hierarchy, - matrixParentIdMigratedId, - index - ); - } + logger.info( + `migrateCriteriaQuestions: --------------------criteria---------------------- ${criteria?.name}` + ); - for (let i = 0; i < instanceQuestions.length; i++) { - const instanceQuestionId = instanceQuestions[i]; - const insQuestion = await getQuestionFromDB(instanceQuestionId); - const insMigratedId = await createQuestionTemplate( - insQuestion, - migratedCount - ); + const data = await migrateQuestions( + (type = solution?.type), + criteriaQuestions, + hierarchy, + matrixHierarchy, + migratedCount, + (index = i), + (criteriaId = criteria?._id.toString()) + ); - hierarchy = updateHierarchyChildren(hierarchy, insMigratedId, index); - - if (!isEmpty(visible)) { - if ( - hierarchy.criterias[index].branchingLogic.hasOwnProperty( - matrixParentIdMigratedId - ) - ) { - hierarchy.criterias[index].branchingLogic[ - matrixParentIdMigratedId - ].target.push(insMigratedId); - - if (insMigratedId) { - hierarchy.criterias[index].branchingLogic[insMigratedId] = { - target: [], - preCondition: getPrecondition( - visible, - matrixParentIdMigratedId, - matrixParentQuestion - ), - source: [matrixParentIdMigratedId], - }; - } - } else { - { - hierarchy.criterias[index].branchingLogic[ - matrixParentIdMigratedId - ] = { - target: [insMigratedId], - preCondition: {}, - source: [], - }; - - if (insMigratedId) { - hierarchy.criterias[index].branchingLogic[insMigratedId] = { - target: [], - preCondition: getPrecondition( - visible, - matrixParentIdMigratedId, - matrixParentQuestion - ), - source: [matrixParentIdMigratedId], - }; - } - } - } - } - } + hierarchy = data.hierarchy; + matrixHierarchy = data.matrixHierarchy; } + return { hierarchy, matrixHierarchy }; }; -const IfChildrenAndNoVisibleIf = async ( - question, +const migrateQuestions = async ( + type, + questions, hierarchy, + matrixHierarchy, + migratedCount, index, - parentId, - migratedCount + criteriaId ) => { - let branching = { - [parentId]: { - target: [], - preCondition: {}, - source: [], - }, - }; - - if (question.children && question.children.length && !question.visibleIf) { - const children = question.children; - for (let i = 0; i < children.length; i++) { - const childId = children[i].toString(); - const childQuestion = await getQuestionFromDB(childId); - const migratedId = await createQuestionTemplate( - childQuestion, - migratedCount - ); - hierarchy = updateHierarchyChildren(hierarchy, migratedId, index); + let matrixQuestions = {}; + let nonMatrixQuestions = []; - if (migratedId) { - branching[parentId].target.push(migratedId); - } + console.log(); + console.log("migrateQuestions", questions.length); + console.log(); - if ( - !isChildrenPresent(childQuestion) && - isVisibleIfPresent(childQuestion) - ) { - const childBranching = await IfNoChildrenAndVisibleIf( - question, - parentId, - childQuestion, - migratedCount - ); - - branching = { - ...branching, - ...childBranching, - }; - } - } - } + logger.info( + `migrateQuestions: criteria:${criteriaId} questions: ${questions.length} ` + ); - hierarchy.criterias[index].branchingLogic = branching; - return hierarchy; -}; + for (let i = 0; i < questions.length; i++) { + const question = questions[i]; -const IfNoChildrenAndVisibleIf = async ( - parentQuestion, - parentId, - question, - migratedCount -) => { - const visibleIf = question.visibleIf || []; - let childBranching = {}; - for (let i = 0; i < visibleIf.length; i++) { - const visible = visibleIf[i]; - const childId = question._id.toString(); - const chiQuestion = await getQuestionFromDB(childId); - const migratedId = await createQuestionTemplate(chiQuestion, migratedCount); - if (migratedId) { - childBranching = { - [migratedId]: { - target: [], - preCondition: getPrecondition(visible, parentId, parentQuestion), - source: [parentId], - }, - }; - } - } - return childBranching; -}; + console.log(); + console.log( + "migrateQuestions question responsetype", + question?.responseType, + "qid", + question._id, -const IfNoChildrenAndVisibleIfAndInnerChildren = async ( - question, - hierarchy, - index, - migratedCount -) => { - const visibleIf = question.visibleIf || []; - for (let i = 0; i < visibleIf.length; i++) { - const visible = visibleIf[i]; - const parentId = visible._id.toString(); - const parentQuestion = await getQuestionFromDB(parentId); - const migratedId = await createQuestionTemplate( - parentQuestion, - migratedCount ); - hierarchy.criterias[index].questions.push(migratedId); - if ( - isChildrenPresent(parentQuestion) && - !isVisibleIfPresent(parentQuestion) - ) { - return await IfChildrenAndNoVisibleIf( - parentQuestion, + console.log(); + + logger.info( + `migrateQuestions: criteria:${criteriaId} question: ${question?._id} question responseType: ${question?.responseType} ` + ); + + + if (question?.responseType === "matrix") { + + + const data = await createSection( + type, + matrixHierarchy, + matrixQuestions, + questions, + criteriaId, + question, + migratedCount + ); + matrixQuestions = data.matrixQuestions; + matrixHierarchy = data.matrixHierarchy; + questions = data.questions; + + console.log(); + console.log("migrateQuestions createSection", matrixHierarchy); + console.log(); + } else { + console.log(); + console.log( + "==========migrateQuestions getNonMatrixQuestions============", + question?.responseType, + question?._id + ); + console.log(); + + const data = await getNonMatrixQuestions( + question, + questions, + nonMatrixQuestions, + matrixQuestions, + matrixHierarchy, hierarchy, index, - migratedId, - migratedCount + type, + migratedCount, + criteriaId ); + + console.log(); + console.log( + "==========migrateQuestions getNonMatrixQuestions end ============", + matrixHierarchy + ); + console.log(); + + hierarchy = data.hierarchy; + matrixHierarchy = data.matrixHierarchy; + matrixQuestions = data.matrixQuestions; + nonMatrixQuestions = data.nonMatrixQuestions; + questions = data.questions; } } + return { hierarchy, matrixHierarchy }; }; module.exports = { diff --git a/script/template/generate/template.js b/script/template/generate/template.js new file mode 100644 index 00000000..627c4762 --- /dev/null +++ b/script/template/generate/template.js @@ -0,0 +1,41 @@ +const getSectionTemplate = (section) => { + return { + _id: section._id, + __v: 0, + concepts: [], + createdAt: section?.createdAt, + createdFor: [], + criteriaType: "manual", + description: "Matrix section description", + evidences: [ + { + code: "OB", + sections: [ + { + code: "S1", + questions: [], + }, + ], + }, + ], + externalId: section?.externalId, + flag: "", + frameworkCriteriaId: "", + keywords: ["Keyword 1", "Keyword 2"], + language: ["English"], + name: "Matrix Section", + owner: "", + remarks: "", + resourceType: ["Program", "Framework", "Criteria"], + score: "", + showRemarks: null, + timesUsed: "", + updatedAt: section?.updatedAt, + weightage: "", + migratedId: null, + }; +}; + +module.exports = { + getSectionTemplate, +}; diff --git a/script/template/helpers/hierarchyHelper.js b/script/template/helpers/hierarchyHelper.js index a8db6263..38c22695 100644 --- a/script/template/helpers/hierarchyHelper.js +++ b/script/template/helpers/hierarchyHelper.js @@ -1,4 +1,12 @@ -const { pick, findIndex, get, compact, find, omit } = require("lodash"); +const { + pick, + findIndex, + get, + compact, + find, + omit, + isArray, +} = require("lodash"); const { publishQuestionSet, updateQuestionSetHierarchy, @@ -7,9 +15,12 @@ const { const { CONFIG } = require("../../constant/config"); const { updateById } = require("../../db"); const logger = require("../../logger"); -const { updateQuestionSetFailedCount } = require("./questionsetHelper"); const updateHierarchyChildren = (hierarchy, migratedId, index) => { + + logger.debug(`updateHierarchyChildren: migratedId = ${migratedId}`) + + if ( migratedId && !hierarchy.criterias[index].questions.includes(migratedId) @@ -31,6 +42,8 @@ const getOperator = (visibleIf) => { }; const getPrecondition = (visible, parentId, parentQuestion) => { + logger.debug(`getPrecondition: parentId = ${parentId}; visible: ${visible}`); + return { and: [ { @@ -40,7 +53,7 @@ const getPrecondition = (visible, parentId, parentQuestion) => { type: "interactions", }, findIndex(parentQuestion.options, { - value: visible.value, + value: isArray(visible?.value) ? visible?.value[0] : visible?.value, }), ], }, @@ -54,6 +67,9 @@ const updateHierarchyTemplate = async ( programId, migratedCount ) => { + logger.debug( + `updateHierarchyTemplate: programId = ${programId}; solution: ${solution?._id}` + ); const updateHierarchyData = { request: { data: { @@ -99,6 +115,13 @@ const updateHierarchyTemplate = async ( console.log("updateHierarchydata", JSON.stringify(updateHierarchyData)); console.log(); + + logger.info( + `updateHierarchyTemplate: Hierarchydata = ${JSON.stringify( + updateHierarchyData + )}` + ); + const questionsetId = hierarchy.questionsetDbId; let query = {}; if (!hierarchy.isHierarchyUpdated) { @@ -106,18 +129,17 @@ const updateHierarchyTemplate = async ( (err) => { logger.error(`Error while updating the questionset for solution_id: ${questionsetId} Error: ${JSON.stringify(err.response.data)}`); - - migratedCount.failed.questionSet.hierarchy.count++; + if ( !migratedCount.failed.questionSet.hierarchy.ids.includes( hierarchy?.questionset ) ) { + migratedCount.failed.questionSet.hierarchy.count++; migratedCount.failed.questionSet.hierarchy.ids.push( hierarchy?.questionset ); } - // updateQuestionSetFailedCount(migratedCount, "hierarchy", questionsetId); } ); @@ -146,18 +168,17 @@ const updateHierarchyTemplate = async ( (err) => { logger.error(`Error while updating the questionset branching for solution_id: ${questionsetId} Error: ${JSON.stringify(err.response.data)}`); - migratedCount.failed.questionSet.branching.count++; if ( !migratedCount.failed.questionSet.branching.ids.includes( hierarchy?.questionset ) ) { + migratedCount.failed.questionSet.branching.count++; migratedCount.failed.questionSet.branching.ids.push( hierarchy?.questionset ); } - // updateQuestionSetFailedCount(migratedCount, "branching", questionsetId); } ); if (!result) { @@ -178,18 +199,18 @@ const updateHierarchyTemplate = async ( hierarchy?.questionset } Error: ${JSON.stringify(err.response.data)}`); - migratedCount.failed.questionSet.published.count++; if ( !migratedCount.failed.questionSet.published.ids.includes( hierarchy?.questionset ) ) { + migratedCount.failed.questionSet.published.count++; + migratedCount.failed.questionSet.published.ids.push( hierarchy?.questionset ); } - // updateQuestionSetFailedCount(migratedCount, "published", hierarchy?.questionset); }); if (!res) { await updateSolutionsDb(query, questionsetId, migratedCount); diff --git a/script/template/helpers/questionsetHelper.js b/script/template/helpers/questionsetHelper.js index 8129a00f..66517965 100644 --- a/script/template/helpers/questionsetHelper.js +++ b/script/template/helpers/questionsetHelper.js @@ -42,12 +42,20 @@ const setQuestionSetTemplate = (solution, programId) => { }; const createQuestionTemplate = async (question, migratedCount) => { - const type = question.responseType; - let migratedId = question.migratedId; + + + console.log(); + console.log("createQuestionTemplate", question); + console.log(); + + const type = question?.responseType; + let migratedId = question?.migratedId; let query = {}; let questionToMigrate = {}; - if (!migratedId) { + let published = question?.isPublished; + + if (type && !migratedId) { if (type.toLowerCase() === "date") { questionToMigrate = getDateTemplate(question); } @@ -65,47 +73,68 @@ const createQuestionTemplate = async (question, migratedCount) => { } if (!isEmpty(questionToMigrate)) { + console.log(); + console.log( + "questionToMigratequestionToMigrate", + JSON.stringify(questionToMigrate) + ); + console.log(); + console.log("db Question", JSON.stringify(question)); + console.log(); + migratedId = await createQuestions(questionToMigrate, question._id); - query = { - ...query, - migratedId, - }; + console.log("migraysyys", migratedId); + question.migratedId = migratedId; + } } - if (!!migratedId && !question.isPublished) { + if (migratedId && !published) { const res = await publishQuestion(migratedId).catch((err) => { - migratedCount.failed.question.count++; if (!migratedCount.failed.question.ids.includes(migratedId)) { + migratedCount.failed.question.count++; migratedCount.failed.question.ids.push(migratedId); } - console.log(`Error while publishing the question for questionid: ${migratedId} questionId: ${question?._id} Error: - ${err.response.data}`); + logger.error(`Error while publishing the question for migratedId: ${migratedId} Error: ${JSON.stringify(err.response.data)}`); }); - console.log("createQuestion Template publish response",res , "migratedId", migratedId, "questionId", question?._id) - logger.info(`createQuestion Template publish response: ${res} , "migratedId" ${migratedId} questionId, ${question?._id}`); + logger.info( + `createQuestion Template publish response: ${res} , "migratedId" ${migratedId} questionId, ${question?._id}` + ); + + - if (!res) { - return migratedId; + if (res) { + question.isPublished = true; + published = true; + logger.info(`createQuestion Template published: ${migratedId}`); } + } + + + if (migratedId) { + question.migratedId = migratedId; - console.log("createQuestion Template published", migratedId) - logger.info(`createQuestion Template published: ${migratedId}`); query = { - ...query, - isPublished: true, + migratedId, + published: published, }; + } else { + query = { + ...query, + published + } } + - if (!isEmpty(query)) { + if (!isEmpty(query) && question) { await updateById(CONFIG.DB.TABLES.questions, question._id, { ...query, }); } - return migratedId; + return question; }; const updateSolutionById = async ({ id, query }) => { diff --git a/script/template/migrate/common.js b/script/template/migrate/common.js new file mode 100644 index 00000000..747866e5 --- /dev/null +++ b/script/template/migrate/common.js @@ -0,0 +1,228 @@ +const { isEmpty, compact } = require("lodash"); +const { getPrecondition } = require("../helpers/hierarchyHelper"); +const { + getQuestionFromDB, + createQuestionTemplate, +} = require("../helpers/questionsetHelper"); +const logger = require("./../../logger"); + +const initHierarchy = (questionsetid, solution, programId, migratedId) => { + return { + questionset: migratedId, + questionsetDbId: questionsetid, + isHierarchyUpdated: solution?.isHierarchyUpdated || false, + isBranchingUpdated: solution?.isBranchingUpdated || false, + isPublished: solution?.isPublished || false, + sourcingProgramId: programId, + isSrcProgramUpdated: solution?.isSrcProgramUpdated || false, + isSrcProgramPublished: solution?.isSrcProgramPublished || false, + isNominated: solution?.isNominated || false, + isContributorAdded: solution?.isContributorAdded || false, + isContributorAccepted: solution?.isContributorAccepted || false, + criterias: [], + }; +}; + +const getCriteriaData = (criteria, type, question = {}) => { + if (isEmpty(question)) { + return { + migratedId: "", + criDbId: criteria?._id.toString(), + code: criteria?.externalId, + name: criteria?.name, + description: criteria?.description, + mimeType: "application/vnd.sunbird.questionset", + primaryCategory: type, + questions: [], + branchingLogic: {}, + allowMultipleInstances: "", + instances: {}, + }; + } else { + return { + migratedId: "", + _id: criteria?._id, + criDbId: criteria?._id.toString(), + code: criteria?.externalId, + name: criteria?.name, + description: criteria?.description, + mimeType: "application/vnd.sunbird.questionset", + primaryCategory: type, + questions: [], + branchingLogic: {}, + allowMultipleInstances: "Yes", + instances: { label: question?.instanceIdentifier }, + }; + } +}; + +const isSectionMatched = (matrixQuestions, criteriaId, matrixId) => { + logger.debug( + `isSectionMatched: criteriaId = ${criteriaId} matrixId = ${matrixId}; matrixQuestions: ${matrixQuestions}` + ); + if (matrixQuestions.hasOwnProperty(criteriaId)) { + if (matrixQuestions[criteriaId].hasOwnProperty(matrixId)) return true; + } + return false; +}; + +const isQuestionMatched = (data, id) => { + + logger.debug(`isQuestionMatched: id = ${id}; `); + + for (let i = 0; i < data.length; i++) { + const qid = data[i]._id; + + if (qid.toString() === id.toString()) { + return data[i]; + } + } + + return false; +}; + +const isQuestionMatchedInMatrixQuestions = ( + matrixQuestions, + criteriaId, + id +) => { + + logger.debug( + `isQuestionMatchedInMatrixQuestions: criteriaId = ${criteriaId}; id = ${id}; matrixQuestions: ${matrixQuestions}` + ); + + if (matrixQuestions.hasOwnProperty(criteriaId)) { + const keys = Object.keys(matrixQuestions[criteriaId]); + + for (let i = 0; i < keys.length; i++) { + const que = matrixQuestions[criteriaId][keys[i]]; + const questions = que?.questions || []; + return isQuestionMatched(questions, id); + } + } + + return false; +}; + +const getQuestion = async (questions, questions2, id, migratedCount) => { + + let matched = isQuestionMatched(questions, id); + + let question = {}; + + if (matched === false) { + matched = isQuestionMatched(questions2, id); + } + + if (matched === false) { + question = await getQuestionFromDB(id); + } + + if (matched !== false && !isEmpty(matched)) { + question = matched; + } + + if (!isEmpty(question) && (!question?.migratedId || !question?.isPublished)) { + question = await createQuestionTemplate(question, migratedCount); + } + + logger.info( + `getQuestion: qid = ${id}; question: ${JSON.stringify(question)}` + ); + + return question; +}; + +const updateMatrixHierarchyBranching = ( + matrixHierarchy, + criteriaId, + matrixId, + parentId, + pQuestion, + child +) => { + const criterias = matrixHierarchy?.criterias || []; + + logger.debug( + `updateMatrixHierarchyBranching: criteriaId = ${criteriaId}; matrixId = ${matrixId}; parentId: ${parentId}; child: ${child}` + ); + + for (let i = 0; i < criterias; i++) { + const criteria = criterias[i]; + if (criteria._id.toString() === criteriaId.toString()) { + matrixHierarchy = updateHierarchyBranching( + matrixHierarchy, + i, + parentId, + pQuestion, + child + ); + } + } + + return matrixHierarchy; +}; + +const updateHierarchyBranching = ( + branching, + index, + parentId, + pQuestion, + child +) => { + const migratedId = child?.migratedId; + + const visible = child?.visibleIf ? child?.visibleIf[0] : {}; + + logger.debug( + `updateHierarchyBranching: migratedId = ${migratedId}; parentId = ${parentId}; visible: ${visible}` + ); + + if (!isEmpty(visible)) { + if (hasProperty(branching, index, parentId) && migratedId) { + if ( + !branching.criterias[index].branchingLogic[parentId].target.includes( + migratedId + ) + ) { + branching.criterias[index].branchingLogic[parentId].target.push( + migratedId + ); + } + + branching.criterias[index].branchingLogic[migratedId] = { + target: [], + preCondition: getPrecondition(visible, parentId, pQuestion), + source: [parentId], + }; + } else if (migratedId) { + branching.criterias[index].branchingLogic[parentId] = { + target: [migratedId], + preCondition: {}, + source: [], + }; + branching.criterias[index].branchingLogic[migratedId] = { + target: [], + preCondition: getPrecondition(visible, parentId, pQuestion), + source: [parentId], + }; + } + } + + return branching; +}; + +const hasProperty = (hierarchy, index, id) => { + return hierarchy.criterias[index].branchingLogic.hasOwnProperty(id); +}; + +module.exports = { + initHierarchy, + getCriteriaData, + isSectionMatched, + getQuestion, + isQuestionMatchedInMatrixQuestions, + isQuestionMatched, + updateMatrixHierarchyBranching, + updateHierarchyBranching, +}; diff --git a/script/template/migrate/matrix.js b/script/template/migrate/matrix.js new file mode 100644 index 00000000..7e7c57dc --- /dev/null +++ b/script/template/migrate/matrix.js @@ -0,0 +1,453 @@ +const { + isChildrenPresent, + isVisibleIfPresent, +} = require("../helpers/questionsetHelper"); +const { + getQuestion, + getCriteriaData, + isQuestionMatched, + isSectionMatched, + updateMatrixHierarchyBranching, +} = require("./common"); + +const { getSectionTemplate } = require("../generate/template"); +const { compact, isEmpty } = require("lodash"); +const logger = require("./../../logger"); + +const createSection = async ( + type, + matrixHierarchy, + matrixQuestions, + questions, + criteriaId, + matrixQue, + migratedCount +) => { + const matrixId = matrixQue?._id ? matrixQue?._id.toString() : ""; + logger.debug( + `createSection: criteria:${criteriaId} question: ${matrixQue?._id} question responseType: ${matrixQue?.responseType} ` + ); + + if (!isSectionMatched(matrixQuestions, criteriaId, matrixId)) { + const section = getSectionTemplate(matrixQue); + + const newCriteria = getCriteriaData(section, type, matrixQue); + + logger.info( + `createSection: criteria:${criteriaId} question: ${matrixQue?._id}: ------------------Matrix as New Criteria---------------- ${newCriteria?.name}` + ); + + matrixHierarchy.criterias.push(newCriteria); + + if (isEmpty(matrixQuestions)) { + matrixQuestions[criteriaId] = { [matrixId]: { questions: [] } }; + } else { + matrixQuestions[criteriaId][matrixId] = { questions: [] }; + } + + const visible = !isEmpty(matrixQue?.visibleIf) + ? matrixQue?.visibleIf[0] + : ""; + + if (visible) { + const parentId = visible?._id; + + let pQuestion = isQuestionMatched(questions, parentId); + + if (pQuestion !== false) { + for (let q = 0; q < questions.length; q++) { + const que = questions[q]; + if (que._id.toString() === parentId.toString()) { + const children = que.children.map((child) => + child.toString() !== matrixQue._id.toString() ? child : null + ); + que.children = compact(children); + } + questions[q] = que; + } + } + } + } + + const instanceQuestions = matrixQue?.instanceQuestions || []; + const instanceData = await getInstanceQuestions( + instanceQuestions, + matrixHierarchy, + questions, + matrixQuestions, + criteriaId, + matrixQue._id.toString(), + type, + migratedCount + ); + + matrixHierarchy = instanceData.matrixHierarchy; + matrixQuestions = instanceData.matrixQuestions; + questions = instanceData.questions; + + return { matrixHierarchy, matrixQuestions, questions }; +}; + +const getInstanceQuestions = async ( + instanceQuestions, + matrixHierarchy, + questions, + matrixQuestions, + criteriaId, + matrixId, + type, + migratedCount +) => { + logger.debug( + `getInstanceQuestions: criteria:${criteriaId} question: ${matrixId}` + ); + + for (let i = 0; i < instanceQuestions.length; i++) { + const childId = instanceQuestions[i]; + + let child = await getQuestion( + questions, + matrixQuestions[criteriaId][matrixId], + childId + ); + + const data = await getMatrixQuestions( + child, + questions, + matrixQuestions, + criteriaId, + matrixId, + matrixHierarchy, + type, + migratedCount + ); + questions = data.questions; + matrixQuestions = data.matrixQuestions; + matrixHierarchy = data.matrixHierarchy; + } + + return { matrixQuestions, matrixHierarchy, questions }; +}; + +const getMatrixQuestions = async ( + question, + questions, + matrixQuestions, + criteriaId, + matrixId, + matrixHierarchy, + type, + migratedCount +) => { + logger.debug( + `getMatrixQuestions: criteriaId = ${criteriaId}; matrixId = ${matrixId}; question: ${question?._id}` + ); + + if (!isChildrenPresent(question) && !isVisibleIfPresent(question)) { + console.log("getMatrixQuestions noChildrenAndnoVisibleIf ", question._id); + const data = noChildrenAndnoVisibleIf( + matrixQuestions, + criteriaId, + question, + matrixId, + matrixHierarchy, + questions + ); + + matrixHierarchy = data.matrixHierarchy; + matrixQuestions = data.matrixQuestions; + } else if (isChildrenPresent(question) && !isVisibleIfPresent(question)) { + const data = await childrenAndnoVisibleIf( + question, + questions, + matrixQuestions, + matrixHierarchy, + criteriaId, + matrixId, + type, + migratedCount + ); + + matrixHierarchy = data.matrixHierarchy; + matrixQuestions = data.matrixQuestions; + questions = data.questions; + } else if (!isChildrenPresent(question) && isVisibleIfPresent(question)) { + const data = await noChildrenAndVisibleIf( + question, + questions, + matrixQuestions, + matrixHierarchy, + criteriaId, + matrixId, + migratedCount, + type + ); + + questions = data.questions; + matrixHierarchy = data.matrixHierarchy; + matrixQuestions = data.matrixQuestions; + } + return { matrixHierarchy, matrixQuestions, questions }; +}; + +const noChildrenAndnoVisibleIf = ( + matrixQuestions, + criteriaId, + question, + matrixId, + matrixHierarchy, + questions +) => { + console.log(); + console.log("noChildrenAndnoVisibleIf"); + console.log(); + + logger.debug( + `noChildrenAndnoVisibleIf: criteriaId = ${criteriaId}; matrixId = ${matrixId}; question: ${question?._id}` + ); + + const data = updateMatrixHierarchyQuestions( + matrixQuestions, + criteriaId, + question, + matrixId, + matrixHierarchy, + questions + ); + + matrixHierarchy = data.matrixHierarchy; + matrixQuestions = data.matrixQuestions; + questions = data.questions; + + return { matrixHierarchy, matrixQuestions, questions }; +}; + +const childrenAndnoVisibleIf = async ( + question, + questions, + matrixQuestions, + matrixHierarchy, + criteriaId, + matrixId, + type, + migratedCount +) => { + const children = question?.children || []; + + logger.info( + `childrenAndnoVisibleIf: criteriaId = ${criteriaId} matrixId = ${matrixId} question = ${question?._id} children = ${children.length}` + ); + + const updatedData = updateMatrixHierarchyQuestions( + matrixQuestions, + criteriaId, + question, + matrixId, + matrixHierarchy, + questions + ); + matrixHierarchy = updatedData.matrixHierarchy; + matrixQuestions = updatedData.matrixQuestions; + questions = updatedData.questions; + + for (let i = 0; i < children.length; i++) { + const childId = children[i]; + let child = await getQuestion( + questions, + matrixQuestions[criteriaId][matrixId], + childId, + migratedCount + ); + + logger.info( + `childrenAndnoVisibleIf: criteriaId = ${criteriaId} matrixId = ${matrixId} childId = ${child?._id} child = ${child} responseType: ${child?.responseType}` + ); + + if (child.responseType === "matrix") { + const data = await createSection( + type, + matrixHierarchy, + matrixQuestions, + questions, + criteriaId, + question, + migratedCount + ); + matrixQuestions = data.matrixQuestions; + matrixHierarchy = data.matrixHierarchy; + questions = data.questions; + } else { + const matched = isQuestionMatched( + matrixQuestions[criteriaId][matrixId], + question._id + ); + + if (matched === false) { + matrixQuestions[criteriaId][matrixId].questions.push(child); + matrixHierarchy = updateMatrixHierarchy( + matrixHierarchy, + matrixId, + child?.migratedId, + criteriaId + ); + matrixHierarchy = updateMatrixHierarchyBranching( + matrixHierarchy, + criteriaId, + matrixId, + question?.migratedId, + question, + child + ); + } + } + } + + return { matrixHierarchy, matrixQuestions, questions }; +}; + +const noChildrenAndVisibleIf = async ( + question, + questions, + matrixQuestions, + matrixHierarchy, + criteriaId, + matrixId, + migratedCount, + type +) => { + const visible = question?.visibleIf ? question.visibleIf[0] : ""; + const parentId = visible._id; + const pQuestion = await getQuestion( + questions, + matrixQuestions[criteriaId][matrixId], + parentId, + migratedCount + ); + + logger.debug( + `noChildrenAndVisibleIf: criteriaId = ${criteriaId} matrixId = ${matrixId} questionId = ${question?._id} responseType: ${question?.responseType} visible = ${visible} ` + ); + + const updatedData = updateMatrixHierarchyQuestions( + matrixQuestions, + criteriaId, + question, + matrixId, + matrixHierarchy, + questions + ); + matrixHierarchy = updatedData.matrixHierarchy; + matrixQuestions = updatedData.matrixQuestions; + questions = updatedData.questions; + + const pMatched = isQuestionMatched( + matrixQuestions[criteriaId][matrixId], + pQuestion._id + ); + + if (pMatched === false) { + matrixQuestions[criteriaId][matrixId].questions.push(pQuestion); + matrixHierarchy = updateMatrixHierarchy( + matrixHierarchy, + matrixId, + pQuestion?.migratedId, + criteriaId + ); + matrixHierarchy = updateMatrixHierarchyBranching( + matrixHierarchy, + criteriaId, + matrixId, + pQuestion?.migratedId, + pQuestion, + question + ); + + const data = await getMatrixQuestions( + pQuestion, + questions, + matrixQuestions, + criteriaId, + matrixId, + matrixHierarchy, + type, + migratedCount + ); + questions = data.questions; + matrixQuestions = data.matrixQuestions; + matrixHierarchy = data.matrixHierarchy; + } + // } + + return { questions, matrixHierarchy, matrixQuestions }; +}; + +const updateMatrixHierarchy = ( + matrixHierarchy, + matrixId, + migratedId, + criteriaId +) => { + logger.debug( + `updateMatrixHierarchy: matrixId = ${matrixId}; migratedId = ${migratedId}` + ); + + const criterias = matrixHierarchy.criterias || []; + for (let i = 0; i < criterias.length; i++) { + const criteria = criterias[i]; + if (criteria?._id.toString() === matrixId) { + if (!criteria.questions.includes(migratedId)) { + criteria.questions.push(migratedId); + } + } + criterias[i] = criteria; + } + matrixHierarchy.criterias = criterias; + + logger.info( + `updateMatrixHierarchy: matrixHierarchy: ${JSON.stringify(matrixHierarchy)}` + ); + + return matrixHierarchy; +}; + +const updateMatrixHierarchyQuestions = ( + matrixQuestions, + criteriaId, + question, + matrixId, + matrixHierarchy, + questions +) => { + logger.debug( + `updateMatrixHierarchyQuestions: criteriaId = ${criteriaId}; matrixId = ${matrixId}; question: ${question?._id}: migratedId: ${question?.migratedId}` + ); + + const matched = isQuestionMatched( + matrixQuestions[criteriaId][matrixId], + question._id + ); + + logger.info( + `updateMatrixHierarchyQuestions: isQuestionMatched: matched = ${ + matched === false ? matched : true + };` + ); + + if (matched === false) { + matrixQuestions[criteriaId][matrixId].questions.push(question); + matrixHierarchy = updateMatrixHierarchy( + matrixHierarchy, + matrixId, + question?.migratedId, + criteriaId + ); + } + + console.log("updateMatrixHierarchyQuestions ", matrixHierarchy); + + return { matrixHierarchy, matrixQuestions, questions }; +}; + +module.exports = { + createSection, +}; diff --git a/script/template/migrate/nonmatrix.js b/script/template/migrate/nonmatrix.js new file mode 100644 index 00000000..157f6771 --- /dev/null +++ b/script/template/migrate/nonmatrix.js @@ -0,0 +1,358 @@ +const { + isChildrenPresent, + isVisibleIfPresent, +} = require("../helpers/questionsetHelper"); +const { + isQuestionMatchedInMatrixQuestions, + getQuestion, + isQuestionMatched, + updateHierarchyBranching, +} = require("./common"); + +const { + updateHierarchyChildren, +} = require("../helpers/hierarchyHelper"); +const { createSection } = require("./matrix"); + +const logger = require("./../../logger"); + + +const updateNonMatrixHierarchyChildren = ( + question, + nonMatrixQuestions, + matrixQuestions, + hierarchy, + index, + criteriaId, + questions, + matrixHierarchy +) => { + + logger.debug(`updateNonMatrixHierarchyChildren: question = ${question?._id} criteriaId=${criteriaId}`) + + let matched = isQuestionMatched(nonMatrixQuestions, question._id); + + if (matched === false) { + matched = isQuestionMatchedInMatrixQuestions( + matrixQuestions, + criteriaId, + question._id + ); + + if (matched === false) { + hierarchy = updateHierarchyChildren( + hierarchy, + question?.migratedId, + index + ); + nonMatrixQuestions.push(question); + } + } + + + return { + hierarchy, + nonMatrixQuestions, + questions, + matrixQuestions, + matrixHierarchy, + }; +}; + +const getNonMatrixQuestions = async ( + question, + questions, + nonMatrixQuestions, + matrixQuestions, + matrixHierarchy, + hierarchy, + index, + type, + migratedCount, + criteriaId +) => { + logger.debug(`getNonMatrixQuestions: criteriaId = ${criteriaId} question = ${question?._id}`) + + question = await getQuestion( + questions, + nonMatrixQuestions, + question._id, + migratedCount + ); + + console.log("getNonMatrixQuestions", question); + + + if (!isChildrenPresent(question) && !isVisibleIfPresent(question)) { + + console.log("getNonMatrixQuestions nonMatrix NoChildrenAndNoVisibleIf", ) + + const data = nonMatrixNoChildrenAndNoVisibleIf( + question, + nonMatrixQuestions, + matrixQuestions, + hierarchy, + index, + criteriaId, + questions, + matrixHierarchy + ); + + nonMatrixQuestions = data.nonMatrixQuestions; + hierarchy = data.hierarchy; + questions = data.questions; + matrixQuestions = data.matrixQuestions; + matrixHierarchy = data.matrixHierarchy; + + console.log("getNonMatrixQuestions nonMatrix NoChildrenAndNoVisibleIf", ) + + } else if (isChildrenPresent(question) && !isVisibleIfPresent(question)) { + + console.log("getNonMatrixQuestions nonMatrix ChildrenAndNoVisibleIf", ) + + const data = await nonMatrixChildrenAndNoVisibleIf( + question, + questions, + nonMatrixQuestions, + matrixQuestions, + matrixHierarchy, + hierarchy, + index, + type, + migratedCount, + criteriaId + ); + hierarchy = data.hierarchy; + matrixHierarchy = data.matrixHierarchy; + matrixQuestions = data.matrixQuestions; + nonMatrixQuestions = data.nonMatrixQuestions; + questions = data.questions; + + } else if (!isChildrenPresent(question) && isVisibleIfPresent(question)) { + + console.log("getNonMatrixQuestions nonMatrix NoChildrenAndVisibleIf", ) + + + const data = await nonMatrixNoChildrenAndVisibleIf( + question, + questions, + nonMatrixQuestions, + matrixQuestions, + matrixHierarchy, + hierarchy, + index, + type, + migratedCount, + criteriaId + ); + hierarchy = data.hierarchy; + matrixHierarchy = data.matrixHierarchy; + matrixQuestions = data.matrixQuestions; + nonMatrixQuestions = data.nonMatrixQuestions; + questions = data.questions; + + } + + return { + hierarchy, + matrixHierarchy, + matrixQuestions, + nonMatrixQuestions, + questions, + }; +}; + +const nonMatrixNoChildrenAndNoVisibleIf = ( + question, + nonMatrixQuestions, + matrixQuestions, + hierarchy, + index, + criteriaId, + questions, + matrixHierarchy +) => { + + logger.debug(`nonMatrix NoChildrenAndNoVisibleIf: criteriaId = ${criteriaId} question = ${question?._id}`) + + return updateNonMatrixHierarchyChildren( + question, + nonMatrixQuestions, + matrixQuestions, + hierarchy, + index, + criteriaId, + questions, + matrixHierarchy + ); +}; + +const nonMatrixChildrenAndNoVisibleIf = async ( + question, + questions, + nonMatrixQuestions, + matrixQuestions, + matrixHierarchy, + hierarchy, + index, + type, + migratedCount, + criteriaId +) => { + + logger.debug(`nonMatrix ChildrenAndNoVisibleIf: criteriaId = ${criteriaId} question = ${question?._id}`) + + const data = updateNonMatrixHierarchyChildren( + question, + nonMatrixQuestions, + matrixQuestions, + hierarchy, + index, + criteriaId, + questions, + matrixHierarchy + ); + nonMatrixQuestions = data.nonMatrixQuestions; + hierarchy = data.hierarchy; + + const children = question?.children || []; + + for (let i = 0; i < children.length; i++) { + const childId = children[i]; + + let child = await getQuestion( + questions, + nonMatrixQuestions, + childId, + migratedCount + ); + + logger.info(`nonMatrix ChildrenAndNoVisibleIf: criteriaId = ${criteriaId} question = ${question?._id} child: ${child?._id} child responseType: ${child?.responseType}`) + + + if (child.responseType === "matrix") { + const data = await createSection( + type, + matrixHierarchy, + matrixQuestions, + questions, + criteriaId, + child, + migratedCount + ); + matrixQuestions = data.matrixQuestions; + matrixHierarchy = data.matrixHierarchy; + questions = data.questions; + } else { + + hierarchy = updateHierarchyBranching( + hierarchy, + index, + question?.migratedId, + question, + child + ); + + } + } + + + return { + hierarchy, + matrixHierarchy, + matrixQuestions, + nonMatrixQuestions, + questions, + }; +}; + +const nonMatrixNoChildrenAndVisibleIf = async ( + question, + questions, + nonMatrixQuestions, + matrixQuestions, + matrixHierarchy, + hierarchy, + index, + type, + migratedCount, + criteriaId +) => { + + const visible = question?.visibleIf ? question.visibleIf[0] : ""; + const parentId = visible._id; + const pQuestion = await getQuestion( + questions, + nonMatrixQuestions, + parentId, + migratedCount + ); + logger.debug(`nonMatrix NoChildrenAndVisibleIf: criteriaId = ${criteriaId} question = ${question?._id} question visibleif= ${visible}`) + + const data = updateNonMatrixHierarchyChildren( + question, + nonMatrixQuestions, + matrixQuestions, + hierarchy, + index, + criteriaId, + questions, + matrixHierarchy + ); + nonMatrixQuestions = data.nonMatrixQuestions; + hierarchy = data.hierarchy; + + if (pQuestion.responseType === "matrix") { + const data = await createSection( + type, + matrixHierarchy, + matrixQuestions, + questions, + criteriaId, + pQuestion, + migratedCount + ); + matrixQuestions = data.matrixQuestions; + matrixHierarchy = data.matrixHierarchy; + questions = data.questions; + + } else { + hierarchy = updateHierarchyBranching( + hierarchy, + index, + pQuestion?.migratedId, + pQuestion, + question + ); + const data = await getNonMatrixQuestions( + pQuestion, + questions, + nonMatrixQuestions, + matrixQuestions, + matrixHierarchy, + hierarchy, + index, + type, + migratedCount, + criteriaId + ); + // return data; + hierarchy = data.hierarchy; + matrixHierarchy = data.matrixHierarchy; + matrixQuestions = data.matrixQuestions; + nonMatrixQuestions = data.nonMatrixQuestions; + questions = data.questions; + } + + return { + hierarchy, + matrixHierarchy, + matrixQuestions, + nonMatrixQuestions, + questions, + }; +}; + +module.exports = { + getNonMatrixQuestions, +}; From 90d4f8e4775c2d10e6fb896620aa0995341e76d7 Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Fri, 22 Jul 2022 11:09:06 +0530 Subject: [PATCH 14/36] added matrix and updated logs --- script/template/config/question.js | 24 +- script/template/generate/gQuestion.js | 27 +- script/template/generate/gQuestionSet.js | 526 +++++++------------ script/template/generate/template.js | 42 ++ script/template/helpers/hierarchyHelper.js | 37 +- script/template/helpers/questionsetHelper.js | 71 ++- script/template/migrate/common.js | 228 ++++++++ script/template/migrate/matrix.js | 479 +++++++++++++++++ script/template/migrate/nonmatrix.js | 377 +++++++++++++ 9 files changed, 1426 insertions(+), 385 deletions(-) create mode 100644 script/template/generate/template.js create mode 100644 script/template/migrate/common.js create mode 100644 script/template/migrate/matrix.js create mode 100644 script/template/migrate/nonmatrix.js diff --git a/script/template/config/question.js b/script/template/config/question.js index 34c089c9..25564843 100644 --- a/script/template/config/question.js +++ b/script/template/config/question.js @@ -7,7 +7,7 @@ const questionTemplate = { interactionTypes: "responseType", showRemarks: "showRemarks", instructions: { - en: [null], + default: "tip", }, body: "question", editorState: { @@ -28,6 +28,9 @@ const questionTemplate = { }, }, hints: "hint", + evidence: { + mimeType: "file.type", + }, }, slider: { name: "Migrated Question-Slider Type", @@ -37,7 +40,7 @@ const questionTemplate = { interactionTypes: "responseType", showRemarks: "showRemarks", instructions: { - en: [null], + default: "tip", }, body: "question", editorState: { @@ -146,7 +149,7 @@ const questionTemplate = { question: "question", }, instructions: { - en: [null], + default: "tip", }, evidence: { mimeType: "file.type", @@ -224,6 +227,9 @@ const questionTemplate = { default: "tip", }, hints: "hint", + evidence: { + mimeType: "file.type", + }, }, text: { name: "Migrated Question", @@ -234,7 +240,7 @@ const questionTemplate = { showRemarks: "showRemarks", body: "question", instructions: { - en: [null], + default: "tip", }, editorState: { question: "question", @@ -261,17 +267,19 @@ const questionTemplate = { }, }, hints: "hint", + evidence: { + mimeType: "file.type", + }, }, }; const questionStatic = { - date: ["name", "mimeType", "instructions", "responseDeclaration"], - slider: ["name", "mimeType", "instructions", "responseDeclaration"], + date: ["name", "mimeType", "responseDeclaration"], + slider: ["name", "mimeType", "responseDeclaration"], multiselect: [ "name", "mimeType", - "instructions", "primaryCategory", "interactionTypes", "responseDeclaration", @@ -286,11 +294,9 @@ const questionStatic = { text: [ "name", "mimeType", - "instructions", "primaryCategory", "interactionTypes", "responseDeclaration", - "instructions" ] }; diff --git a/script/template/generate/gQuestion.js b/script/template/generate/gQuestion.js index cb5b071c..9d51b893 100644 --- a/script/template/generate/gQuestion.js +++ b/script/template/generate/gQuestion.js @@ -44,6 +44,12 @@ const getDateTemplate = (question) => { autoCapture: question["autoCapture"], }, }; + } else if (keyL === "evidence") { + template[key] = question["file"] + ? { ...question["file"], mimeType: question["file"]["type"] } + : { mimeType: [] }; + } else if (keyL === "instructions") { + template[key] = { default: question["tip"] }; } else if (keyL === 'showremarks') { template[key] = question[questionTemplate.date[key]] === true ? 'Yes' : 'No'; } @@ -88,9 +94,11 @@ const getSliderTemplate = (question) => { }; } else if (keyL === "evidence") { template[key] = question["file"] - ? { mimeType: question["file"]["type"] } + ? {...question["file"], mimeType: question["file"]["type"] } : { mimeType: [] }; - }else if (keyL === 'showremarks') { + } else if (keyL === "instructions") { + template[key] = { default: question["tip"] }; + } else if (keyL === 'showremarks') { template[key] = question[questionTemplate.date[key]] === true ? 'Yes' : 'No'; } else { template[key] = question[questionTemplate.slider[key]] || ""; @@ -101,6 +109,7 @@ const getSliderTemplate = (question) => { const getOptions = (options) => { options.map((values, index) => { + values.value = `${values?.value}`; values.label = `${values.label}`; }); @@ -149,9 +158,11 @@ const getMSMCQTemplate = (question) => { }; } else if (keyL === "evidence") { template[key] = question["file"] - ? { mimeType: question["file"]["type"] } + ? { ...question["file"], mimeType: question["file"]["type"] } : { mimeType: [] }; - }else if (keyL === 'showremarks') { + } else if (keyL === "instructions") { + template[key] = { default: question["tip"] }; + } else if (keyL === 'showremarks') { template[key] = question[questionTemplate.date[key]] === true ? 'Yes' : 'No'; } else { template[key] = question[questionTemplate.multiselect[key]] || ""; @@ -189,7 +200,7 @@ const getMCQTemplate = (question) => { }; } else if (keyL === "evidence") { template[key] = question["file"] - ? { mimeType: question["file"]["type"] } + ? { ...question["file"], mimeType: question["file"]["type"] } : { mimeType: [] }; } else if (keyL === "instructions") { template[key] = { default: question["tip"] }; @@ -241,6 +252,12 @@ const getTextTemplate = (question, type) => { }, }, }; + } else if (keyL === "evidence") { + template[key] = question["file"] + ? { ...question["file"], mimeType: question["file"]["type"] } + : { mimeType: [] }; + } else if (keyL === "instructions") { + template[key] = { default: question["tip"] }; } else if (keyL === 'showremarks') { template[key] = question[questionTemplate.date[key]] === true ? 'Yes' : 'No'; }else { diff --git a/script/template/generate/gQuestionSet.js b/script/template/generate/gQuestionSet.js index 679f1cfc..d2ef8705 100644 --- a/script/template/generate/gQuestionSet.js +++ b/script/template/generate/gQuestionSet.js @@ -1,23 +1,15 @@ -const { isEmpty, uniq, get } = require("lodash"); const { ObjectId } = require("mongodb"); const { createQuestionSet } = require("../../api-list/question"); const { CONFIG } = require("../../constant/config"); const { findAll, updateById } = require("../../db"); const logger = require("../../logger"); -const { - updateHierarchyChildren, - getPrecondition, - updateHierarchyTemplate, -} = require("../helpers/hierarchyHelper"); -const { - isVisibleIfPresent, - isChildrenPresent, - setQuestionSetTemplate, - getQuestionFromDB, - createQuestionTemplate, -} = require("../helpers/questionsetHelper"); +const { updateHierarchyTemplate } = require("../helpers/hierarchyHelper"); +const { setQuestionSetTemplate } = require("../helpers/questionsetHelper"); const { createProgramTemplate } = require("./gProgram"); +const { getCriteriaData, initHierarchy } = require("./../migrate/common"); +const { createSection } = require("./../migrate/matrix"); +const { getNonMatrixQuestions } = require("./../migrate/nonmatrix"); const getQuestionSetTemplates = async (solutions, migratedCount) => { const data = Promise.all( @@ -40,398 +32,252 @@ const getQuestionSetTemplates = async (solutions, migratedCount) => { ); console.log(); console.log("ProgramId", programId); + logger.debug( + `-----------------------sourcingProgramId---------------------- + ${programId}` + ); if (!programId) { return; } - return getQuestionSetTemplate(solution, programId, migratedCount); + return await migrateQuestionset(solution, programId, migratedCount); }) ); return data; }; -const getQuestionSetTemplate = async (solution, programId, migratedCount) => { +const migrateQuestionset = async (solution, programId, migratedCount) => { + logger.debug( + `-----------------------migrateQuestionset---------------------- + ${programId}` + ); let templateData = setQuestionSetTemplate(solution, programId); - - const questionsetid = solution._id.toString(); - - let hierarchy = { - questionsetDbId: questionsetid, - isHierarchyUpdated: solution.isHierarchyUpdated || false, - isBranchingUpdated: solution.isBranchingUpdated || false, - isPublished: solution.isPublished || false, - sourcingProgramId: programId, - isSrcProgramUpdated: solution.isSrcProgramUpdated || false, - isSrcProgramPublished: solution.isSrcProgramPublished || false, - isNominated: solution.isNominated || false, - isContributorAdded: solution.isContributorAdded || false, - isContributorAccepted: solution.isContributorAccepted || false, - criterias: [], - }; + const questionSetId = solution?._id.toString(); let questionSetMigratedId = solution.migratedId; - if (!questionSetMigratedId) { + if (questionSetMigratedId) { + migratedCount.success.questionSet.existing.migrated++; + } else { questionSetMigratedId = await createQuestionSet(templateData).catch( (err) => { - console.log(`Error while creating Questionset for solution_id: ${questionsetid} Error:`,err?.response?.data) - logger.error(`Error while creating Questionset for solution_id: ${questionsetid} Error: - ${JSON.stringify(err?.response?.data)}`); - - migratedCount.failed.questionSet.migrated.count++; + logger.error(`migrateQuestionset: Error while creating Questionset for solution_id: ${questionsetid} Error: + ${JSON.stringify(err?.response?.data)}`); if (!migratedCount.failed.questionSet.migrated.ids.includes(id)) { + migratedCount.failed.questionSet.migrated.count++; migratedCount.failed.questionSet.migrated.ids.push(id); } } ); + + logger.info( + `migrateQuestionset: questionSetMigratedId: + ${questionSetMigratedId}` + ); + + console.log(`migrateQuestionset: questionSetMigratedId: + ${questionSetMigratedId}`); + if (!questionSetMigratedId) { return; } - await updateById(CONFIG.DB.TABLES.solutions, questionsetid, { + await updateById(CONFIG.DB.TABLES.solutions, questionSetId, { migratedId: questionSetMigratedId, + }).catch((err) => { + logger.error( + `migrateQuestionset: Error while updating solution migratedId: + ${err}` + ); + console.log(`migrateQuestionset: Error while updating question: + ${err}`); }); migratedCount.success.questionSet.current.migrated++; + } - hierarchy = { - ...hierarchy, - questionset: questionSetMigratedId, - }; - } else { - migratedCount.success.questionSet.existing.migrated++; + let hierarchy = initHierarchy( + questionSetId, + solution, + programId, + questionSetMigratedId + ); + let matrixHierarchy = { criterias: [] }; - hierarchy = { - ...hierarchy, - questionset: questionSetMigratedId, - }; - } + let data = await migrateCriteriaQuestions( + solution, + hierarchy, + matrixHierarchy, + migratedCount + ); - if (solution.themes) { - for (let i = 0; i < solution.themes.length; i++) { - const theme = solution.themes[i]; - let criteriaArrayId = theme.criteria; - for (let j = 0; j < criteriaArrayId.length; j++) { - const criteria = criteriaArrayId[j]; - const criteriaId = criteria.criteriaId.toString(); - const criResult = await findAll(CONFIG.DB.TABLES.criteriaQuestions, { - _id: ObjectId(criteriaId), - }); - - const questions = criResult - ? criResult[0].evidences - ? criResult[0].evidences[0].sections[0].questions - : [] - : []; - - console.log(); - console.log( - "criResultcriResult", - criResult[0].name, - "questions.length", - questions.length - ); - console.log(); - - logger.info(`CriResult name:${criResult[0].name} criteria questions = ${questions.length}`) - - hierarchy.criterias.push({ - questions: [], - }); - const questionTemplates = await getQuestionTemplate( - questions, - hierarchy, - (index = j), - criResult[0], - solution.type, - criteriaId, - (criteriaMigratedId = criResult[0].migratedId || ""), - migratedCount - ); - hierarchy = questionTemplates.hierarchy; - } - } + for (let i = 0; i < data.matrixHierarchy.criterias.length; i++) { + const cri = data.matrixHierarchy.criterias[i]; + data.hierarchy.criterias.push(cri); } - await updateHierarchyTemplate(hierarchy, solution, programId, migratedCount); - return hierarchy; + console.log(); + console.log("migrateQuestionset", JSON.stringify(data.hierarchy)); + console.log(); + await updateHierarchyTemplate( + data.hierarchy, + solution, + programId, + migratedCount + ); + + return data.hierarchy; }; -const getQuestionTemplate = async ( - questions, +const migrateCriteriaQuestions = async ( + solution, hierarchy, - index, - criteria, - type, - criteriaId, - criteriaMigratedId, + matrixHierarchy, migratedCount ) => { - hierarchy.criterias[index] = { - migratedId: criteriaMigratedId, - criDbId: criteriaId, - code: criteria.externalId, - name: criteria.name, - description: criteria.description, - mimeType: "application/vnd.sunbird.questionset", - primaryCategory: type, - questions: [], - branchingLogic: {}, - allowMultipleInstances: "", - instances: {}, - }; - for (let i = 0; i < questions.length; i++) { - const question = questions[i]; - const questionId = question._id.toString(); - const templates = await getChildren( - questionId, - hierarchy, - index, - migratedCount - ); - } - - hierarchy.criterias[index].questions = uniq( - hierarchy.criterias[index].questions + logger.debug( + `migrateCriteriaQuestions: ${solution?._id}` ); - return { questions, hierarchy }; -}; + let criteriaIds = solution?.themes[0]?.criteria || []; + criteriaIds = criteriaIds.map((criteria) => ObjectId(criteria?.criteriaId)); + const criterias = await findAll("criteriaQuestions", { + _id: { $in: criteriaIds }, + }).catch((err) => {}); -const getChildren = async (questionId, hierarchy, index, migratedCount) => { - const question = await getQuestionFromDB(questionId); - const migratedId = await createQuestionTemplate(question, migratedCount); - const parentId = migratedId; - hierarchy = updateHierarchyChildren(hierarchy, migratedId, index); + for (let i = 0; i < criterias.length; i++) { + const criteria = criterias[i]; + let questionIds = criteria?.evidences[0].sections[0]?.questions || []; + questionIds = questionIds.map((question) => question?._id); - logger.info(`getChildren of questionId: ${questionId} and questiontype: ${question?.responseType}`) + const criteriaQuestions = await findAll("questions", { + _id: { $in: questionIds }, + }).catch((err) => {}); - if (question.responseType !== "matrix") { - if (!isChildrenPresent(question) && !isVisibleIfPresent(question)) { - hierarchy = updateHierarchyChildren(hierarchy, parentId, index); - return hierarchy; - } + hierarchy.criterias[i] = getCriteriaData(criteria, solution?.type); - if (isChildrenPresent(question) && !isVisibleIfPresent(question)) { - const data = await IfChildrenAndNoVisibleIf( - question, - hierarchy, - index, - parentId, - migratedCount - ); - } - - if (!isChildrenPresent(question) && isVisibleIfPresent(question)) { - return await IfNoChildrenAndVisibleIfAndInnerChildren( - question, - hierarchy, - index, - migratedCount - ); - } - } else if (question.responseType === "matrix") { - const instanceQuestions = question?.instanceQuestions; - if (hierarchy.criterias[index]) { - hierarchy.criterias[index] = { - ...hierarchy.criterias[index], - allowMultipleInstances: "Yes", - instances: { label: question.instanceIdentifier }, - }; - } + console.log(); + console.log( + "--------------------criteria----------------------", + criteria?.name + ); + console.log(); - const visible = !isEmpty(get(question, "visibleIf")) - ? question?.visibleIf[0] - : {}; - - let matrixParentId = ""; - let matrixParentQuestion = {}; - let matrixParentIdMigratedId = ""; - if (!isEmpty(visible)) { - matrixParentId = visible._id; - matrixParentQuestion = await getQuestionFromDB(matrixParentId); - matrixParentIdMigratedId = matrixParentQuestion?.migratedId || ""; - if (!matrixParentIdMigratedId) { - matrixParentIdMigratedId = await createQuestionTemplate( - question, - migratedCount - ); - } - hierarchy = updateHierarchyChildren( - hierarchy, - matrixParentIdMigratedId, - index - ); - } + logger.info( + `migrateCriteriaQuestions: --------------------criteria---------------------- ${criteria?.name}` + ); - for (let i = 0; i < instanceQuestions.length; i++) { - const instanceQuestionId = instanceQuestions[i]; - const insQuestion = await getQuestionFromDB(instanceQuestionId); - const insMigratedId = await createQuestionTemplate( - insQuestion, - migratedCount - ); + const data = await migrateQuestions( + (type = solution?.type), + criteriaQuestions, + hierarchy, + matrixHierarchy, + migratedCount, + (index = i), + (criteriaId = criteria?._id.toString()) + ); - hierarchy = updateHierarchyChildren(hierarchy, insMigratedId, index); - - if (!isEmpty(visible)) { - if ( - hierarchy.criterias[index].branchingLogic.hasOwnProperty( - matrixParentIdMigratedId - ) - ) { - hierarchy.criterias[index].branchingLogic[ - matrixParentIdMigratedId - ].target.push(insMigratedId); - - if (insMigratedId) { - hierarchy.criterias[index].branchingLogic[insMigratedId] = { - target: [], - preCondition: getPrecondition( - visible, - matrixParentIdMigratedId, - matrixParentQuestion - ), - source: [matrixParentIdMigratedId], - }; - } - } else { - { - hierarchy.criterias[index].branchingLogic[ - matrixParentIdMigratedId - ] = { - target: [insMigratedId], - preCondition: {}, - source: [], - }; - - if (insMigratedId) { - hierarchy.criterias[index].branchingLogic[insMigratedId] = { - target: [], - preCondition: getPrecondition( - visible, - matrixParentIdMigratedId, - matrixParentQuestion - ), - source: [matrixParentIdMigratedId], - }; - } - } - } - } - } + hierarchy = data.hierarchy; + matrixHierarchy = data.matrixHierarchy; } + return { hierarchy, matrixHierarchy }; }; -const IfChildrenAndNoVisibleIf = async ( - question, +const migrateQuestions = async ( + type, + questions, hierarchy, + matrixHierarchy, + migratedCount, index, - parentId, - migratedCount + criteriaId ) => { - let branching = { - [parentId]: { - target: [], - preCondition: {}, - source: [], - }, - }; - - if (question.children && question.children.length && !question.visibleIf) { - const children = question.children; - for (let i = 0; i < children.length; i++) { - const childId = children[i].toString(); - const childQuestion = await getQuestionFromDB(childId); - const migratedId = await createQuestionTemplate( - childQuestion, - migratedCount - ); - hierarchy = updateHierarchyChildren(hierarchy, migratedId, index); + let matrixQuestions = {}; + let nonMatrixQuestions = []; - if (migratedId) { - branching[parentId].target.push(migratedId); - } + console.log(); + console.log("migrateQuestions", questions.length); + console.log(); - if ( - !isChildrenPresent(childQuestion) && - isVisibleIfPresent(childQuestion) - ) { - const childBranching = await IfNoChildrenAndVisibleIf( - question, - parentId, - childQuestion, - migratedCount - ); - - branching = { - ...branching, - ...childBranching, - }; - } - } - } + logger.info( + `migrateQuestions: criteria:${criteriaId} questions: ${questions.length} ` + ); - hierarchy.criterias[index].branchingLogic = branching; - return hierarchy; -}; + for (let i = 0; i < questions.length; i++) { + const question = questions[i]; -const IfNoChildrenAndVisibleIf = async ( - parentQuestion, - parentId, - question, - migratedCount -) => { - const visibleIf = question.visibleIf || []; - let childBranching = {}; - for (let i = 0; i < visibleIf.length; i++) { - const visible = visibleIf[i]; - const childId = question._id.toString(); - const chiQuestion = await getQuestionFromDB(childId); - const migratedId = await createQuestionTemplate(chiQuestion, migratedCount); - if (migratedId) { - childBranching = { - [migratedId]: { - target: [], - preCondition: getPrecondition(visible, parentId, parentQuestion), - source: [parentId], - }, - }; - } - } - return childBranching; -}; + console.log(); + console.log( + "migrateQuestions question responsetype", + question?.responseType, + "qid", + question._id, -const IfNoChildrenAndVisibleIfAndInnerChildren = async ( - question, - hierarchy, - index, - migratedCount -) => { - const visibleIf = question.visibleIf || []; - for (let i = 0; i < visibleIf.length; i++) { - const visible = visibleIf[i]; - const parentId = visible._id.toString(); - const parentQuestion = await getQuestionFromDB(parentId); - const migratedId = await createQuestionTemplate( - parentQuestion, - migratedCount ); - hierarchy.criterias[index].questions.push(migratedId); - if ( - isChildrenPresent(parentQuestion) && - !isVisibleIfPresent(parentQuestion) - ) { - return await IfChildrenAndNoVisibleIf( - parentQuestion, + console.log(); + + logger.info( + `migrateQuestions: criteria:${criteriaId} question: ${question?._id} question responseType: ${question?.responseType} ` + ); + + + if (question?.responseType === "matrix") { + + + const data = await createSection( + type, + matrixHierarchy, + matrixQuestions, + questions, + criteriaId, + question, + migratedCount + ); + matrixQuestions = data.matrixQuestions; + matrixHierarchy = data.matrixHierarchy; + questions = data.questions; + + console.log(); + console.log("migrateQuestions createSection", matrixHierarchy); + console.log(); + } else { + console.log(); + console.log( + "==========migrateQuestions getNonMatrixQuestions============", + question?.responseType, + question?._id + ); + console.log(); + + const data = await getNonMatrixQuestions( + question, + questions, + nonMatrixQuestions, + matrixQuestions, + matrixHierarchy, hierarchy, index, - migratedId, - migratedCount + type, + migratedCount, + criteriaId ); + + console.log(); + console.log( + "==========migrateQuestions getNonMatrixQuestions end ============", + matrixHierarchy + ); + console.log(); + + hierarchy = data.hierarchy; + matrixHierarchy = data.matrixHierarchy; + matrixQuestions = data.matrixQuestions; + nonMatrixQuestions = data.nonMatrixQuestions; + questions = data.questions; } } + return { hierarchy, matrixHierarchy }; }; module.exports = { diff --git a/script/template/generate/template.js b/script/template/generate/template.js new file mode 100644 index 00000000..65461357 --- /dev/null +++ b/script/template/generate/template.js @@ -0,0 +1,42 @@ +const getSectionTemplate = (section) => { + return { + _id: section._id, + __v: 0, + concepts: [], + createdAt: section?.createdAt, + createdFor: [], + criteriaType: "manual", + description: "Matrix section description", + evidences: [ + { + code: "OB", + sections: [ + { + code: "S1", + questions: [], + }, + ], + }, + ], + externalId: section?.externalId, + flag: "", + frameworkCriteriaId: "", + keywords: ["Keyword 1", "Keyword 2"], + language: ["English"], + name: "Matrix Section", + owner: "", + remarks: "", + resourceType: ["Program", "Framework", "Criteria"], + score: "", + showRemarks: null, + timesUsed: "", + updatedAt: section?.updatedAt, + weightage: "", + migratedId: null, + }; +}; + + +module.exports = { + getSectionTemplate, +}; diff --git a/script/template/helpers/hierarchyHelper.js b/script/template/helpers/hierarchyHelper.js index a8db6263..c216f490 100644 --- a/script/template/helpers/hierarchyHelper.js +++ b/script/template/helpers/hierarchyHelper.js @@ -1,4 +1,12 @@ -const { pick, findIndex, get, compact, find, omit } = require("lodash"); +const { + pick, + findIndex, + get, + compact, + find, + omit, + isArray, +} = require("lodash"); const { publishQuestionSet, updateQuestionSetHierarchy, @@ -7,7 +15,6 @@ const { const { CONFIG } = require("../../constant/config"); const { updateById } = require("../../db"); const logger = require("../../logger"); -const { updateQuestionSetFailedCount } = require("./questionsetHelper"); const updateHierarchyChildren = (hierarchy, migratedId, index) => { if ( @@ -31,6 +38,8 @@ const getOperator = (visibleIf) => { }; const getPrecondition = (visible, parentId, parentQuestion) => { + logger.debug(`getPrecondition: parentId = ${parentId}; visible: ${visible}`); + return { and: [ { @@ -40,7 +49,7 @@ const getPrecondition = (visible, parentId, parentQuestion) => { type: "interactions", }, findIndex(parentQuestion.options, { - value: visible.value, + value: isArray(visible?.value) ? visible?.value[0] : visible?.value, }), ], }, @@ -54,6 +63,9 @@ const updateHierarchyTemplate = async ( programId, migratedCount ) => { + logger.debug( + `updateHierarchyTemplate: programId = ${programId}; solution: ${solution?._id}` + ); const updateHierarchyData = { request: { data: { @@ -99,6 +111,13 @@ const updateHierarchyTemplate = async ( console.log("updateHierarchydata", JSON.stringify(updateHierarchyData)); console.log(); + + logger.info( + `updateHierarchyTemplate: Hierarchydata = ${JSON.stringify( + updateHierarchyData + )}` + ); + const questionsetId = hierarchy.questionsetDbId; let query = {}; if (!hierarchy.isHierarchyUpdated) { @@ -106,18 +125,17 @@ const updateHierarchyTemplate = async ( (err) => { logger.error(`Error while updating the questionset for solution_id: ${questionsetId} Error: ${JSON.stringify(err.response.data)}`); - - migratedCount.failed.questionSet.hierarchy.count++; + if ( !migratedCount.failed.questionSet.hierarchy.ids.includes( hierarchy?.questionset ) ) { + migratedCount.failed.questionSet.hierarchy.count++; migratedCount.failed.questionSet.hierarchy.ids.push( hierarchy?.questionset ); } - // updateQuestionSetFailedCount(migratedCount, "hierarchy", questionsetId); } ); @@ -146,18 +164,17 @@ const updateHierarchyTemplate = async ( (err) => { logger.error(`Error while updating the questionset branching for solution_id: ${questionsetId} Error: ${JSON.stringify(err.response.data)}`); - migratedCount.failed.questionSet.branching.count++; if ( !migratedCount.failed.questionSet.branching.ids.includes( hierarchy?.questionset ) ) { + migratedCount.failed.questionSet.branching.count++; migratedCount.failed.questionSet.branching.ids.push( hierarchy?.questionset ); } - // updateQuestionSetFailedCount(migratedCount, "branching", questionsetId); } ); if (!result) { @@ -178,18 +195,18 @@ const updateHierarchyTemplate = async ( hierarchy?.questionset } Error: ${JSON.stringify(err.response.data)}`); - migratedCount.failed.questionSet.published.count++; if ( !migratedCount.failed.questionSet.published.ids.includes( hierarchy?.questionset ) ) { + migratedCount.failed.questionSet.published.count++; + migratedCount.failed.questionSet.published.ids.push( hierarchy?.questionset ); } - // updateQuestionSetFailedCount(migratedCount, "published", hierarchy?.questionset); }); if (!res) { await updateSolutionsDb(query, questionsetId, migratedCount); diff --git a/script/template/helpers/questionsetHelper.js b/script/template/helpers/questionsetHelper.js index 8129a00f..66517965 100644 --- a/script/template/helpers/questionsetHelper.js +++ b/script/template/helpers/questionsetHelper.js @@ -42,12 +42,20 @@ const setQuestionSetTemplate = (solution, programId) => { }; const createQuestionTemplate = async (question, migratedCount) => { - const type = question.responseType; - let migratedId = question.migratedId; + + + console.log(); + console.log("createQuestionTemplate", question); + console.log(); + + const type = question?.responseType; + let migratedId = question?.migratedId; let query = {}; let questionToMigrate = {}; - if (!migratedId) { + let published = question?.isPublished; + + if (type && !migratedId) { if (type.toLowerCase() === "date") { questionToMigrate = getDateTemplate(question); } @@ -65,47 +73,68 @@ const createQuestionTemplate = async (question, migratedCount) => { } if (!isEmpty(questionToMigrate)) { + console.log(); + console.log( + "questionToMigratequestionToMigrate", + JSON.stringify(questionToMigrate) + ); + console.log(); + console.log("db Question", JSON.stringify(question)); + console.log(); + migratedId = await createQuestions(questionToMigrate, question._id); - query = { - ...query, - migratedId, - }; + console.log("migraysyys", migratedId); + question.migratedId = migratedId; + } } - if (!!migratedId && !question.isPublished) { + if (migratedId && !published) { const res = await publishQuestion(migratedId).catch((err) => { - migratedCount.failed.question.count++; if (!migratedCount.failed.question.ids.includes(migratedId)) { + migratedCount.failed.question.count++; migratedCount.failed.question.ids.push(migratedId); } - console.log(`Error while publishing the question for questionid: ${migratedId} questionId: ${question?._id} Error: - ${err.response.data}`); + logger.error(`Error while publishing the question for migratedId: ${migratedId} Error: ${JSON.stringify(err.response.data)}`); }); - console.log("createQuestion Template publish response",res , "migratedId", migratedId, "questionId", question?._id) - logger.info(`createQuestion Template publish response: ${res} , "migratedId" ${migratedId} questionId, ${question?._id}`); + logger.info( + `createQuestion Template publish response: ${res} , "migratedId" ${migratedId} questionId, ${question?._id}` + ); + + - if (!res) { - return migratedId; + if (res) { + question.isPublished = true; + published = true; + logger.info(`createQuestion Template published: ${migratedId}`); } + } + + + if (migratedId) { + question.migratedId = migratedId; - console.log("createQuestion Template published", migratedId) - logger.info(`createQuestion Template published: ${migratedId}`); query = { - ...query, - isPublished: true, + migratedId, + published: published, }; + } else { + query = { + ...query, + published + } } + - if (!isEmpty(query)) { + if (!isEmpty(query) && question) { await updateById(CONFIG.DB.TABLES.questions, question._id, { ...query, }); } - return migratedId; + return question; }; const updateSolutionById = async ({ id, query }) => { diff --git a/script/template/migrate/common.js b/script/template/migrate/common.js new file mode 100644 index 00000000..271e4935 --- /dev/null +++ b/script/template/migrate/common.js @@ -0,0 +1,228 @@ +const { isEmpty } = require("lodash"); +const { getPrecondition } = require("../helpers/hierarchyHelper"); +const { + getQuestionFromDB, + createQuestionTemplate, +} = require("../helpers/questionsetHelper"); +const logger = require("./../../logger"); + +const initHierarchy = (questionsetid, solution, programId, migratedId) => { + return { + questionset: migratedId, + questionsetDbId: questionsetid, + isHierarchyUpdated: solution?.isHierarchyUpdated || false, + isBranchingUpdated: solution?.isBranchingUpdated || false, + isPublished: solution?.isPublished || false, + sourcingProgramId: programId, + isSrcProgramUpdated: solution?.isSrcProgramUpdated || false, + isSrcProgramPublished: solution?.isSrcProgramPublished || false, + isNominated: solution?.isNominated || false, + isContributorAdded: solution?.isContributorAdded || false, + isContributorAccepted: solution?.isContributorAccepted || false, + criterias: [], + }; +}; + +const getCriteriaData = (criteria, type, question = {}) => { + if (isEmpty(question)) { + return { + migratedId: "", + criDbId: criteria?._id.toString(), + code: criteria?.externalId, + name: criteria?.name, + description: criteria?.description, + mimeType: "application/vnd.sunbird.questionset", + primaryCategory: type, + questions: [], + branchingLogic: {}, + allowMultipleInstances: "", + instances: {}, + }; + } else { + return { + migratedId: "", + _id: criteria?._id, + criDbId: criteria?._id.toString(), + code: criteria?.externalId, + name: criteria?.name, + description: criteria?.description, + mimeType: "application/vnd.sunbird.questionset", + primaryCategory: type, + questions: [], + branchingLogic: {}, + allowMultipleInstances: "Yes", + instances: { label: question?.instanceIdentifier }, + }; + } +}; + +const isSectionMatched = (matrixQuestions, criteriaId, matrixId) => { + logger.debug( + `isSectionMatched: criteriaId = ${criteriaId} matrixId = ${matrixId}; matrixQuestions: ${matrixQuestions}` + ); + if (matrixQuestions.hasOwnProperty(criteriaId)) { + if (matrixQuestions[criteriaId].hasOwnProperty(matrixId)) return true; + } + return false; +}; + +const isQuestionMatched = (data, id) => { + + logger.debug(`isQuestionMatched: id = ${id}; `); + + for (let i = 0; i < data.length; i++) { + const qid = data[i]._id; + + if (qid.toString() === id.toString()) { + return data[i]; + } + } + + return false; +}; + +const isQuestionMatchedInMatrixQuestions = ( + matrixQuestions, + criteriaId, + id +) => { + + logger.debug( + `isQuestionMatchedInMatrixQuestions: criteriaId = ${criteriaId}; id = ${id}; matrixQuestions: ${matrixQuestions}` + ); + + if (matrixQuestions.hasOwnProperty(criteriaId)) { + const keys = Object.keys(matrixQuestions[criteriaId]); + + for (let i = 0; i < keys.length; i++) { + const que = matrixQuestions[criteriaId][keys[i]]; + const questions = que?.questions || []; + return isQuestionMatched(questions, id); + } + } + + return false; +}; + +const getQuestion = async (questions, questions2, id, migratedCount) => { + + let matched = isQuestionMatched(questions, id); + + let question = {}; + + if (matched === false) { + matched = isQuestionMatched(questions2, id); + } + + if (matched === false) { + question = await getQuestionFromDB(id); + } + + if (matched !== false && !isEmpty(matched)) { + question = matched; + } + + if (!isEmpty(question) && (!question?.migratedId || !question?.isPublished)) { + question = await createQuestionTemplate(question, migratedCount); + } + + logger.info( + `getQuestion: qid = ${id}; question: ${JSON.stringify(question)}` + ); + + return question; +}; + +const updateMatrixHierarchyBranching = ( + matrixHierarchy, + criteriaId, + matrixId, + parentId, + pQuestion, + child +) => { + const criterias = matrixHierarchy?.criterias || []; + + logger.debug( + `updateMatrixHierarchyBranching: criteriaId = ${criteriaId}; matrixId = ${matrixId}; parentId: ${parentId}; child: ${child}` + ); + + for (let i = 0; i < criterias; i++) { + const criteria = criterias[i]; + if (criteria._id.toString() === criteriaId.toString()) { + matrixHierarchy = updateHierarchyBranching( + matrixHierarchy, + i, + parentId, + pQuestion, + child + ); + } + } + + return matrixHierarchy; +}; + +const updateHierarchyBranching = ( + branching, + index, + parentId, + pQuestion, + child +) => { + const migratedId = child?.migratedId; + + const visible = child?.visibleIf ? child?.visibleIf[0] : {}; + + logger.debug( + `updateHierarchyBranching: migratedId = ${migratedId}; parentId = ${parentId}; visible: ${visible}` + ); + + if (!isEmpty(visible)) { + if (hasProperty(branching, index, parentId) && migratedId) { + if ( + !branching.criterias[index].branchingLogic[parentId].target.includes( + migratedId + ) + ) { + branching.criterias[index].branchingLogic[parentId].target.push( + migratedId + ); + } + + branching.criterias[index].branchingLogic[migratedId] = { + target: [], + preCondition: getPrecondition(visible, parentId, pQuestion), + source: [parentId], + }; + } else if (migratedId) { + branching.criterias[index].branchingLogic[parentId] = { + target: [migratedId], + preCondition: {}, + source: [], + }; + branching.criterias[index].branchingLogic[migratedId] = { + target: [], + preCondition: getPrecondition(visible, parentId, pQuestion), + source: [parentId], + }; + } + } + + return branching; +}; + +const hasProperty = (hierarchy, index, id) => { + return hierarchy.criterias[index].branchingLogic.hasOwnProperty(id); +}; + +module.exports = { + initHierarchy, + getCriteriaData, + isSectionMatched, + getQuestion, + isQuestionMatchedInMatrixQuestions, + isQuestionMatched, + updateMatrixHierarchyBranching, + updateHierarchyBranching, +}; diff --git a/script/template/migrate/matrix.js b/script/template/migrate/matrix.js new file mode 100644 index 00000000..115612e0 --- /dev/null +++ b/script/template/migrate/matrix.js @@ -0,0 +1,479 @@ +const { + isChildrenPresent, + isVisibleIfPresent, +} = require("../helpers/questionsetHelper"); +const { + getQuestion, + getCriteriaData, + isQuestionMatched, + isSectionMatched, + updateMatrixHierarchyBranching, +} = require("./common"); + +const { getSectionTemplate } = require("../generate/template"); +const { compact, isEmpty } = require("lodash"); +const logger = require("./../../logger"); + +const createSection = async ( + type, + matrixHierarchy, + matrixQuestions, + questions, + criteriaId, + matrixQue, + migratedCount +) => { + const matrixId = matrixQue?._id ? matrixQue?._id.toString() : ""; + logger.debug( + `createSection: criteria:${criteriaId} question: ${matrixQue?._id} question responseType: ${matrixQue?.responseType} ` + ); + + if (!isSectionMatched(matrixQuestions, criteriaId, matrixId)) { + const section = getSectionTemplate(matrixQue); + + const newCriteria = getCriteriaData(section, type, matrixQue); + + logger.info( + `createSection: criteria:${criteriaId} question: ${matrixQue?._id}: ------------------Matrix as New Criteria---------------- ${newCriteria?.name}` + ); + + matrixHierarchy.criterias.push(newCriteria); + + if (isEmpty(matrixQuestions)) { + matrixQuestions[criteriaId] = { [matrixId]: { questions: [] } }; + } else { + matrixQuestions[criteriaId][matrixId] = { questions: [] }; + } + + const visible = !isEmpty(matrixQue?.visibleIf) + ? matrixQue?.visibleIf[0] + : ""; + + if (visible) { + const parentId = visible?._id; + + let pQuestion = isQuestionMatched(questions, parentId); + + if (pQuestion !== false) { + for (let q = 0; q < questions.length; q++) { + const que = questions[q]; + if (que._id.toString() === parentId.toString()) { + const children = que.children.map((child) => + child.toString() !== matrixQue._id.toString() ? child : null + ); + que.children = compact(children); + } + questions[q] = que; + } + } + } + } + + const instanceQuestions = matrixQue?.instanceQuestions || []; + const instanceData = await getInstanceQuestions( + instanceQuestions, + matrixHierarchy, + questions, + matrixQuestions, + criteriaId, + matrixQue._id.toString(), + type, + migratedCount + ); + + matrixHierarchy = instanceData.matrixHierarchy; + matrixQuestions = instanceData.matrixQuestions; + questions = instanceData.questions; + + return { matrixHierarchy, matrixQuestions, questions }; +}; + +const getInstanceQuestions = async ( + instanceQuestions, + matrixHierarchy, + questions, + matrixQuestions, + criteriaId, + matrixId, + type, + migratedCount +) => { + logger.debug( + `getInstanceQuestions: criteria:${criteriaId} question: ${matrixId}` + ); + + for (let i = 0; i < instanceQuestions.length; i++) { + const childId = instanceQuestions[i]; + + let child = await getQuestion( + questions, + matrixQuestions[criteriaId][matrixId], + childId + ); + + const data = await getMatrixQuestions( + child, + questions, + matrixQuestions, + criteriaId, + matrixId, + matrixHierarchy, + type, + migratedCount + ); + questions = data.questions; + matrixQuestions = data.matrixQuestions; + matrixHierarchy = data.matrixHierarchy; + } + + return { matrixQuestions, matrixHierarchy, questions }; +}; + +const getMatrixQuestions = async ( + question, + questions, + matrixQuestions, + criteriaId, + matrixId, + matrixHierarchy, + type, + migratedCount +) => { + logger.debug( + `getMatrixQuestions: criteriaId = ${criteriaId}; matrixId = ${matrixId}; question: ${question?._id}` + ); + + if (!isChildrenPresent(question) && !isVisibleIfPresent(question)) { + console.log("getMatrixQuestions noChildrenAndnoVisibleIf ", question._id); + const data = noChildrenAndnoVisibleIf( + matrixQuestions, + criteriaId, + question, + matrixId, + matrixHierarchy, + questions + ); + + matrixHierarchy = data.matrixHierarchy; + matrixQuestions = data.matrixQuestions; + } else if (isChildrenPresent(question) && !isVisibleIfPresent(question)) { + const data = await childrenAndnoVisibleIf( + question, + questions, + matrixQuestions, + matrixHierarchy, + criteriaId, + matrixId, + type, + migratedCount + ); + + matrixHierarchy = data.matrixHierarchy; + matrixQuestions = data.matrixQuestions; + questions = data.questions; + } else if (!isChildrenPresent(question) && isVisibleIfPresent(question)) { + const data = await noChildrenAndVisibleIf( + question, + questions, + matrixQuestions, + matrixHierarchy, + criteriaId, + matrixId, + migratedCount, + type + ); + + questions = data.questions; + matrixHierarchy = data.matrixHierarchy; + matrixQuestions = data.matrixQuestions; + } + return { matrixHierarchy, matrixQuestions, questions }; +}; + +const noChildrenAndnoVisibleIf = ( + matrixQuestions, + criteriaId, + question, + matrixId, + matrixHierarchy, + questions +) => { + console.log(); + console.log("noChildrenAndnoVisibleIf"); + console.log(); + + logger.debug( + `noChildrenAndnoVisibleIf: criteriaId = ${criteriaId}; matrixId = ${matrixId}; question: ${question?._id}` + ); + + const data = updateMatrixHierarchyQuestions( + matrixQuestions, + criteriaId, + question, + matrixId, + matrixHierarchy, + questions + ); + + matrixHierarchy = data.matrixHierarchy; + matrixQuestions = data.matrixQuestions; + questions = data.questions; + + return { matrixHierarchy, matrixQuestions, questions }; +}; + +const childrenAndnoVisibleIf = async ( + question, + questions, + matrixQuestions, + matrixHierarchy, + criteriaId, + matrixId, + type, + migratedCount +) => { + const children = question?.children || []; + + logger.info( + `childrenAndnoVisibleIf: criteriaId = ${criteriaId} matrixId = ${matrixId} question = ${question?._id} children = ${children.length}` + ); + + const updatedData = updateMatrixHierarchyQuestions( + matrixQuestions, + criteriaId, + question, + matrixId, + matrixHierarchy, + questions + ); + matrixHierarchy = updatedData.matrixHierarchy; + matrixQuestions = updatedData.matrixQuestions; + questions = updatedData.questions; + + for (let i = 0; i < children.length; i++) { + const childId = children[i]; + let child = await getQuestion( + questions, + matrixQuestions[criteriaId][matrixId], + childId, + migratedCount + ); + + logger.info( + `childrenAndnoVisibleIf: criteriaId = ${criteriaId} matrixId = ${matrixId} childId = ${child?._id} child = ${child} responseType: ${child?.responseType}` + ); + + if (child.responseType === "matrix") { + const data = await createSection( + type, + matrixHierarchy, + matrixQuestions, + questions, + criteriaId, + question, + migratedCount + ); + matrixQuestions = data.matrixQuestions; + matrixHierarchy = data.matrixHierarchy; + questions = data.questions; + } else { + const matched = isQuestionMatched( + matrixQuestions[criteriaId][matrixId], + question._id + ); + + if (matched === false) { + matrixQuestions[criteriaId][matrixId].questions.push(child); + matrixHierarchy = updateMatrixHierarchy( + matrixHierarchy, + matrixId, + child?.migratedId, + criteriaId + ); + matrixHierarchy = updateMatrixHierarchyBranching( + matrixHierarchy, + criteriaId, + matrixId, + question?.migratedId, + question, + child + ); + + // return await getMatrixQuestions( + // child, + // questions, + // matrixQuestions, + // criteriaId, + // matrixId, + // matrixHierarchy, + // type, + // migratedCount + // ); + } + } + } + + return { matrixHierarchy, matrixQuestions, questions }; +}; + +const noChildrenAndVisibleIf = async ( + question, + questions, + matrixQuestions, + matrixHierarchy, + criteriaId, + matrixId, + migratedCount, + type +) => { + const visible = question?.visibleIf ? question.visibleIf[0] : ""; + const parentId = visible._id; + const pQuestion = await getQuestion( + questions, + matrixQuestions[criteriaId][matrixId], + parentId, + migratedCount + ); + + logger.debug( + `noChildrenAndVisibleIf: criteriaId = ${criteriaId} matrixId = ${matrixId} questionId = ${question?._id} responseType: ${question?.responseType} visible = ${visible} ` + ); + + const updatedData = updateMatrixHierarchyQuestions( + matrixQuestions, + criteriaId, + question, + matrixId, + matrixHierarchy, + questions + ); + matrixHierarchy = updatedData.matrixHierarchy; + matrixQuestions = updatedData.matrixQuestions; + questions = updatedData.questions; + + // if (pQuestion.responseType === "matrix") { + // const data = await createSection( + // type, + // matrixHierarchy, + // matrixQuestions, + // questions, + // criteriaId, + // pQuestion, + // migratedCount + // ); + // matrixQuestions = data.matrixQuestions; + // matrixHierarchy = data.matrixHierarchy; + // questions = data.questions; + // } else { + + const pMatched = isQuestionMatched( + matrixQuestions[criteriaId][matrixId], + pQuestion._id + ); + + if (pMatched === false) { + matrixQuestions[criteriaId][matrixId].questions.push(pQuestion); + matrixHierarchy = updateMatrixHierarchy( + matrixHierarchy, + matrixId, + pQuestion?.migratedId, + criteriaId + ); + matrixHierarchy = updateMatrixHierarchyBranching( + matrixHierarchy, + criteriaId, + matrixId, + pQuestion?.migratedId, + pQuestion, + question + ); + + const data = await getMatrixQuestions( + pQuestion, + questions, + matrixQuestions, + criteriaId, + matrixId, + matrixHierarchy, + type, + migratedCount + ); + questions = data.questions; + matrixQuestions = data.matrixQuestions; + matrixHierarchy = data.matrixHierarchy; + } + // } + + return { questions, matrixHierarchy, matrixQuestions }; +}; + +const updateMatrixHierarchy = ( + matrixHierarchy, + matrixId, + migratedId, + criteriaId +) => { + logger.debug( + `updateMatrixHierarchy: matrixId = ${matrixId}; migratedId = ${migratedId}` + ); + + const criterias = matrixHierarchy.criterias || []; + for (let i = 0; i < criterias.length; i++) { + const criteria = criterias[i]; + if (criteria?._id.toString() === matrixId) { + if (!criteria.questions.includes(migratedId)) { + criteria.questions.push(migratedId); + } + } + criterias[i] = criteria; + } + matrixHierarchy.criterias = criterias; + + logger.info( + `updateMatrixHierarchy: matrixHierarchy: ${JSON.stringify(matrixHierarchy)}` + ); + + return matrixHierarchy; +}; + +const updateMatrixHierarchyQuestions = ( + matrixQuestions, + criteriaId, + question, + matrixId, + matrixHierarchy, + questions +) => { + logger.debug( + `updateMatrixHierarchyQuestions: criteriaId = ${criteriaId}; matrixId = ${matrixId}; question: ${question?._id}: migratedId: ${question?.migratedId}` + ); + + const matched = isQuestionMatched( + matrixQuestions[criteriaId][matrixId], + question._id + ); + + logger.info( + `updateMatrixHierarchyQuestions: isQuestionMatched: matched = ${ + matched === false ? matched : true + };` + ); + + if (matched === false) { + matrixQuestions[criteriaId][matrixId].questions.push(question); + matrixHierarchy = updateMatrixHierarchy( + matrixHierarchy, + matrixId, + question?.migratedId, + criteriaId + ); + } + + console.log("updateMatrixHierarchyQuestions ", matrixHierarchy); + + return { matrixHierarchy, matrixQuestions, questions }; +}; + +module.exports = { + createSection, +}; diff --git a/script/template/migrate/nonmatrix.js b/script/template/migrate/nonmatrix.js new file mode 100644 index 00000000..0d504061 --- /dev/null +++ b/script/template/migrate/nonmatrix.js @@ -0,0 +1,377 @@ +const { + isChildrenPresent, + isVisibleIfPresent, +} = require("../helpers/questionsetHelper"); +const { + isQuestionMatchedInMatrixQuestions, + getQuestion, + isQuestionMatched, + updateHierarchyBranching, +} = require("./common"); + +const { + updateHierarchyChildren, +} = require("../helpers/hierarchyHelper"); +const { createSection } = require("./matrix"); + +const logger = require("./../../logger"); + + +const updateNonMatrixHierarchyChildren = ( + question, + nonMatrixQuestions, + matrixQuestions, + hierarchy, + index, + criteriaId, + questions, + matrixHierarchy +) => { + + logger.debug(`updateNonMatrixHierarchyChildren: question = ${question?._id} criteriaId=${criteriaId}`) + + let matched = isQuestionMatched(nonMatrixQuestions, question._id); + + if (matched === false) { + matched = isQuestionMatchedInMatrixQuestions( + matrixQuestions, + criteriaId, + question._id + ); + + if (matched === false) { + hierarchy = updateHierarchyChildren( + hierarchy, + question?.migratedId, + index + ); + nonMatrixQuestions.push(question); + } + } + + + return { + hierarchy, + nonMatrixQuestions, + questions, + matrixQuestions, + matrixHierarchy, + }; +}; + +const getNonMatrixQuestions = async ( + question, + questions, + nonMatrixQuestions, + matrixQuestions, + matrixHierarchy, + hierarchy, + index, + type, + migratedCount, + criteriaId +) => { + logger.debug(`getNonMatrixQuestions: criteriaId = ${criteriaId} question = ${question?._id}`) + + question = await getQuestion( + questions, + nonMatrixQuestions, + question._id, + migratedCount + ); + + console.log("getNonMatrixQuestions", question); + + + if (!isChildrenPresent(question) && !isVisibleIfPresent(question)) { + + console.log("getNonMatrixQuestions nonMatrix NoChildrenAndNoVisibleIf", ) + + const data = nonMatrixNoChildrenAndNoVisibleIf( + question, + nonMatrixQuestions, + matrixQuestions, + hierarchy, + index, + criteriaId, + questions, + matrixHierarchy + ); + + nonMatrixQuestions = data.nonMatrixQuestions; + hierarchy = data.hierarchy; + questions = data.questions; + matrixQuestions = data.matrixQuestions; + matrixHierarchy = data.matrixHierarchy; + + console.log("getNonMatrixQuestions nonMatrix NoChildrenAndNoVisibleIf", ) + + } else if (isChildrenPresent(question) && !isVisibleIfPresent(question)) { + + console.log("getNonMatrixQuestions nonMatrix ChildrenAndNoVisibleIf", ) + + const data = await nonMatrixChildrenAndNoVisibleIf( + question, + questions, + nonMatrixQuestions, + matrixQuestions, + matrixHierarchy, + hierarchy, + index, + type, + migratedCount, + criteriaId + ); + hierarchy = data.hierarchy; + matrixHierarchy = data.matrixHierarchy; + matrixQuestions = data.matrixQuestions; + nonMatrixQuestions = data.nonMatrixQuestions; + questions = data.questions; + + } else if (!isChildrenPresent(question) && isVisibleIfPresent(question)) { + + console.log("getNonMatrixQuestions nonMatrix NoChildrenAndVisibleIf", ) + + + const data = await nonMatrixNoChildrenAndVisibleIf( + question, + questions, + nonMatrixQuestions, + matrixQuestions, + matrixHierarchy, + hierarchy, + index, + type, + migratedCount, + criteriaId + ); + hierarchy = data.hierarchy; + matrixHierarchy = data.matrixHierarchy; + matrixQuestions = data.matrixQuestions; + nonMatrixQuestions = data.nonMatrixQuestions; + questions = data.questions; + + } + + return { + hierarchy, + matrixHierarchy, + matrixQuestions, + nonMatrixQuestions, + questions, + }; +}; + +const nonMatrixNoChildrenAndNoVisibleIf = ( + question, + nonMatrixQuestions, + matrixQuestions, + hierarchy, + index, + criteriaId, + questions, + matrixHierarchy +) => { + + logger.debug(`nonMatrix NoChildrenAndNoVisibleIf: criteriaId = ${criteriaId} question = ${question?._id}`) + + return updateNonMatrixHierarchyChildren( + question, + nonMatrixQuestions, + matrixQuestions, + hierarchy, + index, + criteriaId, + questions, + matrixHierarchy + ); +}; + +const nonMatrixChildrenAndNoVisibleIf = async ( + question, + questions, + nonMatrixQuestions, + matrixQuestions, + matrixHierarchy, + hierarchy, + index, + type, + migratedCount, + criteriaId +) => { + + logger.debug(`nonMatrix ChildrenAndNoVisibleIf: criteriaId = ${criteriaId} question = ${question?._id}`) + + const data = updateNonMatrixHierarchyChildren( + question, + nonMatrixQuestions, + matrixQuestions, + hierarchy, + index, + criteriaId, + questions, + matrixHierarchy + ); + nonMatrixQuestions = data.nonMatrixQuestions; + hierarchy = data.hierarchy; + + const children = question?.children || []; + + for (let i = 0; i < children.length; i++) { + const childId = children[i]; + + let child = await getQuestion( + questions, + nonMatrixQuestions, + childId, + migratedCount + ); + + logger.info(`nonMatrix ChildrenAndNoVisibleIf: criteriaId = ${criteriaId} question = ${question?._id} child: ${child?._id} child responseType: ${child?.responseType}`) + + + if (child.responseType === "matrix") { + const data = await createSection( + type, + matrixHierarchy, + matrixQuestions, + questions, + criteriaId, + child, + migratedCount + ); + matrixQuestions = data.matrixQuestions; + matrixHierarchy = data.matrixHierarchy; + questions = data.questions; + } else { + + hierarchy = updateHierarchyBranching( + hierarchy, + index, + question?.migratedId, + question, + child + ); + + + // const data = await getNonMatrixQuestions( + // question, + // questions, + // nonMatrixQuestions, + // matrixQuestions, + // matrixHierarchy, + // hierarchy, + // index, + // type, + // migratedCount, + // criteriaId + // ); + // return data; + // hierarchy = data.hierarchy; + // matrixHierarchy = data.matrixHierarchy; + // matrixQuestions = data.matrixQuestions; + // nonMatrixQuestions = data.nonMatrixQuestions; + // questions = data.questions; + } + } + + + return { + hierarchy, + matrixHierarchy, + matrixQuestions, + nonMatrixQuestions, + questions, + }; +}; + +const nonMatrixNoChildrenAndVisibleIf = async ( + question, + questions, + nonMatrixQuestions, + matrixQuestions, + matrixHierarchy, + hierarchy, + index, + type, + migratedCount, + criteriaId +) => { + + const visible = question?.visibleIf ? question.visibleIf[0] : ""; + const parentId = visible._id; + const pQuestion = await getQuestion( + questions, + nonMatrixQuestions, + parentId, + migratedCount + ); + logger.debug(`nonMatrix NoChildrenAndVisibleIf: criteriaId = ${criteriaId} question = ${question?._id} question visibleif= ${visible}`) + + const data = updateNonMatrixHierarchyChildren( + question, + nonMatrixQuestions, + matrixQuestions, + hierarchy, + index, + criteriaId, + questions, + matrixHierarchy + ); + nonMatrixQuestions = data.nonMatrixQuestions; + hierarchy = data.hierarchy; + + if (pQuestion.responseType === "matrix") { + const data = await createSection( + type, + matrixHierarchy, + matrixQuestions, + questions, + criteriaId, + pQuestion, + migratedCount + ); + matrixQuestions = data.matrixQuestions; + matrixHierarchy = data.matrixHierarchy; + questions = data.questions; + + } else { + hierarchy = updateHierarchyBranching( + hierarchy, + index, + pQuestion?.migratedId, + pQuestion, + question + ); + const data = await getNonMatrixQuestions( + pQuestion, + questions, + nonMatrixQuestions, + matrixQuestions, + matrixHierarchy, + hierarchy, + index, + type, + migratedCount, + criteriaId + ); + // return data; + hierarchy = data.hierarchy; + matrixHierarchy = data.matrixHierarchy; + matrixQuestions = data.matrixQuestions; + nonMatrixQuestions = data.nonMatrixQuestions; + questions = data.questions; + } + + return { + hierarchy, + matrixHierarchy, + matrixQuestions, + nonMatrixQuestions, + questions, + }; +}; + +module.exports = { + getNonMatrixQuestions, +}; From 8e37022707e7b8df8470fc5963bb17f2469e52dd Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Mon, 25 Jul 2022 14:01:09 +0530 Subject: [PATCH 15/36] udpated review comments and added transformation script --- .env.sample | 20 +- config/index.js | 12 + config/redisConfig.js | 23 + .../v1/observationSubmissions2Controller.js | 350 +++++++++++++++ controllers/v1/observations2Controller.js | 351 +++++++++++++++ controllers/v1/solutions2Controller.js | 173 ++++++++ controllers/v1/surveys2Controller.js | 91 ++++ envVariables.js | 6 +- generics/messageConstants/endpoints.js | 4 +- generics/services/questions.js | 141 ++++++ healthCheck/healthCheckService.js | 10 +- healthCheck/redis.js | 25 ++ module/surveys/helper.js | 402 ++++++++++++++++++ module/transformation/helper.js | 351 +++++++++++++++ package.json | 10 +- script/constant/config.js | 61 --- script/logger.js | 42 -- .../creation-portal-migration}/README.md | Bin .../api-list/headers.js | 26 +- .../api-list/program.js | 10 +- .../api-list/question.js | 18 +- .../api-list/user.js | 10 +- .../constant/config.js | 60 +++ .../creation-portal-migration}/db/dbConfig.js | 0 .../creation-portal-migration}/db/index.js | 30 +- scripts/creation-portal-migration/logger.js | 46 ++ .../creation-portal-migration}/server.js | 16 +- .../template/config/question.js | 0 .../template/config/questionSet.js | 0 .../template/generate/gProgram.js | 0 .../template/generate/gQuestion.js | 0 .../template/generate/gQuestionSet.js | 10 +- .../template/generate/template.js | 0 .../template/helpers/hierarchyHelper.js | 2 +- .../template/helpers/questionsetHelper.js | 2 +- .../template/migrate/common.js | 2 +- .../template/migrate/matrix.js | 2 +- .../template/migrate/nonmatrix.js | 2 +- templates/questions.js | 205 +++++++++ templates/questionset.js | 381 +++++++++++++++++ 40 files changed, 2702 insertions(+), 192 deletions(-) create mode 100644 config/redisConfig.js create mode 100644 controllers/v1/observationSubmissions2Controller.js create mode 100644 controllers/v1/observations2Controller.js create mode 100644 controllers/v1/solutions2Controller.js create mode 100644 controllers/v1/surveys2Controller.js create mode 100644 generics/services/questions.js create mode 100644 healthCheck/redis.js create mode 100644 module/transformation/helper.js delete mode 100644 script/constant/config.js delete mode 100644 script/logger.js rename {script => scripts/creation-portal-migration}/README.md (100%) rename {script => scripts/creation-portal-migration}/api-list/headers.js (68%) rename {script => scripts/creation-portal-migration}/api-list/program.js (85%) rename {script => scripts/creation-portal-migration}/api-list/question.js (84%) rename {script => scripts/creation-portal-migration}/api-list/user.js (78%) create mode 100644 scripts/creation-portal-migration/constant/config.js rename {script => scripts/creation-portal-migration}/db/dbConfig.js (100%) rename {script => scripts/creation-portal-migration}/db/index.js (50%) create mode 100644 scripts/creation-portal-migration/logger.js rename {script => scripts/creation-portal-migration}/server.js (88%) rename {script => scripts/creation-portal-migration}/template/config/question.js (100%) rename {script => scripts/creation-portal-migration}/template/config/questionSet.js (100%) rename {script => scripts/creation-portal-migration}/template/generate/gProgram.js (100%) rename {script => scripts/creation-portal-migration}/template/generate/gQuestion.js (100%) rename {script => scripts/creation-portal-migration}/template/generate/gQuestionSet.js (95%) rename {script => scripts/creation-portal-migration}/template/generate/template.js (100%) rename {script => scripts/creation-portal-migration}/template/helpers/hierarchyHelper.js (99%) rename {script => scripts/creation-portal-migration}/template/helpers/questionsetHelper.js (98%) rename {script => scripts/creation-portal-migration}/template/migrate/common.js (99%) rename {script => scripts/creation-portal-migration}/template/migrate/matrix.js (99%) rename {script => scripts/creation-portal-migration}/template/migrate/nonmatrix.js (99%) create mode 100644 templates/questions.js create mode 100644 templates/questionset.js diff --git a/.env.sample b/.env.sample index a5e65b43..6d0ed087 100644 --- a/.env.sample +++ b/.env.sample @@ -26,20 +26,20 @@ ML_PROJECT_SERVICE_URL = "http://ml-project-service:3000" KEYCLOAK_PUBLIC_KEY_PATH = "keycloak-public-keys" // Keycloak public keys path -DISABLE_LEARNER_SERVICE_ON_OFF = "ON" // Disable learner service check +DISABLE_LEARNER_SERVICE_ON_OFF = "ON" // Disable learner service check # ML Script -SUNBIRD_USER = string -SUNBIRD_PWD = string -SUNBIRD_GRANT = string -SUNBIRD_CLIENT = string -SUNBIRD_CLIENT_SECRET = string +BASE_USER = string // base user name of the sunbird enviornment +BASE_PWD = string // base user pwd of the sunbird enviornment +BASE_GRANT = string // base grant of the sunbird enviornment +BASE_CLIENT = string // base client of the sunbird enviornment +BASE_CLIENT_SECRET = string // base client secret of the sunbird enviornment -SUNBIRD_HOST = string -SUNBIRD_AUTHORIZATION = 'Bearer token' +BASE_HOST = string // base host url of the sunbird enviornment +BASE_AUTHORIZATION = 'Bearer token' // base bearer token with all the permissions to run the script of the sunbird enviornment -VDN_HOST = string -VDN_AUTHORIZATION= 'Bearer token' \ No newline at end of file +VDN_HOST = string // host url of the vdn enviornment +VDN_AUTHORIZATION= 'Bearer token' // bearer token with all the permissions to run the script of the vdn enviornment \ No newline at end of file diff --git a/config/index.js b/config/index.js index 0b875236..2b642901 100644 --- a/config/index.js +++ b/config/index.js @@ -47,8 +47,20 @@ const configuration = { } }; +/** + * Redis connection information. + * @method + * @name redis_connect +*/ + +let redis_connect = async function () { + global.cache = require("./redisConfig").connect; + console.log("cache", cache); +} + db_connect(); kafka_connect(); // elasticsearch_connect(); +redis_connect() module.exports = configuration; diff --git a/config/redisConfig.js b/config/redisConfig.js new file mode 100644 index 00000000..76a6dbee --- /dev/null +++ b/config/redisConfig.js @@ -0,0 +1,23 @@ +const redis = require("redis"); + +var CACHE = function () { + var client = redis.createClient(process.env.REDIS_URL); + + + client.on("connect", function () { + console.log("redis client connected"); + }); + + client.on("error", console.error.bind(console, "redis connection error:")); + + console.log("redis", client); + + + return { + connect: client.connect(), + client: client, + expiry: 24 * 3600 + }; +}; + +module.exports = CACHE(); diff --git a/controllers/v1/observationSubmissions2Controller.js b/controllers/v1/observationSubmissions2Controller.js new file mode 100644 index 00000000..9148ca8f --- /dev/null +++ b/controllers/v1/observationSubmissions2Controller.js @@ -0,0 +1,350 @@ +// Dependencies + +const observationsHelper = require(MODULES_BASE_PATH + "/observations/helper"); +const solutionsHelper = require(MODULES_BASE_PATH + "/solutions/helper"); +const entitiesHelper = require(MODULES_BASE_PATH + "/entities/helper"); +const submissionsHelper = require(MODULES_BASE_PATH + "/submissions/helper"); +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 transFormationHelper = require(MODULES_BASE_PATH + + "/transformation/helper"); +const redis = require("./../../config/redisConfig"); + +const cache = redis.client; + +/** + * ObservationSubmissions + * @class + */ +module.exports = class ObservationSubmissions extends Abstract { + constructor() { + super(observationSubmissionsSchema); + } + + static get name() { + return "observationSubmissions"; + } + + /** + * create observation submissions. + * @method + * @name create + * @param {Object} req -request data. + * @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. + */ + + async create(req) { + return new Promise(async (resolve, reject) => { + try { + const cacheData = await cache + .get(`submissionCreate:${req.params._id}:${req.params.entityId}`) + .catch((err) => { + console.log("error", err); + }); + + if (cacheData) { + console.log("cahcehData", cacheData); + return resolve(JSON.parse(cacheData)); + } else { + 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, + message: messageConstants.apiResponses.OBSERVATION_NOT_FOUND, + }); + } + + observationDocument = observationDocument[0]; + + let entityDocument = await entitiesHelper.entityDocuments( + { + _id: req.query.entityId, + entityType: observationDocument.entityType, + }, + ["metaInformation", "entityTypeId", "entityType", "registryDetails"] + ); + + if (!entityDocument[0]) { + return resolve({ + status: httpStatusCode.bad_request.status, + message: messageConstants.apiResponses.ENTITY_NOT_FOUND, + }); + } + + entityDocument = entityDocument[0]; + + if ( + entityDocument.registryDetails && + Object.keys(entityDocument.registryDetails).length > 0 + ) { + entityDocument.metaInformation.registryDetails = + entityDocument.registryDetails; + } + + let solutionDocument = await solutionsHelper.solutionDocuments( + { + _id: observationDocument.solutionId, + status: "active", + }, + [ + "externalId", + "themes", + "frameworkId", + "frameworkExternalId", + "evidenceMethods", + "entityTypeId", + "entityType", + "programId", + "programExternalId", + "isAPrivateProgram", + "scoringSystem", + "isRubricDriven", + "project", + "referenceFrom", + "criteriaLevelReport", + "migratedId", + "type", + ] + ); + + if (!solutionDocument[0]) { + return resolve({ + status: httpStatusCode.bad_request.status, + message: messageConstants.apiResponses.SOLUTION_NOT_FOUND, + }); + } + + solutionDocument = solutionDocument[0]; + + const migratedId = solutionDocument?.migratedId; + + console.log("migrated", migratedId); + + let entityProfileForm = await database.models.entityTypes + .findOne(solutionDocument.entityTypeId, { + profileForm: 1, + }) + .lean(); + + if (!entityProfileForm) { + return resolve({ + status: httpStatusCode.bad_request.status, + message: + messageConstants.apiResponses.ENTITY_PROFILE_FORM_NOT_FOUND, + }); + } + + let lastSubmissionNumber = 0; + + const lastSubmissionForObservationEntity = + await observationsHelper.findLastSubmissionForObservationEntity( + req.params._id, + req.query.entityId + ); + + if (!lastSubmissionForObservationEntity.success) { + throw new Error(lastSubmissionForObservationEntity.message); + } + + lastSubmissionNumber = lastSubmissionForObservationEntity.result + 1; + + let submissionDocument = { + entityId: entityDocument._id, + entityExternalId: entityDocument.metaInformation.externalId + ? entityDocument.metaInformation.externalId + : "", + entityInformation: entityDocument.metaInformation, + solutionId: solutionDocument._id, + solutionExternalId: solutionDocument.externalId, + programId: solutionDocument.programId, + programExternalId: solutionDocument.programExternalId, + isAPrivateProgram: solutionDocument.isAPrivateProgram, + frameworkId: solutionDocument.frameworkId, + frameworkExternalId: solutionDocument.frameworkExternalId, + entityTypeId: solutionDocument.entityTypeId, + entityType: solutionDocument.entityType, + observationId: observationDocument._id, + observationInformation: { + ..._.omit(observationDocument, [ + "_id", + "entities", + "deleted", + "__v", + ]), + }, + createdBy: observationDocument.createdBy, + evidenceSubmissions: [], + entityProfile: {}, + status: "started", + scoringSystem: solutionDocument.scoringSystem, + isRubricDriven: solutionDocument.isRubricDriven, + userProfile: observationDocument.userProfile, + }; + + 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 }, + } + ); + } + + if ( + solutionDocument.referenceFrom === messageConstants.common.PROJECT + ) { + submissionDocument["referenceFrom"] = + messageConstants.common.PROJECT; + submissionDocument["project"] = solutionDocument.project; + } + + let submissionDocumentEvidences = {}; + let submissionDocumentCriterias = []; + Object.keys(solutionDocument.evidenceMethods).forEach( + (solutionEcm) => { + if ( + !( + solutionDocument.evidenceMethods[solutionEcm].isActive === + false + ) + ) { + solutionDocument.evidenceMethods[solutionEcm].startTime = ""; + solutionDocument.evidenceMethods[solutionEcm].endTime = ""; + solutionDocument.evidenceMethods[ + solutionEcm + ].isSubmitted = false; + solutionDocument.evidenceMethods[solutionEcm].submissions = + new Array(); + } else { + delete solutionDocument.evidenceMethods[solutionEcm]; + } + } + ); + submissionDocumentEvidences = solutionDocument.evidenceMethods; + + let evidences = {}; + if (!!migratedId) { + evidences = await transFormationHelper.getQuestionSetHierarchy( + migratedId, + submissionDocumentCriterias, + solutionDocument, + false + ); + } + + submissionDocument.evidences = submissionDocumentEvidences; + submissionDocument.evidencesStatus = Object.values( + submissionDocumentEvidences + ); + submissionDocument.criteria = + evidences.submissionDocumentCriterias || {}; + console.log( + "observationSubmissions2", + JSON.stringify(evidences.submissionDocumentCriterias) + ); + submissionDocument.submissionNumber = lastSubmissionNumber; + + submissionDocument["appInformation"] = {}; + + if (req.headers["x-app-id"] || req.headers.appname) { + submissionDocument["appInformation"]["appName"] = req.headers[ + "x-app-id" + ] + ? req.headers["x-app-id"] + : req.headers.appname; + } + + if (req.headers["x-app-ver"] || req.headers.appversion) { + submissionDocument["appInformation"]["appVersion"] = req.headers[ + "x-app-ver" + ] + ? req.headers["x-app-ver"] + : req.headers.appversion; + } + + let newObservationSubmissionDocument = + await database.models.observationSubmissions.create( + submissionDocument + ); + + if ( + newObservationSubmissionDocument.referenceFrom === + messageConstants.common.PROJECT + ) { + await observationSubmissionsHelper.pushSubmissionToImprovementService( + _.pick(newObservationSubmissionDocument, [ + "project", + "status", + "_id", + ]) + ); + } + + // Push new observation submission to kafka for reporting/tracking. + observationSubmissionsHelper.pushObservationSubmissionForReporting( + newObservationSubmissionDocument._id + ); + + let observations = new Array(); + + observations = await observationsHelper.listV2( + req.userDetails.userId + ); + + let responseMessage = + messageConstants.apiResponses.OBSERVATION_SUBMISSION_CREATED; + await cache.setEx( + `submissionCreate:${req.params._id}:${req.params.entityId}`, + redis.expiry, + JSON.stringify({ + message: responseMessage, + result: observations, + }) + ); + + return resolve({ + message: responseMessage, + result: observations, + }); + } + } catch (error) { + return reject({ + status: error.status || httpStatusCode.internal_server_error.status, + message: + error.message || httpStatusCode.internal_server_error.message, + errorObject: error, + }); + } + }); + } +}; diff --git a/controllers/v1/observations2Controller.js b/controllers/v1/observations2Controller.js new file mode 100644 index 00000000..576e5c4a --- /dev/null +++ b/controllers/v1/observations2Controller.js @@ -0,0 +1,351 @@ +const observationsHelper = require(MODULES_BASE_PATH + "/observations/helper"); +const assessmentsHelper = require(MODULES_BASE_PATH + "/assessments/helper"); +const programsHelper = require(MODULES_BASE_PATH + "/programs/helper"); +const transFormationHelper = require(MODULES_BASE_PATH + + "/transformation/helper"); + +const redis = require("./../../config/redisConfig"); + +const cache = redis.client; + +// obId : 60128960f5c000702022546f?entityId=5fd098e2e049735a86b748b7 +// obId : 60110e692d0bbd2f0c3229c3?entityId=5fd098e2e049735a86b748b0 + +module.exports = class Observations2 extends Abstract { + constructor() { + super(observationsSchema); + } + + async assessment(req) { + return new Promise(async (resolve, reject) => { + try { + const cacheData = await cache + .get(`assessment:${req.params._id}:${req.params.entityId}`) + .catch((err) => { + console.log("error", err); + }); + + if (cacheData) { + console.log("cahcehData", cacheData); + return resolve(JSON.parse(cacheData)); + } else { + let response = { + message: messageConstants.apiResponses.ASSESSMENT_FETCHED, + result: {}, + }; + + let observationDocument = await database.models.observations + .findOne({ + _id: req.params._id, + createdBy: req.userDetails.userId, + status: { $ne: "inactive" }, + entities: ObjectId(req.query.entityId), + }) + .lean(); + + if (!observationDocument) { + return resolve({ + status: httpStatusCode.bad_request.status, + message: messageConstants.apiResponses.OBSERVATION_NOT_FOUND, + }); + } + + let entityQueryObject = { + _id: req.query.entityId, + entityType: observationDocument.entityType, + }; + let entityDocument = await database.models.entities + .findOne(entityQueryObject, { + metaInformation: 1, + entityTypeId: 1, + entityType: 1, + registryDetails: 1, + }) + .lean(); + + if (!entityDocument) { + let responseMessage = + messageConstants.apiResponses.ENTITY_NOT_FOUND; + return resolve({ + status: httpStatusCode.bad_request.status, + message: responseMessage, + }); + } + + if ( + entityDocument.registryDetails && + Object.keys(entityDocument.registryDetails).length > 0 + ) { + entityDocument.metaInformation.registryDetails = + entityDocument.registryDetails; + } + + const submissionNumber = + req.query.submissionNumber && req.query.submissionNumber > 1 + ? parseInt(req.query.submissionNumber) + : 1; + + let solutionQueryObject = { + _id: observationDocument.solutionId, + status: "active", + }; + + let solutionDocumentProjectionFields = + await observationsHelper.solutionDocumentProjectionFieldsForDetailsAPI(); + + let solutionDocument = await database.models.solutions + .findOne(solutionQueryObject, { + ...solutionDocumentProjectionFields, + migratedId: 1, + type: 1, + }) + .lean(); + + if (!solutionDocument) { + let responseMessage = + messageConstants.apiResponses.SOLUTION_NOT_FOUND; + return resolve({ + status: httpStatusCode.bad_request.status, + message: responseMessage, + }); + } + + const migratedId = solutionDocument?.migratedId; + console.log("migratedIdmigratedIdmigratedId", migratedId); + + if (req.query.ecmMethod && req.query.ecmMethod !== "") { + if (!solutionDocument.evidenceMethods[req.query.ecmMethod]) { + return resolve({ + status: httpStatusCode.bad_request.status, + message: messageConstants.apiResponses.ECM_NOT_EXIST, + }); + } + } + + let programQueryObject = { + _id: observationDocument.programId, + status: "active", + components: { $in: [ObjectId(observationDocument.solutionId)] }, + }; + + let programDocument = await programsHelper.list(programQueryObject, [ + "externalId", + "name", + "description", + "imageCompression", + "isAPrivateProgram", + ]); + + if (!programDocument[0]._id) { + throw messageConstants.apiResponses.PROGRAM_NOT_FOUND; + } + let entityDocumentQuestionGroup = entityDocument.metaInformation + .questionGroup + ? entityDocument.metaInformation.questionGroup + : ["A1"]; + + response.result.entityProfile = { + _id: entityDocument._id, + entityTypeId: entityDocument.entityTypeId, + entityType: entityDocument.entityType, + // form: form + }; + + let solutionDocumentFieldList = + await observationsHelper.solutionDocumentFieldListInResponse(); + + response.result.solution = await _.pick( + solutionDocument, + solutionDocumentFieldList + ); + response.result.program = programDocument[0]; + + let submissionDocument = { + entityId: entityDocument._id, + entityExternalId: entityDocument.metaInformation.externalId + ? entityDocument.metaInformation.externalId + : "", + entityInformation: entityDocument.metaInformation, + solutionId: solutionDocument._id, + solutionExternalId: solutionDocument.externalId, + programId: programDocument[0]._id, + programExternalId: programDocument[0].externalId, + isAPrivateProgram: programDocument[0].isAPrivateProgram, + programInformation: { + ..._.omit(programDocument[0], [ + "_id", + "components", + "isAPrivateProgram", + ]), + }, + frameworkId: solutionDocument.frameworkId, + frameworkExternalId: solutionDocument.frameworkExternalId, + entityTypeId: solutionDocument.entityTypeId, + entityType: solutionDocument.entityType, + scoringSystem: solutionDocument.scoringSystem, + isRubricDriven: solutionDocument.isRubricDriven, + observationId: observationDocument._id, + observationInformation: { + ..._.omit(observationDocument, [ + "_id", + "entities", + "deleted", + "__v", + ]), + }, + createdBy: observationDocument.createdBy, + evidenceSubmissions: [], + entityProfile: {}, + status: "started", + userProfile: observationDocument.userProfile, + }; + + 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 }, + } + ); + } + + if ( + solutionDocument.referenceFrom === messageConstants.common.PROJECT + ) { + submissionDocument["referenceFrom"] = + messageConstants.common.PROJECT; + submissionDocument["project"] = solutionDocument.project; + } + + let assessment = {}; + + assessment.name = solutionDocument.name; + assessment.description = solutionDocument.description; + assessment.externalId = solutionDocument.externalId; + assessment.pageHeading = solutionDocument.pageHeading; + + let evidenceMethodArray = {}; + let submissionDocumentEvidences = {}; + let submissionDocumentCriterias = []; + Object.keys(solutionDocument.evidenceMethods).forEach( + (solutionEcm) => { + solutionDocument.evidenceMethods[solutionEcm].startTime = ""; + solutionDocument.evidenceMethods[solutionEcm].endTime = ""; + solutionDocument.evidenceMethods[solutionEcm].isSubmitted = false; + solutionDocument.evidenceMethods[solutionEcm].submissions = + new Array(); + } + ); + submissionDocumentEvidences = solutionDocument.evidenceMethods; + + let evidences = {}; + if (!!migratedId) { + console.log("migrattetemigrattetemigrattete", migratedId); + response.result.solution._id = migratedId; + evidences = await transFormationHelper.getQuestionSetHierarchy( + migratedId, + submissionDocumentCriterias, + solutionDocument + ); + } + + submissionDocument.evidences = submissionDocumentEvidences; + submissionDocument.evidencesStatus = Object.values( + submissionDocumentEvidences + ); + submissionDocument.criteria = + evidences.submissionDocumentCriterias || {}; + submissionDocument.submissionNumber = submissionNumber; + + submissionDocument["appInformation"] = {}; + + if (req.headers["x-app-id"] || req.headers.appname) { + submissionDocument["appInformation"]["appName"] = req.headers[ + "x-app-id" + ] + ? req.headers["x-app-id"] + : req.headers.appname; + } + + if (req.headers["x-app-ver"] || req.headers.appversion) { + submissionDocument["appInformation"]["appVersion"] = req.headers[ + "x-app-ver" + ] + ? req.headers["x-app-ver"] + : req.headers.appversion; + } + + let submissionDoc = await observationsHelper.findSubmission( + submissionDocument + ); + + assessment.submissionId = submissionDoc.result._id; + + if (req.query.ecmMethod && req.query.ecmMethod !== "") { + if (evidenceMethodArray[req.query.ecmMethod]) { + evidenceMethodArray = { + [req.query.ecmMethod]: evidenceMethodArray[req.query.ecmMethod], + }; + } + } + + + const parsedAssessment = await assessmentsHelper.parseQuestionsV2( + Object.values(evidenceMethodArray), + entityDocumentQuestionGroup, + submissionDoc.result.evidences, + solutionDocument && solutionDocument.questionSequenceByEcm + ? solutionDocument.questionSequenceByEcm + : false, + entityDocument.metaInformation + ); + + + assessment.evidences = evidences.evidences; + assessment.submissions = parsedAssessment.submissions; + if ( + parsedAssessment.generalQuestions && + parsedAssessment.generalQuestions.length > 0 + ) { + assessment.generalQuestions = parsedAssessment.generalQuestions; + } + + response.result.assessment = assessment; + + await cache.setEx( + `assessment:${req.params._id}:${req.params.entityId}`, + redis.expiry, + JSON.stringify(response) + ); + + return resolve(response); + } + } catch (error) { + console.log("errororooro", error); + return reject({ + status: error?.status || httpStatusCode.internal_server_error.status, + message: + error?.message || httpStatusCode.internal_server_error.message, + errorObject: error, + }); + } + }); + } +}; diff --git a/controllers/v1/solutions2Controller.js b/controllers/v1/solutions2Controller.js new file mode 100644 index 00000000..7c255d47 --- /dev/null +++ b/controllers/v1/solutions2Controller.js @@ -0,0 +1,173 @@ +// Dependencies +const csv = require("csvtojson"); +const solutionsHelper = require(MODULES_BASE_PATH + "/solutions/helper"); +const criteriaHelper = require(MODULES_BASE_PATH + "/criteria/helper"); +const questionsHelper = require(MODULES_BASE_PATH + "/questions/helper"); +const FileStream = require(ROOT_PATH + "/generics/fileStream"); +const observationsHelper = require(MODULES_BASE_PATH + "/observations/helper"); +const assessmentsHelper = require(MODULES_BASE_PATH + "/assessments/helper"); +const transFormationHelper = require(MODULES_BASE_PATH + + "/transformation/helper"); + +const redis = require("./../../config/redisConfig"); + +const cache = redis.client; + +/** + * Solutions + * @class + */ +module.exports = class Solutions2 extends Abstract { + constructor() { + super(solutionsSchema); + } + + static get name() { + return "solutions"; + } + + /** + * Get Questions in solution. + * @method + * @name deleteSolution + * @param {Object} req - requested data. + * @param {String} req.params._id - solutiion internal id. + * @returns {JSON} consists of solution id. + */ + + async questions(req) { + return new Promise(async (resolve, reject) => { + try { + const cacheData = await cache + .get(`solutionQuestions:${req.params._id}`) + .catch((err) => { + console.log("error", err); + }); + + if (cacheData) { + console.log("cahcehData", cacheData); + return resolve(JSON.parse(cacheData)); + } else { + let response = { + message: messageConstants.apiResponses.ASSESSMENT_FETCHED, + result: {}, + }; + + let solutionId = req.params._id; + let userId = req.userDetails.userId; + + if (userId == "") { + throw new Error( + messageConstants.apiResponses.USER_ID_REQUIRED_CHECK + ); + } + + let solutionDocumentProjectionFields = + await observationsHelper.solutionDocumentProjectionFieldsForDetailsAPI(); + + let solutionDocument = await database.models.solutions + .findOne( + { _id: solutionId }, + { ...solutionDocumentProjectionFields, migratedId: 1, type: 1 } + ) + .lean(); + + if (!solutionDocument) { + return resolve({ + status: httpStatusCode.bad_request.status, + message: messageConstants.apiResponses.SOLUTION_NOT_FOUND, + }); + } + + const migratedId = solutionDocument?.migratedId; + + let solutionDocumentFieldList = + await observationsHelper.solutionDocumentFieldListInResponse(); + + response.result.solution = await _.pick( + solutionDocument, + solutionDocumentFieldList + ); + + let assessment = {}; + assessment.name = solutionDocument.name; + assessment.description = solutionDocument.description; + assessment.externalId = solutionDocument.externalId; + assessment.pageHeading = solutionDocument.pageHeading; + assessment.submissionId = ""; + + let evidenceMethodArray = {}; + let submissionDocumentEvidences = {}; + let submissionDocumentCriterias = []; + Object.keys(solutionDocument.evidenceMethods).forEach( + (solutionEcm) => { + solutionDocument.evidenceMethods[solutionEcm].startTime = ""; + solutionDocument.evidenceMethods[solutionEcm].endTime = ""; + solutionDocument.evidenceMethods[solutionEcm].isSubmitted = false; + solutionDocument.evidenceMethods[solutionEcm].submissions = + new Array(); + } + ); + + submissionDocumentEvidences = solutionDocument.evidenceMethods; + + let evidences = {}; + if (!!migratedId) { + console.log("migrattetemigrattetemigrattete", migratedId); + response.result.solution._id = migratedId; + evidences = await transFormationHelper.getQuestionSetHierarchy( + migratedId, + submissionDocumentCriterias, + solutionDocument + ); + } + + let entityDocument = { + metaInformation: {}, + questionGroup: "", + }; + + let entityDocumentQuestionGroup = entityDocument.metaInformation + .questionGroup + ? entityDocument.metaInformation.questionGroup + : ["A1"]; + assessment.evidences = []; + const parsedAssessment = await assessmentsHelper.parseQuestionsV2( + Object.values(evidenceMethodArray), + entityDocumentQuestionGroup, + submissionDocumentEvidences, + solutionDocument && solutionDocument.questionSequenceByEcm + ? solutionDocument.questionSequenceByEcm + : false, + {} + ); + + assessment.evidences = evidences.evidences; + assessment.submissions = parsedAssessment.submissions; + + if ( + parsedAssessment.generalQuestions && + parsedAssessment.generalQuestions.length > 0 + ) { + assessment.generalQuestions = parsedAssessment.generalQuestions; + } + + response.result.assessment = assessment; + await cache.setEx( + `solutionQuestions:${req.params._id}`, + redis.expiry, + JSON.stringify(response) + ); + return resolve(response); + } + } catch (error) { + return reject({ + status: error.status || httpStatusCode.internal_server_error.status, + message: + error.message || httpStatusCode.internal_server_error.message, + errorObject: error, + }); + } + }); + } +}; diff --git a/controllers/v1/surveys2Controller.js b/controllers/v1/surveys2Controller.js new file mode 100644 index 00000000..7ba5bd95 --- /dev/null +++ b/controllers/v1/surveys2Controller.js @@ -0,0 +1,91 @@ +// Dependencies +const surveysHelper = require(MODULES_BASE_PATH + "/surveys/helper"); +const redis = require("./../../config/redisConfig"); + +const cache = redis.client; + + +module.exports = class Surveys extends Abstract { + constructor() { + super(surveysSchema); + } + + static get name() { + return "surveys2"; + } + + /** + * Survey details. + * @method + * @name details + * @param {Request} req request body. + * @param {req.param._id} Either surveyId or link. + * @param {req.query.solutionId} solutionId (not required in the case of passing link). + * @returns {Object} returns survey details information. + * Result will have the details of survey. + */ + + async details(req) { + return new Promise(async (resolve, reject) => { + try { + const cacheData = await cache + .get(`surveyDetails:${req.params._id}`) + .catch((err) => { + console.log("error", err); + }); + + if (cacheData) { + console.log("cahcehData", cacheData); + return resolve(JSON.parse(cacheData)); + } else { + + let validateSurveyId = gen.utils.isValidMongoId(req.params._id); + + let surveyDetails = {}; + + if (validateSurveyId || req.query.solutionId) { + let surveyId = req.params._id ? req.params._id : ""; + + surveyDetails = await surveysHelper.detailsV3( + req.body, + surveyId, + req.query.solutionId, + req.userDetails.userId, + req.userDetails.userToken + ); + } else { + let bodyData = req.body ? req.body : {}; + + surveyDetails = await surveysHelper.getDetailsByLink2( + req.params._id, + req.userDetails.userId, + req.userDetails.userToken, + bodyData + ); + } + + await cache.setEx( + `surveyDetails:${req.params._id}`, + redis.expiry, + JSON.stringify({ + message: surveyDetails.message, + result: surveyDetails.data, + }) + ); + + return resolve({ + message: surveyDetails.message, + result: surveyDetails.data, + }); + } + } catch (error) { + return reject({ + status: error.status || httpStatusCode.internal_server_error.status, + message: + error.message || httpStatusCode.internal_server_error.message, + errorObject: error, + }); + } + }); + } +}; diff --git a/envVariables.js b/envVariables.js index bcd4113d..97396511 100644 --- a/envVariables.js +++ b/envVariables.js @@ -85,7 +85,11 @@ let enviromentVariables = { "DISABLE_LEARNER_SERVICE_ON_OFF": { "message" : "Disable learner service on/off", "default": "ON" - } + }, + "REDIS_URL": { + "message" : "Required redis url", + "optional" : false +}, } let success = true; diff --git a/generics/messageConstants/endpoints.js b/generics/messageConstants/endpoints.js index fc7bdb00..094d6823 100644 --- a/generics/messageConstants/endpoints.js +++ b/generics/messageConstants/endpoints.js @@ -18,5 +18,7 @@ module.exports = { GET_PROJECT_TEMPLATE_LISTS : "/v1/project/templates/listByIds", DOWNLOADABLE_FILE_URL: "/v1/cloud-services/files/getDownloadableUrl", USER_READ : "/user/v1/read", - USER_READ_V5 : "/v5/user/read" + USER_READ_V5 : "/v5/user/read", + QUESTIONSET_READ: "api/questionset/v1/hierarchy", + QUESTION_READ: "api/question/v1/read" } \ No newline at end of file diff --git a/generics/services/questions.js b/generics/services/questions.js new file mode 100644 index 00000000..3e3c1a91 --- /dev/null +++ b/generics/services/questions.js @@ -0,0 +1,141 @@ +const request = require("request"); +const hostUrl = process.env.VDN_HOST; + + +const genToken = async () => { + return new Promise(async (resolve, reject) => { + const query = { + username: process.env.SUNBIRD_USER, + password: process.env.SUNBIRD_PWD, + grant_type: process.env.SUNBIRD_GRANT, + client_id: process.env.SUNBIRD_CLIENT, + client_secret: process.env.SUNBIRD_CLIENT_SECRET, + }; + + const url = hostUrl + "/auth/realms/sunbird/protocol/openid-connect/token"; + request.post(url, { form: { ...query } }, genTokenCallback); + + function genTokenCallback(err, res, body) { + if (err) { + return reject(err); + } else { + return resolve(res?.data?.access_token || ""); + } + } + }); + + // return res ? res.data.access_token : ""; +}; + +const questionset = function (questionsetId = "") { + return new Promise(async (resolve, reject) => { + try { + const token = await genToken(); + + console.log("tokejekkeke", token); + let url = hostUrl + messageConstants.endpoints.QUESTIONSET_READ; + + if (questionsetId !== "") { + url = url + "/" + questionsetId + "?" + "mode=edit"; + } + + const options = { + headers: { + "content-type": "application/json", + Authorization: process.env.VDN_AUTHORIZATION, + "x-authenticated-user-token": token, + }, + }; + + request.get(url, options, questionSetReadCallback); + let result = { + success: true, + }; + function questionSetReadCallback(err, data) { + if (err) { + console.log("errr", err); + result.success = false; + } else { + let response = JSON.parse(data.body); + if (response.responseCode === httpStatusCode["ok"].code) { + result["data"] = response?.result?.questionSet; + } else { + result.success = false; + } + } + + return resolve(result); + } + setTimeout(function () { + return resolve( + (result = { + success: false, + }) + ); + }, messageConstants.common.SERVER_TIME_OUT); + } catch (error) { + console.log("errrorr", error); + return reject(error); + } + }); +}; + +const question = function (questionId = "") { + return new Promise(async (resolve, reject) => { + try { + console.log(); + const token = await genToken(); + let url = hostUrl + messageConstants.endpoints.QUESTION_READ; + + if (questionId !== "") { + url = + url + + "/" + + questionId + + "?" + + "fields=body,question,primaryCategory,mimeType,qType,answer,templateId,responseDeclaration,interactionTypes,interactions,name,solutions,editorState,media,remarks,evidence,hints,instructions,name,numberOnly,characterLimit,showEvidence,evidenceMimeType,showRemarks,remarksLimit,markAsNotMandatory"; + } + + const options = { + headers: { + "content-type": "application/json", + Authorization: process.env.VDN_AUTHORIZATION, + "x-authenticated-user-token": token, + }, + }; + + request.get(url, options, questionReadCallback); + let result = { + success: true, + }; + function questionReadCallback(err, data) { + if (err) { + result.success = false; + } else { + let response = JSON.parse(data.body); + if (response.responseCode === httpStatusCode["ok"].code) { + result["data"] = response?.result?.question; + } else { + result.success = false; + } + } + + return resolve(result); + } + setTimeout(function () { + return resolve( + (result = { + success: false, + }) + ); + }, messageConstants.common.SERVER_TIME_OUT); + } catch (error) { + return reject(error); + } + }); +}; + +module.exports = { + questionset: questionset, + question: question, +}; diff --git a/healthCheck/healthCheckService.js b/healthCheck/healthCheckService.js index ab792f74..fe26479b 100644 --- a/healthCheck/healthCheckService.js +++ b/healthCheck/healthCheckService.js @@ -11,6 +11,7 @@ const kendraHealthCheck = require("./kendra"); const mongodbHealthCheck = require("./mongodb"); const kafkaHealthCheck = require("./kafka"); const improvementHealthCheck = require("./improvement-project"); +const redisHealthCheck = require("./redis"); const obj = { MONGO_DB: { @@ -33,6 +34,11 @@ const obj = { FAILED_CODE: 'IMPROVEMENT_SERVICE_HEALTH_FAILED', FAILED_MESSAGE: 'Improvement service is not healthy' }, + REDIS: { + NAME: 'Redis.db', + FAILED_CODE: 'REDIS_HEALTH_FAILED', + FAILED_MESSAGE: 'Redis db is not connected' + }, NAME: 'AssessmentServiceHealthCheck', API_VERSION: '1.0' } @@ -42,16 +48,18 @@ let health_check = async function(req,res) { let checks = []; let mongodbConnection = await mongodbHealthCheck.health_check(); let kafkaConnection = await kafkaHealthCheck.health_check(); - + let coreServiceStatus = await kendraHealthCheck.health_check(); let elasticSearchConnection = await elasticSearchHealthCheck.health_check(); let improvementHealthCheckStatus = await improvementHealthCheck.health_check(); + let redisConnection = await redisHealthCheck.health_check(); checks.push(checkResult("KAFKA",kafkaConnection)); checks.push(checkResult("MONGO_DB",mongodbConnection)); checks.push(checkResult("KENDRA_SERVICE",coreServiceStatus)); checks.push(checkResult("IMPROVEMENT_SERVICE",improvementHealthCheckStatus)); + checks.push(checkResult("REDIS", redisConnection)); let checkServices = checks.filter( check => check.healthy === false); diff --git a/healthCheck/redis.js b/healthCheck/redis.js new file mode 100644 index 00000000..9baa83bc --- /dev/null +++ b/healthCheck/redis.js @@ -0,0 +1,25 @@ +// Dependencies +const redis = require("redis"); + +function health_check() { + return new Promise( async (resolve,reject) => { + + const db = redis.createClient(process.env.REDIS_URL); + + console.log("health_check", db); + + db.on("error", function () { + return resolve(false) + }); + + db.once("open", function() { + console.log("Redis connection open") + db.close(function(){}); + return resolve(true); + }); + }) +} + +module.exports = { + health_check : health_check +} \ No newline at end of file diff --git a/module/surveys/helper.js b/module/surveys/helper.js index 9f9fb23a..cada4fdc 100644 --- a/module/surveys/helper.js +++ b/module/surveys/helper.js @@ -21,6 +21,7 @@ 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"); +const transFormationHelper = require(MODULES_BASE_PATH + "/transformation/helper"); /** * SurveysHelper @@ -1808,4 +1809,405 @@ module.exports = class SurveysHelper { }) } + static getDetailsByLink2(link= "", userId= "", token= "", roleInformation= {},version = "") { + return new Promise(async (resolve, reject) => { + try { + + if (link == "") { + throw new Error(messageConstants.apiResponses.LINK_REQUIRED_CHECK) + } + + if (userId == "") { + throw new Error(messageConstants.apiResponses.USER_ID_REQUIRED_CHECK) + } + + if(token == "") { + throw new Error(messageConstants.apiResponses.REQUIRED_USER_AUTH_TOKEN); + } + + + console.log(); + console.log("getDetailsByLink"); + console.log(); + + let solutionDocument = await solutionsHelper.solutionDocuments + ( + { + link: link, + }, + [ + "externalId", + "name", + "description", + "type", + "endDate", + "status", + "programId", + "programExternalId", + "isAPrivateProgram" + ] + ) + console.log(); + console.log("solutionDocument", solutionDocument); + console.log(); + + if (!solutionDocument.length) { + throw new Error(messageConstants.apiResponses.SOLUTION_NOT_FOUND) + } + + if ( version === "" ) { + + if (new Date() > new Date(solutionDocument[0].endDate)) { + + if (solutionDocument[0].status == messageConstants.common.ACTIVE_STATUS) { + await solutionsHelper.updateSolutionDocument + ( + { link : link }, + { $set : { status: messageConstants.common.INACTIVE_STATUS } } + ) + } + + throw new Error(messageConstants.apiResponses.LINK_IS_EXPIRED) + } + } + + let surveyDocument = await this.surveyDocuments + ( + { solutionId: solutionDocument[0]._id, + createdBy: userId }, + ["_id"] + ) + + console.log(); + console.log("surveyDocument", surveyDocument); + console.log(); + let surveyId; + + if (surveyDocument.length > 0) { + surveyId = surveyDocument[0]._id; + } + else { + + let createSurveyDocument = await this.createSurveyDocument + ( + userId, + solutionDocument[0] + // userOrgDetails[userId] + ) + + if (!createSurveyDocument.success) { + throw new Error(messageConstants.apiResponses.SURVEY_CREATION_FAILED) + } + + surveyId = createSurveyDocument.data._id; + } + + let validateSurvey = await this.validateSurvey + ( + surveyId, + userId, + version && version === messageConstants.common.VERSION_3 ? false : true + ) + + if (!validateSurvey.success) { + return resolve(validateSurvey); + } + + let surveyDetails = await this.detailsV4 + ( + surveyId, + userId, + validateSurvey.data.submissionId, + roleInformation, + token + ) + + if (!surveyDetails.success) { + return resolve(surveyDetails); + } + + return resolve({ + success: true, + message: surveyDetails.message, + data: surveyDetails.data + }); + + + } catch (error) { + return resolve({ + success: false, + message: error.message, + data: false + }); + } + }); + } + + + static detailsV4(surveyId = "", userId= "", submissionId = "", roleInformation = {}, userToken ="") { + return new Promise(async (resolve, reject) => { + try { + + if (surveyId == "") { + throw new Error(messageConstants.apiResponses.SURVEY_ID_REQUIRED) + } + + if (userId == "") { + throw new Error(messageConstants.apiResponses.USER_ID_REQUIRED_CHECK) + } + + let surveyDocument = await this.surveyDocuments + ( + { + _id: surveyId, + status: messageConstants.common.PUBLISHED, + isDeleted: false + } + ) + + if (!surveyDocument.length) { + throw new Error(messageConstants.apiResponses.SURVEY_NOT_FOUND) + } + + surveyDocument = surveyDocument[0]; + + let solutionQueryObject = { + _id: surveyDocument.solutionId, + status: messageConstants.common.ACTIVE_STATUS, + isDeleted: false + }; + + let solutionDocumentProjectionFields = await this.solutionDocumentProjectionFieldsForDetailsAPI(); + + let solutionDocument = await solutionsHelper.solutionDocuments( + solutionQueryObject, [ + ...solutionDocumentProjectionFields, + "migratedId", + "type" + ] + + ) + + if (!solutionDocument.length) { + throw new Error(messageConstants.apiResponses.SOLUTION_NOT_FOUND) + } + const migratedId = solutionDocument[0]?.migratedId; + + solutionDocument = solutionDocument[0]; + + let endDateCheckRequired = true; + + if ( submissionId != "" ) { + let submissionDocument = await surveySubmissionsHelper.surveySubmissionDocuments( + { + _id: submissionId + }, + [ + "status" + ] + ); + + if(submissionDocument && submissionDocument.length > 0){ + if ( submissionDocument[0].status == messageConstants.common.SUBMISSION_STATUS_COMPLETED ){ + endDateCheckRequired = false; + } + } + } + + if (endDateCheckRequired) { + + if (new Date() > new Date(solutionDocument.endDate)) { + if (solutionDocument.status == messageConstants.common.ACTIVE_STATUS) { + await solutionHelper.updateSolutionDocument + ( + { _id : solutionDocument._id }, + { $set : { status: messageConstants.common.INACTIVE_STATUS } } + ) + } + + throw new Error(messageConstants.apiResponses.LINK_IS_EXPIRED) + } + } + + let programDocument = []; + + if (surveyDocument.programId) { + + let programQueryObject = { + _id: surveyDocument.programId, + status: messageConstants.common.ACTIVE_STATUS, + components: { $in: [ObjectId(surveyDocument.solutionId)] } + }; + + programDocument = await programsHelper.list( + programQueryObject, + [ + "externalId", + "name", + "description", + "imageCompression", + "isAPrivateProgram" + ] + ); + } + + let solutionDocumentFieldList = await this.solutionDocumentFieldListInResponse(); + + let result = {}; + + result.solution = await _.pick(solutionDocument, solutionDocumentFieldList); + + if (programDocument.length > 0) { + result.program = programDocument[0]; + } + + let assessment = {}; + + assessment.name = solutionDocument.name; + assessment.description = solutionDocument.description; + assessment.externalId = solutionDocument.externalId; + + let criteriaId = solutionDocument.themes[0].criteria[0].criteriaId; + let weightage = solutionDocument.themes[0].criteria[0].weightage; + + let criteriaQuestionDocument = await criteriaQuestionsHelper.list( + { _id: criteriaId }, + "all", + [ + "resourceType", + "language", + "keywords", + "concepts" + ] + ) + + let evidenceMethodArray = {}; + let submissionDocumentEvidences = {}; + let submissionDocumentCriterias = []; + + Object.keys(solutionDocument.evidenceMethods).forEach(solutionEcm => { + + solutionDocument.evidenceMethods[solutionEcm].startTime = ""; + solutionDocument.evidenceMethods[solutionEcm].endTime = ""; + solutionDocument.evidenceMethods[solutionEcm].isSubmitted = false; + solutionDocument.evidenceMethods[solutionEcm].submissions = new Array; + }) + + submissionDocumentEvidences = solutionDocument.evidenceMethods; + + + let evidences = {}; + + if (!!migratedId) { + console.log("migrattetemigrattetemigrattete", migratedId); + solutionDocument._id = migratedId; + evidences = await transFormationHelper.getQuestionSetHierarchy(migratedId, submissionDocumentCriterias, solutionDocument); + } + + let criteria = criteriaQuestionDocument[0]; + + criteria.weightage = weightage; + + + if (submissionId !== "") { + assessment.submissionId = submissionId; + + let surveySubmissionDocument = + await surveySubmissionsHelper.surveySubmissionDocuments( + { + _id : submissionId + },["evidences"] + ); + + submissionDocumentEvidences = surveySubmissionDocument[0].evidences; + + } else { + let submissionDocument = { + solutionId: solutionDocument._id, + solutionExternalId: solutionDocument.externalId, + surveyId: surveyDocument._id, + createdBy: surveyDocument.createdBy, + evidenceSubmissions: [], + status: messageConstants.common.SUBMISSION_STATUS_STARTED, + evidences: submissionDocumentEvidences, + evidencesStatus: Object.values(submissionDocumentEvidences), + criteria: evidences.submissionDocumentCriterias, + surveyInformation: { + ..._.omit(surveyDocument, ["_id", "deleted", "__v"]) + }, + isAPrivateProgram: surveyDocument.isAPrivateProgram + }; + 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 + // ( { code : roleInformation.role }, + // [ "_id"] + // ) + + // if (roleDocument.length > 0) { + // roleInformation.roleId = roleDocument[0]._id; + // } + + submissionDocument.userRoleInformation = roleInformation; + } + + if (programDocument.length > 0) { + submissionDocument.programId = programDocument[0]._id; + submissionDocument.programExternalId = programDocument[0].externalId; + } + + let submissionDoc = await database.models.surveySubmissions.create( + submissionDocument + ); + + if (submissionDoc._id) { + assessment.submissionId = submissionDoc._id; + } + } + + const parsedAssessment = await assessmentsHelper.parseQuestionsV2( + Object.values(evidenceMethodArray), + ["A1"], + submissionDocumentEvidences, + (solutionDocument && solutionDocument.questionSequenceByEcm) ? solutionDocument.questionSequenceByEcm : false + ); + + assessment.evidences = evidences.evidences; + assessment.submissions = parsedAssessment.submissions; + if (parsedAssessment.generalQuestions && parsedAssessment.generalQuestions.length > 0) { + assessment.generalQuestions = parsedAssessment.generalQuestions; + } + + result.assessment = assessment; + + return resolve({ + success: true, + message: messageConstants.apiResponses.SURVEY_DETAILS_FETCHED_SUCCESSFULLY, + data: result + }); + + } + catch (error) { + return reject({ + success: false, + message: error.message, + data: false + }); + } + }) + } + } \ No newline at end of file diff --git a/module/transformation/helper.js b/module/transformation/helper.js new file mode 100644 index 00000000..1c7a1752 --- /dev/null +++ b/module/transformation/helper.js @@ -0,0 +1,351 @@ +// Dependencies. +const { isEmpty, find, capitalize } = require("lodash"); +const { question, questionset } = require("../../generics/services/questions"); +const { baseAssessment, questionType } = require("../../templates/questions"); +const { + criteriaTemplate, + defaultCriteria, + assessmentTemplate, +} = require("../../templates/questionset"); + +module.exports = class Transformation { + static getQuestionSetHierarchy( + migratedId, + submissionDocumentCriterias, + solutionDocument, + isPageQuestionsRequired = true + ) { + return new Promise(async (resolve, reject) => { + const res = await questionset(migratedId).catch((err) => { + console.log("Error", err?.response?.data); + reject(err?.response?.data); + }); + const questionSetHierarchy = res?.data; + const migratedCriteriaQuestions = questionSetHierarchy?.children || []; + const evidences = await this.questionEvidences( + migratedCriteriaQuestions, + submissionDocumentCriterias, + isPageQuestionsRequired + ); + + assessmentTemplate.assessment.evidences[0].name = capitalize( + solutionDocument?.type + ); + assessmentTemplate.assessment.evidences[0].sections[0].name = `${capitalize( + solutionDocument?.type + )} Questions`; + + assessmentTemplate.assessment.evidences[0].sections[0].questions = + evidences?.evidenceSections || []; + assessmentTemplate.assessment.evidences[0].sections[0].code = "SQ"; + assessmentTemplate.assessment.evidences[0].code = + assessmentTemplate.assessment.evidences[0].externalId = "SF"; + assessmentTemplate.assessment.evidences[0].description = + questionSetHierarchy?.descripton || ""; + + resolve({ + ...evidences, + evidences: assessmentTemplate.assessment.evidences, + }); + }); + } + + static questionEvidences( + criteriaQuestions, + submissionDocumentCriterias, + isPageQuestionsRequired + ) { + return new Promise(async (resolve, reject) => { + const evidenceSections = []; + for (let i = 0; i < criteriaQuestions.length; i++) { + const criteria = criteriaQuestions[i]; + const assessment = { ...baseAssessment }; + const criteriaObj = {}; + + for (let key in criteriaTemplate) { + if (key === "createdFor") { + criteriaObj[key] = criteria[criteriaTemplate[key]] + ? [criteria[criteriaTemplate[key]]] + : []; + } else if (defaultCriteria.includes(key)) { + criteriaObj[key] = criteriaTemplate[key]; + } else if (!isEmpty(criteriaTemplate[key])) { + criteriaObj[key] = criteria[criteriaTemplate[key]] || ""; + } + } + + assessment.page = "p" + (i + 1); + + const children = criteria?.children || []; + + + if (isPageQuestionsRequired && children.length > 0) { + const pageQuestions = await this.getPageQuestions( + criteria, + children, + [] + ); + + const isMatrixQuestion = criteria?.instances?.label; + + let matrixQuestion = {}; + if (isMatrixQuestion) { + matrixQuestion = this.getMatrixQuestions(criteria); + matrixQuestion.instanceQuestions = pageQuestions; + assessment.pageQuestions = matrixQuestion; + } else { + assessment.pageQuestions = pageQuestions; + } + } + submissionDocumentCriterias.push(criteriaObj); + if (children.length > 0) evidenceSections.push(assessment); + } + resolve({ evidenceSections, submissionDocumentCriterias }); + }); + } + + static getPageQuestions(criteria, children, pageQuestions) { + const readQuestions = []; + return new Promise(async (resolve, reject) => { + + + + for (let j = 0; j < children.length; j++) { + const res = await question(children[j]?.identifier); + + let childData = res?.data; + readQuestions.push(childData); + + // branchingLogic + const branching = criteria?.branchingLogic; + + if (!isEmpty(branching) && childData) { + childData = await this.updateChildDataWithBranching( + branching, + childData, + readQuestions, + children + ); + } + + + // question transformation + const childTemplate = await this.tranformQuestionData( + {}, + childData, + j, + children[j] + ); + + pageQuestions.push(childTemplate); + } + + + resolve(pageQuestions); + }); + } + + static getTemplateType(childData) { + const responseType = childData?.primaryCategory?.toLowerCase(); + let type = ""; + if (responseType === "text") { + type = + childData?.interactions.response1.type.number.toLowerCase() === "yes" + ? "number" + : "text"; + } else if (responseType === "multiselect multiple choice question") { + type = + childData?.responseDeclaration?.response1?.cardinality.toLowerCase() === + "single" + ? "radio" + : "multiselect"; + } else { + type = responseType; + } + return type; + } + static updateChildDataWithBranching( + branching, + childData, + readQuestions, + children + ) { + return new Promise(async (resolve, reject) => { + if (branching.hasOwnProperty(childData?.identifier)) { + const ques = branching[childData.identifier]; + if (ques?.target?.length > 0) { + childData.children = ques.target || []; + childData.options = childData?.interactions?.response1.options; + } else if (!isEmpty(ques?.preCondition)) { + const operator = Object.keys(ques?.preCondition?.and[0]); + + const index = ques?.preCondition?.and[0][operator]; + + + let branchingQue = find(readQuestions, { + identifier: ques?.source[0], + }); + let branchingQueId = !isEmpty(branchingQue) + ? branchingQue?.identifier + : ""; + + if (!branchingQueId) { + branchingQue = find(children, { identifier: ques?.source[0] }); + branchingQueId = branchingQue?.identifier; + const res = await question(branchingQueId); + branchingQue = res?.data; + } + + const i = index[1] === -1 ? 0 : index[1]; + + const visibleIf = [ + { + operator: operator[0] === "eq" ? "===" : "!==", + value: [branchingQue?.interactions?.response1.options[i]?.value], + _id: ques?.source[0], + }, + ]; + childData.visibleIf = visibleIf || []; + } + resolve(childData); + } + resolve(childData); + }); + } + + static tranformQuestionData(childQuestion, childData, index, child) { + + const type = this.getTemplateType(childData); + + + return new Promise((resolve, reject) => { + for (let key in questionType[type]) { + const keyData = questionType[type][key]; + if (questionType.defaultFields.includes(key)) { + childQuestion[key] = keyData; + } else if (questionType.arrayFields.includes(key)) { + + childQuestion[key] = childData[keyData] || []; + } else if (key === "question") { + const questionData = []; + let str = childData[keyData] ? childData[keyData] : ""; + while (str.length > 0) { + const startIndex = str.indexOf("

"); + if (startIndex > -1) { + const start = str.slice(startIndex + 3); + const endIndex = start.indexOf("

"); + let end = start.slice(0, endIndex); + + end = end.replace(" ", ""); + + questionData.push(end); + + str = start.slice(endIndex); + } else str = ""; + } + + childQuestion[key] = questionData; + } else if (key === "validation") { + const obj = {}; + for (const cKey in keyData) { + if (cKey === "required") { + const requir = childData?.interactions?.validation?.required; + + obj[cKey] = + requir === true || requir === "Yes" || requir === "yes" + ? true + : false; + } else if (cKey === "IsNumber") { + const typen = childData?.interactions?.response1?.type?.number; + obj[cKey] = typen.toLowerCase() === "yes" ? true : false; + } else if (cKey === "max" || cKey === "min") { + const slider = + type === "slider" + ? childData?.interactions.response1.validation.range[cKey] + : type === "date" + ? childData?.interactions.validation[cKey] + : ""; + obj[cKey] = slider; + } + } + childQuestion[key] = obj; + } else if (key === "payload") { + childQuestion[key] = { + ...keyData, + criteriaId: childData.identifier, + responseType: type, + }; + } else if (key === "file") { + if ( + childData[keyData["type"]] && + childData[keyData["type"]].length > 0 + ) { + const requir = childData?.interactions?.validation?.required; + childQuestion[key] = { + ...keyData, + required: + requir === true || requir === "Yes" || requir === "yes" + ? "Yes" + : "No", + type: childData?.evidence?.type, + }; + } else childQuestion[key] = ""; + } else if (key === "updatedAt" || key === "createdAt") { + childQuestion[key] = child[keyData]; + } else if (key === "dateFormat") { + childQuestion[key] = + childData?.interactions?.response1?.validation?.pattern || ""; + } else if (key === "options") { + childQuestion[key] = + childData?.interactions?.response1?.options || []; + } else if (key === "showRemarks" || key === "autoCapture") { + const typen = childData[keyData]; + childQuestion[key] = + key !== "autoCapture" + ? typen === true || typen === "Yes" || typen === "yes" + ? true + : false + : childData[keyData] || false; + } else if (key === "questionNumber" || key === "page") { + childQuestion[key] = + key === "questionNumber" ? `${index + 1}` : baseAssessment.page; + } else { + childQuestion[key] = childData[keyData] + ? childData[keyData] + : child[keyData] + ? child[keyData] + : ""; + + } + } + resolve(childQuestion); + }); + } + + static getMatrixQuestions(criteria) { + const matrixObj = {}; + for (let key in questionType.matrix) { + if (key === "instanceIdentifier") { + matrixObj["instanceIdentifier"] = criteria?.instances?.label || ""; + } else if (key === "payload") { + matrixObj[key] = { + ...questionType.matrix.payload, + criteriaId: criteria.identifier, + responseType: "matrix", + }; + } else if (questionType.arrayFields.includes(key)) { + matrixObj[key] = + key !== "children" ? criteria[questionType.matrix[key]] : []; + } else if ( + questionType.defaultFields.includes(key) || + key === "validation" + ) { + matrixObj[key] = questionType.matrix[key] || ""; + } else if (key !== "validation") { + matrixObj[key] = criteria[questionType.matrix[key]] || ""; + } + } + + return matrixObj; + } +}; diff --git a/package.json b/package.json index 76c0823b..499dc1e1 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "author": "Akash Shah ", "license": "ISC", "dependencies": { + "axios": "^0.26.1", "body-parser": "^1.18.2", "bunyan": "^1.8.12", "cache-manager": "^2.9.0", @@ -45,6 +46,7 @@ "jwt-decode": "^2.2.0", "kafka-node": "^4.1.3", "keycloak-auth-utils": "^3.3.0", + "lodash": "^4.17.21", "log": "^1.4.0", "mathjs": "^5.2.3", "mkdir": "0.0.2", @@ -59,14 +61,12 @@ "morgan": "^1.9.0", "p-each-series": "^2.1.0", "path": "^0.12.7", + "querystring": "^0.2.1", + "redis": "^4.2.0", "request": "^2.88.0", "require-all": "^2.2.0", "uuid": "^3.3.2", - "yaml-js": "^0.1.3", - "axios": "^0.26.1", - "lodash": "^4.17.21", - "querystring": "^0.2.1", - "winston": "^3.8.1" + "yaml-js": "^0.1.3" }, "devDependencies": {} } diff --git a/script/constant/config.js b/script/constant/config.js deleted file mode 100644 index 55b7790e..00000000 --- a/script/constant/config.js +++ /dev/null @@ -1,61 +0,0 @@ -require("dotenv").config(); - -const CONFIG = { - DB: { - DB_HOST: process.env.MONGODB_URL, - DB_NAME: process.env.DB_NAME, - TABLES: { - solutions: "solutions", - criteriaQuestions: "criteriaQuestions", - questions: "questions", - }, - }, - SUNBIRD: { - HOST: { - sunbird: process.env.SUNBIRD_HOST, - vdn: process.env.VDN_HOST - }, - APIS: { - token: "auth/realms/sunbird/protocol/openid-connect/token", - read_user: "api/user/v5/read/", - search_user: "api/user/v3/search", - create_questionset: "api/questionset/v1/create", - update_hierarchy: "api/questionset/v1/hierarchy/update", - publish_questionset: "api/questionset/v1/publish", - read_questionset: "api/questionset/v1/hierarchy/", - create_question: "api/question/v1/create", - publish_question: "api/question/v1/publish", - create_program: "api/program/v1/create", - update_program: "api/program/v1/update", - add_program_nomination: "api/program/v1/nomination/add", - update_program_nomination: "api/program/v1/nomination/update", - publish_program: "api/program/v1/publish", - }, - config: { - sunbird: { - query: { - username: process.env.SUNBIRD_USER, - password: process.env.SUNBIRD_PWD, - grant_type: process.env.SUNBIRD_GRANT, - client_id: process.env.SUNBIRD_CLIENT, - client_secret: process.env.SUNBIRD_CLIENT_SECRET, - }, - authorization: process.env.SUNBIRD_AUTHORIZATION, - }, - vdn: { - query: { - username: process.env.SUNBIRD_USER, - password: process.env.SUNBIRD_PWD, - grant_type: process.env.SUNBIRD_GRANT, - client_id: process.env.SUNBIRD_CLIENT, - client_secret: process.env.SUNBIRD_CLIENT_SECRET, - }, - authorization: process.env.VDN_AUTHORIZATION, - }, - }, - }, -}; - -module.exports = { - CONFIG, -}; diff --git a/script/logger.js b/script/logger.js deleted file mode 100644 index 790c495b..00000000 --- a/script/logger.js +++ /dev/null @@ -1,42 +0,0 @@ -var path = require('path') -const { createLogger, format, transports } = require('winston'); -const { combine, timestamp, label, printf } = format; -const myFormat = printf(({ level, message, timestamp }) => { - return `${timestamp} ${level}: ${message}`; -}); - -const logLevels = { - error: 0, - warn: 1, - info: 2, - debug: 3, - } -const date = new Date().getDate()+'-'+new Date().getMonth()+'-'+new Date().getFullYear() -const logger = createLogger({ - level: "logLevels", - format: combine( - timestamp(), - myFormat - ), - transports: [ - new transports.File({ filename: path.join(__dirname, `/logs/${date}-error.log`), level: 'error' }), - new transports.File({ filename: path.join(__dirname, `/logs/${date}-info.log`), level: 'info' }), - new transports.File({ filename: path.join(__dirname, `/logs/${date}-debug.log`), level: 'debug' }), - ] -}); - - -error = async(msg) =>{ - logger.error(msg) -} - -debug = async(msg) =>{ - logger.debug(msg) -} -info = async(msg) =>{ - logger.info(msg) -} - -module.exports = { - info, error, debug -} \ No newline at end of file diff --git a/script/README.md b/scripts/creation-portal-migration/README.md similarity index 100% rename from script/README.md rename to scripts/creation-portal-migration/README.md diff --git a/script/api-list/headers.js b/scripts/creation-portal-migration/api-list/headers.js similarity index 68% rename from script/api-list/headers.js rename to scripts/creation-portal-migration/api-list/headers.js index 91839944..56c45b70 100644 --- a/script/api-list/headers.js +++ b/scripts/creation-portal-migration/api-list/headers.js @@ -18,14 +18,14 @@ const genToken = async (url, body, type) => { }); return res ? res.data.access_token : ""; } else { - const token = type === "sunbird" ? this.sunbird_token : this.vdn_token; + const token = type === "base" ? this.base_token : this.vdn_token; return token; } }; const validateToken = (type) => { - const token = type === "sunbird" ? this.sunbird_token : this.vdn_token; + const token = type === "base" ? this.base_token : this.vdn_token; try { jwt.verify(token, "shhhhh"); @@ -40,14 +40,14 @@ const generateToken = async (type) => { let body = {}; switch (type) { - case "sunbird": - url = CONFIG.SUNBIRD.HOST.sunbird + CONFIG.SUNBIRD.APIS.token; - body = querystring.stringify({ ...CONFIG.SUNBIRD.config.sunbird.query }); - this.sunbird_token = await genToken(url, body, "sunbird"); - return this.sunbird_token; + case "base": + url = CONFIG.HOST.base + CONFIG.APIS.token; + body = querystring.stringify({ ...CONFIG.KEYS.BASE.QUERY }); + this.base_token = await genToken(url, body, "base"); + return this.base_token; case "vdn": - url = CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.token; - body = querystring.stringify({ ...CONFIG.SUNBIRD.config.vdn.query }); + url = CONFIG.HOST.vdn + CONFIG.APIS.token; + body = querystring.stringify({ ...CONFIG.KEYS.VDN.QUERY }); this.vdn_token = await genToken(url, body, "vdn"); return this.vdn_token; } @@ -57,20 +57,20 @@ const getHeaders = async (isTokenReq, type) => { let headers = {}; switch (type) { - case "sunbird": + case "base": headers = { "Content-Type": "application/json", - Authorization: CONFIG.SUNBIRD.config.sunbird.authorization, + Authorization: CONFIG.KEYS.BASE.AUTHORIZATION, }; if (isTokenReq) { - headers["x-authenticated-user-token"] = await generateToken("sunbird"); + headers["x-authenticated-user-token"] = await generateToken("base"); } break; case "vdn": headers = { "Content-Type": "application/json", - Authorization: CONFIG.SUNBIRD.config.vdn.authorization, + Authorization: CONFIG.KEYS.VDN.AUTHORIZATION, }; if (isTokenReq) { headers["x-authenticated-user-token"] = await generateToken("vdn"); diff --git a/script/api-list/program.js b/scripts/creation-portal-migration/api-list/program.js similarity index 85% rename from script/api-list/program.js rename to scripts/creation-portal-migration/api-list/program.js index 7a286e6a..67677787 100644 --- a/script/api-list/program.js +++ b/scripts/creation-portal-migration/api-list/program.js @@ -5,7 +5,7 @@ const { getHeaders } = require("./headers"); const createProgram = async (templateData) => { - const url = CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.create_program; + const url = CONFIG.HOST.vdn + CONFIG.APIS.create_program; const data = { request: { ...templateData, @@ -23,7 +23,7 @@ const createProgram = async (templateData) => { }; const updateProgram = async (templateData) => { - const url = CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.update_program; + const url = CONFIG.HOST.vdn + CONFIG.APIS.update_program; const data = { request: { ...templateData, @@ -40,7 +40,7 @@ const updateProgram = async (templateData) => { }; const publishProgram = async (templateData) => { - const url = CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.publish_program; + const url = CONFIG.HOST.vdn + CONFIG.APIS.publish_program; const data = { request: { ...templateData, @@ -58,7 +58,7 @@ const publishProgram = async (templateData) => { }; const nominateProgram = async (program_id, author) => { const url = - CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.add_program_nomination; + CONFIG.HOST.vdn + CONFIG.APIS.add_program_nomination; const data = { request: { program_id: program_id, @@ -94,7 +94,7 @@ const nominateProgram = async (program_id, author) => { const updateContributorToProgram = async (reqData) => { const url = - CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.update_program_nomination; + CONFIG.HOST.vdn + CONFIG.APIS.update_program_nomination; const data = { request: { ...reqData, diff --git a/script/api-list/question.js b/scripts/creation-portal-migration/api-list/question.js similarity index 84% rename from script/api-list/question.js rename to scripts/creation-portal-migration/api-list/question.js index 4720547e..8e3cc3fa 100644 --- a/script/api-list/question.js +++ b/scripts/creation-portal-migration/api-list/question.js @@ -6,7 +6,7 @@ const { getHeaders } = require("./headers"); // Questionset const createQuestionSet = async (templateData) => { - const url = CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.create_questionset; + const url = CONFIG.HOST.vdn + CONFIG.APIS.create_questionset; const data = { request: { questionset: { ...templateData }, @@ -24,7 +24,7 @@ const createQuestionSet = async (templateData) => { }; const updateQuestionSetHierarchy = async (templateData) => { - const url = CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.update_hierarchy; + const url = CONFIG.HOST.vdn + CONFIG.APIS.update_hierarchy; const config = { method: "patch", @@ -39,8 +39,8 @@ const updateQuestionSetHierarchy = async (templateData) => { const publishQuestionSet = async (questionsetId) => { const url = - CONFIG.SUNBIRD.HOST.vdn + - CONFIG.SUNBIRD.APIS.publish_questionset + + CONFIG.HOST.vdn + + CONFIG.APIS.publish_questionset + "/" + questionsetId; const config = { @@ -56,8 +56,8 @@ const publishQuestionSet = async (questionsetId) => { const readQuestionSetHierarchy = async (questionSetId) => { const url = - CONFIG.SUNBIRD.HOST.vdn + - CONFIG.SUNBIRD.APIS.read_questionset + + CONFIG.HOST.vdn + + CONFIG.APIS.read_questionset + questionSetId + "?mode=edit"; @@ -73,7 +73,7 @@ const readQuestionSetHierarchy = async (questionSetId) => { // Questions const createQuestions = async (templateData, questionId) => { - const url = CONFIG.SUNBIRD.HOST.vdn + CONFIG.SUNBIRD.APIS.create_question; + const url = CONFIG.HOST.vdn + CONFIG.APIS.create_question; const data = { request: { question: { ...templateData }, @@ -95,8 +95,8 @@ const createQuestions = async (templateData, questionId) => { const publishQuestion = async (questionId) => { const url = - CONFIG.SUNBIRD.HOST.vdn + - CONFIG.SUNBIRD.APIS.publish_question + + CONFIG.HOST.vdn + + CONFIG.APIS.publish_question + "/" + questionId; const config = { diff --git a/script/api-list/user.js b/scripts/creation-portal-migration/api-list/user.js similarity index 78% rename from script/api-list/user.js rename to scripts/creation-portal-migration/api-list/user.js index 381a59a1..90c604bb 100644 --- a/script/api-list/user.js +++ b/scripts/creation-portal-migration/api-list/user.js @@ -6,25 +6,25 @@ const { getHeaders } = require("./headers"); const readUser = async (userId) => { const params = "organisations,roles,locations,declarations,externalIds"; const url = - CONFIG.SUNBIRD.HOST.sunbird + - CONFIG.SUNBIRD.APIS.read_user + + CONFIG.HOST.base + + CONFIG.APIS.read_user + userId + "?fields=" + params; const res = await axios - .get(url, await getHeaders(true, "sunbird")) + .get(url, await getHeaders(true, "base")) .catch((err) => { logger.error(`Error while reading User: ${JSON.stringify(err?.response?.data)}`) }); }; const searchUser = async (userId) => { - const url = CONFIG.SUNBIRD.HOST.sunbird + CONFIG.SUNBIRD.APIS.search_user; + const url = CONFIG.HOST.base + CONFIG.APIS.search_user; const config = { method: "post", url: url, - headers: await getHeaders(true, "sunbird"), + headers: await getHeaders(true, "base"), data: { request: { filters: { id: userId } }, }, diff --git a/scripts/creation-portal-migration/constant/config.js b/scripts/creation-portal-migration/constant/config.js new file mode 100644 index 00000000..8c7fc5bc --- /dev/null +++ b/scripts/creation-portal-migration/constant/config.js @@ -0,0 +1,60 @@ +require("dotenv").config(); + +const CONFIG = { + DB: { + DB_HOST: process.env.MONGODB_URL, + DB_NAME: process.env.DB_NAME, + TABLES: { + solutions: "solutions", + criteria_questions: "criteriaQuestions", + questions: "questions", + }, + }, + + HOST: { + base: process.env.BASE_HOST, + vdn: process.env.VDN_HOST, + }, + APIS: { + token: "auth/realms/sunbird/protocol/openid-connect/token", + read_user: "api/user/v5/read/", + search_user: "api/user/v3/search", + create_questionset: "api/questionset/v1/create", + update_hierarchy: "api/questionset/v1/hierarchy/update", + publish_questionset: "api/questionset/v1/publish", + read_questionset: "api/questionset/v1/hierarchy/", + create_question: "api/question/v1/create", + publish_question: "api/question/v1/publish", + create_program: "api/program/v1/create", + update_program: "api/program/v1/update", + add_program_nomination: "api/program/v1/nomination/add", + update_program_nomination: "api/program/v1/nomination/update", + publish_program: "api/program/v1/publish", + }, + KEYS: { + BASE: { + QUERY: { + username: process.env.BASE_USER, + password: process.env.BASE_PWD, + grant_type: process.env.BASE_GRANT, + client_id: process.env.BASE_CLIENT, + client_secret: process.env.BASE_CLIENT_SECRET, + }, + AUTHORIZATION: process.env.BASE_AUTHORIZATION, + }, + VDN: { + QUERY: { + username: process.env.BASE_USER, + password: process.env.BASE_PWD, + grant_type: process.env.BASE_GRANT, + client_id: process.env.BASE_CLIENT, + client_secret: process.env.BASE_CLIENT_SECRET, + }, + AUTHORIZATION: process.env.VDN_AUTHORIZATION, + }, + }, +}; + +module.exports = { + CONFIG, +}; diff --git a/script/db/dbConfig.js b/scripts/creation-portal-migration/db/dbConfig.js similarity index 100% rename from script/db/dbConfig.js rename to scripts/creation-portal-migration/db/dbConfig.js diff --git a/script/db/index.js b/scripts/creation-portal-migration/db/index.js similarity index 50% rename from script/db/index.js rename to scripts/creation-portal-migration/db/index.js index 43431c79..2234edb9 100644 --- a/script/db/index.js +++ b/scripts/creation-portal-migration/db/index.js @@ -1,28 +1,15 @@ const { getDBInstance } = require("./dbConfig"); const { ObjectId } = require("mongodb"); - - const findAll = async (clName, query) => { try { const db = await getDBInstance(); return await db .collection(clName) - .find({...query}) + .find({ ...query }) .toArray(); - } catch(err) { - console.log('findAll Error: ', err); - } -}; - -const findOne = async (clName, query) => { - try { - const db = await getDBInstance(); - return await db - .collection(clName) - .findOne({...query}) - } catch(err) { - console.log('findOne Error:', err); + } catch (err) { + console.log("findAll Error: ", err); } }; @@ -31,16 +18,17 @@ const updateById = async (clName, id, query) => { const db = await getDBInstance(); const res = await db .collection(clName) - .updateOne({ _id: ObjectId(id) }, { $set: { ...query } }, { upsert: true }); + .updateOne( + { _id: ObjectId(id) }, + { $set: { ...query } }, + { upsert: true } + ); } catch (err) { - console.log("updateById = ", id, "Error: " , err); + console.log("updateById = ", id, "Error: ", err); } }; - - module.exports = { findAll, updateById, - findOne }; diff --git a/scripts/creation-portal-migration/logger.js b/scripts/creation-portal-migration/logger.js new file mode 100644 index 00000000..e398314f --- /dev/null +++ b/scripts/creation-portal-migration/logger.js @@ -0,0 +1,46 @@ +var path = require("path"); + +var Logger = require("bunyan"); + +const date = + new Date().getDate() + + "-" + + new Date().getMonth() + + "-" + + new Date().getFullYear(); + +var logger = new Logger({ + name: "creation-portal-migration", + streams: [ + { + level: "error", + path: path.join(__dirname, `/logs/${date}-error.log`), + }, + { + level: "debug", + path: path.join(__dirname, `/logs/${date}-debug.log`), + }, + { + level: "info", + path: path.join(__dirname, `/logs/${date}-info.log`), + }, + ], +}); + + +error = async (msg) => { + logger.error(msg); +}; + +debug = async (msg) => { + logger.debug(msg); +}; +info = async (msg) => { + logger.info(msg); +}; + +module.exports = { + info, + error, + debug, +}; diff --git a/script/server.js b/scripts/creation-portal-migration/server.js similarity index 88% rename from script/server.js rename to scripts/creation-portal-migration/server.js index 62e77734..b46522e2 100644 --- a/script/server.js +++ b/scripts/creation-portal-migration/server.js @@ -1,5 +1,4 @@ -require("dotenv").config({path: "./../.env"}); -const { CONFIG } = require("./constant/config"); +require("dotenv").config({path: "./../../.env"}); const { createDBInstance } = require("./db/dbConfig"); const { findAll } = require("./db"); const { @@ -8,10 +7,11 @@ const { const { ObjectId } = require("mongodb"); const logger = require("./logger"); +var fs = require("fs"); +const { CONFIG } = require("./constant/config"); - - +fs.existsSync("logs") || fs.mkdirSync("logs"); const migrateData = async (req, res) => { try { @@ -71,11 +71,11 @@ const migrateData = async (req, res) => { }; const db = await createDBInstance(); // req.query.questionsetID || - // const id = "61a8b0a519c32c0007df55a8"; + const id = "600b21c57ea68a7ed9278873"; - const data = await findAll("solutions", { - // _id: ObjectId(id), - programId: { $exists: true }, + const data = await findAll(CONFIG.DB.TABLES.solutions, { + _id: ObjectId(id), + // programId: { $exists: true }, type: { $in: ["observation", "survey"] }, }); diff --git a/script/template/config/question.js b/scripts/creation-portal-migration/template/config/question.js similarity index 100% rename from script/template/config/question.js rename to scripts/creation-portal-migration/template/config/question.js diff --git a/script/template/config/questionSet.js b/scripts/creation-portal-migration/template/config/questionSet.js similarity index 100% rename from script/template/config/questionSet.js rename to scripts/creation-portal-migration/template/config/questionSet.js diff --git a/script/template/generate/gProgram.js b/scripts/creation-portal-migration/template/generate/gProgram.js similarity index 100% rename from script/template/generate/gProgram.js rename to scripts/creation-portal-migration/template/generate/gProgram.js diff --git a/script/template/generate/gQuestion.js b/scripts/creation-portal-migration/template/generate/gQuestion.js similarity index 100% rename from script/template/generate/gQuestion.js rename to scripts/creation-portal-migration/template/generate/gQuestion.js diff --git a/script/template/generate/gQuestionSet.js b/scripts/creation-portal-migration/template/generate/gQuestionSet.js similarity index 95% rename from script/template/generate/gQuestionSet.js rename to scripts/creation-portal-migration/template/generate/gQuestionSet.js index 7ded652b..f0996763 100644 --- a/script/template/generate/gQuestionSet.js +++ b/scripts/creation-portal-migration/template/generate/gQuestionSet.js @@ -1,15 +1,15 @@ const { ObjectId } = require("mongodb"); const { createQuestionSet } = require("../../api-list/question"); -const { CONFIG } = require("../../constant/config"); +const { CONFIG } = require("./../../constant/config"); const { findAll, updateById } = require("../../db"); const logger = require("../../logger"); const { updateHierarchyTemplate } = require("../helpers/hierarchyHelper"); const { setQuestionSetTemplate } = require("../helpers/questionsetHelper"); const { createProgramTemplate } = require("./gProgram"); const { getCriteriaData, initHierarchy } = require("../migrate/common"); -const { createSection } = require("./../migrate/matrix"); -const { getNonMatrixQuestions } = require("./../migrate/nonmatrix"); +const { createSection } = require("../migrate/matrix"); +const { getNonMatrixQuestions } = require("../migrate/nonmatrix"); const getQuestionSetTemplates = async (solutions, migratedCount) => { const data = Promise.all( @@ -142,7 +142,7 @@ const migrateCriteriaQuestions = async ( let criteriaIds = solution?.themes[0]?.criteria || []; criteriaIds = criteriaIds.map((criteria) => ObjectId(criteria?.criteriaId)); - const criterias = await findAll("criteriaQuestions", { + const criterias = await findAll(CONFIG.DB.TABLES.criteria_questions, { _id: { $in: criteriaIds }, }).catch((err) => {}); @@ -151,7 +151,7 @@ const migrateCriteriaQuestions = async ( let questionIds = criteria?.evidences[0].sections[0]?.questions || []; questionIds = questionIds.map((question) => question?._id); - const criteriaQuestions = await findAll("questions", { + const criteriaQuestions = await findAll(CONFIG.DB.TABLES.questions, { _id: { $in: questionIds }, }).catch((err) => {}); diff --git a/script/template/generate/template.js b/scripts/creation-portal-migration/template/generate/template.js similarity index 100% rename from script/template/generate/template.js rename to scripts/creation-portal-migration/template/generate/template.js diff --git a/script/template/helpers/hierarchyHelper.js b/scripts/creation-portal-migration/template/helpers/hierarchyHelper.js similarity index 99% rename from script/template/helpers/hierarchyHelper.js rename to scripts/creation-portal-migration/template/helpers/hierarchyHelper.js index 38c22695..2cea5fed 100644 --- a/script/template/helpers/hierarchyHelper.js +++ b/scripts/creation-portal-migration/template/helpers/hierarchyHelper.js @@ -12,7 +12,7 @@ const { updateQuestionSetHierarchy, readQuestionSetHierarchy, } = require("../../api-list/question"); -const { CONFIG } = require("../../constant/config"); +const { CONFIG } = require("./../../constant/config"); const { updateById } = require("../../db"); const logger = require("../../logger"); diff --git a/script/template/helpers/questionsetHelper.js b/scripts/creation-portal-migration/template/helpers/questionsetHelper.js similarity index 98% rename from script/template/helpers/questionsetHelper.js rename to scripts/creation-portal-migration/template/helpers/questionsetHelper.js index 66517965..5ad229f6 100644 --- a/script/template/helpers/questionsetHelper.js +++ b/scripts/creation-portal-migration/template/helpers/questionsetHelper.js @@ -1,7 +1,7 @@ const { capitalize, isEmpty, get } = require("lodash"); const { ObjectId } = require("mongodb"); const { createQuestions, publishQuestion } = require("../../api-list/question"); -const { CONFIG } = require("../../constant/config"); +const { CONFIG } = require("./../../constant/config"); const { updateById, findAll } = require("../../db"); const logger = require("../../logger"); const { diff --git a/script/template/migrate/common.js b/scripts/creation-portal-migration/template/migrate/common.js similarity index 99% rename from script/template/migrate/common.js rename to scripts/creation-portal-migration/template/migrate/common.js index 747866e5..90914c9b 100644 --- a/script/template/migrate/common.js +++ b/scripts/creation-portal-migration/template/migrate/common.js @@ -4,7 +4,7 @@ const { getQuestionFromDB, createQuestionTemplate, } = require("../helpers/questionsetHelper"); -const logger = require("./../../logger"); +const logger = require("../../logger"); const initHierarchy = (questionsetid, solution, programId, migratedId) => { return { diff --git a/script/template/migrate/matrix.js b/scripts/creation-portal-migration/template/migrate/matrix.js similarity index 99% rename from script/template/migrate/matrix.js rename to scripts/creation-portal-migration/template/migrate/matrix.js index 7e7c57dc..2c10100a 100644 --- a/script/template/migrate/matrix.js +++ b/scripts/creation-portal-migration/template/migrate/matrix.js @@ -12,7 +12,7 @@ const { const { getSectionTemplate } = require("../generate/template"); const { compact, isEmpty } = require("lodash"); -const logger = require("./../../logger"); +const logger = require("../../logger"); const createSection = async ( type, diff --git a/script/template/migrate/nonmatrix.js b/scripts/creation-portal-migration/template/migrate/nonmatrix.js similarity index 99% rename from script/template/migrate/nonmatrix.js rename to scripts/creation-portal-migration/template/migrate/nonmatrix.js index 157f6771..cfc8fe48 100644 --- a/script/template/migrate/nonmatrix.js +++ b/scripts/creation-portal-migration/template/migrate/nonmatrix.js @@ -14,7 +14,7 @@ const { } = require("../helpers/hierarchyHelper"); const { createSection } = require("./matrix"); -const logger = require("./../../logger"); +const logger = require("../../logger"); const updateNonMatrixHierarchyChildren = ( diff --git a/templates/questions.js b/templates/questions.js new file mode 100644 index 00000000..111b2948 --- /dev/null +++ b/templates/questions.js @@ -0,0 +1,205 @@ + +const baseAssessment = { + _id: "", + question: "", + isCompleted: "", + showRemarks: "", + options: "", + sliderOptions: "", + children: "", + questionGroup: "", + fileName: "", + instanceQuestions: "", + isAGeneralQuestion: "", + autoCapture: "", + allowAudioRecording: "", + prefillFromEntityProfile: "", + entityFieldName: "", + isEditable: "", + showQuestionInPreview: "", + deleted: "", + remarks: "", + value: "", + usedForScoring: "", + questionType: "", + canBeNotApplicable: "", + visibleIf: "", + validation: "", + file: "", + externalId: "", + tip: "", + hint: "", + responseType: "pageQuestions", + modeOfCollection: "", + accessibility: "", + rubricLevel: "", + sectionHeader: "", + page: "p1", + questionNumber: "", + updatedAt: "", + createdAt: "", + __v: "", + createdFromQuestionId: "", + evidenceMethod: "", + payload: "", + startTime: "", + endTime: "", + gpsLocation: "", + dateFormat: "", + instanceIdentifier: "", + pageQuestions: [], + +}; + +const baseQuestion = { + _id: "identifier", + question: 'body', + isCompleted: false, + showRemarks: "showRemarks", + options: "interactions.response1.options", + sliderOptions: "sliderOptions", + children: "children", + questionGroup: ["A1"], + fileName: "fileName", + instanceQuestions: "instanceQuestions", + isAGeneralQuestion: false, + autoCapture: "autocapture", + allowAudioRecording: false, + prefillFromEntityProfile: false, + entityFieldName: "", + isEditable: true, + showQuestionInPreview: false, + deleted: false, + remarks: "", + value: "", + usedForScoring: "", + questionType: "auto", + canBeNotApplicable: false, + visibleIf: "visibleIf", + validation: { + required: "interactions.validation.required", + }, + file: { + required: true, + type: "evidence.mimeType", + minCount: 0, + maxCount: 10, + caption: "FALSE", + }, + externalId: "code", + tip: "", + hint: "hints", + responseType: "primaryCategory", + modeOfCollection: "onfield", + accessibility: "No", + rubricLevel: "", + sectionHeader: "", + page: "", + questionNumber: "", + updatedAt: "lastUpdatedOn", + createdAt: "createdOn", + __v: 0, + createdFromQuestionId: "", + evidenceMethod: "OB", + payload: { + criteriaId: "sectionId", + responseType: "primaryCategory", + evidenceMethod: "OB", + rubricLevel: "", + }, + startTime: "", + endTime: "", + gpsLocation: "", + dateFormat: "", + instanceIdentifier: "", +}; + +const questionType = { + text: { + ...baseQuestion, + responseType: "text", + }, + number: { + ...baseQuestion, + responseType: "number", + validation: { + ...baseQuestion.validation, + IsNumber: "interactions.response1.type.number", + }, + }, + slider: { + ...baseQuestion, + responseType: "slider", + validation: { + ...baseQuestion.validation, + max: "interactions.response1.validation.range.max", + min: "interactions.response1.validation.range.min", + }, + }, + date: { + ...baseQuestion, + responseType: "date", + validation: { + ...baseQuestion.validation, + max: "interactions.validation.max", + min: "interactions.validation.min", + }, + dateFormat: "interactions.response1.validation.pattern", + }, + multiselect: { + ...baseQuestion, + responseType: "multiselect", + }, + radio: { + ...baseQuestion, + responseType: "radio", + }, + + defaultFields: [ + "__v", + "responseType", + "isCompleted", + "questionGroup", + "isAGeneralQuestion", + "allowAudioRecording", + "prefillFromEntityProfile", + "entityFieldName", + "isEditable", + "showQuestionInPreview", + "deleted", + "remarks", + "value", + "usedForScoring", + "questionType", + "canBeNotApplicable", + "tip", + "modeOfCollection", + "accessibility", + "rubricLevel", + "sectionHeader", + "_v", + "createdFromQuestionId", + "evidenceMethod", + "startTime", + "endTime", + "gpsLocation", + ], + + arrayFields: ["sliderOptions", "children", "fileName", "instanceQuestions"], + + matrix: { + ...baseQuestion, + instanceIdentifier: "instances", + validation: { + required: true + }, + responseType: 'matrix', + } + +}; + +module.exports = { + baseAssessment, + baseQuestion, + questionType, +}; diff --git a/templates/questionset.js b/templates/questionset.js new file mode 100644 index 00000000..c3a58e33 --- /dev/null +++ b/templates/questionset.js @@ -0,0 +1,381 @@ + +const solutionTemplate = { + _id: "", + externalId: "code", + name: "name", + description: "description", + registry: [], + captureGpsLocationAtQuestionLevel: false, + enableQuestionReadOut: false, + scoringSystem: null, + isRubricDriven: false, +}; + +defaultSolutionTemplateFields = [ + "registry", + "captureGpsLocationAtQuestionLevel", + "enableQuestionReadOut", + "scoringSystem", + "isRubricDriven", +]; +module.exports = { + solutionTemplate, + defaultSolutionTemplateFields, +}; + +const assessmentTemplate = { + assessment: { + evidences: [ + { + code: "OB", + sections: [ + { + code: "S1", + questions: [], + name: "type Questions", + }, + ], + externalId: "OB", + tip: null, + name: "name", + description: null, + modeOfCollection: "onfield", + canBeNotApplicable: false, + notApplicable: false, + canBeNotAllowed: false, + remarks: null, + startTime: "", + endTime: "", + isSubmitted: false, + submissions: [], + }, + ], + }, +}; + +const questionTemplate = { + date: { + name: "Migrated Question", + code: "externalId", + mimeType: "application/vnd.sunbird.question", + primaryCategory: "responseType", + interactionTypes: "responseType", + showRemarks: "showRemarks", + instructions: { + en: [null], + }, + body: "question", + editorState: { + question: "question", + }, + responseDeclaration: { + response1: { + type: "string", + }, + }, + interactions: { + validation: "validation", + response1: { + validation: { + pattern: "dateFormat", + }, + autoCapture: "autoCapture", + }, + }, + hints: "hint", + }, + slider: { + name: "Migrated Question-Slider Type", + code: "externalId", + mimeType: "application/vnd.sunbird.question", + primaryCategory: "responseType", + interactionTypes: "responseType", + showRemarks: "showRemarks", + instructions: { + en: [null], + }, + body: "question", + editorState: { + question: "question", + }, + responseDeclaration: { + response1: { + type: "integer", + maxScore: 1, + }, + }, + interactions: { + validation: { + required: "validation.required", + }, + response1: { + validation: { + range: { + min: "validation.min", + max: "validation.max", + }, + }, + step: "1", + }, + }, + hints: "hint", + evidence: { + mimeType: "file.type", + }, + }, + multiselect: { + name: "Migrated Question", + code: "externalId", + mimeType: "application/vnd.sunbird.question", + primaryCategory: "Multiselect Multiple Choice Question", + interactionTypes: ["choice"], + showRemarks: "showRemarks", + body: "question", + hints: "hint", + interactions: { + response1: { + type: "choice", + options: [ + { + label: "options[0]", + value: 0, + }, + { + label: "options[1]", + value: 1, + }, + { + label: "options[2]", + value: 2, + }, + { + label: "options[3]", + value: 3, + }, + { + label: "options[4]", + value: 4, + }, + ], + }, + validation: "validation", + }, + editorState: { + options: [ + { + answer: false, + value: { + body: "options[0]", + value: 0, + }, + }, + { + answer: false, + value: { + body: "options[1]", + value: 1, + }, + }, + { + answer: false, + value: { + body: "options[2]", + value: 2, + }, + }, + { + answer: false, + value: { + body: "options[3]", + value: 3, + }, + }, + { + answer: false, + value: { + body: "options[4]", + value: 4, + }, + }, + ], + question: "question", + }, + instructions: { + en: [null], + }, + evidence: { + mimeType: "file.type", + }, + responseDeclaration: { + response1: { + maxScore: 1, + cardinality: "multiple", + type: "integer", + correctResponse: { + outcomes: { + SCORE: 1, + }, + }, + mapping: [], + }, + }, + }, + mcq: { + name: "Migrated Question", + code: "externalId", + description: "", + mimeType: "application/vnd.sunbird.question", + primaryCategory: "Multiselect Multiple Choice Question", + interactionTypes: ["choice"], + body: "question", + interactions: { + validation: "validation", + response1: { + type: "choice", + options: [ + { + value: 0, + label: "options[0]", + }, + { + value: 1, + label: "options[1]", + }, + ], + }, + }, + editorState: { + options: [ + { + answer: false, + value: { + body: "options[0]", + value: 0, + }, + }, + { + answer: false, + value: { + body: "options[1]", + value: 1, + }, + }, + ], + question: "question", + }, + responseDeclaration: { + response1: { + maxScore: 0, + cardinality: "single", + type: "integer", + correctResponse: { + outcomes: { + SCORE: 0, + }, + }, + }, + }, + instructions: { + default: "tip", + }, + hints: "hint", + }, + text: { + name: "Migrated Question", + code: "externalId", + mimeType: "application/vnd.sunbird.question", + primaryCategory: "Text", + interactionTypes: ["text"], + showRemarks: "showRemarks", + body: "question", + instructions: { + en: [null], + }, + editorState: { + question: "question", + }, + responseDeclaration: { + response1: { + type: "string", + maxScore: 1, + }, + }, + interactions: { + validation: { + required: "validation.required", + }, + response1: { + validation: { + limit: { + maxLength: "100", + }, + }, + type: { + number: "validation.isNumber", + }, + }, + }, + hints: "hint", + }, +}; + +const questionStatic = { + date: ["name", "mimeType", "instructions", "responseDeclaration"], + slider: ["name", "mimeType", "instructions", "responseDeclaration"], + multiselect: [ + "name", + "mimeType", + "instructions", + "primaryCategory", + "interactionTypes", + "responseDeclaration", + ], + mcq: [ + "name", + "mimeType", + "primaryCategory", + "interactionTypes", + "responseDeclaration", + ], + text: [ + "name", + "mimeType", + "instructions", + "primaryCategory", + "interactionTypes", + "responseDeclaration", + "instructions", + ], +}; + +const criteriaTemplate = { + _id: "identifier", + __v: 0, + createdAt: "createdOn", + createdFor: "channel", + criteriaType: "manual", + description: "description", + externalId: "code", + flag: "", + frameworkCriteriaId: "identifier", + name: "name", + owner: "consumerId", + remarks: "", + score: "", + showRemarks: "showRemarks", + timesUsed: "", + updatedAt: "lastUpdatedOn", + weightage: "", + migratedId: "identifier", + rubric: {}, +}; + +const defaultCriteria = ["__v", "criteriaType", "flag","remarks", "score", "timesUsed", "weightage", "rubric"] + +module.exports = { + assessmentTemplate, + questionTemplate, + questionStatic, + criteriaTemplate, + defaultCriteria +}; From 69a611a3ce709aa9929d4c7beeb5c494d44b8aa2 Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Mon, 25 Jul 2022 14:03:16 +0530 Subject: [PATCH 16/36] updated review comments and added transformation script --- .../v1/observationSubmissionsController.js | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/controllers/v1/observationSubmissionsController.js b/controllers/v1/observationSubmissionsController.js index de5ee603..81b5fa1f 100644 --- a/controllers/v1/observationSubmissionsController.js +++ b/controllers/v1/observationSubmissionsController.js @@ -837,8 +837,11 @@ module.exports = class ObservationSubmissions extends Abstract { externalId: solutionId, type : "observation", // scoringSystem : "pointsBasedScoring" - }, { themes: 1, levelToScoreMapping: 1, scoringSystem : 1, flattenedThemes : 1}).lean() - + }, + { themes: 1, levelToScoreMapping: 1, scoringSystem : 1, flattenedThemes : 1} + ).lean() + console.log("solutionDocumentntnt", solutionDocument); + if (!solutionDocument) { return resolve({ status: httpStatusCode.bad_request.status, @@ -847,7 +850,7 @@ module.exports = class ObservationSubmissions extends Abstract { } let queryObject = { - "createdBy": createdBy, + // "createdBy": createdBy, "entityExternalId": entityId, "solutionExternalId": solutionId, "submissionNumber" : (submissionNumber) ? submissionNumber : 1 @@ -858,6 +861,7 @@ module.exports = class ObservationSubmissions extends Abstract { { "answers": 1, "criteria": 1, "evidencesStatus": 1, "entityInformation": 1, "entityProfile": 1, "solutionExternalId": 1 , "scoringSystem" : 1} ).lean(); + console.log("sbdnsdbsndbs", queryObject, submissionDocument); if (!submissionDocument._id) { throw messageConstants.apiResponses.SUBMISSION_NOT_FOUND } @@ -993,14 +997,14 @@ module.exports = class ObservationSubmissions extends Abstract { req.body = req.body || {}; let message = messageConstants.apiResponses.CRITERIA_RATING; - let createdBy = req.query.createdBy; + // let createdBy = req.query.createdBy; let solutionId = req.query.solutionId; let submissionNumber = (req.query.submissionNumber) ? req.query.submissionNumber : "all"; let entityId = req.query.entityId.split(","); - if (!createdBy) { - throw messageConstants.apiResponses.CREATED_BY_NOT_FOUND; - } + // if (!createdBy) { + // throw messageConstants.apiResponses.CREATED_BY_NOT_FOUND; + // } if (!solutionId) { throw messageConstants.apiResponses.SOLUTION_ID_NOT_FOUND; @@ -1024,7 +1028,7 @@ module.exports = class ObservationSubmissions extends Abstract { } let queryObject = { - "createdBy": createdBy, + // "createdBy": createdBy, "solutionExternalId": solutionId, "entityExternalId": { $in: entityId } }; From 7df1b6beacf0a85b6b50ced422bb6d9406fe2233 Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Thu, 28 Jul 2022 12:33:43 +0530 Subject: [PATCH 17/36] removed transformation --- .env.sample | 19 +- config/index.js | 10 - config/redisConfig.js | 23 - .../v1/observationSubmissions2Controller.js | 350 --------------- controllers/v1/observations2Controller.js | 351 --------------- controllers/v1/solutions2Controller.js | 173 -------- controllers/v1/surveys2Controller.js | 91 ---- envVariables.js | 6 +- generics/services/questions.js | 141 ------ healthCheck/healthCheckService.js | 8 - module/surveys/helper.js | 402 ------------------ module/transformation/helper.js | 351 --------------- package.json | 1 - scripts/creation-portal-migration/README.md | Bin 1168 -> 2550 bytes .../constant/config.js | 4 +- 15 files changed, 13 insertions(+), 1917 deletions(-) delete mode 100644 config/redisConfig.js delete mode 100644 controllers/v1/observationSubmissions2Controller.js delete mode 100644 controllers/v1/observations2Controller.js delete mode 100644 controllers/v1/solutions2Controller.js delete mode 100644 controllers/v1/surveys2Controller.js delete mode 100644 generics/services/questions.js delete mode 100644 module/transformation/helper.js diff --git a/.env.sample b/.env.sample index 6d0ed087..30c164e6 100644 --- a/.env.sample +++ b/.env.sample @@ -32,14 +32,15 @@ DISABLE_LEARNER_SERVICE_ON_OFF = "ON" # ML Script -BASE_USER = string // base user name of the sunbird enviornment -BASE_PWD = string // base user pwd of the sunbird enviornment -BASE_GRANT = string // base grant of the sunbird enviornment -BASE_CLIENT = string // base client of the sunbird enviornment -BASE_CLIENT_SECRET = string // base client secret of the sunbird enviornment +BASE_USER = "n11@yopmail.com" // base user name of the sunbird enviornment +BASE_PWD = "password" // base user pwd of the sunbird enviornment +BASE_GRANT = "password-grant" // base grant of the sunbird enviornment +BASE_CLIENT = "client-id" // base client of the sunbird enviornment +BASE_CLIENT_SECRET = "client-secret-key" // base client secret of the sunbird enviornment -BASE_HOST = string // base host url of the sunbird enviornment -BASE_AUTHORIZATION = 'Bearer token' // base bearer token with all the permissions to run the script of the sunbird enviornment +BASE_HOST = "https://dev.sunbirded.org/" // base host url of the sunbird enviornment +BASE_AUTHORIZATION = 'Bearer token' // base bearer token with all the permissions to run the script of the sunbird enviornment -VDN_HOST = string // host url of the vdn enviornment -VDN_AUTHORIZATION= 'Bearer token' // bearer token with all the permissions to run the script of the vdn enviornment \ No newline at end of file +CREATION_PORTAL_URL = "https://dock.sunbirded.org/" // creation portal Url + +CREATION_PORTAL_URL_AUTHORIZATION= 'Bearer token' // creation portal bearer token with all the permissions \ No newline at end of file diff --git a/config/index.js b/config/index.js index 2b642901..9a9d0f1d 100644 --- a/config/index.js +++ b/config/index.js @@ -47,20 +47,10 @@ const configuration = { } }; -/** - * Redis connection information. - * @method - * @name redis_connect -*/ -let redis_connect = async function () { - global.cache = require("./redisConfig").connect; - console.log("cache", cache); -} db_connect(); kafka_connect(); // elasticsearch_connect(); -redis_connect() module.exports = configuration; diff --git a/config/redisConfig.js b/config/redisConfig.js deleted file mode 100644 index 76a6dbee..00000000 --- a/config/redisConfig.js +++ /dev/null @@ -1,23 +0,0 @@ -const redis = require("redis"); - -var CACHE = function () { - var client = redis.createClient(process.env.REDIS_URL); - - - client.on("connect", function () { - console.log("redis client connected"); - }); - - client.on("error", console.error.bind(console, "redis connection error:")); - - console.log("redis", client); - - - return { - connect: client.connect(), - client: client, - expiry: 24 * 3600 - }; -}; - -module.exports = CACHE(); diff --git a/controllers/v1/observationSubmissions2Controller.js b/controllers/v1/observationSubmissions2Controller.js deleted file mode 100644 index 9148ca8f..00000000 --- a/controllers/v1/observationSubmissions2Controller.js +++ /dev/null @@ -1,350 +0,0 @@ -// Dependencies - -const observationsHelper = require(MODULES_BASE_PATH + "/observations/helper"); -const solutionsHelper = require(MODULES_BASE_PATH + "/solutions/helper"); -const entitiesHelper = require(MODULES_BASE_PATH + "/entities/helper"); -const submissionsHelper = require(MODULES_BASE_PATH + "/submissions/helper"); -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 transFormationHelper = require(MODULES_BASE_PATH + - "/transformation/helper"); -const redis = require("./../../config/redisConfig"); - -const cache = redis.client; - -/** - * ObservationSubmissions - * @class - */ -module.exports = class ObservationSubmissions extends Abstract { - constructor() { - super(observationSubmissionsSchema); - } - - static get name() { - return "observationSubmissions"; - } - - /** - * create observation submissions. - * @method - * @name create - * @param {Object} req -request data. - * @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. - */ - - async create(req) { - return new Promise(async (resolve, reject) => { - try { - const cacheData = await cache - .get(`submissionCreate:${req.params._id}:${req.params.entityId}`) - .catch((err) => { - console.log("error", err); - }); - - if (cacheData) { - console.log("cahcehData", cacheData); - return resolve(JSON.parse(cacheData)); - } else { - 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, - message: messageConstants.apiResponses.OBSERVATION_NOT_FOUND, - }); - } - - observationDocument = observationDocument[0]; - - let entityDocument = await entitiesHelper.entityDocuments( - { - _id: req.query.entityId, - entityType: observationDocument.entityType, - }, - ["metaInformation", "entityTypeId", "entityType", "registryDetails"] - ); - - if (!entityDocument[0]) { - return resolve({ - status: httpStatusCode.bad_request.status, - message: messageConstants.apiResponses.ENTITY_NOT_FOUND, - }); - } - - entityDocument = entityDocument[0]; - - if ( - entityDocument.registryDetails && - Object.keys(entityDocument.registryDetails).length > 0 - ) { - entityDocument.metaInformation.registryDetails = - entityDocument.registryDetails; - } - - let solutionDocument = await solutionsHelper.solutionDocuments( - { - _id: observationDocument.solutionId, - status: "active", - }, - [ - "externalId", - "themes", - "frameworkId", - "frameworkExternalId", - "evidenceMethods", - "entityTypeId", - "entityType", - "programId", - "programExternalId", - "isAPrivateProgram", - "scoringSystem", - "isRubricDriven", - "project", - "referenceFrom", - "criteriaLevelReport", - "migratedId", - "type", - ] - ); - - if (!solutionDocument[0]) { - return resolve({ - status: httpStatusCode.bad_request.status, - message: messageConstants.apiResponses.SOLUTION_NOT_FOUND, - }); - } - - solutionDocument = solutionDocument[0]; - - const migratedId = solutionDocument?.migratedId; - - console.log("migrated", migratedId); - - let entityProfileForm = await database.models.entityTypes - .findOne(solutionDocument.entityTypeId, { - profileForm: 1, - }) - .lean(); - - if (!entityProfileForm) { - return resolve({ - status: httpStatusCode.bad_request.status, - message: - messageConstants.apiResponses.ENTITY_PROFILE_FORM_NOT_FOUND, - }); - } - - let lastSubmissionNumber = 0; - - const lastSubmissionForObservationEntity = - await observationsHelper.findLastSubmissionForObservationEntity( - req.params._id, - req.query.entityId - ); - - if (!lastSubmissionForObservationEntity.success) { - throw new Error(lastSubmissionForObservationEntity.message); - } - - lastSubmissionNumber = lastSubmissionForObservationEntity.result + 1; - - let submissionDocument = { - entityId: entityDocument._id, - entityExternalId: entityDocument.metaInformation.externalId - ? entityDocument.metaInformation.externalId - : "", - entityInformation: entityDocument.metaInformation, - solutionId: solutionDocument._id, - solutionExternalId: solutionDocument.externalId, - programId: solutionDocument.programId, - programExternalId: solutionDocument.programExternalId, - isAPrivateProgram: solutionDocument.isAPrivateProgram, - frameworkId: solutionDocument.frameworkId, - frameworkExternalId: solutionDocument.frameworkExternalId, - entityTypeId: solutionDocument.entityTypeId, - entityType: solutionDocument.entityType, - observationId: observationDocument._id, - observationInformation: { - ..._.omit(observationDocument, [ - "_id", - "entities", - "deleted", - "__v", - ]), - }, - createdBy: observationDocument.createdBy, - evidenceSubmissions: [], - entityProfile: {}, - status: "started", - scoringSystem: solutionDocument.scoringSystem, - isRubricDriven: solutionDocument.isRubricDriven, - userProfile: observationDocument.userProfile, - }; - - 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 }, - } - ); - } - - if ( - solutionDocument.referenceFrom === messageConstants.common.PROJECT - ) { - submissionDocument["referenceFrom"] = - messageConstants.common.PROJECT; - submissionDocument["project"] = solutionDocument.project; - } - - let submissionDocumentEvidences = {}; - let submissionDocumentCriterias = []; - Object.keys(solutionDocument.evidenceMethods).forEach( - (solutionEcm) => { - if ( - !( - solutionDocument.evidenceMethods[solutionEcm].isActive === - false - ) - ) { - solutionDocument.evidenceMethods[solutionEcm].startTime = ""; - solutionDocument.evidenceMethods[solutionEcm].endTime = ""; - solutionDocument.evidenceMethods[ - solutionEcm - ].isSubmitted = false; - solutionDocument.evidenceMethods[solutionEcm].submissions = - new Array(); - } else { - delete solutionDocument.evidenceMethods[solutionEcm]; - } - } - ); - submissionDocumentEvidences = solutionDocument.evidenceMethods; - - let evidences = {}; - if (!!migratedId) { - evidences = await transFormationHelper.getQuestionSetHierarchy( - migratedId, - submissionDocumentCriterias, - solutionDocument, - false - ); - } - - submissionDocument.evidences = submissionDocumentEvidences; - submissionDocument.evidencesStatus = Object.values( - submissionDocumentEvidences - ); - submissionDocument.criteria = - evidences.submissionDocumentCriterias || {}; - console.log( - "observationSubmissions2", - JSON.stringify(evidences.submissionDocumentCriterias) - ); - submissionDocument.submissionNumber = lastSubmissionNumber; - - submissionDocument["appInformation"] = {}; - - if (req.headers["x-app-id"] || req.headers.appname) { - submissionDocument["appInformation"]["appName"] = req.headers[ - "x-app-id" - ] - ? req.headers["x-app-id"] - : req.headers.appname; - } - - if (req.headers["x-app-ver"] || req.headers.appversion) { - submissionDocument["appInformation"]["appVersion"] = req.headers[ - "x-app-ver" - ] - ? req.headers["x-app-ver"] - : req.headers.appversion; - } - - let newObservationSubmissionDocument = - await database.models.observationSubmissions.create( - submissionDocument - ); - - if ( - newObservationSubmissionDocument.referenceFrom === - messageConstants.common.PROJECT - ) { - await observationSubmissionsHelper.pushSubmissionToImprovementService( - _.pick(newObservationSubmissionDocument, [ - "project", - "status", - "_id", - ]) - ); - } - - // Push new observation submission to kafka for reporting/tracking. - observationSubmissionsHelper.pushObservationSubmissionForReporting( - newObservationSubmissionDocument._id - ); - - let observations = new Array(); - - observations = await observationsHelper.listV2( - req.userDetails.userId - ); - - let responseMessage = - messageConstants.apiResponses.OBSERVATION_SUBMISSION_CREATED; - await cache.setEx( - `submissionCreate:${req.params._id}:${req.params.entityId}`, - redis.expiry, - JSON.stringify({ - message: responseMessage, - result: observations, - }) - ); - - return resolve({ - message: responseMessage, - result: observations, - }); - } - } catch (error) { - return reject({ - status: error.status || httpStatusCode.internal_server_error.status, - message: - error.message || httpStatusCode.internal_server_error.message, - errorObject: error, - }); - } - }); - } -}; diff --git a/controllers/v1/observations2Controller.js b/controllers/v1/observations2Controller.js deleted file mode 100644 index 576e5c4a..00000000 --- a/controllers/v1/observations2Controller.js +++ /dev/null @@ -1,351 +0,0 @@ -const observationsHelper = require(MODULES_BASE_PATH + "/observations/helper"); -const assessmentsHelper = require(MODULES_BASE_PATH + "/assessments/helper"); -const programsHelper = require(MODULES_BASE_PATH + "/programs/helper"); -const transFormationHelper = require(MODULES_BASE_PATH + - "/transformation/helper"); - -const redis = require("./../../config/redisConfig"); - -const cache = redis.client; - -// obId : 60128960f5c000702022546f?entityId=5fd098e2e049735a86b748b7 -// obId : 60110e692d0bbd2f0c3229c3?entityId=5fd098e2e049735a86b748b0 - -module.exports = class Observations2 extends Abstract { - constructor() { - super(observationsSchema); - } - - async assessment(req) { - return new Promise(async (resolve, reject) => { - try { - const cacheData = await cache - .get(`assessment:${req.params._id}:${req.params.entityId}`) - .catch((err) => { - console.log("error", err); - }); - - if (cacheData) { - console.log("cahcehData", cacheData); - return resolve(JSON.parse(cacheData)); - } else { - let response = { - message: messageConstants.apiResponses.ASSESSMENT_FETCHED, - result: {}, - }; - - let observationDocument = await database.models.observations - .findOne({ - _id: req.params._id, - createdBy: req.userDetails.userId, - status: { $ne: "inactive" }, - entities: ObjectId(req.query.entityId), - }) - .lean(); - - if (!observationDocument) { - return resolve({ - status: httpStatusCode.bad_request.status, - message: messageConstants.apiResponses.OBSERVATION_NOT_FOUND, - }); - } - - let entityQueryObject = { - _id: req.query.entityId, - entityType: observationDocument.entityType, - }; - let entityDocument = await database.models.entities - .findOne(entityQueryObject, { - metaInformation: 1, - entityTypeId: 1, - entityType: 1, - registryDetails: 1, - }) - .lean(); - - if (!entityDocument) { - let responseMessage = - messageConstants.apiResponses.ENTITY_NOT_FOUND; - return resolve({ - status: httpStatusCode.bad_request.status, - message: responseMessage, - }); - } - - if ( - entityDocument.registryDetails && - Object.keys(entityDocument.registryDetails).length > 0 - ) { - entityDocument.metaInformation.registryDetails = - entityDocument.registryDetails; - } - - const submissionNumber = - req.query.submissionNumber && req.query.submissionNumber > 1 - ? parseInt(req.query.submissionNumber) - : 1; - - let solutionQueryObject = { - _id: observationDocument.solutionId, - status: "active", - }; - - let solutionDocumentProjectionFields = - await observationsHelper.solutionDocumentProjectionFieldsForDetailsAPI(); - - let solutionDocument = await database.models.solutions - .findOne(solutionQueryObject, { - ...solutionDocumentProjectionFields, - migratedId: 1, - type: 1, - }) - .lean(); - - if (!solutionDocument) { - let responseMessage = - messageConstants.apiResponses.SOLUTION_NOT_FOUND; - return resolve({ - status: httpStatusCode.bad_request.status, - message: responseMessage, - }); - } - - const migratedId = solutionDocument?.migratedId; - console.log("migratedIdmigratedIdmigratedId", migratedId); - - if (req.query.ecmMethod && req.query.ecmMethod !== "") { - if (!solutionDocument.evidenceMethods[req.query.ecmMethod]) { - return resolve({ - status: httpStatusCode.bad_request.status, - message: messageConstants.apiResponses.ECM_NOT_EXIST, - }); - } - } - - let programQueryObject = { - _id: observationDocument.programId, - status: "active", - components: { $in: [ObjectId(observationDocument.solutionId)] }, - }; - - let programDocument = await programsHelper.list(programQueryObject, [ - "externalId", - "name", - "description", - "imageCompression", - "isAPrivateProgram", - ]); - - if (!programDocument[0]._id) { - throw messageConstants.apiResponses.PROGRAM_NOT_FOUND; - } - let entityDocumentQuestionGroup = entityDocument.metaInformation - .questionGroup - ? entityDocument.metaInformation.questionGroup - : ["A1"]; - - response.result.entityProfile = { - _id: entityDocument._id, - entityTypeId: entityDocument.entityTypeId, - entityType: entityDocument.entityType, - // form: form - }; - - let solutionDocumentFieldList = - await observationsHelper.solutionDocumentFieldListInResponse(); - - response.result.solution = await _.pick( - solutionDocument, - solutionDocumentFieldList - ); - response.result.program = programDocument[0]; - - let submissionDocument = { - entityId: entityDocument._id, - entityExternalId: entityDocument.metaInformation.externalId - ? entityDocument.metaInformation.externalId - : "", - entityInformation: entityDocument.metaInformation, - solutionId: solutionDocument._id, - solutionExternalId: solutionDocument.externalId, - programId: programDocument[0]._id, - programExternalId: programDocument[0].externalId, - isAPrivateProgram: programDocument[0].isAPrivateProgram, - programInformation: { - ..._.omit(programDocument[0], [ - "_id", - "components", - "isAPrivateProgram", - ]), - }, - frameworkId: solutionDocument.frameworkId, - frameworkExternalId: solutionDocument.frameworkExternalId, - entityTypeId: solutionDocument.entityTypeId, - entityType: solutionDocument.entityType, - scoringSystem: solutionDocument.scoringSystem, - isRubricDriven: solutionDocument.isRubricDriven, - observationId: observationDocument._id, - observationInformation: { - ..._.omit(observationDocument, [ - "_id", - "entities", - "deleted", - "__v", - ]), - }, - createdBy: observationDocument.createdBy, - evidenceSubmissions: [], - entityProfile: {}, - status: "started", - userProfile: observationDocument.userProfile, - }; - - 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 }, - } - ); - } - - if ( - solutionDocument.referenceFrom === messageConstants.common.PROJECT - ) { - submissionDocument["referenceFrom"] = - messageConstants.common.PROJECT; - submissionDocument["project"] = solutionDocument.project; - } - - let assessment = {}; - - assessment.name = solutionDocument.name; - assessment.description = solutionDocument.description; - assessment.externalId = solutionDocument.externalId; - assessment.pageHeading = solutionDocument.pageHeading; - - let evidenceMethodArray = {}; - let submissionDocumentEvidences = {}; - let submissionDocumentCriterias = []; - Object.keys(solutionDocument.evidenceMethods).forEach( - (solutionEcm) => { - solutionDocument.evidenceMethods[solutionEcm].startTime = ""; - solutionDocument.evidenceMethods[solutionEcm].endTime = ""; - solutionDocument.evidenceMethods[solutionEcm].isSubmitted = false; - solutionDocument.evidenceMethods[solutionEcm].submissions = - new Array(); - } - ); - submissionDocumentEvidences = solutionDocument.evidenceMethods; - - let evidences = {}; - if (!!migratedId) { - console.log("migrattetemigrattetemigrattete", migratedId); - response.result.solution._id = migratedId; - evidences = await transFormationHelper.getQuestionSetHierarchy( - migratedId, - submissionDocumentCriterias, - solutionDocument - ); - } - - submissionDocument.evidences = submissionDocumentEvidences; - submissionDocument.evidencesStatus = Object.values( - submissionDocumentEvidences - ); - submissionDocument.criteria = - evidences.submissionDocumentCriterias || {}; - submissionDocument.submissionNumber = submissionNumber; - - submissionDocument["appInformation"] = {}; - - if (req.headers["x-app-id"] || req.headers.appname) { - submissionDocument["appInformation"]["appName"] = req.headers[ - "x-app-id" - ] - ? req.headers["x-app-id"] - : req.headers.appname; - } - - if (req.headers["x-app-ver"] || req.headers.appversion) { - submissionDocument["appInformation"]["appVersion"] = req.headers[ - "x-app-ver" - ] - ? req.headers["x-app-ver"] - : req.headers.appversion; - } - - let submissionDoc = await observationsHelper.findSubmission( - submissionDocument - ); - - assessment.submissionId = submissionDoc.result._id; - - if (req.query.ecmMethod && req.query.ecmMethod !== "") { - if (evidenceMethodArray[req.query.ecmMethod]) { - evidenceMethodArray = { - [req.query.ecmMethod]: evidenceMethodArray[req.query.ecmMethod], - }; - } - } - - - const parsedAssessment = await assessmentsHelper.parseQuestionsV2( - Object.values(evidenceMethodArray), - entityDocumentQuestionGroup, - submissionDoc.result.evidences, - solutionDocument && solutionDocument.questionSequenceByEcm - ? solutionDocument.questionSequenceByEcm - : false, - entityDocument.metaInformation - ); - - - assessment.evidences = evidences.evidences; - assessment.submissions = parsedAssessment.submissions; - if ( - parsedAssessment.generalQuestions && - parsedAssessment.generalQuestions.length > 0 - ) { - assessment.generalQuestions = parsedAssessment.generalQuestions; - } - - response.result.assessment = assessment; - - await cache.setEx( - `assessment:${req.params._id}:${req.params.entityId}`, - redis.expiry, - JSON.stringify(response) - ); - - return resolve(response); - } - } catch (error) { - console.log("errororooro", error); - return reject({ - status: error?.status || httpStatusCode.internal_server_error.status, - message: - error?.message || httpStatusCode.internal_server_error.message, - errorObject: error, - }); - } - }); - } -}; diff --git a/controllers/v1/solutions2Controller.js b/controllers/v1/solutions2Controller.js deleted file mode 100644 index 7c255d47..00000000 --- a/controllers/v1/solutions2Controller.js +++ /dev/null @@ -1,173 +0,0 @@ -// Dependencies -const csv = require("csvtojson"); -const solutionsHelper = require(MODULES_BASE_PATH + "/solutions/helper"); -const criteriaHelper = require(MODULES_BASE_PATH + "/criteria/helper"); -const questionsHelper = require(MODULES_BASE_PATH + "/questions/helper"); -const FileStream = require(ROOT_PATH + "/generics/fileStream"); -const observationsHelper = require(MODULES_BASE_PATH + "/observations/helper"); -const assessmentsHelper = require(MODULES_BASE_PATH + "/assessments/helper"); -const transFormationHelper = require(MODULES_BASE_PATH + - "/transformation/helper"); - -const redis = require("./../../config/redisConfig"); - -const cache = redis.client; - -/** - * Solutions - * @class - */ -module.exports = class Solutions2 extends Abstract { - constructor() { - super(solutionsSchema); - } - - static get name() { - return "solutions"; - } - - /** - * Get Questions in solution. - * @method - * @name deleteSolution - * @param {Object} req - requested data. - * @param {String} req.params._id - solutiion internal id. - * @returns {JSON} consists of solution id. - */ - - async questions(req) { - return new Promise(async (resolve, reject) => { - try { - const cacheData = await cache - .get(`solutionQuestions:${req.params._id}`) - .catch((err) => { - console.log("error", err); - }); - - if (cacheData) { - console.log("cahcehData", cacheData); - return resolve(JSON.parse(cacheData)); - } else { - let response = { - message: messageConstants.apiResponses.ASSESSMENT_FETCHED, - result: {}, - }; - - let solutionId = req.params._id; - let userId = req.userDetails.userId; - - if (userId == "") { - throw new Error( - messageConstants.apiResponses.USER_ID_REQUIRED_CHECK - ); - } - - let solutionDocumentProjectionFields = - await observationsHelper.solutionDocumentProjectionFieldsForDetailsAPI(); - - let solutionDocument = await database.models.solutions - .findOne( - { _id: solutionId }, - { ...solutionDocumentProjectionFields, migratedId: 1, type: 1 } - ) - .lean(); - - if (!solutionDocument) { - return resolve({ - status: httpStatusCode.bad_request.status, - message: messageConstants.apiResponses.SOLUTION_NOT_FOUND, - }); - } - - const migratedId = solutionDocument?.migratedId; - - let solutionDocumentFieldList = - await observationsHelper.solutionDocumentFieldListInResponse(); - - response.result.solution = await _.pick( - solutionDocument, - solutionDocumentFieldList - ); - - let assessment = {}; - assessment.name = solutionDocument.name; - assessment.description = solutionDocument.description; - assessment.externalId = solutionDocument.externalId; - assessment.pageHeading = solutionDocument.pageHeading; - assessment.submissionId = ""; - - let evidenceMethodArray = {}; - let submissionDocumentEvidences = {}; - let submissionDocumentCriterias = []; - Object.keys(solutionDocument.evidenceMethods).forEach( - (solutionEcm) => { - solutionDocument.evidenceMethods[solutionEcm].startTime = ""; - solutionDocument.evidenceMethods[solutionEcm].endTime = ""; - solutionDocument.evidenceMethods[solutionEcm].isSubmitted = false; - solutionDocument.evidenceMethods[solutionEcm].submissions = - new Array(); - } - ); - - submissionDocumentEvidences = solutionDocument.evidenceMethods; - - let evidences = {}; - if (!!migratedId) { - console.log("migrattetemigrattetemigrattete", migratedId); - response.result.solution._id = migratedId; - evidences = await transFormationHelper.getQuestionSetHierarchy( - migratedId, - submissionDocumentCriterias, - solutionDocument - ); - } - - let entityDocument = { - metaInformation: {}, - questionGroup: "", - }; - - let entityDocumentQuestionGroup = entityDocument.metaInformation - .questionGroup - ? entityDocument.metaInformation.questionGroup - : ["A1"]; - assessment.evidences = []; - const parsedAssessment = await assessmentsHelper.parseQuestionsV2( - Object.values(evidenceMethodArray), - entityDocumentQuestionGroup, - submissionDocumentEvidences, - solutionDocument && solutionDocument.questionSequenceByEcm - ? solutionDocument.questionSequenceByEcm - : false, - {} - ); - - assessment.evidences = evidences.evidences; - assessment.submissions = parsedAssessment.submissions; - - if ( - parsedAssessment.generalQuestions && - parsedAssessment.generalQuestions.length > 0 - ) { - assessment.generalQuestions = parsedAssessment.generalQuestions; - } - - response.result.assessment = assessment; - await cache.setEx( - `solutionQuestions:${req.params._id}`, - redis.expiry, - JSON.stringify(response) - ); - return resolve(response); - } - } catch (error) { - return reject({ - status: error.status || httpStatusCode.internal_server_error.status, - message: - error.message || httpStatusCode.internal_server_error.message, - errorObject: error, - }); - } - }); - } -}; diff --git a/controllers/v1/surveys2Controller.js b/controllers/v1/surveys2Controller.js deleted file mode 100644 index 7ba5bd95..00000000 --- a/controllers/v1/surveys2Controller.js +++ /dev/null @@ -1,91 +0,0 @@ -// Dependencies -const surveysHelper = require(MODULES_BASE_PATH + "/surveys/helper"); -const redis = require("./../../config/redisConfig"); - -const cache = redis.client; - - -module.exports = class Surveys extends Abstract { - constructor() { - super(surveysSchema); - } - - static get name() { - return "surveys2"; - } - - /** - * Survey details. - * @method - * @name details - * @param {Request} req request body. - * @param {req.param._id} Either surveyId or link. - * @param {req.query.solutionId} solutionId (not required in the case of passing link). - * @returns {Object} returns survey details information. - * Result will have the details of survey. - */ - - async details(req) { - return new Promise(async (resolve, reject) => { - try { - const cacheData = await cache - .get(`surveyDetails:${req.params._id}`) - .catch((err) => { - console.log("error", err); - }); - - if (cacheData) { - console.log("cahcehData", cacheData); - return resolve(JSON.parse(cacheData)); - } else { - - let validateSurveyId = gen.utils.isValidMongoId(req.params._id); - - let surveyDetails = {}; - - if (validateSurveyId || req.query.solutionId) { - let surveyId = req.params._id ? req.params._id : ""; - - surveyDetails = await surveysHelper.detailsV3( - req.body, - surveyId, - req.query.solutionId, - req.userDetails.userId, - req.userDetails.userToken - ); - } else { - let bodyData = req.body ? req.body : {}; - - surveyDetails = await surveysHelper.getDetailsByLink2( - req.params._id, - req.userDetails.userId, - req.userDetails.userToken, - bodyData - ); - } - - await cache.setEx( - `surveyDetails:${req.params._id}`, - redis.expiry, - JSON.stringify({ - message: surveyDetails.message, - result: surveyDetails.data, - }) - ); - - return resolve({ - message: surveyDetails.message, - result: surveyDetails.data, - }); - } - } catch (error) { - return reject({ - status: error.status || httpStatusCode.internal_server_error.status, - message: - error.message || httpStatusCode.internal_server_error.message, - errorObject: error, - }); - } - }); - } -}; diff --git a/envVariables.js b/envVariables.js index 97396511..bcd4113d 100644 --- a/envVariables.js +++ b/envVariables.js @@ -85,11 +85,7 @@ let enviromentVariables = { "DISABLE_LEARNER_SERVICE_ON_OFF": { "message" : "Disable learner service on/off", "default": "ON" - }, - "REDIS_URL": { - "message" : "Required redis url", - "optional" : false -}, + } } let success = true; diff --git a/generics/services/questions.js b/generics/services/questions.js deleted file mode 100644 index 3e3c1a91..00000000 --- a/generics/services/questions.js +++ /dev/null @@ -1,141 +0,0 @@ -const request = require("request"); -const hostUrl = process.env.VDN_HOST; - - -const genToken = async () => { - return new Promise(async (resolve, reject) => { - const query = { - username: process.env.SUNBIRD_USER, - password: process.env.SUNBIRD_PWD, - grant_type: process.env.SUNBIRD_GRANT, - client_id: process.env.SUNBIRD_CLIENT, - client_secret: process.env.SUNBIRD_CLIENT_SECRET, - }; - - const url = hostUrl + "/auth/realms/sunbird/protocol/openid-connect/token"; - request.post(url, { form: { ...query } }, genTokenCallback); - - function genTokenCallback(err, res, body) { - if (err) { - return reject(err); - } else { - return resolve(res?.data?.access_token || ""); - } - } - }); - - // return res ? res.data.access_token : ""; -}; - -const questionset = function (questionsetId = "") { - return new Promise(async (resolve, reject) => { - try { - const token = await genToken(); - - console.log("tokejekkeke", token); - let url = hostUrl + messageConstants.endpoints.QUESTIONSET_READ; - - if (questionsetId !== "") { - url = url + "/" + questionsetId + "?" + "mode=edit"; - } - - const options = { - headers: { - "content-type": "application/json", - Authorization: process.env.VDN_AUTHORIZATION, - "x-authenticated-user-token": token, - }, - }; - - request.get(url, options, questionSetReadCallback); - let result = { - success: true, - }; - function questionSetReadCallback(err, data) { - if (err) { - console.log("errr", err); - result.success = false; - } else { - let response = JSON.parse(data.body); - if (response.responseCode === httpStatusCode["ok"].code) { - result["data"] = response?.result?.questionSet; - } else { - result.success = false; - } - } - - return resolve(result); - } - setTimeout(function () { - return resolve( - (result = { - success: false, - }) - ); - }, messageConstants.common.SERVER_TIME_OUT); - } catch (error) { - console.log("errrorr", error); - return reject(error); - } - }); -}; - -const question = function (questionId = "") { - return new Promise(async (resolve, reject) => { - try { - console.log(); - const token = await genToken(); - let url = hostUrl + messageConstants.endpoints.QUESTION_READ; - - if (questionId !== "") { - url = - url + - "/" + - questionId + - "?" + - "fields=body,question,primaryCategory,mimeType,qType,answer,templateId,responseDeclaration,interactionTypes,interactions,name,solutions,editorState,media,remarks,evidence,hints,instructions,name,numberOnly,characterLimit,showEvidence,evidenceMimeType,showRemarks,remarksLimit,markAsNotMandatory"; - } - - const options = { - headers: { - "content-type": "application/json", - Authorization: process.env.VDN_AUTHORIZATION, - "x-authenticated-user-token": token, - }, - }; - - request.get(url, options, questionReadCallback); - let result = { - success: true, - }; - function questionReadCallback(err, data) { - if (err) { - result.success = false; - } else { - let response = JSON.parse(data.body); - if (response.responseCode === httpStatusCode["ok"].code) { - result["data"] = response?.result?.question; - } else { - result.success = false; - } - } - - return resolve(result); - } - setTimeout(function () { - return resolve( - (result = { - success: false, - }) - ); - }, messageConstants.common.SERVER_TIME_OUT); - } catch (error) { - return reject(error); - } - }); -}; - -module.exports = { - questionset: questionset, - question: question, -}; diff --git a/healthCheck/healthCheckService.js b/healthCheck/healthCheckService.js index fe26479b..13b809a2 100644 --- a/healthCheck/healthCheckService.js +++ b/healthCheck/healthCheckService.js @@ -11,7 +11,6 @@ const kendraHealthCheck = require("./kendra"); const mongodbHealthCheck = require("./mongodb"); const kafkaHealthCheck = require("./kafka"); const improvementHealthCheck = require("./improvement-project"); -const redisHealthCheck = require("./redis"); const obj = { MONGO_DB: { @@ -34,11 +33,6 @@ const obj = { FAILED_CODE: 'IMPROVEMENT_SERVICE_HEALTH_FAILED', FAILED_MESSAGE: 'Improvement service is not healthy' }, - REDIS: { - NAME: 'Redis.db', - FAILED_CODE: 'REDIS_HEALTH_FAILED', - FAILED_MESSAGE: 'Redis db is not connected' - }, NAME: 'AssessmentServiceHealthCheck', API_VERSION: '1.0' } @@ -53,13 +47,11 @@ let health_check = async function(req,res) { let elasticSearchConnection = await elasticSearchHealthCheck.health_check(); let improvementHealthCheckStatus = await improvementHealthCheck.health_check(); - let redisConnection = await redisHealthCheck.health_check(); checks.push(checkResult("KAFKA",kafkaConnection)); checks.push(checkResult("MONGO_DB",mongodbConnection)); checks.push(checkResult("KENDRA_SERVICE",coreServiceStatus)); checks.push(checkResult("IMPROVEMENT_SERVICE",improvementHealthCheckStatus)); - checks.push(checkResult("REDIS", redisConnection)); let checkServices = checks.filter( check => check.healthy === false); diff --git a/module/surveys/helper.js b/module/surveys/helper.js index cada4fdc..9f9fb23a 100644 --- a/module/surveys/helper.js +++ b/module/surveys/helper.js @@ -21,7 +21,6 @@ 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"); -const transFormationHelper = require(MODULES_BASE_PATH + "/transformation/helper"); /** * SurveysHelper @@ -1809,405 +1808,4 @@ module.exports = class SurveysHelper { }) } - static getDetailsByLink2(link= "", userId= "", token= "", roleInformation= {},version = "") { - return new Promise(async (resolve, reject) => { - try { - - if (link == "") { - throw new Error(messageConstants.apiResponses.LINK_REQUIRED_CHECK) - } - - if (userId == "") { - throw new Error(messageConstants.apiResponses.USER_ID_REQUIRED_CHECK) - } - - if(token == "") { - throw new Error(messageConstants.apiResponses.REQUIRED_USER_AUTH_TOKEN); - } - - - console.log(); - console.log("getDetailsByLink"); - console.log(); - - let solutionDocument = await solutionsHelper.solutionDocuments - ( - { - link: link, - }, - [ - "externalId", - "name", - "description", - "type", - "endDate", - "status", - "programId", - "programExternalId", - "isAPrivateProgram" - ] - ) - console.log(); - console.log("solutionDocument", solutionDocument); - console.log(); - - if (!solutionDocument.length) { - throw new Error(messageConstants.apiResponses.SOLUTION_NOT_FOUND) - } - - if ( version === "" ) { - - if (new Date() > new Date(solutionDocument[0].endDate)) { - - if (solutionDocument[0].status == messageConstants.common.ACTIVE_STATUS) { - await solutionsHelper.updateSolutionDocument - ( - { link : link }, - { $set : { status: messageConstants.common.INACTIVE_STATUS } } - ) - } - - throw new Error(messageConstants.apiResponses.LINK_IS_EXPIRED) - } - } - - let surveyDocument = await this.surveyDocuments - ( - { solutionId: solutionDocument[0]._id, - createdBy: userId }, - ["_id"] - ) - - console.log(); - console.log("surveyDocument", surveyDocument); - console.log(); - let surveyId; - - if (surveyDocument.length > 0) { - surveyId = surveyDocument[0]._id; - } - else { - - let createSurveyDocument = await this.createSurveyDocument - ( - userId, - solutionDocument[0] - // userOrgDetails[userId] - ) - - if (!createSurveyDocument.success) { - throw new Error(messageConstants.apiResponses.SURVEY_CREATION_FAILED) - } - - surveyId = createSurveyDocument.data._id; - } - - let validateSurvey = await this.validateSurvey - ( - surveyId, - userId, - version && version === messageConstants.common.VERSION_3 ? false : true - ) - - if (!validateSurvey.success) { - return resolve(validateSurvey); - } - - let surveyDetails = await this.detailsV4 - ( - surveyId, - userId, - validateSurvey.data.submissionId, - roleInformation, - token - ) - - if (!surveyDetails.success) { - return resolve(surveyDetails); - } - - return resolve({ - success: true, - message: surveyDetails.message, - data: surveyDetails.data - }); - - - } catch (error) { - return resolve({ - success: false, - message: error.message, - data: false - }); - } - }); - } - - - static detailsV4(surveyId = "", userId= "", submissionId = "", roleInformation = {}, userToken ="") { - return new Promise(async (resolve, reject) => { - try { - - if (surveyId == "") { - throw new Error(messageConstants.apiResponses.SURVEY_ID_REQUIRED) - } - - if (userId == "") { - throw new Error(messageConstants.apiResponses.USER_ID_REQUIRED_CHECK) - } - - let surveyDocument = await this.surveyDocuments - ( - { - _id: surveyId, - status: messageConstants.common.PUBLISHED, - isDeleted: false - } - ) - - if (!surveyDocument.length) { - throw new Error(messageConstants.apiResponses.SURVEY_NOT_FOUND) - } - - surveyDocument = surveyDocument[0]; - - let solutionQueryObject = { - _id: surveyDocument.solutionId, - status: messageConstants.common.ACTIVE_STATUS, - isDeleted: false - }; - - let solutionDocumentProjectionFields = await this.solutionDocumentProjectionFieldsForDetailsAPI(); - - let solutionDocument = await solutionsHelper.solutionDocuments( - solutionQueryObject, [ - ...solutionDocumentProjectionFields, - "migratedId", - "type" - ] - - ) - - if (!solutionDocument.length) { - throw new Error(messageConstants.apiResponses.SOLUTION_NOT_FOUND) - } - const migratedId = solutionDocument[0]?.migratedId; - - solutionDocument = solutionDocument[0]; - - let endDateCheckRequired = true; - - if ( submissionId != "" ) { - let submissionDocument = await surveySubmissionsHelper.surveySubmissionDocuments( - { - _id: submissionId - }, - [ - "status" - ] - ); - - if(submissionDocument && submissionDocument.length > 0){ - if ( submissionDocument[0].status == messageConstants.common.SUBMISSION_STATUS_COMPLETED ){ - endDateCheckRequired = false; - } - } - } - - if (endDateCheckRequired) { - - if (new Date() > new Date(solutionDocument.endDate)) { - if (solutionDocument.status == messageConstants.common.ACTIVE_STATUS) { - await solutionHelper.updateSolutionDocument - ( - { _id : solutionDocument._id }, - { $set : { status: messageConstants.common.INACTIVE_STATUS } } - ) - } - - throw new Error(messageConstants.apiResponses.LINK_IS_EXPIRED) - } - } - - let programDocument = []; - - if (surveyDocument.programId) { - - let programQueryObject = { - _id: surveyDocument.programId, - status: messageConstants.common.ACTIVE_STATUS, - components: { $in: [ObjectId(surveyDocument.solutionId)] } - }; - - programDocument = await programsHelper.list( - programQueryObject, - [ - "externalId", - "name", - "description", - "imageCompression", - "isAPrivateProgram" - ] - ); - } - - let solutionDocumentFieldList = await this.solutionDocumentFieldListInResponse(); - - let result = {}; - - result.solution = await _.pick(solutionDocument, solutionDocumentFieldList); - - if (programDocument.length > 0) { - result.program = programDocument[0]; - } - - let assessment = {}; - - assessment.name = solutionDocument.name; - assessment.description = solutionDocument.description; - assessment.externalId = solutionDocument.externalId; - - let criteriaId = solutionDocument.themes[0].criteria[0].criteriaId; - let weightage = solutionDocument.themes[0].criteria[0].weightage; - - let criteriaQuestionDocument = await criteriaQuestionsHelper.list( - { _id: criteriaId }, - "all", - [ - "resourceType", - "language", - "keywords", - "concepts" - ] - ) - - let evidenceMethodArray = {}; - let submissionDocumentEvidences = {}; - let submissionDocumentCriterias = []; - - Object.keys(solutionDocument.evidenceMethods).forEach(solutionEcm => { - - solutionDocument.evidenceMethods[solutionEcm].startTime = ""; - solutionDocument.evidenceMethods[solutionEcm].endTime = ""; - solutionDocument.evidenceMethods[solutionEcm].isSubmitted = false; - solutionDocument.evidenceMethods[solutionEcm].submissions = new Array; - }) - - submissionDocumentEvidences = solutionDocument.evidenceMethods; - - - let evidences = {}; - - if (!!migratedId) { - console.log("migrattetemigrattetemigrattete", migratedId); - solutionDocument._id = migratedId; - evidences = await transFormationHelper.getQuestionSetHierarchy(migratedId, submissionDocumentCriterias, solutionDocument); - } - - let criteria = criteriaQuestionDocument[0]; - - criteria.weightage = weightage; - - - if (submissionId !== "") { - assessment.submissionId = submissionId; - - let surveySubmissionDocument = - await surveySubmissionsHelper.surveySubmissionDocuments( - { - _id : submissionId - },["evidences"] - ); - - submissionDocumentEvidences = surveySubmissionDocument[0].evidences; - - } else { - let submissionDocument = { - solutionId: solutionDocument._id, - solutionExternalId: solutionDocument.externalId, - surveyId: surveyDocument._id, - createdBy: surveyDocument.createdBy, - evidenceSubmissions: [], - status: messageConstants.common.SUBMISSION_STATUS_STARTED, - evidences: submissionDocumentEvidences, - evidencesStatus: Object.values(submissionDocumentEvidences), - criteria: evidences.submissionDocumentCriterias, - surveyInformation: { - ..._.omit(surveyDocument, ["_id", "deleted", "__v"]) - }, - isAPrivateProgram: surveyDocument.isAPrivateProgram - }; - 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 - // ( { code : roleInformation.role }, - // [ "_id"] - // ) - - // if (roleDocument.length > 0) { - // roleInformation.roleId = roleDocument[0]._id; - // } - - submissionDocument.userRoleInformation = roleInformation; - } - - if (programDocument.length > 0) { - submissionDocument.programId = programDocument[0]._id; - submissionDocument.programExternalId = programDocument[0].externalId; - } - - let submissionDoc = await database.models.surveySubmissions.create( - submissionDocument - ); - - if (submissionDoc._id) { - assessment.submissionId = submissionDoc._id; - } - } - - const parsedAssessment = await assessmentsHelper.parseQuestionsV2( - Object.values(evidenceMethodArray), - ["A1"], - submissionDocumentEvidences, - (solutionDocument && solutionDocument.questionSequenceByEcm) ? solutionDocument.questionSequenceByEcm : false - ); - - assessment.evidences = evidences.evidences; - assessment.submissions = parsedAssessment.submissions; - if (parsedAssessment.generalQuestions && parsedAssessment.generalQuestions.length > 0) { - assessment.generalQuestions = parsedAssessment.generalQuestions; - } - - result.assessment = assessment; - - return resolve({ - success: true, - message: messageConstants.apiResponses.SURVEY_DETAILS_FETCHED_SUCCESSFULLY, - data: result - }); - - } - catch (error) { - return reject({ - success: false, - message: error.message, - data: false - }); - } - }) - } - } \ No newline at end of file diff --git a/module/transformation/helper.js b/module/transformation/helper.js deleted file mode 100644 index 1c7a1752..00000000 --- a/module/transformation/helper.js +++ /dev/null @@ -1,351 +0,0 @@ -// Dependencies. -const { isEmpty, find, capitalize } = require("lodash"); -const { question, questionset } = require("../../generics/services/questions"); -const { baseAssessment, questionType } = require("../../templates/questions"); -const { - criteriaTemplate, - defaultCriteria, - assessmentTemplate, -} = require("../../templates/questionset"); - -module.exports = class Transformation { - static getQuestionSetHierarchy( - migratedId, - submissionDocumentCriterias, - solutionDocument, - isPageQuestionsRequired = true - ) { - return new Promise(async (resolve, reject) => { - const res = await questionset(migratedId).catch((err) => { - console.log("Error", err?.response?.data); - reject(err?.response?.data); - }); - const questionSetHierarchy = res?.data; - const migratedCriteriaQuestions = questionSetHierarchy?.children || []; - const evidences = await this.questionEvidences( - migratedCriteriaQuestions, - submissionDocumentCriterias, - isPageQuestionsRequired - ); - - assessmentTemplate.assessment.evidences[0].name = capitalize( - solutionDocument?.type - ); - assessmentTemplate.assessment.evidences[0].sections[0].name = `${capitalize( - solutionDocument?.type - )} Questions`; - - assessmentTemplate.assessment.evidences[0].sections[0].questions = - evidences?.evidenceSections || []; - assessmentTemplate.assessment.evidences[0].sections[0].code = "SQ"; - assessmentTemplate.assessment.evidences[0].code = - assessmentTemplate.assessment.evidences[0].externalId = "SF"; - assessmentTemplate.assessment.evidences[0].description = - questionSetHierarchy?.descripton || ""; - - resolve({ - ...evidences, - evidences: assessmentTemplate.assessment.evidences, - }); - }); - } - - static questionEvidences( - criteriaQuestions, - submissionDocumentCriterias, - isPageQuestionsRequired - ) { - return new Promise(async (resolve, reject) => { - const evidenceSections = []; - for (let i = 0; i < criteriaQuestions.length; i++) { - const criteria = criteriaQuestions[i]; - const assessment = { ...baseAssessment }; - const criteriaObj = {}; - - for (let key in criteriaTemplate) { - if (key === "createdFor") { - criteriaObj[key] = criteria[criteriaTemplate[key]] - ? [criteria[criteriaTemplate[key]]] - : []; - } else if (defaultCriteria.includes(key)) { - criteriaObj[key] = criteriaTemplate[key]; - } else if (!isEmpty(criteriaTemplate[key])) { - criteriaObj[key] = criteria[criteriaTemplate[key]] || ""; - } - } - - assessment.page = "p" + (i + 1); - - const children = criteria?.children || []; - - - if (isPageQuestionsRequired && children.length > 0) { - const pageQuestions = await this.getPageQuestions( - criteria, - children, - [] - ); - - const isMatrixQuestion = criteria?.instances?.label; - - let matrixQuestion = {}; - if (isMatrixQuestion) { - matrixQuestion = this.getMatrixQuestions(criteria); - matrixQuestion.instanceQuestions = pageQuestions; - assessment.pageQuestions = matrixQuestion; - } else { - assessment.pageQuestions = pageQuestions; - } - } - submissionDocumentCriterias.push(criteriaObj); - if (children.length > 0) evidenceSections.push(assessment); - } - resolve({ evidenceSections, submissionDocumentCriterias }); - }); - } - - static getPageQuestions(criteria, children, pageQuestions) { - const readQuestions = []; - return new Promise(async (resolve, reject) => { - - - - for (let j = 0; j < children.length; j++) { - const res = await question(children[j]?.identifier); - - let childData = res?.data; - readQuestions.push(childData); - - // branchingLogic - const branching = criteria?.branchingLogic; - - if (!isEmpty(branching) && childData) { - childData = await this.updateChildDataWithBranching( - branching, - childData, - readQuestions, - children - ); - } - - - // question transformation - const childTemplate = await this.tranformQuestionData( - {}, - childData, - j, - children[j] - ); - - pageQuestions.push(childTemplate); - } - - - resolve(pageQuestions); - }); - } - - static getTemplateType(childData) { - const responseType = childData?.primaryCategory?.toLowerCase(); - let type = ""; - if (responseType === "text") { - type = - childData?.interactions.response1.type.number.toLowerCase() === "yes" - ? "number" - : "text"; - } else if (responseType === "multiselect multiple choice question") { - type = - childData?.responseDeclaration?.response1?.cardinality.toLowerCase() === - "single" - ? "radio" - : "multiselect"; - } else { - type = responseType; - } - return type; - } - static updateChildDataWithBranching( - branching, - childData, - readQuestions, - children - ) { - return new Promise(async (resolve, reject) => { - if (branching.hasOwnProperty(childData?.identifier)) { - const ques = branching[childData.identifier]; - if (ques?.target?.length > 0) { - childData.children = ques.target || []; - childData.options = childData?.interactions?.response1.options; - } else if (!isEmpty(ques?.preCondition)) { - const operator = Object.keys(ques?.preCondition?.and[0]); - - const index = ques?.preCondition?.and[0][operator]; - - - let branchingQue = find(readQuestions, { - identifier: ques?.source[0], - }); - let branchingQueId = !isEmpty(branchingQue) - ? branchingQue?.identifier - : ""; - - if (!branchingQueId) { - branchingQue = find(children, { identifier: ques?.source[0] }); - branchingQueId = branchingQue?.identifier; - const res = await question(branchingQueId); - branchingQue = res?.data; - } - - const i = index[1] === -1 ? 0 : index[1]; - - const visibleIf = [ - { - operator: operator[0] === "eq" ? "===" : "!==", - value: [branchingQue?.interactions?.response1.options[i]?.value], - _id: ques?.source[0], - }, - ]; - childData.visibleIf = visibleIf || []; - } - resolve(childData); - } - resolve(childData); - }); - } - - static tranformQuestionData(childQuestion, childData, index, child) { - - const type = this.getTemplateType(childData); - - - return new Promise((resolve, reject) => { - for (let key in questionType[type]) { - const keyData = questionType[type][key]; - if (questionType.defaultFields.includes(key)) { - childQuestion[key] = keyData; - } else if (questionType.arrayFields.includes(key)) { - - childQuestion[key] = childData[keyData] || []; - } else if (key === "question") { - const questionData = []; - let str = childData[keyData] ? childData[keyData] : ""; - while (str.length > 0) { - const startIndex = str.indexOf("

"); - if (startIndex > -1) { - const start = str.slice(startIndex + 3); - const endIndex = start.indexOf("

"); - let end = start.slice(0, endIndex); - - end = end.replace(" ", ""); - - questionData.push(end); - - str = start.slice(endIndex); - } else str = ""; - } - - childQuestion[key] = questionData; - } else if (key === "validation") { - const obj = {}; - for (const cKey in keyData) { - if (cKey === "required") { - const requir = childData?.interactions?.validation?.required; - - obj[cKey] = - requir === true || requir === "Yes" || requir === "yes" - ? true - : false; - } else if (cKey === "IsNumber") { - const typen = childData?.interactions?.response1?.type?.number; - obj[cKey] = typen.toLowerCase() === "yes" ? true : false; - } else if (cKey === "max" || cKey === "min") { - const slider = - type === "slider" - ? childData?.interactions.response1.validation.range[cKey] - : type === "date" - ? childData?.interactions.validation[cKey] - : ""; - obj[cKey] = slider; - } - } - childQuestion[key] = obj; - } else if (key === "payload") { - childQuestion[key] = { - ...keyData, - criteriaId: childData.identifier, - responseType: type, - }; - } else if (key === "file") { - if ( - childData[keyData["type"]] && - childData[keyData["type"]].length > 0 - ) { - const requir = childData?.interactions?.validation?.required; - childQuestion[key] = { - ...keyData, - required: - requir === true || requir === "Yes" || requir === "yes" - ? "Yes" - : "No", - type: childData?.evidence?.type, - }; - } else childQuestion[key] = ""; - } else if (key === "updatedAt" || key === "createdAt") { - childQuestion[key] = child[keyData]; - } else if (key === "dateFormat") { - childQuestion[key] = - childData?.interactions?.response1?.validation?.pattern || ""; - } else if (key === "options") { - childQuestion[key] = - childData?.interactions?.response1?.options || []; - } else if (key === "showRemarks" || key === "autoCapture") { - const typen = childData[keyData]; - childQuestion[key] = - key !== "autoCapture" - ? typen === true || typen === "Yes" || typen === "yes" - ? true - : false - : childData[keyData] || false; - } else if (key === "questionNumber" || key === "page") { - childQuestion[key] = - key === "questionNumber" ? `${index + 1}` : baseAssessment.page; - } else { - childQuestion[key] = childData[keyData] - ? childData[keyData] - : child[keyData] - ? child[keyData] - : ""; - - } - } - resolve(childQuestion); - }); - } - - static getMatrixQuestions(criteria) { - const matrixObj = {}; - for (let key in questionType.matrix) { - if (key === "instanceIdentifier") { - matrixObj["instanceIdentifier"] = criteria?.instances?.label || ""; - } else if (key === "payload") { - matrixObj[key] = { - ...questionType.matrix.payload, - criteriaId: criteria.identifier, - responseType: "matrix", - }; - } else if (questionType.arrayFields.includes(key)) { - matrixObj[key] = - key !== "children" ? criteria[questionType.matrix[key]] : []; - } else if ( - questionType.defaultFields.includes(key) || - key === "validation" - ) { - matrixObj[key] = questionType.matrix[key] || ""; - } else if (key !== "validation") { - matrixObj[key] = criteria[questionType.matrix[key]] || ""; - } - } - - return matrixObj; - } -}; diff --git a/package.json b/package.json index 499dc1e1..16811338 100644 --- a/package.json +++ b/package.json @@ -62,7 +62,6 @@ "p-each-series": "^2.1.0", "path": "^0.12.7", "querystring": "^0.2.1", - "redis": "^4.2.0", "request": "^2.88.0", "require-all": "^2.2.0", "uuid": "^3.3.2", diff --git a/scripts/creation-portal-migration/README.md b/scripts/creation-portal-migration/README.md index f2d0d26369f248545c28c5a6873226dcbe506545..5befeb8c2c6e1ded459f78bb937e73d48112abc8 100644 GIT binary patch literal 2550 zcmb`JTW?Z95Xa}WCVqzvJoJgy`eM?=M2e&~DcVqsi7!aGr*J?HVEW~)znNt{mmZ`R zvf=E7-P!;A=eCc(%i^RV*L*8B8p}`?(vhW%WhN18x6);0EOXXg%d5Y?vS@R1!@qs{ zD|W~1>BxkZm9S*ToPWc4XJ@k#RLW0TaZ}7s@9%F;92vH$;TiH(DSjm_?eU7q)N8T2!cEK&d<_BLP7}zQ|{}Fep^6ro|;2SKX97LQ@+w@ph%c!MPm&i^8OC`8(wiAarucn zbDKgv0^ibds?RQ(1=qx7Hg@`0aH#+E4lCcg6> ze{a!i?)1@mO!m~QrMXM9!V z&6+XI*TZ;&(1BI6RI)ox)ye(13)c6Zn()=8)+&oFIK0kPoDv%3Jwofv{Z5|<89HA* zT&8+Btf*e`f7sd}-DZ8QFS~62Tx^H5ZqQKsr1ocDH{mVO!2`4scwdO{5&y<$Fv`E9 k(*6bn6RmlBJg=H(-$l;y*@DcVu@!Fp188=DXaE2J literal 1168 zcmbW1OHaZ;6ot=L6aPaK7OV{F#)QNL0znc%0;q`_MBWi9mPg{BSHC;8twm5`CQPT( zbI(2Z%pE@7GxAi^HS@7sO*B%Vu9lkUMLv5i_1Kwc&fbv@<6lNLJT>?_;8_K|0Mpf! zk!DzenDaG?_grk6!B}3kBGZDO`CBWDRGLrbNTi^q8`V`|F6mMo-Kyz)yns8=+Silv$}y%y z_nbRpR_0V*XFAu3PMLGWOsR9>w60O}29J$LSK*Mz6^JG8Kda4vvq+z8R#D$!T4<`H zt%|aeQ`n;FCRo#G3L;mGaTDFpJy9X{Z*&FT0@>PovKR5HikkmLbWmGXk(}k78MoDR z#c8#6Mr=Azz`wREris=E>TC0~Qx9Fq;_ZNQvCa+HTXL??u8*?u8QtMw9q)h79HDk+ zv=iGWU(&7V`xte9mmcZE6}WEk<6BWy>xn`Z7U_Jp7#H&yM diff --git a/scripts/creation-portal-migration/constant/config.js b/scripts/creation-portal-migration/constant/config.js index 8c7fc5bc..71f590ea 100644 --- a/scripts/creation-portal-migration/constant/config.js +++ b/scripts/creation-portal-migration/constant/config.js @@ -13,7 +13,7 @@ const CONFIG = { HOST: { base: process.env.BASE_HOST, - vdn: process.env.VDN_HOST, + vdn: process.env.CREATION_PORTAL_URL, }, APIS: { token: "auth/realms/sunbird/protocol/openid-connect/token", @@ -50,7 +50,7 @@ const CONFIG = { client_id: process.env.BASE_CLIENT, client_secret: process.env.BASE_CLIENT_SECRET, }, - AUTHORIZATION: process.env.VDN_AUTHORIZATION, + AUTHORIZATION: process.env.CREATION_PORTAL_URL_AUTHORIZATION, }, }, }; From c9a1a10e5ba2f6f9b8a267b964b0dab493966068 Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Thu, 28 Jul 2022 12:47:49 +0530 Subject: [PATCH 18/36] removed redis config --- config/index.js | 2 -- .../v1/observationSubmissionsController.js | 17 ++++++------- healthCheck/healthCheckService.js | 2 +- healthCheck/redis.js | 25 ------------------- .../api-list/headers.js | 20 +++++++-------- .../api-list/program.js | 20 +++++++-------- .../api-list/question.js | 24 +++++++++--------- .../constant/config.js | 4 +-- 8 files changed, 42 insertions(+), 72 deletions(-) delete mode 100644 healthCheck/redis.js diff --git a/config/index.js b/config/index.js index 9a9d0f1d..0b875236 100644 --- a/config/index.js +++ b/config/index.js @@ -47,8 +47,6 @@ const configuration = { } }; - - db_connect(); kafka_connect(); // elasticsearch_connect(); diff --git a/controllers/v1/observationSubmissionsController.js b/controllers/v1/observationSubmissionsController.js index 81b5fa1f..ee7f03a6 100644 --- a/controllers/v1/observationSubmissionsController.js +++ b/controllers/v1/observationSubmissionsController.js @@ -837,10 +837,8 @@ module.exports = class ObservationSubmissions extends Abstract { externalId: solutionId, type : "observation", // scoringSystem : "pointsBasedScoring" - }, - { themes: 1, levelToScoreMapping: 1, scoringSystem : 1, flattenedThemes : 1} + }, { themes: 1, levelToScoreMapping: 1, scoringSystem : 1, flattenedThemes : 1} ).lean() - console.log("solutionDocumentntnt", solutionDocument); if (!solutionDocument) { return resolve({ @@ -850,7 +848,7 @@ module.exports = class ObservationSubmissions extends Abstract { } let queryObject = { - // "createdBy": createdBy, + "createdBy": createdBy, "entityExternalId": entityId, "solutionExternalId": solutionId, "submissionNumber" : (submissionNumber) ? submissionNumber : 1 @@ -861,7 +859,6 @@ module.exports = class ObservationSubmissions extends Abstract { { "answers": 1, "criteria": 1, "evidencesStatus": 1, "entityInformation": 1, "entityProfile": 1, "solutionExternalId": 1 , "scoringSystem" : 1} ).lean(); - console.log("sbdnsdbsndbs", queryObject, submissionDocument); if (!submissionDocument._id) { throw messageConstants.apiResponses.SUBMISSION_NOT_FOUND } @@ -997,14 +994,14 @@ module.exports = class ObservationSubmissions extends Abstract { req.body = req.body || {}; let message = messageConstants.apiResponses.CRITERIA_RATING; - // let createdBy = req.query.createdBy; + let createdBy = req.query.createdBy; let solutionId = req.query.solutionId; let submissionNumber = (req.query.submissionNumber) ? req.query.submissionNumber : "all"; let entityId = req.query.entityId.split(","); - // if (!createdBy) { - // throw messageConstants.apiResponses.CREATED_BY_NOT_FOUND; - // } + if (!createdBy) { + throw messageConstants.apiResponses.CREATED_BY_NOT_FOUND; + } if (!solutionId) { throw messageConstants.apiResponses.SOLUTION_ID_NOT_FOUND; @@ -1028,7 +1025,7 @@ module.exports = class ObservationSubmissions extends Abstract { } let queryObject = { - // "createdBy": createdBy, + "createdBy": createdBy, "solutionExternalId": solutionId, "entityExternalId": { $in: entityId } }; diff --git a/healthCheck/healthCheckService.js b/healthCheck/healthCheckService.js index 13b809a2..ab792f74 100644 --- a/healthCheck/healthCheckService.js +++ b/healthCheck/healthCheckService.js @@ -42,7 +42,7 @@ let health_check = async function(req,res) { let checks = []; let mongodbConnection = await mongodbHealthCheck.health_check(); let kafkaConnection = await kafkaHealthCheck.health_check(); - + let coreServiceStatus = await kendraHealthCheck.health_check(); let elasticSearchConnection = await elasticSearchHealthCheck.health_check(); diff --git a/healthCheck/redis.js b/healthCheck/redis.js deleted file mode 100644 index 9baa83bc..00000000 --- a/healthCheck/redis.js +++ /dev/null @@ -1,25 +0,0 @@ -// Dependencies -const redis = require("redis"); - -function health_check() { - return new Promise( async (resolve,reject) => { - - const db = redis.createClient(process.env.REDIS_URL); - - console.log("health_check", db); - - db.on("error", function () { - return resolve(false) - }); - - db.once("open", function() { - console.log("Redis connection open") - db.close(function(){}); - return resolve(true); - }); - }) -} - -module.exports = { - health_check : health_check -} \ No newline at end of file diff --git a/scripts/creation-portal-migration/api-list/headers.js b/scripts/creation-portal-migration/api-list/headers.js index 56c45b70..d9b30342 100644 --- a/scripts/creation-portal-migration/api-list/headers.js +++ b/scripts/creation-portal-migration/api-list/headers.js @@ -18,14 +18,14 @@ const genToken = async (url, body, type) => { }); return res ? res.data.access_token : ""; } else { - const token = type === "base" ? this.base_token : this.vdn_token; + const token = type === "base" ? this.base_token : this.creation_portal_token; return token; } }; const validateToken = (type) => { - const token = type === "base" ? this.base_token : this.vdn_token; + const token = type === "base" ? this.base_token : this.creation_portal_token; try { jwt.verify(token, "shhhhh"); @@ -45,11 +45,11 @@ const generateToken = async (type) => { body = querystring.stringify({ ...CONFIG.KEYS.BASE.QUERY }); this.base_token = await genToken(url, body, "base"); return this.base_token; - case "vdn": - url = CONFIG.HOST.vdn + CONFIG.APIS.token; - body = querystring.stringify({ ...CONFIG.KEYS.VDN.QUERY }); - this.vdn_token = await genToken(url, body, "vdn"); - return this.vdn_token; + case "creation_portal": + url = CONFIG.HOST.creation_portal + CONFIG.APIS.token; + body = querystring.stringify({ ...CONFIG.KEYS.CREATION_PORTAL.QUERY }); + this.creation_portal_token = await genToken(url, body, "creation_portal"); + return this.creation_portal_token; } }; @@ -67,13 +67,13 @@ const getHeaders = async (isTokenReq, type) => { } break; - case "vdn": + case "creation_portal": headers = { "Content-Type": "application/json", - Authorization: CONFIG.KEYS.VDN.AUTHORIZATION, + Authorization: CONFIG.KEYS.CREATION_PORTAL.AUTHORIZATION, }; if (isTokenReq) { - headers["x-authenticated-user-token"] = await generateToken("vdn"); + headers["x-authenticated-user-token"] = await generateToken("creation_portal"); } break; } diff --git a/scripts/creation-portal-migration/api-list/program.js b/scripts/creation-portal-migration/api-list/program.js index 67677787..4890e83b 100644 --- a/scripts/creation-portal-migration/api-list/program.js +++ b/scripts/creation-portal-migration/api-list/program.js @@ -5,7 +5,7 @@ const { getHeaders } = require("./headers"); const createProgram = async (templateData) => { - const url = CONFIG.HOST.vdn + CONFIG.APIS.create_program; + const url = CONFIG.HOST.creation_portal + CONFIG.APIS.create_program; const data = { request: { ...templateData, @@ -14,7 +14,7 @@ const createProgram = async (templateData) => { const config = { method: "post", url: url, - headers: await getHeaders(false, "vdn"), + headers: await getHeaders(false, "creation_portal"), data: data, }; @@ -23,7 +23,7 @@ const createProgram = async (templateData) => { }; const updateProgram = async (templateData) => { - const url = CONFIG.HOST.vdn + CONFIG.APIS.update_program; + const url = CONFIG.HOST.creation_portal + CONFIG.APIS.update_program; const data = { request: { ...templateData, @@ -32,7 +32,7 @@ const updateProgram = async (templateData) => { const config = { method: "post", url: url, - headers: await getHeaders(false, "vdn"), + headers: await getHeaders(false, "creation_portal"), data: data, }; const res = await axios(config); @@ -40,7 +40,7 @@ const updateProgram = async (templateData) => { }; const publishProgram = async (templateData) => { - const url = CONFIG.HOST.vdn + CONFIG.APIS.publish_program; + const url = CONFIG.HOST.creation_portal + CONFIG.APIS.publish_program; const data = { request: { ...templateData, @@ -49,7 +49,7 @@ const publishProgram = async (templateData) => { const config = { method: "post", url: url, - headers: await getHeaders(false, "vdn"), + headers: await getHeaders(false, "creation_portal"), data: data, }; @@ -58,7 +58,7 @@ const publishProgram = async (templateData) => { }; const nominateProgram = async (program_id, author) => { const url = - CONFIG.HOST.vdn + CONFIG.APIS.add_program_nomination; + CONFIG.HOST.creation_portal + CONFIG.APIS.add_program_nomination; const data = { request: { program_id: program_id, @@ -84,7 +84,7 @@ const nominateProgram = async (program_id, author) => { const config = { method: "post", url: url, - headers: await getHeaders(true, "vdn"), + headers: await getHeaders(true, "creation_portal"), data: data, }; @@ -94,7 +94,7 @@ const nominateProgram = async (program_id, author) => { const updateContributorToProgram = async (reqData) => { const url = - CONFIG.HOST.vdn + CONFIG.APIS.update_program_nomination; + CONFIG.HOST.creation_portal + CONFIG.APIS.update_program_nomination; const data = { request: { ...reqData, @@ -104,7 +104,7 @@ const updateContributorToProgram = async (reqData) => { const config = { method: "post", url: url, - headers: await getHeaders(true, "vdn"), + headers: await getHeaders(true, "creation_portal"), data: data, }; diff --git a/scripts/creation-portal-migration/api-list/question.js b/scripts/creation-portal-migration/api-list/question.js index 8e3cc3fa..7be4047c 100644 --- a/scripts/creation-portal-migration/api-list/question.js +++ b/scripts/creation-portal-migration/api-list/question.js @@ -6,7 +6,7 @@ const { getHeaders } = require("./headers"); // Questionset const createQuestionSet = async (templateData) => { - const url = CONFIG.HOST.vdn + CONFIG.APIS.create_questionset; + const url = CONFIG.HOST.creation_portal + CONFIG.APIS.create_questionset; const data = { request: { questionset: { ...templateData }, @@ -15,7 +15,7 @@ const createQuestionSet = async (templateData) => { const config = { method: "post", url: url, - headers: await getHeaders(true, "vdn"), + headers: await getHeaders(true, "creation_portal"), data: data, }; const res = await axios(config); @@ -24,12 +24,12 @@ const createQuestionSet = async (templateData) => { }; const updateQuestionSetHierarchy = async (templateData) => { - const url = CONFIG.HOST.vdn + CONFIG.APIS.update_hierarchy; + const url = CONFIG.HOST.creation_portal + CONFIG.APIS.update_hierarchy; const config = { method: "patch", url: url, - headers: await getHeaders(true, "vdn"), + headers: await getHeaders(true, "creation_portal"), data: templateData, }; @@ -39,14 +39,14 @@ const updateQuestionSetHierarchy = async (templateData) => { const publishQuestionSet = async (questionsetId) => { const url = - CONFIG.HOST.vdn + + CONFIG.HOST.creation_portal + CONFIG.APIS.publish_questionset + "/" + questionsetId; const config = { method: "post", url: url, - headers: await getHeaders(true, "vdn"), + headers: await getHeaders(true, "creation_portal"), data: {}, }; @@ -56,7 +56,7 @@ const publishQuestionSet = async (questionsetId) => { const readQuestionSetHierarchy = async (questionSetId) => { const url = - CONFIG.HOST.vdn + + CONFIG.HOST.creation_portal + CONFIG.APIS.read_questionset + questionSetId + "?mode=edit"; @@ -64,7 +64,7 @@ const readQuestionSetHierarchy = async (questionSetId) => { const config = { method: "get", url: url, - headers: await getHeaders(true, "vdn"), + headers: await getHeaders(true, "creation_portal"), }; const res = await axios(config); @@ -73,7 +73,7 @@ const readQuestionSetHierarchy = async (questionSetId) => { // Questions const createQuestions = async (templateData, questionId) => { - const url = CONFIG.HOST.vdn + CONFIG.APIS.create_question; + const url = CONFIG.HOST.creation_portal + CONFIG.APIS.create_question; const data = { request: { question: { ...templateData }, @@ -82,7 +82,7 @@ const createQuestions = async (templateData, questionId) => { const config = { method: "post", url: url, - headers: await getHeaders(true, "vdn"), + headers: await getHeaders(true, "creation_portal"), data: data, }; const res = await axios(config).catch((err) => { @@ -95,14 +95,14 @@ const createQuestions = async (templateData, questionId) => { const publishQuestion = async (questionId) => { const url = - CONFIG.HOST.vdn + + CONFIG.HOST.creation_portal + CONFIG.APIS.publish_question + "/" + questionId; const config = { method: "post", url: url, - headers: await getHeaders(true, "vdn") + headers: await getHeaders(true, "creation_portal") }; const res = await axios(config) diff --git a/scripts/creation-portal-migration/constant/config.js b/scripts/creation-portal-migration/constant/config.js index 71f590ea..f39d89b9 100644 --- a/scripts/creation-portal-migration/constant/config.js +++ b/scripts/creation-portal-migration/constant/config.js @@ -13,7 +13,7 @@ const CONFIG = { HOST: { base: process.env.BASE_HOST, - vdn: process.env.CREATION_PORTAL_URL, + creation_portal: process.env.CREATION_PORTAL_URL, }, APIS: { token: "auth/realms/sunbird/protocol/openid-connect/token", @@ -42,7 +42,7 @@ const CONFIG = { }, AUTHORIZATION: process.env.BASE_AUTHORIZATION, }, - VDN: { + CREATION_PORTAL: { QUERY: { username: process.env.BASE_USER, password: process.env.BASE_PWD, From 8184b8190e135706a165822a1a9859f0762f77d9 Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Thu, 28 Jul 2022 12:52:37 +0530 Subject: [PATCH 19/36] updated readme --- scripts/creation-portal-migration/README.md | Bin 2550 -> 2842 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/scripts/creation-portal-migration/README.md b/scripts/creation-portal-migration/README.md index 5befeb8c2c6e1ded459f78bb937e73d48112abc8..3c87fe8cb3666b922db2126f0e44f22f6f8582a4 100644 GIT binary patch delta 226 zcmew+JWFf?2NNqNgD!)@WOk-dW?lxa$p_gaCtqM=nS6^)V6qM~*W@}TC#Z`3OtF*q zu*prn!6pQh@q&t$G9yGEF}pxTRap?ClUUrKqSui`U05TaqVri1YI)fFprVm%jX<0B hL3wl8Gf`Z(h(m3%4hJ7xR()~;hZC}j&5Jlf7y)hgJFWl# delta 140 zcmbOw_Dy&L2h(IerjW@B%q){Hu!&4AVCI@UhskO36Q-ESSJ(uAe6Ps|m?3N*7MIC+ ztYVYbu(*NcezEdRHej^?ib3Rmu|_cSGH^`}XNv{O$4$0iZL`x Date: Thu, 28 Jul 2022 12:54:47 +0530 Subject: [PATCH 20/36] updated readme --- scripts/creation-portal-migration/logger.js | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/scripts/creation-portal-migration/logger.js b/scripts/creation-portal-migration/logger.js index e398314f..2c1794fc 100644 --- a/scripts/creation-portal-migration/logger.js +++ b/scripts/creation-portal-migration/logger.js @@ -28,19 +28,5 @@ var logger = new Logger({ }); -error = async (msg) => { - logger.error(msg); -}; -debug = async (msg) => { - logger.debug(msg); -}; -info = async (msg) => { - logger.info(msg); -}; - -module.exports = { - info, - error, - debug, -}; +module.exports = logger; From 88a716daadf55a57b1604fd486d32b005fd2419a Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Thu, 28 Jul 2022 12:56:01 +0530 Subject: [PATCH 21/36] updated server --- scripts/creation-portal-migration/server.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/scripts/creation-portal-migration/server.js b/scripts/creation-portal-migration/server.js index b46522e2..606772e7 100644 --- a/scripts/creation-portal-migration/server.js +++ b/scripts/creation-portal-migration/server.js @@ -70,12 +70,9 @@ const migrateData = async (req, res) => { }, }; const db = await createDBInstance(); - // req.query.questionsetID || - const id = "600b21c57ea68a7ed9278873"; const data = await findAll(CONFIG.DB.TABLES.solutions, { - _id: ObjectId(id), - // programId: { $exists: true }, + programId: { $exists: true }, type: { $in: ["observation", "survey"] }, }); From 6641b072e49683e1e73bb10916c9106cec91982a Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Thu, 28 Jul 2022 12:56:17 +0530 Subject: [PATCH 22/36] updated server --- scripts/creation-portal-migration/server.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/scripts/creation-portal-migration/server.js b/scripts/creation-portal-migration/server.js index 606772e7..1fd58fbd 100644 --- a/scripts/creation-portal-migration/server.js +++ b/scripts/creation-portal-migration/server.js @@ -5,7 +5,6 @@ const { getQuestionSetTemplates, } = require("./template/generate/gQuestionSet.js"); -const { ObjectId } = require("mongodb"); const logger = require("./logger"); var fs = require("fs"); const { CONFIG } = require("./constant/config"); @@ -86,11 +85,9 @@ const migrateData = async (req, res) => { console.log(); console.log("migratedCounttt", JSON.stringify(migratedCount)); console.log(); - // logger.in + logger.info(`\n migratedCount ${JSON.stringify(migratedCount)}`); - - // return template; - process.exit(); + process.exit(); } catch (err) { logger.error(`Error while migrating : ${err}`) From 08027dceb069e189607523dd1cfb9375f67d46ff Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Thu, 28 Jul 2022 12:57:54 +0530 Subject: [PATCH 23/36] updated server --- controllers/v1/observationSubmissionsController.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/controllers/v1/observationSubmissionsController.js b/controllers/v1/observationSubmissionsController.js index ee7f03a6..c3395df8 100644 --- a/controllers/v1/observationSubmissionsController.js +++ b/controllers/v1/observationSubmissionsController.js @@ -837,8 +837,7 @@ module.exports = class ObservationSubmissions extends Abstract { externalId: solutionId, type : "observation", // scoringSystem : "pointsBasedScoring" - }, { themes: 1, levelToScoreMapping: 1, scoringSystem : 1, flattenedThemes : 1} - ).lean() + }, { themes: 1, levelToScoreMapping: 1, scoringSystem : 1, flattenedThemes : 1}).lean() if (!solutionDocument) { return resolve({ From 22d561a9b8708201838f1e3d713d925e86e79f2d Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Thu, 28 Jul 2022 12:58:25 +0530 Subject: [PATCH 24/36] updated server --- controllers/v1/observationSubmissionsController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/v1/observationSubmissionsController.js b/controllers/v1/observationSubmissionsController.js index c3395df8..4ac1170c 100644 --- a/controllers/v1/observationSubmissionsController.js +++ b/controllers/v1/observationSubmissionsController.js @@ -838,7 +838,7 @@ module.exports = class ObservationSubmissions extends Abstract { type : "observation", // scoringSystem : "pointsBasedScoring" }, { themes: 1, levelToScoreMapping: 1, scoringSystem : 1, flattenedThemes : 1}).lean() - + if (!solutionDocument) { return resolve({ status: httpStatusCode.bad_request.status, From 94f1e57fe84fc39835d34f701fc6be90dbfdc239 Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Thu, 28 Jul 2022 12:58:59 +0530 Subject: [PATCH 25/36] updated server --- controllers/v1/observationSubmissionsController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/v1/observationSubmissionsController.js b/controllers/v1/observationSubmissionsController.js index 4ac1170c..de5ee603 100644 --- a/controllers/v1/observationSubmissionsController.js +++ b/controllers/v1/observationSubmissionsController.js @@ -838,7 +838,7 @@ module.exports = class ObservationSubmissions extends Abstract { type : "observation", // scoringSystem : "pointsBasedScoring" }, { themes: 1, levelToScoreMapping: 1, scoringSystem : 1, flattenedThemes : 1}).lean() - + if (!solutionDocument) { return resolve({ status: httpStatusCode.bad_request.status, From 65e7d80c574e2b9a184b81cd1da967150e401057 Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Thu, 28 Jul 2022 13:08:22 +0530 Subject: [PATCH 26/36] updated env --- .env.sample | 2 +- scripts/creation-portal-migration/README.md | Bin 2842 -> 2842 bytes .../constant/config.js | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.env.sample b/.env.sample index 30c164e6..80b4e09a 100644 --- a/.env.sample +++ b/.env.sample @@ -43,4 +43,4 @@ BASE_AUTHORIZATION = 'Bearer token' // base bearer t CREATION_PORTAL_URL = "https://dock.sunbirded.org/" // creation portal Url -CREATION_PORTAL_URL_AUTHORIZATION= 'Bearer token' // creation portal bearer token with all the permissions \ No newline at end of file +CREATION_PORTAL_AUTHORIZATION_KEY= 'Bearer token' // creation portal bearer token with all the permissions \ No newline at end of file diff --git a/scripts/creation-portal-migration/README.md b/scripts/creation-portal-migration/README.md index 3c87fe8cb3666b922db2126f0e44f22f6f8582a4..158b8b376aed0e983d90ec3167095228bfa941b6 100644 GIT binary patch delta 24 fcmbOwHcM Date: Thu, 28 Jul 2022 15:53:13 +0530 Subject: [PATCH 27/36] updated env --- .env.sample | 6 +++--- scripts/creation-portal-migration/README.md | Bin 2842 -> 2858 bytes 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.env.sample b/.env.sample index 80b4e09a..dba2ada1 100644 --- a/.env.sample +++ b/.env.sample @@ -30,7 +30,7 @@ DISABLE_LEARNER_SERVICE_ON_OFF = "ON" -# ML Script +# creation portal migration Script BASE_USER = "n11@yopmail.com" // base user name of the sunbird enviornment BASE_PWD = "password" // base user pwd of the sunbird enviornment @@ -39,8 +39,8 @@ BASE_CLIENT = "client-id" // base client o BASE_CLIENT_SECRET = "client-secret-key" // base client secret of the sunbird enviornment BASE_HOST = "https://dev.sunbirded.org/" // base host url of the sunbird enviornment -BASE_AUTHORIZATION = 'Bearer token' // base bearer token with all the permissions to run the script of the sunbird enviornment +BASE_AUTHORIZATION = "Bearer + 'token'" // base bearer token with all the permissions to run the script of the sunbird enviornment CREATION_PORTAL_URL = "https://dock.sunbirded.org/" // creation portal Url -CREATION_PORTAL_AUTHORIZATION_KEY= 'Bearer token' // creation portal bearer token with all the permissions \ No newline at end of file +CREATION_PORTAL_AUTHORIZATION_KEY= "Bearer + 'token'" // creation portal bearer token with all the permissions \ No newline at end of file diff --git a/scripts/creation-portal-migration/README.md b/scripts/creation-portal-migration/README.md index 158b8b376aed0e983d90ec3167095228bfa941b6..db49e11ea778e7eb47e5e5993e169eacf101a76b 100644 GIT binary patch delta 97 zcmbOwwn}V661%bzgA+q4Ln1>FkSteGNFI=_G+B^Ya`HPClg+2t OcQF#9Ve@>>YDNI>#uPmO delta 41 pcmZ1_HcMd~5di+{3;+NC From 5477f2abfdaa66a6e88cde60a8b12d50913251da Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Tue, 2 Aug 2022 16:54:14 +0530 Subject: [PATCH 28/36] renamed migratedId to referenceQuestionSetId --- .../template/generate/gQuestionSet.js | 6 ++-- .../template/generate/template.js | 2 +- .../template/helpers/hierarchyHelper.js | 24 +++++++-------- .../template/helpers/questionsetHelper.js | 29 +++++++++---------- .../template/migrate/common.js | 28 +++++++++--------- .../template/migrate/matrix.js | 20 ++++++------- .../template/migrate/nonmatrix.js | 6 ++-- 7 files changed, 57 insertions(+), 58 deletions(-) diff --git a/scripts/creation-portal-migration/template/generate/gQuestionSet.js b/scripts/creation-portal-migration/template/generate/gQuestionSet.js index f0996763..fd26c944 100644 --- a/scripts/creation-portal-migration/template/generate/gQuestionSet.js +++ b/scripts/creation-portal-migration/template/generate/gQuestionSet.js @@ -55,7 +55,7 @@ const migrateQuestionset = async (solution, programId, migratedCount) => { let templateData = setQuestionSetTemplate(solution, programId); const questionSetId = solution?._id.toString(); - let questionSetMigratedId = solution.migratedId; + let questionSetMigratedId = solution.referenceQuestionSetId; if (questionSetMigratedId) { migratedCount.success.questionSet.existing.migrated++; @@ -84,10 +84,10 @@ const migrateQuestionset = async (solution, programId, migratedCount) => { } await updateById(CONFIG.DB.TABLES.solutions, questionSetId, { - migratedId: questionSetMigratedId, + referenceQuestionSetId: questionSetMigratedId, }).catch((err) => { logger.error( - `migrateQuestionset: Error while updating solution migratedId: + `migrateQuestionset: Error while updating solution referenceQuestionSetId: ${err}` ); console.log(`migrateQuestionset: Error while updating question: diff --git a/scripts/creation-portal-migration/template/generate/template.js b/scripts/creation-portal-migration/template/generate/template.js index 627c4762..8257873e 100644 --- a/scripts/creation-portal-migration/template/generate/template.js +++ b/scripts/creation-portal-migration/template/generate/template.js @@ -32,7 +32,7 @@ const getSectionTemplate = (section) => { timesUsed: "", updatedAt: section?.updatedAt, weightage: "", - migratedId: null, + referenceQuestionSetId: null, }; }; diff --git a/scripts/creation-portal-migration/template/helpers/hierarchyHelper.js b/scripts/creation-portal-migration/template/helpers/hierarchyHelper.js index 2cea5fed..063aa2b4 100644 --- a/scripts/creation-portal-migration/template/helpers/hierarchyHelper.js +++ b/scripts/creation-portal-migration/template/helpers/hierarchyHelper.js @@ -16,16 +16,16 @@ const { CONFIG } = require("./../../constant/config"); const { updateById } = require("../../db"); const logger = require("../../logger"); -const updateHierarchyChildren = (hierarchy, migratedId, index) => { +const updateHierarchyChildren = (hierarchy, referenceQuestionSetId, index) => { - logger.debug(`updateHierarchyChildren: migratedId = ${migratedId}`) + logger.debug(`updateHierarchyChildren: referenceQuestionSetId = ${referenceQuestionSetId}`) if ( - migratedId && - !hierarchy.criterias[index].questions.includes(migratedId) + referenceQuestionSetId && + !hierarchy.criterias[index].questions.includes(referenceQuestionSetId) ) { - hierarchy.criterias[index].questions.push(migratedId); + hierarchy.criterias[index].questions.push(referenceQuestionSetId); } return hierarchy; }; @@ -154,7 +154,7 @@ const updateHierarchyTemplate = async ( for (let i = 0; i < hierarchy.criterias.length; i++) { const criterias = hierarchy.criterias[i]; - hierarchy.criterias[i].migratedId = result[criterias.name]; + hierarchy.criterias[i].referenceQuestionSetId = result[criterias.name]; } } else { migratedCount.success.questionSet.existing.hierarchy++; @@ -252,11 +252,11 @@ const branchingQuestionSetHierarchy = async (hierarchy) => { const hierarchyData = find(questionSetHierarchy.children, { name: criteria?.name, }); - criteria.migratedId = + criteria.referenceQuestionSetId = hierarchy.questionset && hierarchy.isHierarchyUpdated ? hierarchyData?.identifier - : criteria.migratedId; - if (criteria?.migratedId) { + : criteria.referenceQuestionSetId; + if (criteria?.referenceQuestionSetId) { const metadata = pick(criteria, [ "code", "name", @@ -266,7 +266,7 @@ const branchingQuestionSetHierarchy = async (hierarchy) => { "allowMultipleInstances", "instances", ]); - updateHierarchyData.request.data.nodesModified[criteria.migratedId] = { + updateHierarchyData.request.data.nodesModified[criteria.referenceQuestionSetId] = { metadata: { ...metadata, allowBranching: "Yes", @@ -278,9 +278,9 @@ const branchingQuestionSetHierarchy = async (hierarchy) => { }; updateHierarchyData.request.data.hierarchy[ hierarchy.questionset - ].children.push(criteria.migratedId); + ].children.push(criteria.referenceQuestionSetId); - updateHierarchyData.request.data.hierarchy[criteria.migratedId] = { + updateHierarchyData.request.data.hierarchy[criteria.referenceQuestionSetId] = { children: compact(criteria.questions), root: false, }; diff --git a/scripts/creation-portal-migration/template/helpers/questionsetHelper.js b/scripts/creation-portal-migration/template/helpers/questionsetHelper.js index 5ad229f6..c226f7fa 100644 --- a/scripts/creation-portal-migration/template/helpers/questionsetHelper.js +++ b/scripts/creation-portal-migration/template/helpers/questionsetHelper.js @@ -49,13 +49,13 @@ const createQuestionTemplate = async (question, migratedCount) => { console.log(); const type = question?.responseType; - let migratedId = question?.migratedId; + let referenceQuestionSetId = question?.referenceQuestionSetId; let query = {}; let questionToMigrate = {}; let published = question?.isPublished; - if (type && !migratedId) { + if (type && !referenceQuestionSetId) { if (type.toLowerCase() === "date") { questionToMigrate = getDateTemplate(question); } @@ -82,26 +82,25 @@ const createQuestionTemplate = async (question, migratedCount) => { console.log("db Question", JSON.stringify(question)); console.log(); - migratedId = await createQuestions(questionToMigrate, question._id); - console.log("migraysyys", migratedId); - question.migratedId = migratedId; + referenceQuestionSetId = await createQuestions(questionToMigrate, question._id); + question.referenceQuestionSetId = referenceQuestionSetId; } } - if (migratedId && !published) { - const res = await publishQuestion(migratedId).catch((err) => { - if (!migratedCount.failed.question.ids.includes(migratedId)) { + if (referenceQuestionSetId && !published) { + const res = await publishQuestion(referenceQuestionSetId).catch((err) => { + if (!migratedCount.failed.question.ids.includes(referenceQuestionSetId)) { migratedCount.failed.question.count++; - migratedCount.failed.question.ids.push(migratedId); + migratedCount.failed.question.ids.push(referenceQuestionSetId); } - logger.error(`Error while publishing the question for migratedId: ${migratedId} Error: + logger.error(`Error while publishing the question for referenceQuestionSetId: ${referenceQuestionSetId} Error: ${JSON.stringify(err.response.data)}`); }); logger.info( - `createQuestion Template publish response: ${res} , "migratedId" ${migratedId} questionId, ${question?._id}` + `createQuestion Template publish response: ${res} , "referenceQuestionSetId" ${referenceQuestionSetId} questionId, ${question?._id}` ); @@ -109,16 +108,16 @@ const createQuestionTemplate = async (question, migratedCount) => { if (res) { question.isPublished = true; published = true; - logger.info(`createQuestion Template published: ${migratedId}`); + logger.info(`createQuestion Template published: ${referenceQuestionSetId}`); } } - if (migratedId) { - question.migratedId = migratedId; + if (referenceQuestionSetId) { + question.referenceQuestionSetId = referenceQuestionSetId; query = { - migratedId, + referenceQuestionSetId, published: published, }; } else { diff --git a/scripts/creation-portal-migration/template/migrate/common.js b/scripts/creation-portal-migration/template/migrate/common.js index 90914c9b..9fde10ca 100644 --- a/scripts/creation-portal-migration/template/migrate/common.js +++ b/scripts/creation-portal-migration/template/migrate/common.js @@ -6,9 +6,9 @@ const { } = require("../helpers/questionsetHelper"); const logger = require("../../logger"); -const initHierarchy = (questionsetid, solution, programId, migratedId) => { +const initHierarchy = (questionsetid, solution, programId, referenceQuestionSetId) => { return { - questionset: migratedId, + questionset: referenceQuestionSetId, questionsetDbId: questionsetid, isHierarchyUpdated: solution?.isHierarchyUpdated || false, isBranchingUpdated: solution?.isBranchingUpdated || false, @@ -26,7 +26,7 @@ const initHierarchy = (questionsetid, solution, programId, migratedId) => { const getCriteriaData = (criteria, type, question = {}) => { if (isEmpty(question)) { return { - migratedId: "", + referenceQuestionSetId: "", criDbId: criteria?._id.toString(), code: criteria?.externalId, name: criteria?.name, @@ -40,7 +40,7 @@ const getCriteriaData = (criteria, type, question = {}) => { }; } else { return { - migratedId: "", + referenceQuestionSetId: "", _id: criteria?._id, criDbId: criteria?._id.toString(), code: criteria?.externalId, @@ -122,7 +122,7 @@ const getQuestion = async (questions, questions2, id, migratedCount) => { question = matched; } - if (!isEmpty(question) && (!question?.migratedId || !question?.isPublished)) { + if (!isEmpty(question) && (!question?.referenceQuestionSetId || !question?.isPublished)) { question = await createQuestionTemplate(question, migratedCount); } @@ -170,38 +170,38 @@ const updateHierarchyBranching = ( pQuestion, child ) => { - const migratedId = child?.migratedId; + const referenceQuestionSetId = child?.referenceQuestionSetId; const visible = child?.visibleIf ? child?.visibleIf[0] : {}; logger.debug( - `updateHierarchyBranching: migratedId = ${migratedId}; parentId = ${parentId}; visible: ${visible}` + `updateHierarchyBranching: referenceQuestionSetId = ${referenceQuestionSetId}; parentId = ${parentId}; visible: ${visible}` ); if (!isEmpty(visible)) { - if (hasProperty(branching, index, parentId) && migratedId) { + if (hasProperty(branching, index, parentId) && referenceQuestionSetId) { if ( !branching.criterias[index].branchingLogic[parentId].target.includes( - migratedId + referenceQuestionSetId ) ) { branching.criterias[index].branchingLogic[parentId].target.push( - migratedId + referenceQuestionSetId ); } - branching.criterias[index].branchingLogic[migratedId] = { + branching.criterias[index].branchingLogic[referenceQuestionSetId] = { target: [], preCondition: getPrecondition(visible, parentId, pQuestion), source: [parentId], }; - } else if (migratedId) { + } else if (referenceQuestionSetId) { branching.criterias[index].branchingLogic[parentId] = { - target: [migratedId], + target: [referenceQuestionSetId], preCondition: {}, source: [], }; - branching.criterias[index].branchingLogic[migratedId] = { + branching.criterias[index].branchingLogic[referenceQuestionSetId] = { target: [], preCondition: getPrecondition(visible, parentId, pQuestion), source: [parentId], diff --git a/scripts/creation-portal-migration/template/migrate/matrix.js b/scripts/creation-portal-migration/template/migrate/matrix.js index 2c10100a..bbac356d 100644 --- a/scripts/creation-portal-migration/template/migrate/matrix.js +++ b/scripts/creation-portal-migration/template/migrate/matrix.js @@ -287,14 +287,14 @@ const childrenAndnoVisibleIf = async ( matrixHierarchy = updateMatrixHierarchy( matrixHierarchy, matrixId, - child?.migratedId, + child?.referenceQuestionSetId, criteriaId ); matrixHierarchy = updateMatrixHierarchyBranching( matrixHierarchy, criteriaId, matrixId, - question?.migratedId, + question?.referenceQuestionSetId, question, child ); @@ -350,14 +350,14 @@ const noChildrenAndVisibleIf = async ( matrixHierarchy = updateMatrixHierarchy( matrixHierarchy, matrixId, - pQuestion?.migratedId, + pQuestion?.referenceQuestionSetId, criteriaId ); matrixHierarchy = updateMatrixHierarchyBranching( matrixHierarchy, criteriaId, matrixId, - pQuestion?.migratedId, + pQuestion?.referenceQuestionSetId, pQuestion, question ); @@ -384,19 +384,19 @@ const noChildrenAndVisibleIf = async ( const updateMatrixHierarchy = ( matrixHierarchy, matrixId, - migratedId, + referenceQuestionSetId, criteriaId ) => { logger.debug( - `updateMatrixHierarchy: matrixId = ${matrixId}; migratedId = ${migratedId}` + `updateMatrixHierarchy: matrixId = ${matrixId}; referenceQuestionSetId = ${referenceQuestionSetId}` ); const criterias = matrixHierarchy.criterias || []; for (let i = 0; i < criterias.length; i++) { const criteria = criterias[i]; if (criteria?._id.toString() === matrixId) { - if (!criteria.questions.includes(migratedId)) { - criteria.questions.push(migratedId); + if (!criteria.questions.includes(referenceQuestionSetId)) { + criteria.questions.push(referenceQuestionSetId); } } criterias[i] = criteria; @@ -419,7 +419,7 @@ const updateMatrixHierarchyQuestions = ( questions ) => { logger.debug( - `updateMatrixHierarchyQuestions: criteriaId = ${criteriaId}; matrixId = ${matrixId}; question: ${question?._id}: migratedId: ${question?.migratedId}` + `updateMatrixHierarchyQuestions: criteriaId = ${criteriaId}; matrixId = ${matrixId}; question: ${question?._id}: referenceQuestionSetId: ${question?.referenceQuestionSetId}` ); const matched = isQuestionMatched( @@ -438,7 +438,7 @@ const updateMatrixHierarchyQuestions = ( matrixHierarchy = updateMatrixHierarchy( matrixHierarchy, matrixId, - question?.migratedId, + question?.referenceQuestionSetId, criteriaId ); } diff --git a/scripts/creation-portal-migration/template/migrate/nonmatrix.js b/scripts/creation-portal-migration/template/migrate/nonmatrix.js index cfc8fe48..c858298e 100644 --- a/scripts/creation-portal-migration/template/migrate/nonmatrix.js +++ b/scripts/creation-portal-migration/template/migrate/nonmatrix.js @@ -42,7 +42,7 @@ const updateNonMatrixHierarchyChildren = ( if (matched === false) { hierarchy = updateHierarchyChildren( hierarchy, - question?.migratedId, + question?.referenceQuestionSetId, index ); nonMatrixQuestions.push(question); @@ -248,7 +248,7 @@ const nonMatrixChildrenAndNoVisibleIf = async ( hierarchy = updateHierarchyBranching( hierarchy, index, - question?.migratedId, + question?.referenceQuestionSetId, question, child ); @@ -320,7 +320,7 @@ const nonMatrixNoChildrenAndVisibleIf = async ( hierarchy = updateHierarchyBranching( hierarchy, index, - pQuestion?.migratedId, + pQuestion?.referenceQuestionSetId, pQuestion, question ); From 75b30aca1a00263abb795e929b8980aff8ddcabc Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Fri, 12 Aug 2022 14:45:13 +0530 Subject: [PATCH 29/36] review changes --- .env.sample | 59 +++++++++++++++--- package.json | 2 +- scripts/creation-portal-migration/README.md | Bin 2858 -> 2920 bytes .../api-list/headers.js | 32 ++++++---- .../api-list/program.js | 4 +- .../api-list/question.js | 1 - .../api-list/user.js | 8 +-- .../constant/config.js | 56 ++++++++--------- scripts/creation-portal-migration/db/index.js | 5 +- .../{server.js => index.js} | 10 +-- scripts/creation-portal-migration/logger.js | 4 +- .../template/config/questionSet.js | 37 ++++++----- .../template/generate/gProgram.js | 24 +++---- .../template/generate/gQuestionSet.js | 58 +---------------- .../template/helpers/hierarchyHelper.js | 13 ++-- .../template/helpers/questionsetHelper.js | 44 +++++-------- .../template/migrate/common.js | 20 +++--- .../template/migrate/matrix.js | 27 ++++---- .../template/migrate/nonmatrix.js | 18 +----- 19 files changed, 187 insertions(+), 235 deletions(-) rename scripts/creation-portal-migration/{server.js => index.js} (91%) diff --git a/.env.sample b/.env.sample index dba2ada1..50cd8ea9 100644 --- a/.env.sample +++ b/.env.sample @@ -32,15 +32,56 @@ DISABLE_LEARNER_SERVICE_ON_OFF = "ON" # creation portal migration Script -BASE_USER = "n11@yopmail.com" // base user name of the sunbird enviornment -BASE_PWD = "password" // base user pwd of the sunbird enviornment -BASE_GRANT = "password-grant" // base grant of the sunbird enviornment -BASE_CLIENT = "client-id" // base client of the sunbird enviornment -BASE_CLIENT_SECRET = "client-secret-key" // base client secret of the sunbird enviornment +MASTER_USER_EMAIL = "n11@yopmail.com" // master user name of the sunbird enviornment to generate token +MASTER_USER_PWD = "password" // master user pwd of the sunbird enviornment to generate token +TOKEN_GEN_GRANT_TYPE = "password-grant" // grant type to generate token for the sunbird enviornment +TOKEN_GEN_CLIENT = "client-id" // client to generate token for the sunbird enviornment +TOKEN_GEN_CLIENT_SECRET = "client-secret-key" // client secret to generate token for the sunbird enviornment -BASE_HOST = "https://dev.sunbirded.org/" // base host url of the sunbird enviornment -BASE_AUTHORIZATION = "Bearer + 'token'" // base bearer token with all the permissions to run the script of the sunbird enviornment +ED_BASE_URL = "https://dev.sunbirded.org/" // base host url of the sunbird enviornment +ED_AUTHORIZATION = "Bearer + 'token'" // base bearer token with all the permissions to run the script of the sunbird enviornment -CREATION_PORTAL_URL = "https://dock.sunbirded.org/" // creation portal Url +CREATION_PORTAL_URL = "https://dock.sunbirded.org/" // creation portal Url + +CREATION_PORTAL_AUTHORIZATION= "Bearer + 'token'" // creation portal bearer token with all the permissions for below urls, + + + +# Migration + + # API'S required for migration + + GEN_TOKEN: "auth/realms/sunbird/protocol/openid-connect/token", // To generate token + READ_USER: "api/user/v5/read/", // To read user data, Authorization: ED_AUTHORIZATION + SEARCH_USER: "api/user/v3/search", // To search user data, Authorization: ED_AUTHORIZATION + CREATE_QUESTIONSET: "api/questionset/v1/create", // To create questionset, Authorization: CREATION_PORTAL_AUTHORIZATION + UPDATE_HIERARCHY: "api/questionset/v1/hierarchy/update", // To update questionset hierarchy, Authorization: CREATION_PORTAL_AUTHORIZATION + PUBLISH_QUESTIONSET: "api/questionset/v1/publish", // To publish questionset, Authorization: CREATION_PORTAL_AUTHORIZATION + READ_QUESTIONSET: "api/questionset/v1/hierarchy/", // To read questionset, Authorization: CREATION_PORTAL_AUTHORIZATION + CREATE_QUESTION: "api/question/v1/create", // To create question, Authorization: CREATION_PORTAL_AUTHORIZATION + PUBLISH_QUESTION: "api/question/v1/publish", // To publish question, Authorization: CREATION_PORTAL_AUTHORIZATION + CREATE_PROGRAM: "api/program/v1/create", // To create program, Authorization: CREATION_PORTAL_AUTHORIZATION + UPDATE_PROGRAM: "api/program/v1/update", // To update program, Authorization: CREATION_PORTAL_AUTHORIZATION + ADD_PROGRAM_NOMINATION: "api/program/v1/nomination/add", // To add nomination to program, Authorization: CREATION_PORTAL_AUTHORIZATION + UPDATE_PROGRAM_NOMINATION: "api/program/v1/nomination/update", // To update program nomination, Authorization: CREATION_PORTAL_AUTHORIZATION + PUBLISH_PROGRAM: "api/program/v1/publish", // To publish program, Authorization: CREATION_PORTAL_AUTHORIZATION + + # Default users to create program and question + + DEFAULT_SLUG = 'sunbird' // default slug + + DEFAULT_USER_ID_TO_CREATE_PROGRAM = '5a587cc1-e018-4859-a0a8-e842650b9d64' // default user id to create program if solution author is not present + DEFAULT_USER_CHANNEL_ID_TO_CREATE_PROGRAM = '01285019302823526477' // default user channel id to create program if solution author is not present + DEFAULT_USER_SOURCING_ORG_NAME_TO_CREATE_PROGRAM = 'NIT' // default user sourcing org name + DEFAULT_PROGRAM_CREATOR_ORGANISATION_ID = '937dd865-b256-4c1a-9830-a9b5b89f0913'// default user org id to create program if solution author is not present + + + DEFAULT_FRAMEWORK_ID = 'nit_tpd' // default framework id to create program + DEFAULT_FRAMEWORK_TYPE = 'TPD' // default framework type to create program + + DEFAULT_USER_ID_TO_ADD_CONTRIBUTOR = 'bb551fff-121e-4a18-b969-984ac62bd572' // default user id to nominate and add contributor to program + + DEFAULT_CONTRIBUTOR_USER_ID = '4e397c42-495e-4fdb-8558-f98176230916' // default user id to contribute content to the program + DEFAULT_CONTRIBUTOR_USER_NAME = 'check1@yopmail.com' // default user name to contribute content to the program + DEFAULT_CONTRIBUTOR_USER_CHANNEL_ID = '01309282781705830427' // default user user channel to contribute content to the program -CREATION_PORTAL_AUTHORIZATION_KEY= "Bearer + 'token'" // creation portal bearer token with all the permissions \ No newline at end of file diff --git a/package.json b/package.json index 16811338..7d695c81 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "morgan": "^1.9.0", "p-each-series": "^2.1.0", "path": "^0.12.7", - "querystring": "^0.2.1", + "query-string": "^7.1.1", "request": "^2.88.0", "require-all": "^2.2.0", "uuid": "^3.3.2", diff --git a/scripts/creation-portal-migration/README.md b/scripts/creation-portal-migration/README.md index db49e11ea778e7eb47e5e5993e169eacf101a76b..03a312758c0208d12c49ea2c10ac802a71b817b2 100644 GIT binary patch delta 199 zcmZ1__Cjm}ACsalgCj#QLkNQ_Ll8qeLnx39VYxDZWIP#sCd)G&6~(G*@@uB6>LCpN z4BkLJen8#sa5@O6*AHlFJP=1R1TeTxzRY}B5JTTa@JgtK?EK1MRtgTWtYZph^K9MY4|v2B5?TFEK9c@et`6MH;^H_-aX%||&G GGXen010zlV diff --git a/scripts/creation-portal-migration/api-list/headers.js b/scripts/creation-portal-migration/api-list/headers.js index d9b30342..60f4d80e 100644 --- a/scripts/creation-portal-migration/api-list/headers.js +++ b/scripts/creation-portal-migration/api-list/headers.js @@ -1,6 +1,6 @@ const { default: axios } = require("axios"); const { CONFIG } = require("../constant/config"); -const querystring = require("querystring"); +const querystring = require("query-string"); const jwt = require("jsonwebtoken"); const logger = require("../logger"); @@ -18,18 +18,24 @@ const genToken = async (url, body, type) => { }); return res ? res.data.access_token : ""; } else { - const token = type === "base" ? this.base_token : this.creation_portal_token; + const token = type === "ed" ? this.ed_token : this.creation_portal_token; return token; } }; const validateToken = (type) => { - const token = type === "base" ? this.base_token : this.creation_portal_token; + const token = type === "ed" ? this.ed_token : this.creation_portal_token; try { - jwt.verify(token, "shhhhh"); - return true; + if (token) { + const decoded = jwt.decode(token, {header: true}); + if (Date.now() >= decoded?.exp * 1000) { + return false; + } + return true; + } + return false; } catch (err) { return false; } @@ -40,11 +46,11 @@ const generateToken = async (type) => { let body = {}; switch (type) { - case "base": - url = CONFIG.HOST.base + CONFIG.APIS.token; - body = querystring.stringify({ ...CONFIG.KEYS.BASE.QUERY }); - this.base_token = await genToken(url, body, "base"); - return this.base_token; + case "ed": + url = CONFIG.HOST.ed + CONFIG.APIS.token; + body = querystring.stringify({ ...CONFIG.KEYS.ED.QUERY }); + this.ed_token = await genToken(url, body, "ed"); + return this.ed_token; case "creation_portal": url = CONFIG.HOST.creation_portal + CONFIG.APIS.token; body = querystring.stringify({ ...CONFIG.KEYS.CREATION_PORTAL.QUERY }); @@ -57,13 +63,13 @@ const getHeaders = async (isTokenReq, type) => { let headers = {}; switch (type) { - case "base": + case "ed": headers = { "Content-Type": "application/json", - Authorization: CONFIG.KEYS.BASE.AUTHORIZATION, + Authorization: CONFIG.KEYS.ED.AUTHORIZATION, }; if (isTokenReq) { - headers["x-authenticated-user-token"] = await generateToken("base"); + headers["x-authenticated-user-token"] = await generateToken("ed"); } break; diff --git a/scripts/creation-portal-migration/api-list/program.js b/scripts/creation-portal-migration/api-list/program.js index 4890e83b..7673945d 100644 --- a/scripts/creation-portal-migration/api-list/program.js +++ b/scripts/creation-portal-migration/api-list/program.js @@ -77,8 +77,8 @@ const nominateProgram = async (program_id, author) => { }, ], content_types: [], - organisation_id: "937dd865-b256-4c1a-9830-a9b5b89f0913", - user_id: "bb551fff-121e-4a18-b969-984ac62bd572", + organisation_id: process.env.DEFAULT_PROGRAM_CREATOR_ORGANISATION_ID, + user_id: process.env.DEFAULT_USER_ID_TO_ADD_CONTRIBUTOR, }, }; const config = { diff --git a/scripts/creation-portal-migration/api-list/question.js b/scripts/creation-portal-migration/api-list/question.js index 7be4047c..d06e1289 100644 --- a/scripts/creation-portal-migration/api-list/question.js +++ b/scripts/creation-portal-migration/api-list/question.js @@ -86,7 +86,6 @@ const createQuestions = async (templateData, questionId) => { data: data, }; const res = await axios(config).catch((err) => { - console.log(`Error while creating the question for questionid: ${questionId} Error:`,err?.response?.data) logger.error(`Error while creating the question for questionid: ${questionId} Error: ${JSON.stringify(err.response.data)}`); }); diff --git a/scripts/creation-portal-migration/api-list/user.js b/scripts/creation-portal-migration/api-list/user.js index 90c604bb..7442ac26 100644 --- a/scripts/creation-portal-migration/api-list/user.js +++ b/scripts/creation-portal-migration/api-list/user.js @@ -6,25 +6,25 @@ const { getHeaders } = require("./headers"); const readUser = async (userId) => { const params = "organisations,roles,locations,declarations,externalIds"; const url = - CONFIG.HOST.base + + CONFIG.HOST.ed + CONFIG.APIS.read_user + userId + "?fields=" + params; const res = await axios - .get(url, await getHeaders(true, "base")) + .get(url, await getHeaders(true, "ed")) .catch((err) => { logger.error(`Error while reading User: ${JSON.stringify(err?.response?.data)}`) }); }; const searchUser = async (userId) => { - const url = CONFIG.HOST.base + CONFIG.APIS.search_user; + const url = CONFIG.HOST.ed + CONFIG.APIS.search_user; const config = { method: "post", url: url, - headers: await getHeaders(true, "base"), + headers: await getHeaders(true, "ed"), data: { request: { filters: { id: userId } }, }, diff --git a/scripts/creation-portal-migration/constant/config.js b/scripts/creation-portal-migration/constant/config.js index 336df739..24ebcb63 100644 --- a/scripts/creation-portal-migration/constant/config.js +++ b/scripts/creation-portal-migration/constant/config.js @@ -12,45 +12,45 @@ const CONFIG = { }, HOST: { - base: process.env.BASE_HOST, + ed: process.env.ED_BASE_URL, creation_portal: process.env.CREATION_PORTAL_URL, }, APIS: { - token: "auth/realms/sunbird/protocol/openid-connect/token", - read_user: "api/user/v5/read/", - search_user: "api/user/v3/search", - create_questionset: "api/questionset/v1/create", - update_hierarchy: "api/questionset/v1/hierarchy/update", - publish_questionset: "api/questionset/v1/publish", - read_questionset: "api/questionset/v1/hierarchy/", - create_question: "api/question/v1/create", - publish_question: "api/question/v1/publish", - create_program: "api/program/v1/create", - update_program: "api/program/v1/update", - add_program_nomination: "api/program/v1/nomination/add", - update_program_nomination: "api/program/v1/nomination/update", - publish_program: "api/program/v1/publish", + token: process.env.GEN_TOKEN || "auth/realms/sunbird/protocol/openid-connect/token", + read_user: process.env.READ_USER || "api/user/v5/read/", + search_user: process.env.SEARCH_USER || "api/user/v3/search", + create_questionset: process.env.CREATE_QUESTIONSET || "api/questionset/v1/create", + update_hierarchy: process.env.UPDATE_HIERARCHY || "api/questionset/v1/hierarchy/update", + publish_questionset: process.env.PUBLISH_QUESTIONSET || "api/questionset/v1/publish", + read_questionset: process.env.READ_QUESTIONSET || "api/questionset/v1/hierarchy/", + create_question: process.env.CREATE_QUESTION || "api/question/v1/create", + publish_question: process.env.PUBLISH_QUESTION || "api/question/v1/publish", + create_program: process.env.CREATE_PROGRAM || "api/program/v1/create", + update_program: process.env.UPDATE_PROGRAM || "api/program/v1/update", + add_program_nomination: process.env.ADD_PROGRAM_NOMINATION || "api/program/v1/nomination/add", + update_program_nomination: process.env.UPDATE_PROGRAM_NOMINATION || "api/program/v1/nomination/update", + publish_program: process.env.PUBLISH_PROGRAM || "api/program/v1/publish", }, KEYS: { - BASE: { + ED: { QUERY: { - username: process.env.BASE_USER, - password: process.env.BASE_PWD, - grant_type: process.env.BASE_GRANT, - client_id: process.env.BASE_CLIENT, - client_secret: process.env.BASE_CLIENT_SECRET, + username: process.env.MASTER_USER_EMAIL, + password: process.env.MASTER_USER_PWD, + grant_type: process.env.TOKEN_GEN_GRANT_TYPE, + client_id: process.env.TOKEN_GEN_CLIENT, + client_secret: process.env.TOKEN_GEN_CLIENT_SECRET, }, - AUTHORIZATION: process.env.BASE_AUTHORIZATION, + AUTHORIZATION: process.env.ED_AUTHORIZATION, }, CREATION_PORTAL: { QUERY: { - username: process.env.BASE_USER, - password: process.env.BASE_PWD, - grant_type: process.env.BASE_GRANT, - client_id: process.env.BASE_CLIENT, - client_secret: process.env.BASE_CLIENT_SECRET, + username: process.env.MASTER_USER_EMAIL, + password: process.env.MASTER_USER_PWD, + grant_type: process.env.TOKEN_GEN_GRANT_TYPE, + client_id: process.env.TOKEN_GEN_CLIENT, + client_secret: process.env.TOKEN_GEN_CLIENT_SECRET, }, - AUTHORIZATION: process.env.CREATION_PORTAL_AUTHORIZATION_KEY, + AUTHORIZATION: process.env.CREATION_PORTAL_AUTHORIZATION, }, }, }; diff --git a/scripts/creation-portal-migration/db/index.js b/scripts/creation-portal-migration/db/index.js index 2234edb9..b1c9d5bc 100644 --- a/scripts/creation-portal-migration/db/index.js +++ b/scripts/creation-portal-migration/db/index.js @@ -1,5 +1,6 @@ const { getDBInstance } = require("./dbConfig"); const { ObjectId } = require("mongodb"); +const logger = require("../logger"); const findAll = async (clName, query) => { try { @@ -9,7 +10,7 @@ const findAll = async (clName, query) => { .find({ ...query }) .toArray(); } catch (err) { - console.log("findAll Error: ", err); + logger.error(`findAll Error: , ${err}`); } }; @@ -24,7 +25,7 @@ const updateById = async (clName, id, query) => { { upsert: true } ); } catch (err) { - console.log("updateById = ", id, "Error: ", err); + logger.error(`"updateById = ", ${id}, "Error: ", ${err}`); } }; diff --git a/scripts/creation-portal-migration/server.js b/scripts/creation-portal-migration/index.js similarity index 91% rename from scripts/creation-portal-migration/server.js rename to scripts/creation-portal-migration/index.js index 1fd58fbd..520bf737 100644 --- a/scripts/creation-portal-migration/server.js +++ b/scripts/creation-portal-migration/index.js @@ -6,12 +6,8 @@ const { } = require("./template/generate/gQuestionSet.js"); const logger = require("./logger"); -var fs = require("fs"); const { CONFIG } = require("./constant/config"); - -fs.existsSync("logs") || fs.mkdirSync("logs"); - const migrateData = async (req, res) => { try { @@ -82,12 +78,8 @@ const migrateData = async (req, res) => { migratedCount ); - console.log(); - console.log("migratedCounttt", JSON.stringify(migratedCount)); - console.log(); - logger.info(`\n migratedCount ${JSON.stringify(migratedCount)}`); - process.exit(); + process.exit(); } catch (err) { logger.error(`Error while migrating : ${err}`) diff --git a/scripts/creation-portal-migration/logger.js b/scripts/creation-portal-migration/logger.js index 2c1794fc..fe9d25ab 100644 --- a/scripts/creation-portal-migration/logger.js +++ b/scripts/creation-portal-migration/logger.js @@ -1,5 +1,5 @@ var path = require("path"); - +var fs = require("fs"); var Logger = require("bunyan"); const date = @@ -9,6 +9,8 @@ const date = "-" + new Date().getFullYear(); +fs.existsSync("logs") || fs.mkdirSync("logs"); + var logger = new Logger({ name: "creation-portal-migration", streams: [ diff --git a/scripts/creation-portal-migration/template/config/questionSet.js b/scripts/creation-portal-migration/template/config/questionSet.js index 3218e134..4059b946 100644 --- a/scripts/creation-portal-migration/template/config/questionSet.js +++ b/scripts/creation-portal-migration/template/config/questionSet.js @@ -9,21 +9,20 @@ const questionSetTemplate = { keywords: "keywords", startDate: "startDate", endDate: "endDate", - createdBy: "4e397c42-495e-4fdb-8558-f98176230916", - organisationId: "937dd865-b256-4c1a-9830-a9b5b89f0913", - creator: "check1@yopmail.com", - createdFor: ["01309282781705830427"], - channel: "01309282781705830427", - programId: "546b3ca0-cb7c-11ec-a165-33909bc91f74", - author: "check1@yopmail.com", - framework: "nit_tpd", + createdBy: process.env.DEFAULT_CONTRIBUTOR_USER_ID, + organisationId: process.env.DEFAULT_PROGRAM_CREATOR_ORGANISATION_ID, + creator: process.env.DEFAULT_CONTRIBUTOR_USER_NAME, + createdFor: [process.env.DEFAULT_CONTRIBUTOR_USER_CHANNEL_ID], + channel: process.env.DEFAULT_CONTRIBUTOR_USER_CHANNEL_ID, + programId: "", + author: process.env.DEFAULT_CONTRIBUTOR_USER_NAME, + framework: process.env.DEFAULT_FRAMEWORK_ID, }; const questionSetTemplateStatic = [ "mimeType", "createdBy", "creator", - "programId", "author", "organisationId", "framework", @@ -34,21 +33,21 @@ const questionSetTemplateStatic = [ const program = { name: `{solutionName} sourcing project`, description: `{solutionName} sourcing project description`, - nomination_enddate: "2022-05-26", - shortlisting_enddate: "2022-05-26", - enddate: "2022-05-26", - content_submission_enddate: "2022-05-26", + nomination_enddate: "", + shortlisting_enddate: "", + enddate: "", + content_submission_enddate: "", rewards: null, content_types: [], target_collection_category: [], - sourcing_org_name: "NIT", + sourcing_org_name: "", type: "public", target_type: "searchCriteria", - rootorg_id: "01285019302823526477", - createdby: "5a587cc1-e018-4859-a0a8-e842650b9d64", - createdOn: "2022-05-26", - startdate: "2022-05-26", - slug: "sunbird", + rootorg_id: process.env.DEFAULT_USER_CHANNEL_ID_TO_CREATE_PROGRAM, + createdby: process.env.DEFAULT_USER_ID_TO_CREATE_PROGRAM, + createdOn: "", + startdate: "", + slug: process.env.DEFAULT_SLUG, status: "Draft", program_id: "", config: { diff --git a/scripts/creation-portal-migration/template/generate/gProgram.js b/scripts/creation-portal-migration/template/generate/gProgram.js index 91e33393..776f135c 100644 --- a/scripts/creation-portal-migration/template/generate/gProgram.js +++ b/scripts/creation-portal-migration/template/generate/gProgram.js @@ -24,9 +24,9 @@ const createProgramTemplate = async (solution, program_id, migratedCount) => { const userId = userData?.length > 0 ? solution.author - : "5a587cc1-e018-4859-a0a8-e842650b9d64"; + : process.env.DEFAULT_USER_ID_TO_CREATE_PROGRAM; const rootOrgId = - userData?.length > 0 ? userData[0]?.rootOrgId : "01309282781705830427"; + userData?.length > 0 ? userData[0]?.rootOrgId : process.env.DEFAULT_CONTRIBUTOR_USER_CHANNEL_ID; const template = { name: `Migrated ${solution?.name} sourcing project`, @@ -40,12 +40,12 @@ const createProgramTemplate = async (solution, program_id, migratedCount) => { target_type: "searchCriteria", content_types: [], target_collection_category: [], - sourcing_org_name: "NIT", + sourcing_org_name: process.env.DEFAULT_USER_SOURCING_ORG_NAME_TO_CREATE_PROGRAM, rootorg_id: rootOrgId, createdby: userId, createdOn: `${getDate(0)}`, startdate: `${getDate(1)}`, - slug: "sunbird", + slug: process.env.DEFAULT_SLUG, status: "Draft", program_id: "", rolemapping: [], @@ -170,9 +170,9 @@ const createProgramTemplate = async (solution, program_id, migratedCount) => { if (!solution.isContributorAdded) { const add_contri = { program_id: programId, - user_id: "bb551fff-121e-4a18-b969-984ac62bd572", + user_id: process.env.DEFAULT_USER_ID_TO_ADD_CONTRIBUTOR, rolemapping: { - CONTRIBUTOR: ["4e397c42-495e-4fdb-8558-f98176230916"], + CONTRIBUTOR: [process.env.DEFAULT_CONTRIBUTOR_USER_ID], }, }; @@ -206,7 +206,7 @@ const createProgramTemplate = async (solution, program_id, migratedCount) => { if (!solution.isContributorAccepted) { const accept_contri = { program_id: programId, - user_id: "bb551fff-121e-4a18-b969-984ac62bd572", + user_id: process.env.DEFAULT_USER_ID_TO_ADD_CONTRIBUTOR, status: "Approved", updatedby: userId, }; @@ -294,12 +294,12 @@ const updateProgramTemplate = async (program_id, solution) => { defaultTab: 2, }, ], - framework: ["nit_tpd"], + framework: [process.env.DEFAULT_FRAMEWORK_ID], frameworkObj: { - code: "nit_tpd", - name: "nit_tpd", - type: "TPD", - identifier: "nit_tpd", + code: process.env.DEFAULT_FRAMEWORK_ID, + name: process.env.DEFAULT_FRAMEWORK_ID, + type: process.env.DEFAULT_FRAMEWORK_TYPE, + identifier: process.env.DEFAULT_FRAMEWORK, }, sharedContext: [], }, diff --git a/scripts/creation-portal-migration/template/generate/gQuestionSet.js b/scripts/creation-portal-migration/template/generate/gQuestionSet.js index fd26c944..1353a028 100644 --- a/scripts/creation-portal-migration/template/generate/gQuestionSet.js +++ b/scripts/creation-portal-migration/template/generate/gQuestionSet.js @@ -15,23 +15,11 @@ const getQuestionSetTemplates = async (solutions, migratedCount) => { const data = Promise.all( solutions.map(async (solution) => { let programId = solution.sourcingProgramId; - console.log(); - console.log( - "-----------------------sourcingProgramId----------------------", - programId - ); - console.log(); programId = await createProgramTemplate( solution, programId, migratedCount ); - console.log(); - console.log( - "-----------------------------program-------------------------------------" - ); - console.log(); - console.log("ProgramId", programId); logger.debug( `-----------------------sourcingProgramId---------------------- ${programId}` @@ -76,8 +64,6 @@ const migrateQuestionset = async (solution, programId, migratedCount) => { ${questionSetMigratedId}` ); - console.log(`migrateQuestionset: questionSetMigratedId: - ${questionSetMigratedId}`); if (!questionSetMigratedId) { return; @@ -90,8 +76,7 @@ const migrateQuestionset = async (solution, programId, migratedCount) => { `migrateQuestionset: Error while updating solution referenceQuestionSetId: ${err}` ); - console.log(`migrateQuestionset: Error while updating question: - ${err}`); + }); migratedCount.success.questionSet.current.migrated++; @@ -117,9 +102,7 @@ const migrateQuestionset = async (solution, programId, migratedCount) => { data.hierarchy.criterias.push(cri); } - console.log(); - console.log("migrateQuestionset", JSON.stringify(data.hierarchy)); - console.log(); + await updateHierarchyTemplate( data.hierarchy, solution, @@ -157,13 +140,6 @@ const migrateCriteriaQuestions = async ( hierarchy.criterias[i] = getCriteriaData(criteria, solution?.type); - console.log(); - console.log( - "--------------------criteria----------------------", - criteria?.name - ); - console.log(); - logger.info( `migrateCriteriaQuestions: --------------------criteria---------------------- ${criteria?.name}` ); @@ -196,9 +172,6 @@ const migrateQuestions = async ( let matrixQuestions = {}; let nonMatrixQuestions = []; - console.log(); - console.log("migrateQuestions", questions.length); - console.log(); logger.info( `migrateQuestions: criteria:${criteriaId} questions: ${questions.length} ` @@ -207,15 +180,6 @@ const migrateQuestions = async ( for (let i = 0; i < questions.length; i++) { const question = questions[i]; - console.log(); - console.log( - "migrateQuestions question responsetype", - question?.responseType, - "qid", - question._id, - - ); - console.log(); logger.info( `migrateQuestions: criteria:${criteriaId} question: ${question?._id} question responseType: ${question?.responseType} ` @@ -238,18 +202,7 @@ const migrateQuestions = async ( matrixHierarchy = data.matrixHierarchy; questions = data.questions; - console.log(); - console.log("migrateQuestions createSection", matrixHierarchy); - console.log(); } else { - console.log(); - console.log( - "==========migrateQuestions getNonMatrixQuestions============", - question?.responseType, - question?._id - ); - console.log(); - const data = await getNonMatrixQuestions( question, questions, @@ -263,13 +216,6 @@ const migrateQuestions = async ( criteriaId ); - console.log(); - console.log( - "==========migrateQuestions getNonMatrixQuestions end ============", - matrixHierarchy - ); - console.log(); - hierarchy = data.hierarchy; matrixHierarchy = data.matrixHierarchy; matrixQuestions = data.matrixQuestions; diff --git a/scripts/creation-portal-migration/template/helpers/hierarchyHelper.js b/scripts/creation-portal-migration/template/helpers/hierarchyHelper.js index 063aa2b4..de00bb95 100644 --- a/scripts/creation-portal-migration/template/helpers/hierarchyHelper.js +++ b/scripts/creation-portal-migration/template/helpers/hierarchyHelper.js @@ -16,16 +16,16 @@ const { CONFIG } = require("./../../constant/config"); const { updateById } = require("../../db"); const logger = require("../../logger"); -const updateHierarchyChildren = (hierarchy, referenceQuestionSetId, index) => { +const updateHierarchyChildren = (hierarchy, referenceQuestionId, index) => { - logger.debug(`updateHierarchyChildren: referenceQuestionSetId = ${referenceQuestionSetId}`) + logger.debug(`updateHierarchyChildren: referenceQuestionId = ${referenceQuestionId}`) if ( - referenceQuestionSetId && - !hierarchy.criterias[index].questions.includes(referenceQuestionSetId) + referenceQuestionId && + !hierarchy.criterias[index].questions.includes(referenceQuestionId) ) { - hierarchy.criterias[index].questions.push(referenceQuestionSetId); + hierarchy.criterias[index].questions.push(referenceQuestionId); } return hierarchy; }; @@ -113,8 +113,6 @@ const updateHierarchyTemplate = async ( }; } - console.log("updateHierarchydata", JSON.stringify(updateHierarchyData)); - console.log(); logger.info( `updateHierarchyTemplate: Hierarchydata = ${JSON.stringify( @@ -162,7 +160,6 @@ const updateHierarchyTemplate = async ( if (!hierarchy.isBranchingUpdated) { const branchinghierarchy = await branchingQuestionSetHierarchy(hierarchy); - console.log("branchinghierarchy", JSON.stringify(branchinghierarchy)); const result = await updateQuestionSetHierarchy(branchinghierarchy).catch( (err) => { diff --git a/scripts/creation-portal-migration/template/helpers/questionsetHelper.js b/scripts/creation-portal-migration/template/helpers/questionsetHelper.js index c226f7fa..c7b32001 100644 --- a/scripts/creation-portal-migration/template/helpers/questionsetHelper.js +++ b/scripts/creation-portal-migration/template/helpers/questionsetHelper.js @@ -43,19 +43,14 @@ const setQuestionSetTemplate = (solution, programId) => { const createQuestionTemplate = async (question, migratedCount) => { - - console.log(); - console.log("createQuestionTemplate", question); - console.log(); - const type = question?.responseType; - let referenceQuestionSetId = question?.referenceQuestionSetId; + let referenceQuestionId = question?.referenceQuestionId; let query = {}; let questionToMigrate = {}; let published = question?.isPublished; - if (type && !referenceQuestionSetId) { + if (type && !referenceQuestionId) { if (type.toLowerCase() === "date") { questionToMigrate = getDateTemplate(question); } @@ -73,34 +68,26 @@ const createQuestionTemplate = async (question, migratedCount) => { } if (!isEmpty(questionToMigrate)) { - console.log(); - console.log( - "questionToMigratequestionToMigrate", - JSON.stringify(questionToMigrate) - ); - console.log(); - console.log("db Question", JSON.stringify(question)); - console.log(); - - referenceQuestionSetId = await createQuestions(questionToMigrate, question._id); - question.referenceQuestionSetId = referenceQuestionSetId; + + referenceQuestionId = await createQuestions(questionToMigrate, question._id); + question.referenceQuestionId = referenceQuestionId; } } - if (referenceQuestionSetId && !published) { - const res = await publishQuestion(referenceQuestionSetId).catch((err) => { - if (!migratedCount.failed.question.ids.includes(referenceQuestionSetId)) { + if (referenceQuestionId && !published) { + const res = await publishQuestion(referenceQuestionId).catch((err) => { + if (!migratedCount.failed.question.ids.includes(referenceQuestionId)) { migratedCount.failed.question.count++; - migratedCount.failed.question.ids.push(referenceQuestionSetId); + migratedCount.failed.question.ids.push(referenceQuestionId); } - logger.error(`Error while publishing the question for referenceQuestionSetId: ${referenceQuestionSetId} Error: + logger.error(`Error while publishing the question for referenceQuestionId: ${referenceQuestionId} Error: ${JSON.stringify(err.response.data)}`); }); logger.info( - `createQuestion Template publish response: ${res} , "referenceQuestionSetId" ${referenceQuestionSetId} questionId, ${question?._id}` + `createQuestion Template publish response: ${res} , "referenceQuestionId" ${referenceQuestionId} questionId, ${question?._id}` ); @@ -108,16 +95,16 @@ const createQuestionTemplate = async (question, migratedCount) => { if (res) { question.isPublished = true; published = true; - logger.info(`createQuestion Template published: ${referenceQuestionSetId}`); + logger.info(`createQuestion Template published: ${referenceQuestionId}`); } } - if (referenceQuestionSetId) { - question.referenceQuestionSetId = referenceQuestionSetId; + if (referenceQuestionId) { + question.referenceQuestionId = referenceQuestionId; query = { - referenceQuestionSetId, + referenceQuestionId, published: published, }; } else { @@ -126,7 +113,6 @@ const createQuestionTemplate = async (question, migratedCount) => { published } } - if (!isEmpty(query) && question) { await updateById(CONFIG.DB.TABLES.questions, question._id, { diff --git a/scripts/creation-portal-migration/template/migrate/common.js b/scripts/creation-portal-migration/template/migrate/common.js index 9fde10ca..8510c16e 100644 --- a/scripts/creation-portal-migration/template/migrate/common.js +++ b/scripts/creation-portal-migration/template/migrate/common.js @@ -122,7 +122,7 @@ const getQuestion = async (questions, questions2, id, migratedCount) => { question = matched; } - if (!isEmpty(question) && (!question?.referenceQuestionSetId || !question?.isPublished)) { + if (!isEmpty(question) && (!question?.referenceQuestionId || !question?.isPublished)) { question = await createQuestionTemplate(question, migratedCount); } @@ -170,38 +170,38 @@ const updateHierarchyBranching = ( pQuestion, child ) => { - const referenceQuestionSetId = child?.referenceQuestionSetId; + const referenceQuestionId = child?.referenceQuestionId; const visible = child?.visibleIf ? child?.visibleIf[0] : {}; logger.debug( - `updateHierarchyBranching: referenceQuestionSetId = ${referenceQuestionSetId}; parentId = ${parentId}; visible: ${visible}` + `updateHierarchyBranching: referenceQuestionId = ${referenceQuestionId}; parentId = ${parentId}; visible: ${visible}` ); if (!isEmpty(visible)) { - if (hasProperty(branching, index, parentId) && referenceQuestionSetId) { + if (hasProperty(branching, index, parentId) && referenceQuestionId) { if ( !branching.criterias[index].branchingLogic[parentId].target.includes( - referenceQuestionSetId + referenceQuestionId ) ) { branching.criterias[index].branchingLogic[parentId].target.push( - referenceQuestionSetId + referenceQuestionId ); } - branching.criterias[index].branchingLogic[referenceQuestionSetId] = { + branching.criterias[index].branchingLogic[referenceQuestionId] = { target: [], preCondition: getPrecondition(visible, parentId, pQuestion), source: [parentId], }; - } else if (referenceQuestionSetId) { + } else if (referenceQuestionId) { branching.criterias[index].branchingLogic[parentId] = { - target: [referenceQuestionSetId], + target: [referenceQuestionId], preCondition: {}, source: [], }; - branching.criterias[index].branchingLogic[referenceQuestionSetId] = { + branching.criterias[index].branchingLogic[referenceQuestionId] = { target: [], preCondition: getPrecondition(visible, parentId, pQuestion), source: [parentId], diff --git a/scripts/creation-portal-migration/template/migrate/matrix.js b/scripts/creation-portal-migration/template/migrate/matrix.js index bbac356d..2cc7a21c 100644 --- a/scripts/creation-portal-migration/template/migrate/matrix.js +++ b/scripts/creation-portal-migration/template/migrate/matrix.js @@ -144,7 +144,6 @@ const getMatrixQuestions = async ( ); if (!isChildrenPresent(question) && !isVisibleIfPresent(question)) { - console.log("getMatrixQuestions noChildrenAndnoVisibleIf ", question._id); const data = noChildrenAndnoVisibleIf( matrixQuestions, criteriaId, @@ -198,9 +197,7 @@ const noChildrenAndnoVisibleIf = ( matrixHierarchy, questions ) => { - console.log(); - console.log("noChildrenAndnoVisibleIf"); - console.log(); + logger.debug( `noChildrenAndnoVisibleIf: criteriaId = ${criteriaId}; matrixId = ${matrixId}; question: ${question?._id}` @@ -287,14 +284,14 @@ const childrenAndnoVisibleIf = async ( matrixHierarchy = updateMatrixHierarchy( matrixHierarchy, matrixId, - child?.referenceQuestionSetId, + child?.referenceQuestionId, criteriaId ); matrixHierarchy = updateMatrixHierarchyBranching( matrixHierarchy, criteriaId, matrixId, - question?.referenceQuestionSetId, + question?.referenceQuestionId, question, child ); @@ -350,14 +347,14 @@ const noChildrenAndVisibleIf = async ( matrixHierarchy = updateMatrixHierarchy( matrixHierarchy, matrixId, - pQuestion?.referenceQuestionSetId, + pQuestion?.referenceQuestionId, criteriaId ); matrixHierarchy = updateMatrixHierarchyBranching( matrixHierarchy, criteriaId, matrixId, - pQuestion?.referenceQuestionSetId, + pQuestion?.referenceQuestionId, pQuestion, question ); @@ -384,19 +381,19 @@ const noChildrenAndVisibleIf = async ( const updateMatrixHierarchy = ( matrixHierarchy, matrixId, - referenceQuestionSetId, + referenceQuestionId, criteriaId ) => { logger.debug( - `updateMatrixHierarchy: matrixId = ${matrixId}; referenceQuestionSetId = ${referenceQuestionSetId}` + `updateMatrixHierarchy: matrixId = ${matrixId}; referenceQuestionId = ${referenceQuestionId}` ); const criterias = matrixHierarchy.criterias || []; for (let i = 0; i < criterias.length; i++) { const criteria = criterias[i]; if (criteria?._id.toString() === matrixId) { - if (!criteria.questions.includes(referenceQuestionSetId)) { - criteria.questions.push(referenceQuestionSetId); + if (!criteria.questions.includes(referenceQuestionId)) { + criteria.questions.push(referenceQuestionId); } } criterias[i] = criteria; @@ -419,7 +416,7 @@ const updateMatrixHierarchyQuestions = ( questions ) => { logger.debug( - `updateMatrixHierarchyQuestions: criteriaId = ${criteriaId}; matrixId = ${matrixId}; question: ${question?._id}: referenceQuestionSetId: ${question?.referenceQuestionSetId}` + `updateMatrixHierarchyQuestions: criteriaId = ${criteriaId}; matrixId = ${matrixId}; question: ${question?._id}: referenceQuestionId: ${question?.referenceQuestionId}` ); const matched = isQuestionMatched( @@ -438,13 +435,11 @@ const updateMatrixHierarchyQuestions = ( matrixHierarchy = updateMatrixHierarchy( matrixHierarchy, matrixId, - question?.referenceQuestionSetId, + question?.referenceQuestionId, criteriaId ); } - console.log("updateMatrixHierarchyQuestions ", matrixHierarchy); - return { matrixHierarchy, matrixQuestions, questions }; }; diff --git a/scripts/creation-portal-migration/template/migrate/nonmatrix.js b/scripts/creation-portal-migration/template/migrate/nonmatrix.js index c858298e..1e150c20 100644 --- a/scripts/creation-portal-migration/template/migrate/nonmatrix.js +++ b/scripts/creation-portal-migration/template/migrate/nonmatrix.js @@ -42,7 +42,7 @@ const updateNonMatrixHierarchyChildren = ( if (matched === false) { hierarchy = updateHierarchyChildren( hierarchy, - question?.referenceQuestionSetId, + question?.referenceQuestionId, index ); nonMatrixQuestions.push(question); @@ -80,13 +80,8 @@ const getNonMatrixQuestions = async ( migratedCount ); - console.log("getNonMatrixQuestions", question); - - if (!isChildrenPresent(question) && !isVisibleIfPresent(question)) { - console.log("getNonMatrixQuestions nonMatrix NoChildrenAndNoVisibleIf", ) - const data = nonMatrixNoChildrenAndNoVisibleIf( question, nonMatrixQuestions, @@ -104,12 +99,8 @@ const getNonMatrixQuestions = async ( matrixQuestions = data.matrixQuestions; matrixHierarchy = data.matrixHierarchy; - console.log("getNonMatrixQuestions nonMatrix NoChildrenAndNoVisibleIf", ) - } else if (isChildrenPresent(question) && !isVisibleIfPresent(question)) { - console.log("getNonMatrixQuestions nonMatrix ChildrenAndNoVisibleIf", ) - const data = await nonMatrixChildrenAndNoVisibleIf( question, questions, @@ -130,9 +121,6 @@ const getNonMatrixQuestions = async ( } else if (!isChildrenPresent(question) && isVisibleIfPresent(question)) { - console.log("getNonMatrixQuestions nonMatrix NoChildrenAndVisibleIf", ) - - const data = await nonMatrixNoChildrenAndVisibleIf( question, questions, @@ -248,7 +236,7 @@ const nonMatrixChildrenAndNoVisibleIf = async ( hierarchy = updateHierarchyBranching( hierarchy, index, - question?.referenceQuestionSetId, + question?.referenceQuestionId, question, child ); @@ -320,7 +308,7 @@ const nonMatrixNoChildrenAndVisibleIf = async ( hierarchy = updateHierarchyBranching( hierarchy, index, - pQuestion?.referenceQuestionSetId, + pQuestion?.referenceQuestionId, pQuestion, question ); From d8f2964e6f4b49884c84661397fa99c11fa8c997 Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Fri, 12 Aug 2022 16:26:18 +0530 Subject: [PATCH 30/36] added api's in env --- .env.sample | 49 +++++++++++++------ .../constant/config.js | 8 +-- 2 files changed, 37 insertions(+), 20 deletions(-) diff --git a/.env.sample b/.env.sample index 50cd8ea9..5333a699 100644 --- a/.env.sample +++ b/.env.sample @@ -30,6 +30,14 @@ DISABLE_LEARNER_SERVICE_ON_OFF = "ON" +# Redis configuration + +REDIS_URL = redis://localhost:6379 // Redis connection url +REDIS_USERNAME = "redis" // Redis username +REDIS_PWD = "1234" // Redis password +REDIS_DB = 1 // Redis db index +REDIS_TTL = 86400 // Redis ttl (for how many seconds a given key value will continue to be part of the dataset) + # creation portal migration Script MASTER_USER_EMAIL = "n11@yopmail.com" // master user name of the sunbird enviornment to generate token @@ -38,8 +46,8 @@ TOKEN_GEN_GRANT_TYPE = "password-grant" TOKEN_GEN_CLIENT = "client-id" // client to generate token for the sunbird enviornment TOKEN_GEN_CLIENT_SECRET = "client-secret-key" // client secret to generate token for the sunbird enviornment -ED_BASE_URL = "https://dev.sunbirded.org/" // base host url of the sunbird enviornment -ED_AUTHORIZATION = "Bearer + 'token'" // base bearer token with all the permissions to run the script of the sunbird enviornment +ED_BASE_URL = "https://dev.sunbirded.org/" // base host url of the sunbird enviornment +ED_AUTHORIZATION = "Bearer + 'token'" // base bearer token with all the permissions to run the script of the sunbird enviornment CREATION_PORTAL_URL = "https://dock.sunbirded.org/" // creation portal Url @@ -51,20 +59,20 @@ CREATION_PORTAL_AUTHORIZATION= "Bearer + 'token'" / # API'S required for migration - GEN_TOKEN: "auth/realms/sunbird/protocol/openid-connect/token", // To generate token - READ_USER: "api/user/v5/read/", // To read user data, Authorization: ED_AUTHORIZATION - SEARCH_USER: "api/user/v3/search", // To search user data, Authorization: ED_AUTHORIZATION - CREATE_QUESTIONSET: "api/questionset/v1/create", // To create questionset, Authorization: CREATION_PORTAL_AUTHORIZATION - UPDATE_HIERARCHY: "api/questionset/v1/hierarchy/update", // To update questionset hierarchy, Authorization: CREATION_PORTAL_AUTHORIZATION - PUBLISH_QUESTIONSET: "api/questionset/v1/publish", // To publish questionset, Authorization: CREATION_PORTAL_AUTHORIZATION - READ_QUESTIONSET: "api/questionset/v1/hierarchy/", // To read questionset, Authorization: CREATION_PORTAL_AUTHORIZATION - CREATE_QUESTION: "api/question/v1/create", // To create question, Authorization: CREATION_PORTAL_AUTHORIZATION - PUBLISH_QUESTION: "api/question/v1/publish", // To publish question, Authorization: CREATION_PORTAL_AUTHORIZATION - CREATE_PROGRAM: "api/program/v1/create", // To create program, Authorization: CREATION_PORTAL_AUTHORIZATION - UPDATE_PROGRAM: "api/program/v1/update", // To update program, Authorization: CREATION_PORTAL_AUTHORIZATION - ADD_PROGRAM_NOMINATION: "api/program/v1/nomination/add", // To add nomination to program, Authorization: CREATION_PORTAL_AUTHORIZATION - UPDATE_PROGRAM_NOMINATION: "api/program/v1/nomination/update", // To update program nomination, Authorization: CREATION_PORTAL_AUTHORIZATION - PUBLISH_PROGRAM: "api/program/v1/publish", // To publish program, Authorization: CREATION_PORTAL_AUTHORIZATION + GEN_TOKEN: "auth/realms/sunbird/protocol/openid-connect/token" // To generate token + READ_USER: "api/user/v5/read/" // To read user data Authorization: ED_AUTHORIZATION + SEARCH_USER: "api/user/v3/search" // To search user data, Authorization: ED_AUTHORIZATION + CREATE_QUESTION_SET: "api/questionset/v1/create" // To create questionset, Authorization: CREATION_PORTAL_AUTHORIZATION + UPDATE_QUESTION_SET_HIERARCHY: "api/questionset/v1/hierarchy/update" // To update questionset hierarchy, Authorization: CREATION_PORTAL_AUTHORIZATION + PUBLISH_QUESTION_SET: "api/questionset/v1/publish" // To publish questionset, Authorization: CREATION_PORTAL_AUTHORIZATION + READ_QUESTION_SET: "questionset/v1/hierarchy" // To read questionset, Authorization: CREATION_PORTAL_AUTHORIZATION + CREATE_QUESTION: "api/question/v1/create" // To create question, Authorization: CREATION_PORTAL_AUTHORIZATION + PUBLISH_QUESTION: "api/question/v1/publish" // To publish question, Authorization: CREATION_PORTAL_AUTHORIZATION + CREATE_PROGRAM: "api/program/v1/create" // To create program, Authorization: CREATION_PORTAL_AUTHORIZATION + UPDATE_PROGRAM: "api/program/v1/update" // To update program, Authorization: CREATION_PORTAL_AUTHORIZATION + ADD_PROGRAM_NOMINATION: "api/program/v1/nomination/add" // To add nomination to program, Authorization: CREATION_PORTAL_AUTHORIZATION + UPDATE_PROGRAM_NOMINATION: "api/program/v1/nomination/update" // To update program nomination, Authorization: CREATION_PORTAL_AUTHORIZATION + PUBLISH_PROGRAM: "api/program/v1/publish" // To publish program, Authorization: CREATION_PORTAL_AUTHORIZATION # Default users to create program and question @@ -85,3 +93,12 @@ CREATION_PORTAL_AUTHORIZATION= "Bearer + 'token'" / DEFAULT_CONTRIBUTOR_USER_NAME = 'check1@yopmail.com' // default user name to contribute content to the program DEFAULT_CONTRIBUTOR_USER_CHANNEL_ID = '01309282781705830427' // default user user channel to contribute content to the program + +# API'S required for Transformation + + COPY_QUESTION_SET: "/api/questionset/v1/copy" // To copy questionset + UPDATE_QUESTION_SET:"/api/questionset/v1/update" // To update questionset + READ_QUESTION: "/api/question/v1/read" // To read questionset + UPDATE_QUESTION_SET_HIERARCHY // To update questionset hierarchy + PUBLISH_QUESTION_SET // To publish questionset + READ_QUESTION_SET // To read questionset \ No newline at end of file diff --git a/scripts/creation-portal-migration/constant/config.js b/scripts/creation-portal-migration/constant/config.js index 24ebcb63..61e4dcd2 100644 --- a/scripts/creation-portal-migration/constant/config.js +++ b/scripts/creation-portal-migration/constant/config.js @@ -19,10 +19,10 @@ const CONFIG = { token: process.env.GEN_TOKEN || "auth/realms/sunbird/protocol/openid-connect/token", read_user: process.env.READ_USER || "api/user/v5/read/", search_user: process.env.SEARCH_USER || "api/user/v3/search", - create_questionset: process.env.CREATE_QUESTIONSET || "api/questionset/v1/create", - update_hierarchy: process.env.UPDATE_HIERARCHY || "api/questionset/v1/hierarchy/update", - publish_questionset: process.env.PUBLISH_QUESTIONSET || "api/questionset/v1/publish", - read_questionset: process.env.READ_QUESTIONSET || "api/questionset/v1/hierarchy/", + create_questionset: process.env.CREATE_QUESTION_SET || "api/questionset/v1/create", + update_hierarchy: process.env.UPDATE_QUESTION_SET_HIERARCHY || "api/questionset/v1/hierarchy/update", + publish_questionset: process.env.PUBLISH_QUESTION_SET || "api/questionset/v1/publish", + read_questionset: "api/" + process.env.READ_QUESTION_SET || "api/questionset/v1/hierarchy/", create_question: process.env.CREATE_QUESTION || "api/question/v1/create", publish_question: process.env.PUBLISH_QUESTION || "api/question/v1/publish", create_program: process.env.CREATE_PROGRAM || "api/program/v1/create", From 9f660e3a8a6e6d91113e4d4c44fb73f3276aa2e6 Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Fri, 12 Aug 2022 16:52:57 +0530 Subject: [PATCH 31/36] udpated questionsetId to questionid --- generics/messageConstants/endpoints.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/generics/messageConstants/endpoints.js b/generics/messageConstants/endpoints.js index 094d6823..fc7bdb00 100644 --- a/generics/messageConstants/endpoints.js +++ b/generics/messageConstants/endpoints.js @@ -18,7 +18,5 @@ module.exports = { GET_PROJECT_TEMPLATE_LISTS : "/v1/project/templates/listByIds", DOWNLOADABLE_FILE_URL: "/v1/cloud-services/files/getDownloadableUrl", USER_READ : "/user/v1/read", - USER_READ_V5 : "/v5/user/read", - QUESTIONSET_READ: "api/questionset/v1/hierarchy", - QUESTION_READ: "api/question/v1/read" + USER_READ_V5 : "/v5/user/read" } \ No newline at end of file From eb72ec9f39995d514cf2d63f4b97e88ab178b237 Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Fri, 12 Aug 2022 16:54:10 +0530 Subject: [PATCH 32/36] udpated questionsetId to questionid --- templates/questions.js | 205 --------------------- templates/questionset.js | 381 --------------------------------------- 2 files changed, 586 deletions(-) delete mode 100644 templates/questions.js delete mode 100644 templates/questionset.js diff --git a/templates/questions.js b/templates/questions.js deleted file mode 100644 index 111b2948..00000000 --- a/templates/questions.js +++ /dev/null @@ -1,205 +0,0 @@ - -const baseAssessment = { - _id: "", - question: "", - isCompleted: "", - showRemarks: "", - options: "", - sliderOptions: "", - children: "", - questionGroup: "", - fileName: "", - instanceQuestions: "", - isAGeneralQuestion: "", - autoCapture: "", - allowAudioRecording: "", - prefillFromEntityProfile: "", - entityFieldName: "", - isEditable: "", - showQuestionInPreview: "", - deleted: "", - remarks: "", - value: "", - usedForScoring: "", - questionType: "", - canBeNotApplicable: "", - visibleIf: "", - validation: "", - file: "", - externalId: "", - tip: "", - hint: "", - responseType: "pageQuestions", - modeOfCollection: "", - accessibility: "", - rubricLevel: "", - sectionHeader: "", - page: "p1", - questionNumber: "", - updatedAt: "", - createdAt: "", - __v: "", - createdFromQuestionId: "", - evidenceMethod: "", - payload: "", - startTime: "", - endTime: "", - gpsLocation: "", - dateFormat: "", - instanceIdentifier: "", - pageQuestions: [], - -}; - -const baseQuestion = { - _id: "identifier", - question: 'body', - isCompleted: false, - showRemarks: "showRemarks", - options: "interactions.response1.options", - sliderOptions: "sliderOptions", - children: "children", - questionGroup: ["A1"], - fileName: "fileName", - instanceQuestions: "instanceQuestions", - isAGeneralQuestion: false, - autoCapture: "autocapture", - allowAudioRecording: false, - prefillFromEntityProfile: false, - entityFieldName: "", - isEditable: true, - showQuestionInPreview: false, - deleted: false, - remarks: "", - value: "", - usedForScoring: "", - questionType: "auto", - canBeNotApplicable: false, - visibleIf: "visibleIf", - validation: { - required: "interactions.validation.required", - }, - file: { - required: true, - type: "evidence.mimeType", - minCount: 0, - maxCount: 10, - caption: "FALSE", - }, - externalId: "code", - tip: "", - hint: "hints", - responseType: "primaryCategory", - modeOfCollection: "onfield", - accessibility: "No", - rubricLevel: "", - sectionHeader: "", - page: "", - questionNumber: "", - updatedAt: "lastUpdatedOn", - createdAt: "createdOn", - __v: 0, - createdFromQuestionId: "", - evidenceMethod: "OB", - payload: { - criteriaId: "sectionId", - responseType: "primaryCategory", - evidenceMethod: "OB", - rubricLevel: "", - }, - startTime: "", - endTime: "", - gpsLocation: "", - dateFormat: "", - instanceIdentifier: "", -}; - -const questionType = { - text: { - ...baseQuestion, - responseType: "text", - }, - number: { - ...baseQuestion, - responseType: "number", - validation: { - ...baseQuestion.validation, - IsNumber: "interactions.response1.type.number", - }, - }, - slider: { - ...baseQuestion, - responseType: "slider", - validation: { - ...baseQuestion.validation, - max: "interactions.response1.validation.range.max", - min: "interactions.response1.validation.range.min", - }, - }, - date: { - ...baseQuestion, - responseType: "date", - validation: { - ...baseQuestion.validation, - max: "interactions.validation.max", - min: "interactions.validation.min", - }, - dateFormat: "interactions.response1.validation.pattern", - }, - multiselect: { - ...baseQuestion, - responseType: "multiselect", - }, - radio: { - ...baseQuestion, - responseType: "radio", - }, - - defaultFields: [ - "__v", - "responseType", - "isCompleted", - "questionGroup", - "isAGeneralQuestion", - "allowAudioRecording", - "prefillFromEntityProfile", - "entityFieldName", - "isEditable", - "showQuestionInPreview", - "deleted", - "remarks", - "value", - "usedForScoring", - "questionType", - "canBeNotApplicable", - "tip", - "modeOfCollection", - "accessibility", - "rubricLevel", - "sectionHeader", - "_v", - "createdFromQuestionId", - "evidenceMethod", - "startTime", - "endTime", - "gpsLocation", - ], - - arrayFields: ["sliderOptions", "children", "fileName", "instanceQuestions"], - - matrix: { - ...baseQuestion, - instanceIdentifier: "instances", - validation: { - required: true - }, - responseType: 'matrix', - } - -}; - -module.exports = { - baseAssessment, - baseQuestion, - questionType, -}; diff --git a/templates/questionset.js b/templates/questionset.js deleted file mode 100644 index c3a58e33..00000000 --- a/templates/questionset.js +++ /dev/null @@ -1,381 +0,0 @@ - -const solutionTemplate = { - _id: "", - externalId: "code", - name: "name", - description: "description", - registry: [], - captureGpsLocationAtQuestionLevel: false, - enableQuestionReadOut: false, - scoringSystem: null, - isRubricDriven: false, -}; - -defaultSolutionTemplateFields = [ - "registry", - "captureGpsLocationAtQuestionLevel", - "enableQuestionReadOut", - "scoringSystem", - "isRubricDriven", -]; -module.exports = { - solutionTemplate, - defaultSolutionTemplateFields, -}; - -const assessmentTemplate = { - assessment: { - evidences: [ - { - code: "OB", - sections: [ - { - code: "S1", - questions: [], - name: "type Questions", - }, - ], - externalId: "OB", - tip: null, - name: "name", - description: null, - modeOfCollection: "onfield", - canBeNotApplicable: false, - notApplicable: false, - canBeNotAllowed: false, - remarks: null, - startTime: "", - endTime: "", - isSubmitted: false, - submissions: [], - }, - ], - }, -}; - -const questionTemplate = { - date: { - name: "Migrated Question", - code: "externalId", - mimeType: "application/vnd.sunbird.question", - primaryCategory: "responseType", - interactionTypes: "responseType", - showRemarks: "showRemarks", - instructions: { - en: [null], - }, - body: "question", - editorState: { - question: "question", - }, - responseDeclaration: { - response1: { - type: "string", - }, - }, - interactions: { - validation: "validation", - response1: { - validation: { - pattern: "dateFormat", - }, - autoCapture: "autoCapture", - }, - }, - hints: "hint", - }, - slider: { - name: "Migrated Question-Slider Type", - code: "externalId", - mimeType: "application/vnd.sunbird.question", - primaryCategory: "responseType", - interactionTypes: "responseType", - showRemarks: "showRemarks", - instructions: { - en: [null], - }, - body: "question", - editorState: { - question: "question", - }, - responseDeclaration: { - response1: { - type: "integer", - maxScore: 1, - }, - }, - interactions: { - validation: { - required: "validation.required", - }, - response1: { - validation: { - range: { - min: "validation.min", - max: "validation.max", - }, - }, - step: "1", - }, - }, - hints: "hint", - evidence: { - mimeType: "file.type", - }, - }, - multiselect: { - name: "Migrated Question", - code: "externalId", - mimeType: "application/vnd.sunbird.question", - primaryCategory: "Multiselect Multiple Choice Question", - interactionTypes: ["choice"], - showRemarks: "showRemarks", - body: "question", - hints: "hint", - interactions: { - response1: { - type: "choice", - options: [ - { - label: "options[0]", - value: 0, - }, - { - label: "options[1]", - value: 1, - }, - { - label: "options[2]", - value: 2, - }, - { - label: "options[3]", - value: 3, - }, - { - label: "options[4]", - value: 4, - }, - ], - }, - validation: "validation", - }, - editorState: { - options: [ - { - answer: false, - value: { - body: "options[0]", - value: 0, - }, - }, - { - answer: false, - value: { - body: "options[1]", - value: 1, - }, - }, - { - answer: false, - value: { - body: "options[2]", - value: 2, - }, - }, - { - answer: false, - value: { - body: "options[3]", - value: 3, - }, - }, - { - answer: false, - value: { - body: "options[4]", - value: 4, - }, - }, - ], - question: "question", - }, - instructions: { - en: [null], - }, - evidence: { - mimeType: "file.type", - }, - responseDeclaration: { - response1: { - maxScore: 1, - cardinality: "multiple", - type: "integer", - correctResponse: { - outcomes: { - SCORE: 1, - }, - }, - mapping: [], - }, - }, - }, - mcq: { - name: "Migrated Question", - code: "externalId", - description: "", - mimeType: "application/vnd.sunbird.question", - primaryCategory: "Multiselect Multiple Choice Question", - interactionTypes: ["choice"], - body: "question", - interactions: { - validation: "validation", - response1: { - type: "choice", - options: [ - { - value: 0, - label: "options[0]", - }, - { - value: 1, - label: "options[1]", - }, - ], - }, - }, - editorState: { - options: [ - { - answer: false, - value: { - body: "options[0]", - value: 0, - }, - }, - { - answer: false, - value: { - body: "options[1]", - value: 1, - }, - }, - ], - question: "question", - }, - responseDeclaration: { - response1: { - maxScore: 0, - cardinality: "single", - type: "integer", - correctResponse: { - outcomes: { - SCORE: 0, - }, - }, - }, - }, - instructions: { - default: "tip", - }, - hints: "hint", - }, - text: { - name: "Migrated Question", - code: "externalId", - mimeType: "application/vnd.sunbird.question", - primaryCategory: "Text", - interactionTypes: ["text"], - showRemarks: "showRemarks", - body: "question", - instructions: { - en: [null], - }, - editorState: { - question: "question", - }, - responseDeclaration: { - response1: { - type: "string", - maxScore: 1, - }, - }, - interactions: { - validation: { - required: "validation.required", - }, - response1: { - validation: { - limit: { - maxLength: "100", - }, - }, - type: { - number: "validation.isNumber", - }, - }, - }, - hints: "hint", - }, -}; - -const questionStatic = { - date: ["name", "mimeType", "instructions", "responseDeclaration"], - slider: ["name", "mimeType", "instructions", "responseDeclaration"], - multiselect: [ - "name", - "mimeType", - "instructions", - "primaryCategory", - "interactionTypes", - "responseDeclaration", - ], - mcq: [ - "name", - "mimeType", - "primaryCategory", - "interactionTypes", - "responseDeclaration", - ], - text: [ - "name", - "mimeType", - "instructions", - "primaryCategory", - "interactionTypes", - "responseDeclaration", - "instructions", - ], -}; - -const criteriaTemplate = { - _id: "identifier", - __v: 0, - createdAt: "createdOn", - createdFor: "channel", - criteriaType: "manual", - description: "description", - externalId: "code", - flag: "", - frameworkCriteriaId: "identifier", - name: "name", - owner: "consumerId", - remarks: "", - score: "", - showRemarks: "showRemarks", - timesUsed: "", - updatedAt: "lastUpdatedOn", - weightage: "", - migratedId: "identifier", - rubric: {}, -}; - -const defaultCriteria = ["__v", "criteriaType", "flag","remarks", "score", "timesUsed", "weightage", "rubric"] - -module.exports = { - assessmentTemplate, - questionTemplate, - questionStatic, - criteriaTemplate, - defaultCriteria -}; From f460384769dfc9ac708c9a1559c0fd3ed8746c9f Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Thu, 8 Sep 2022 16:48:43 +0530 Subject: [PATCH 33/36] updated sections order --- .../constant/config.js | 2 +- .../template/generate/gProgram.js | 36 +- .../template/generate/gQuestionSet.js | 2 +- .../template/helpers/hierarchyHelper.js | 420 ++++++++++++++---- .../template/helpers/questionsetHelper.js | 20 +- .../template/migrate/common.js | 24 +- .../template/migrate/nonmatrix.js | 3 +- 7 files changed, 368 insertions(+), 139 deletions(-) diff --git a/scripts/creation-portal-migration/constant/config.js b/scripts/creation-portal-migration/constant/config.js index 61e4dcd2..b5a2ceaf 100644 --- a/scripts/creation-portal-migration/constant/config.js +++ b/scripts/creation-portal-migration/constant/config.js @@ -22,7 +22,7 @@ const CONFIG = { create_questionset: process.env.CREATE_QUESTION_SET || "api/questionset/v1/create", update_hierarchy: process.env.UPDATE_QUESTION_SET_HIERARCHY || "api/questionset/v1/hierarchy/update", publish_questionset: process.env.PUBLISH_QUESTION_SET || "api/questionset/v1/publish", - read_questionset: "api/" + process.env.READ_QUESTION_SET || "api/questionset/v1/hierarchy/", + read_questionset: "api/" + process.env.READ_QUESTION_SET + "/" || "api/questionset/v1/hierarchy/", create_question: process.env.CREATE_QUESTION || "api/question/v1/create", publish_question: process.env.PUBLISH_QUESTION || "api/question/v1/publish", create_program: process.env.CREATE_PROGRAM || "api/program/v1/create", diff --git a/scripts/creation-portal-migration/template/generate/gProgram.js b/scripts/creation-portal-migration/template/generate/gProgram.js index 776f135c..63d90a9b 100644 --- a/scripts/creation-portal-migration/template/generate/gProgram.js +++ b/scripts/creation-portal-migration/template/generate/gProgram.js @@ -69,7 +69,7 @@ const createProgramTemplate = async (solution, program_id, migratedCount) => { }, }; - let programId = solution.sourcingProgramId; + let programId = solution?.migrationReference?.sourcingProgramId; let query = {}; @@ -91,12 +91,12 @@ const createProgramTemplate = async (solution, program_id, migratedCount) => { logger.info( `Sourcing Program created for solution_id: ${solution?._id} === ${programId}` ); - query = { ...query, sourcingProgramId: programId }; + query = { ...query, "migrationReference.sourcingProgramId": programId }; } else { migratedCount.success.program.existing.migrated++; } - if (!solution.isSrcProgramUpdated) { + if (!solution?.migrationReference?.isSrcProgramUpdated) { const update_res = await updateProgramTemplate(programId, solution); if (!update_res) { migratedCount.failed.program.updated.count++; @@ -112,13 +112,13 @@ const createProgramTemplate = async (solution, program_id, migratedCount) => { query = { ...query, - isSrcProgramUpdated: true, + "migrationReference.isSrcProgramUpdated": true, }; } else { migratedCount.success.program.existing.updated++; } - if (!solution.isSrcProgramPublished) { + if (!solution?.migrationReference?.isSrcProgramPublished) { const pub_res = await publishProgramTemplate(programId, solution?._id); if (!pub_res) { @@ -136,13 +136,13 @@ const createProgramTemplate = async (solution, program_id, migratedCount) => { query = { ...query, - isSrcProgramPublished: true, + "migrationReference.isSrcProgramPublished": true, }; } else { migratedCount.success.program.existing.published++; } - if (!solution.isNominated) { + if (!solution?.migrationReference?.isNominated) { const res = await nominateProgram(programId, userId).catch((err) => { // updateFailedCount(migratedCount, "nominated", solution?._id); migratedCount.failed.program.nominated.count++; @@ -162,12 +162,12 @@ const createProgramTemplate = async (solution, program_id, migratedCount) => { `Sourcing Program nominated for solution_id: ${solution?._id} === ${programId}` ); - query = { ...query, isNominated: true }; + query = { ...query, "migrationReference.isNominated": true }; } else { migratedCount.success.program.existing.nominated++; } - if (!solution.isContributorAdded) { + if (!solution?.migrationReference?.isContributorAdded) { const add_contri = { program_id: programId, user_id: process.env.DEFAULT_USER_ID_TO_ADD_CONTRIBUTOR, @@ -198,12 +198,12 @@ const createProgramTemplate = async (solution, program_id, migratedCount) => { `Sourcing Program added contributor for solution_id: ${solution?._id} === ${programId}` ); - query = { ...query, isContributorAdded: true }; + query = { ...query, "migrationReference.isContributorAdded": true }; } else { migratedCount.success.program.existing.contributor++; } - if (!solution.isContributorAccepted) { + if (!solution?.migrationReference?.isContributorAccepted) { const accept_contri = { program_id: programId, user_id: process.env.DEFAULT_USER_ID_TO_ADD_CONTRIBUTOR, @@ -232,7 +232,7 @@ const createProgramTemplate = async (solution, program_id, migratedCount) => { `Sourcing Program accepted nomination for solution_id: ${solution?._id} === ${programId}` ); - query = { ...query, isContributorAccepted: true }; + query = { ...query, "migrationReference.isContributorAccepted": true }; } else { migratedCount.success.program.existing.accepted++; } @@ -254,22 +254,22 @@ const updateSolutionDb = async (query, solution, migratedCount) => { ); }); - if (query.hasOwnProperty("sourcingProgramId")) { + if (query.hasOwnProperty("migrationReference.sourcingProgramId")) { migratedCount.success.program.current.migrated++; } - if (query.hasOwnProperty("isSrcProgramUpdated")) { + if (query.hasOwnProperty("migrationReference.isSrcProgramUpdated")) { migratedCount.success.program.current.updated++; } - if (query.hasOwnProperty("isSrcProgramPublished")) { + if (query.hasOwnProperty("migrationReference.isSrcProgramPublished")) { migratedCount.success.program.current.published++; } - if (query.hasOwnProperty("isNominated")) { + if (query.hasOwnProperty("migrationReference.isNominated")) { migratedCount.success.program.current.nominated++; } - if (query.hasOwnProperty("isContributorAdded")) { + if (query.hasOwnProperty("migrationReference.isContributorAdded")) { migratedCount.success.program.current.contributor++; } - if (query.hasOwnProperty("isContributorAccepted")) { + if (query.hasOwnProperty("migrationReference.isContributorAccepted")) { migratedCount.success.program.current.accepted++; } }; diff --git a/scripts/creation-portal-migration/template/generate/gQuestionSet.js b/scripts/creation-portal-migration/template/generate/gQuestionSet.js index 1353a028..ada795ae 100644 --- a/scripts/creation-portal-migration/template/generate/gQuestionSet.js +++ b/scripts/creation-portal-migration/template/generate/gQuestionSet.js @@ -14,7 +14,7 @@ const { getNonMatrixQuestions } = require("../migrate/nonmatrix"); const getQuestionSetTemplates = async (solutions, migratedCount) => { const data = Promise.all( solutions.map(async (solution) => { - let programId = solution.sourcingProgramId; + let programId = solution?.migrationReference?.sourcingProgramId; programId = await createProgramTemplate( solution, programId, diff --git a/scripts/creation-portal-migration/template/helpers/hierarchyHelper.js b/scripts/creation-portal-migration/template/helpers/hierarchyHelper.js index de00bb95..47352a41 100644 --- a/scripts/creation-portal-migration/template/helpers/hierarchyHelper.js +++ b/scripts/creation-portal-migration/template/helpers/hierarchyHelper.js @@ -16,10 +16,15 @@ const { CONFIG } = require("./../../constant/config"); const { updateById } = require("../../db"); const logger = require("../../logger"); -const updateHierarchyChildren = (hierarchy, referenceQuestionId, index) => { - - logger.debug(`updateHierarchyChildren: referenceQuestionId = ${referenceQuestionId}`) - +const updateHierarchyChildren = ( + hierarchy, + referenceQuestionId, + index, + question +) => { + logger.debug( + `updateHierarchyChildren: referenceQuestionId = ${referenceQuestionId}` + ); if ( referenceQuestionId && @@ -27,6 +32,28 @@ const updateHierarchyChildren = (hierarchy, referenceQuestionId, index) => { ) { hierarchy.criterias[index].questions.push(referenceQuestionId); } + + if (question?.page) { + const page = question?.page ? question?.page?.trim() : "" + if ( + hierarchy.criterias[index].pageQuestions.hasOwnProperty(page) + ) { + if ( + !hierarchy.criterias[index].pageQuestions[page].includes( + referenceQuestionId + ) + ) { + hierarchy.criterias[index].pageQuestions[page].push( + referenceQuestionId + ); + } + } else { + hierarchy.criterias[index].pageQuestions[page] = [ + referenceQuestionId, + ]; + } + } + return hierarchy; }; @@ -42,8 +69,11 @@ const getOperator = (visibleIf) => { }; const getPrecondition = (visible, parentId, parentQuestion) => { - logger.debug(`getPrecondition: parentId = ${parentId}; visible: ${visible}`); - + logger.debug( + `getPrecondition: parentId = ${parentId}; visible: ${JSON.stringify( + visible + )}` + ); return { and: [ { @@ -66,6 +96,122 @@ const updateHierarchyTemplate = async ( solution, programId, migratedCount +) => { + // console.log("updateHierarchyTemplate", JSON.stringify(hierarchy)); + await updateCriteriasList(hierarchy, solution, programId, migratedCount); +}; + +const branchingQuestionSetHierarchy = async (hierarchy, newCriterias) => { + + logger.debug("branchingQuestionSetHierarchy", hierarchy) + + let questionSetHierarchy = {}; + if (hierarchy.questionset && hierarchy.isHierarchyUpdated) { + questionSetHierarchy = await readQuestionSetHierarchy( + hierarchy?.questionset + ).catch(err => { + console.log("Error", err); + return; + }); + console.log("questionset", hierarchy.questionset ) + logger.info(`${"questionset", hierarchy.questionset, "questionSetHierarchy", questionSetHierarchy }`) + } + + const updateHierarchyData = { + request: { + data: { + nodesModified: {}, + hierarchy: { + [hierarchy.questionset]: { + children: [], + root: true, + }, + }, + }, + }, + }; + + + for (let i = 0; i < newCriterias.length; i++) { + const criteria = newCriterias[i]; + const hierarchyData = find(questionSetHierarchy.children, { + name: criteria?.name, + }); + criteria.referenceQuestionSetId = + hierarchy.questionset && hierarchy.isHierarchyUpdated + ? hierarchyData?.identifier + : criteria.referenceQuestionSetId; + if (criteria?.referenceQuestionSetId) { + const metadata = pick(criteria, [ + "code", + "name", + "description", + "mimeType", + "primaryCategory", + "allowMultipleInstances", + "instances", + ]); + updateHierarchyData.request.data.nodesModified[ + criteria.referenceQuestionSetId + ] = { + metadata: { + ...metadata, + allowBranching: "Yes", + branchingLogic: get(criteria, "branchingLogic") || {}, + }, + objectType: "QuestionSet", + root: false, + isNew: false, + }; + updateHierarchyData.request.data.hierarchy[ + hierarchy.questionset + ].children.push(criteria.referenceQuestionSetId); + + updateHierarchyData.request.data.hierarchy[ + criteria.referenceQuestionSetId + ] = { + children: compact(criteria.questions), + root: false, + }; + } + } + + console.log("branchingLogic", JSON.stringify(updateHierarchyData)) + + return updateHierarchyData; +}; + +const updateSolutionsDb = async ( + query, + referenceQuestionsetId, + migratedCount +) => { + const res = await updateById( + CONFIG.DB.TABLES.solutions, + referenceQuestionsetId, + query + ).catch((err) => { + logger.error( + `Error while updating questionset in solutions collection: ${solution?._id}` + ); + }); + + if (query.hasOwnProperty("migrationReference.isHierarchyUpdated")) { + migratedCount.success.questionSet.current.hierarchy++; + } + if (query.hasOwnProperty("migrationReference.isBranchingUpdated")) { + migratedCount.success.questionSet.current.branching++; + } + if (query.hasOwnProperty("migrationReference.isPublished")) { + migratedCount.success.questionSet.current.published++; + } +}; + +const updateCriteriasList = async ( + hierarchy, + solution, + programId, + migratedCount ) => { logger.debug( `updateHierarchyTemplate: programId = ${programId}; solution: ${solution?._id}` @@ -84,9 +230,96 @@ const updateHierarchyTemplate = async ( }, }; + console.log(); + console.log("5f3fcd6daf0a4decfa9a10bd5f3fcd6daf0a4decfa9a10bd", JSON.stringify(hierarchy)); + console.log(); + + let pageSections = {}; + const newCriterias = []; + for (let i = 0; i < hierarchy.criterias.length; i++) { - const criteria = hierarchy.criterias[i]; - const metadata = pick(criteria, [ + let criteria = hierarchy.criterias[i]; + const pageKeys = Object.keys(criteria.pageQuestions) || []; + const branchingKeys = Object.keys(criteria.branchingLogic) || []; + let questionsCopy = []; + if (!criteria.isMatrix) { + questionsCopy = criteria.questions; + for (let index = 0; index < criteria.questions.length; index++) { + const qId = criteria.questions[index]; + if (branchingKeys.includes(qId)) { + if (criteria.branchingLogic[qId].target.length > 0) { + const data = updatePageData( + pageKeys, + criteria, + pageSections, + questionsCopy, + qId, + qId, + true + ); + questionsCopy = data.questionsCopy; + pageSections = data.pageSections; + criteria = data.criteria; + } else { + const parentId = criteria.branchingLogic[qId].source[0]; + const data = updatePageData( + pageKeys, + criteria, + pageSections, + questionsCopy, + parentId, + qId, + true + ); + questionsCopy = data.questionsCopy; + pageSections = data.pageSections; + criteria = data.criteria; + } + } else { + const data = updatePageData( + pageKeys, + criteria, + pageSections, + questionsCopy, + qId, + qId, + false + ); + questionsCopy = data.questionsCopy; + pageSections = data.pageSections; + criteria = data.criteria; + } + } + // console.log("questionscioy", questionsCopy.length, criteria?.name, criteria?.branchingLogic) + if (questionsCopy.length > 0) { + criteria.questions = questionsCopy; + newCriterias.push(criteria); + } + } else { + newCriterias.push(criteria); + } + } + + const pageSectionKeys = Object.keys(pageSections); + for ( + let pageSectionIndex = 0; + pageSectionIndex < pageSectionKeys.length; + pageSectionIndex++ + ) { + // console.log(); + // console.log( + // "pageSectionkeys", + // pageSectionKeys, + // pageSections[pageSectionIndex] + // ); + // console.log(); + + newCriterias.push(pageSections[pageSectionKeys[pageSectionIndex]]); + } + + for (let section = 0; section < newCriterias.length; section++) { + const sectionData = newCriterias[section]; + const metadata = pick(sectionData, [ "code", "name", "description", @@ -95,7 +328,7 @@ const updateHierarchyTemplate = async ( "allowMultipleInstances", "instances", ]); - updateHierarchyData.request.data.nodesModified[criteria.name] = { + updateHierarchyData.request.data.nodesModified[sectionData.name] = { metadata: { ...metadata, }, @@ -105,28 +338,31 @@ const updateHierarchyTemplate = async ( }; updateHierarchyData.request.data.hierarchy[ hierarchy.questionset - ].children.push(criteria.name); + ].children.push(sectionData?.name); - updateHierarchyData.request.data.hierarchy[criteria.name] = { - children: compact(criteria.questions), + updateHierarchyData.request.data.hierarchy[sectionData.name] = { + children: compact(sectionData.questions), root: false, }; } - logger.info( `updateHierarchyTemplate: Hierarchydata = ${JSON.stringify( updateHierarchyData )}` ); + console.log(); + console.log("updateHierarchyTemplate", JSON.stringify(updateHierarchyData)); + console.log(); + const questionsetId = hierarchy.questionsetDbId; let query = {}; if (!hierarchy.isHierarchyUpdated) { const result = await updateQuestionSetHierarchy(updateHierarchyData).catch( (err) => { logger.error(`Error while updating the questionset for solution_id: ${questionsetId} Error: - ${JSON.stringify(err.response.data)}`); + ${JSON.stringify(err.response.data)}`); if ( !migratedCount.failed.questionSet.hierarchy.ids.includes( @@ -147,24 +383,27 @@ const updateHierarchyTemplate = async ( } query = { ...query, - isHierarchyUpdated: true, + "migrationReference.isHierarchyUpdated": true, }; - for (let i = 0; i < hierarchy.criterias.length; i++) { - const criterias = hierarchy.criterias[i]; - hierarchy.criterias[i].referenceQuestionSetId = result[criterias.name]; + for (let i = 0; i < newCriterias.length; i++) { + const criterias = newCriterias[i]; + newCriterias[i].referenceQuestionSetId = result[criterias.name]; } } else { migratedCount.success.questionSet.existing.hierarchy++; } if (!hierarchy.isBranchingUpdated) { - const branchinghierarchy = await branchingQuestionSetHierarchy(hierarchy); + const branchinghierarchy = await branchingQuestionSetHierarchy( + hierarchy, + newCriterias + ); const result = await updateQuestionSetHierarchy(branchinghierarchy).catch( (err) => { logger.error(`Error while updating the questionset branching for solution_id: ${questionsetId} Error: - ${JSON.stringify(err.response.data)}`); + ${JSON.stringify(err.response.data)}`); if ( !migratedCount.failed.questionSet.branching.ids.includes( @@ -178,13 +417,15 @@ const updateHierarchyTemplate = async ( } } ); + + console.log("Branchinfnfnfnffn", result) if (!result) { await updateSolutionsDb(query, questionsetId, migratedCount); return; } query = { ...query, - isBranchingUpdated: true, + "migrationReference.isBranchingUpdated": true, }; } else { migratedCount.success.questionSet.existing.branching++; @@ -195,7 +436,7 @@ const updateHierarchyTemplate = async ( logger.error(`Error while publishing the questionset for solution_id: ${questionsetId} === ${ hierarchy?.questionset } Error: - ${JSON.stringify(err.response.data)}`); + ${JSON.stringify(err.response.data)}`); if ( !migratedCount.failed.questionSet.published.ids.includes( @@ -215,7 +456,7 @@ const updateHierarchyTemplate = async ( } query = { ...query, - isPublished: true, + "migrationReference.isPublished": true, }; } else if (hierarchy.isPublished) { migratedCount.success.questionSet.existing.published++; @@ -223,89 +464,72 @@ const updateHierarchyTemplate = async ( const res = await updateSolutionsDb(query, questionsetId, migratedCount); }; -const branchingQuestionSetHierarchy = async (hierarchy) => { - let questionSetHierarchy = {}; - if (hierarchy.questionset && hierarchy.isHierarchyUpdated) { - questionSetHierarchy = await readQuestionSetHierarchy( - hierarchy.questionset - ); +const updatePageData = ( + pageKeys, + criteria, + pageSections, + questionsCopy, + idInPages, + idToAdd, + isBranching +) => { + let pageName = ""; + for (let page = 0; page < pageKeys.length; page++) { + pageName = criteria.pageQuestions[pageKeys[page]].includes(idInPages) + ? pageKeys[page].trim() + : pageName.trim(); + } + if (pageName && pageSections.hasOwnProperty(pageName)) { + if (!pageSections[pageName].questions.includes(idToAdd)) { + pageSections[pageName].questions.push(idToAdd); + questionsCopy = questionsCopy.filter((id) => id !== idToAdd); + } else { + questionsCopy = questionsCopy.filter((id) => id !== idToAdd); + } + } else if (pageName && !pageSections.hasOwnProperty(pageName)) { + questionsCopy = questionsCopy.filter((id) => id !== idToAdd); + pageSections[pageName] = { + questions: [idToAdd], + referenceQuestionSetId: "", + criDbId: "", + code: criteria?.code, + name: `Page ${pageName}`, + description: `Description ${pageName}`, + mimeType: "application/vnd.sunbird.questionset", + primaryCategory: "observation", + allowMultipleInstances: "", + instances: {}, + branchingLogic: {}, + isMatrix: false, + }; } - const updateHierarchyData = { - request: { - data: { - nodesModified: {}, - hierarchy: { - [hierarchy.questionset]: { - children: [], - root: true, - }, - }, - }, - }, - }; - for (let i = 0; i < hierarchy.criterias.length; i++) { - const criteria = hierarchy.criterias[i]; - const hierarchyData = find(questionSetHierarchy.children, { - name: criteria?.name, - }); - criteria.referenceQuestionSetId = - hierarchy.questionset && hierarchy.isHierarchyUpdated - ? hierarchyData?.identifier - : criteria.referenceQuestionSetId; - if (criteria?.referenceQuestionSetId) { - const metadata = pick(criteria, [ - "code", - "name", - "description", - "mimeType", - "primaryCategory", - "allowMultipleInstances", - "instances", - ]); - updateHierarchyData.request.data.nodesModified[criteria.referenceQuestionSetId] = { - metadata: { - ...metadata, - allowBranching: "Yes", - branchingLogic: get(criteria, "branchingLogic") || {}, - }, - objectType: "QuestionSet", - root: false, - isNew: false, - }; - updateHierarchyData.request.data.hierarchy[ - hierarchy.questionset - ].children.push(criteria.referenceQuestionSetId); + if (pageName && isBranching) { + const branching = criteria.branchingLogic[idToAdd]; + console.log("idtoAdddd", idToAdd, criteria?.name, criteria?.isMatrix, criteria.branchingLogic); - updateHierarchyData.request.data.hierarchy[criteria.referenceQuestionSetId] = { - children: compact(criteria.questions), - root: false, + if (!pageSections[pageName].branchingLogic.hasOwnProperty(idToAdd)) { + pageSections[pageName].branchingLogic = { + ...pageSections[pageName].branchingLogic, + [idToAdd]: {...branching}, }; + delete criteria.branchingLogic[idToAdd]; } - } - return updateHierarchyData; -}; -const updateSolutionsDb = async (query, questionsetId, migratedCount) => { - const res = await updateById( - CONFIG.DB.TABLES.solutions, - questionsetId, - query - ).catch((err) => { - logger.error( - `Error while updating questionset in solutions collection: ${solution?._id}` - ); - }); - - if (query.hasOwnProperty("isHierarchyUpdated")) { - migratedCount.success.questionSet.current.hierarchy++; - } - if (query.hasOwnProperty("isBranchingUpdated")) { - migratedCount.success.questionSet.current.branching++; + for (let target=0; target < branching.target.length; target++) { + + if (!pageSections[pageName].branchingLogic.hasOwnProperty(idToAdd)) { + pageSections[pageName].branchingLogic = { + ...pageSections[pageName].branchingLogic, + [branching.target[target]]: criteria.branchingLogic[branching.target[target]] , + }; + delete criteria.branchingLogic[branching.target[target]]; + } } - if (query.hasOwnProperty("isPublished")) { - migratedCount.success.questionSet.current.published++; + } + + return { questionsCopy, pageSections, criteria }; }; module.exports = { diff --git a/scripts/creation-portal-migration/template/helpers/questionsetHelper.js b/scripts/creation-portal-migration/template/helpers/questionsetHelper.js index c7b32001..aa406278 100644 --- a/scripts/creation-portal-migration/template/helpers/questionsetHelper.js +++ b/scripts/creation-portal-migration/template/helpers/questionsetHelper.js @@ -48,7 +48,7 @@ const createQuestionTemplate = async (question, migratedCount) => { let query = {}; let questionToMigrate = {}; - let published = question?.isPublished; + let isPublished = question?.migrationReference?.isPublished; if (type && !referenceQuestionId) { if (type.toLowerCase() === "date") { @@ -75,7 +75,7 @@ const createQuestionTemplate = async (question, migratedCount) => { } } - if (referenceQuestionId && !published) { + if (referenceQuestionId && !isPublished) { const res = await publishQuestion(referenceQuestionId).catch((err) => { if (!migratedCount.failed.question.ids.includes(referenceQuestionId)) { migratedCount.failed.question.count++; @@ -90,28 +90,28 @@ const createQuestionTemplate = async (question, migratedCount) => { `createQuestion Template publish response: ${res} , "referenceQuestionId" ${referenceQuestionId} questionId, ${question?._id}` ); - - if (res) { - question.isPublished = true; - published = true; + question = { + ...question, + migrationReference: { isPublished: true }, + }; + isPublished = true; logger.info(`createQuestion Template published: ${referenceQuestionId}`); } } - if (referenceQuestionId) { question.referenceQuestionId = referenceQuestionId; query = { referenceQuestionId, - published: published, + "migrationReference.isPublished": isPublished, }; } else { query = { ...query, - published - } + "migrationReference.isPublished": isPublished, + }; } if (!isEmpty(query) && question) { diff --git a/scripts/creation-portal-migration/template/migrate/common.js b/scripts/creation-portal-migration/template/migrate/common.js index 8510c16e..3487b5cf 100644 --- a/scripts/creation-portal-migration/template/migrate/common.js +++ b/scripts/creation-portal-migration/template/migrate/common.js @@ -1,4 +1,4 @@ -const { isEmpty, compact } = require("lodash"); +const { isEmpty } = require("lodash"); const { getPrecondition } = require("../helpers/hierarchyHelper"); const { getQuestionFromDB, @@ -10,15 +10,15 @@ const initHierarchy = (questionsetid, solution, programId, referenceQuestionSetI return { questionset: referenceQuestionSetId, questionsetDbId: questionsetid, - isHierarchyUpdated: solution?.isHierarchyUpdated || false, - isBranchingUpdated: solution?.isBranchingUpdated || false, - isPublished: solution?.isPublished || false, + isHierarchyUpdated: solution?.migrationReference?.isHierarchyUpdated || false, + isBranchingUpdated: solution?.migrationReference?.isBranchingUpdated || false, + isPublished: solution?.migrationReference?.isPublished || false, sourcingProgramId: programId, - isSrcProgramUpdated: solution?.isSrcProgramUpdated || false, - isSrcProgramPublished: solution?.isSrcProgramPublished || false, - isNominated: solution?.isNominated || false, - isContributorAdded: solution?.isContributorAdded || false, - isContributorAccepted: solution?.isContributorAccepted || false, + isSrcProgramUpdated: solution?.migrationReference?.isSrcProgramUpdated || false, + isSrcProgramPublished: solution?.migrationReference?.isSrcProgramPublished || false, + isNominated: solution?.migrationReference?.isNominated || false, + isContributorAdded: solution?.migrationReference?.isContributorAdded || false, + isContributorAccepted: solution?.migrationReference?.isContributorAccepted || false, criterias: [], }; }; @@ -37,6 +37,8 @@ const getCriteriaData = (criteria, type, question = {}) => { branchingLogic: {}, allowMultipleInstances: "", instances: {}, + pageQuestions: {}, + isMatrix: false, }; } else { return { @@ -52,6 +54,8 @@ const getCriteriaData = (criteria, type, question = {}) => { branchingLogic: {}, allowMultipleInstances: "Yes", instances: { label: question?.instanceIdentifier }, + pageQuestions: {}, + isMatrix: true }; } }; @@ -122,7 +126,7 @@ const getQuestion = async (questions, questions2, id, migratedCount) => { question = matched; } - if (!isEmpty(question) && (!question?.referenceQuestionId || !question?.isPublished)) { + if (!isEmpty(question) && (!question?.referenceQuestionId || !question?.migrationReference?.isPublished)) { question = await createQuestionTemplate(question, migratedCount); } diff --git a/scripts/creation-portal-migration/template/migrate/nonmatrix.js b/scripts/creation-portal-migration/template/migrate/nonmatrix.js index 1e150c20..c0810472 100644 --- a/scripts/creation-portal-migration/template/migrate/nonmatrix.js +++ b/scripts/creation-portal-migration/template/migrate/nonmatrix.js @@ -43,7 +43,8 @@ const updateNonMatrixHierarchyChildren = ( hierarchy = updateHierarchyChildren( hierarchy, question?.referenceQuestionId, - index + index, + question ); nonMatrixQuestions.push(question); } From 973b626ab4dfd8531b2d8017a40f8160934b1a4b Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Thu, 8 Sep 2022 18:22:48 +0530 Subject: [PATCH 34/36] resolved merge conflict --- .env.sample | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.env.sample b/.env.sample index 5333a699..940dbdb2 100644 --- a/.env.sample +++ b/.env.sample @@ -27,6 +27,9 @@ ML_PROJECT_SERVICE_URL = "http://ml-project-service:3000" KEYCLOAK_PUBLIC_KEY_PATH = "keycloak-public-keys" // Keycloak public keys path DISABLE_LEARNER_SERVICE_ON_OFF = "ON" // Disable learner service check +FORM_SERVICE_URL = "http://player:3000" // Base url for form search + + From 7f92b2501822b4922d52afc63f968dbdbd20e274 Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Fri, 9 Sep 2022 10:32:41 +0530 Subject: [PATCH 35/36] resolved merge conflict --- .env.sample | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.env.sample b/.env.sample index 940dbdb2..d726dcc2 100644 --- a/.env.sample +++ b/.env.sample @@ -26,11 +26,9 @@ ML_PROJECT_SERVICE_URL = "http://ml-project-service:3000" KEYCLOAK_PUBLIC_KEY_PATH = "keycloak-public-keys" // Keycloak public keys path -DISABLE_LEARNER_SERVICE_ON_OFF = "ON" // Disable learner service check -FORM_SERVICE_URL = "http://player:3000" // Base url for form search - - +DISABLE_LEARNER_SERVICE_ON_OFF = "ON" // Disable learner service check +FORM_SERVICE_URL = "http://player:3000" // Base url for form search # Redis configuration From 2eb880ea4e9bc42eaf3a0a4145c26319703d18ac Mon Sep 17 00:00:00 2001 From: swetha-aggidevara Date: Mon, 6 Mar 2023 12:27:17 +0530 Subject: [PATCH 36/36] sections as pages --- package.json | 7 +- .../SL-DataMapping.csv | 22 +++ .../api-list/headers.js | 16 +- .../api-list/program.js | 11 +- .../api-list/user.js | 63 ++++++-- .../constant/config.js | 3 +- .../creation-portal-migration/db/dbConfig.js | 5 +- scripts/creation-portal-migration/logger.js | 8 +- .../creation-portal-migration/orgDetails.js | 80 ++++++++++ .../template/config/question.js | 1 + .../template/config/questionSet.js | 76 ---------- .../template/generate/gProgram.js | 140 +++++++++++------- .../template/generate/gQuestion.js | 22 ++- .../template/generate/gQuestionSet.js | 40 +++-- .../template/helpers/csvHelper.js | 95 ++++++++++++ .../template/helpers/hierarchyHelper.js | 20 +-- .../template/helpers/questionsetHelper.js | 60 ++++---- .../template/migrate/nonmatrix.js | 12 ++ 18 files changed, 460 insertions(+), 221 deletions(-) create mode 100644 scripts/creation-portal-migration/SL-DataMapping.csv create mode 100644 scripts/creation-portal-migration/orgDetails.js delete mode 100644 scripts/creation-portal-migration/template/config/questionSet.js create mode 100644 scripts/creation-portal-migration/template/helpers/csvHelper.js diff --git a/package.json b/package.json index 7d695c81..cddd939f 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "test": "mocha --timeout 10000", "start": "node app.js", "dev": "nodemon app.js", - "coverage": " nyc --reporter=lcov mocha --timeout 10000 test --exit" + "coverage": " nyc --reporter=lcov mocha --timeout 10000 test --exit", + "creation_portal_script": "node scripts/creation-portal-migration/index.js" }, "keywords": [ "shikshalokam", @@ -29,7 +30,9 @@ "commander": "^2.20.0", "cookie-parser": "^1.4.3", "cors": "^2.8.4", - "csvtojson": "^2.0.8", + "csv-parse": "^5.3.0", + "csv-stringify": "^6.2.0", + "csvtojson": "^2.0.10", "dotenv": "^6.1.0", "express": "^4.16.3", "express-cassandra": "^2.3.2", diff --git a/scripts/creation-portal-migration/SL-DataMapping.csv b/scripts/creation-portal-migration/SL-DataMapping.csv new file mode 100644 index 00000000..c5d2910d --- /dev/null +++ b/scripts/creation-portal-migration/SL-DataMapping.csv @@ -0,0 +1,22 @@ +ENV,authorId,mappedUserId,userName,rootOrgId,rootOrgName,org_id,srcOrgAdminId,srcOrgAdminUserName,contributorOrgAdminId,contributorOrgAdminUserName,solutionId,solutionName,programId,programName +STAGE,140558b9-7df4-4993-be3c-31eb8b9ca368,d8d54588-82f7-420d-b098-c03948135d6f,abhi234,01283607456185548825093,NCERT,fba93280-27b5-4d29-90e0-1f79ecbfa4bf,8e92e8da-83fe-43f1-b390-44da44675718,akhil23,8e92e8da-83fe-43f1-b390-44da44675718,akhil23,5f34e44681871d939950bca7,TN01-Mantra4Change-APSWREIS School Leader Feedback,e01178e0-7c76-11ed-bcf5-df48f63ad78b,MIGRATED 21TH TN01-Mantra4Change-APSWREIS School Leader Feedback sourcing project +STAGE,86d2d978-5b20-4453-8a76-82b5a4c728c9,b8e3c5f2-07b3-49f3-964f-ef8e90897513,karan121,01338111579044249633,dockstaging,d7da22f6-b737-4817-a194-6a205e535559,2730f876-735d-4935-ba52-849c524a53fe,dockstaging1@yopmail.com,2730f876-735d-4935-ba52-849c524a53fe,dockstaging1@yopmail.com,5f34ec17585244939f89f90d,MH01-Mantra4Change-APSWREIS School Leader Feedback,b00e8f10-7c77-11ed-bcf5-df48f63ad78b,MIGRATED 21TH MH01-Mantra4Change-APSWREIS School Leader Feedback sourcing project +STAGE,9304641b-71c6-4e95-ac2c-27f222904fbe,b717deee-4251-4161-9aae-cd73bf9055f3,rakesh22,01334203864941363283,Haryana,09cfa04a-e841-4168-b320-a3e7c19d8e88,be0ac426-2145-4ca1-a3b7-0d14947a3b3b,haryana1@yopmail.com,be0ac426-2145-4ca1-a3b7-0d14947a3b3b,haryana1@yopmail.com,5f36d6d019377eecddb06946,Need Assessment Form_Teacher Training,b1955621-7c77-11ed-bcf5-df48f63ad78b,MIGRATED 21TH Need Assessment Form_Teacher Training sourcing project +STAGE,60c4bfaa-1650-413c-9fdf-32444a68e78d,18c80735-4823-432f-bd70-b2ffa1b68d70,raj119,0133725426790973441,LMS_12Staging,345b54ff-2563-4123-bc7e-4b5111c805a3,f836b1a5-4008-4283-acf9-f29078e88f9c,lmps2@yopmail.com,f836b1a5-4008-4283-acf9-f29078e88f9c,lmps2@yopmail.com,5f6db0cd7157bee754e6abdd,MH01-Mantra4Change-APSWREIS School Leader Feedback,b50b4300-7c77-11ed-bcf5-df48f63ad78b,MIGRATED 21TH MH01-Mantra4Change-APSWREIS School Leader Feedback sourcing project +STAGE,b87160fb-16c3-4951-82cc-31149bbb64b9,47859d2b-16a2-4555-83d5-8821876f86d4,kamesh99,01338113710893465654,dockstaging_vdn,7278c982-872c-4854-942b-b9aa1a32292d,8eee44cf-088a-4bdf-b0f0-f0c4cd0d1b7b,mahesh2@yopmail.com,8eee44cf-088a-4bdf-b0f0-f0c4cd0d1b7b,mahesh2@yopmail.com,5f7580ce5ccb683ca97f365b,Doubt Resolution-Test,b54f50e0-7c77-11ed-bcf5-df48f63ad78b,MIGRATED 21TH Doubt Resolution-Test sourcing project +STAGE,66926c63-df26-4040-a222-3240c5c1dcea,559f4029-5c86-42e1-b465-92b57e194d78,ranu21,013085024460783616158023,Classmate,398a880a-bdee-41e1-84c4-b968edb20874,38949d9d-4a35-482e-b45d-85475e0c17df,google2yopmail.com_sw81,38949d9d-4a35-482e-b45d-85475e0c17df,google2yopmail.com_sw81,5f870d83d144f124575da5c2,Enrollment challenges in DIKSHA Courses,b67fe1a1-7c77-11ed-bcf5-df48f63ad78b,MIGRATED 21TH Enrollment challenges in DIKSHA Courses sourcing project +STAGE,1deba81d-3be0-4f9a-b2c7-e284ac75227f,08f44793-f256-4ed8-bee5-b0cbc083a082,mamta22,01338113627113881647,org4thOct2021,596c410a-1dfd-4a8d-8832-1dee41df601a,4bdfca08-0f4e-4cd1-a899-dad9a15d8ac0,4oct_pb6t,4bdfca08-0f4e-4cd1-a899-dad9a15d8ac0,4oct_pb6t,5f8745f5788bc8241ebcb172,Teachers' Weekly Check-in Form,b6c68790-7c77-11ed-bcf5-df48f63ad78b,MIGRATED 21TH Teachers' Weekly Check-in Form sourcing project +STAGE,c9f5bb44-5a55-460c-8466-5299a1065689,c3aa35d3-344e-4b16-a845-25ca72285fec,anupma17,01338111839367987237,org3rdOct2021,bd76269b-785a-4e37-973e-b87104a40024,257566b5-613a-43d9-a61b-385cacc9ac52,3oct_ubnp,257566b5-613a-43d9-a61b-385cacc9ac52,3oct_ubnp,5f874f7abbaab2241230d1dc,Enrollment challenges in DIKSHA Courses,b7098400-7c77-11ed-bcf5-df48f63ad78b,MIGRATED 21TH Enrollment challenges in DIKSHA Courses sourcing project +STAGE,a96826e9-ecd3-4d43-b9c9-37b4eaaf056e,cb9f1598-8f0f-4ca8-b02d-3b9e7d3d7a6a,lalita26,01347079436417433655,Shiksha,70795630-bd0a-438a-9310-90c6cc913ab5,f38ebb4a-fe11-4faf-98bd-4d7023b20abb,shiksha1yopmail.com_em2j,f38ebb4a-fe11-4faf-98bd-4d7023b20abb,shiksha1yopmail.com_em2j,5f89288cbbaab2241230d1e7,MH01-Mantra4Change-APSWREIS School Leader Feedback,b77983e1-7c77-11ed-bcf5-df48f63ad78b,MIGRATED 21TH MH01-Mantra4Change-APSWREIS School Leader Feedback sourcing project +STAGE,1dc2e743-1504-47e1-adad-54749e2b833c,fa7474d8-1fc5-418f-b236-f0703d95eb8f,manoj87,01344484093886464035,Sunrise,6c0e71b0-3620-429f-9da3-aec213e13928,616df817-97d3-4b0e-a622-f2494e957b26,sunrise1yopmail.com_eeod,616df817-97d3-4b0e-a622-f2494e957b26,sunrise1yopmail.com_eeod,5f9e3f0b2132c21d9c061b8a,Need Assessment Form_Teacher Training,b7ec42e1-7c77-11ed-bcf5-df48f63ad78b,MIGRATED 21TH Need Assessment Form_Teacher Training sourcing project +STAGE,575de5bb-e295-4546-9fc4-32f81b5ed081,75f8ad98-d888-4bac-97f4-0c273c200e7b,jay65,013535284215791616159,Dayanand Sagar,bcf2cdc0-39f3-45c0-bc1d-a8d643e05a4a,33512b57-af74-4f6b-86b0-cb7af15d1c09,dayanandyopmail.com_mslq,33512b57-af74-4f6b-86b0-cb7af15d1c09,dayanandyopmail.com_mslq,5fa28620b6bd9b757dc4e943,स्कूल सुरक्षा चेकलिस्ट,b8befc30-7c77-11ed-bcf5-df48f63ad78b,MIGRATED 21TH स्कूल सुरक्षा चेकलिस्ट sourcing project +STAGE,0c274517-d3b9-4858-9d88-b41cfcc2727e,57848f7c-f736-4781-a27f-206e663e3b4e,kiran584,01346442741037465621,cokreate,4f9703cb-05ec-4ebf-8967-ecd7a71ce346,e616ae45-ec26-41e4-85b2-146e031b3ec1,cokreate3yopmail.com_jwt1,e616ae45-ec26-41e4-85b2-146e031b3ec1,cokreate3yopmail.com_jwt1,5fa299f0b6bd9b757dc4e95c,MH01-Mantra4Change-APSWREIS School Leader Feedback,b92f2320-7c77-11ed-bcf5-df48f63ad78b,MIGRATED 21TH MH01-Mantra4Change-APSWREIS School Leader Feedback sourcing project +STAGE,2b655fd1-201d-4d2a-a1b7-9048a25c0afa,3b247583-89e3-46ae-b7f8-bea66ac7d364,shrishti26,01345815127107174426,Globe,228b25b1-a08a-4ba9-bb4f-513641924eaf,6ff8dd54-541b-4694-b1aa-b8295add8301,globeyopmail.com_b8iu,6ff8dd54-541b-4694-b1aa-b8295add8301,globeyopmail.com_b8iu,5fa2a08a9e65d8758624e977,We love reading program-Teacher Check in,b97309f0-7c77-11ed-bcf5-df48f63ad78b,MIGRATED 21TH We love reading program-Teacher Check in sourcing project +STAGE,5cc9d214-c938-4655-8ec2-c1fdab29888b,d7850ce5-7076-463e-bdf5-4e112118fa74,ram45,01347076031606784034,Google,bb8dac15-782e-42c6-a6d4-e425e7b9a1af,94409d3e-b76f-4afb-ae51-b308d87c9834,anbu2yopmail.com_kayh,94409d3e-b76f-4afb-ae51-b308d87c9834,anbu2yopmail.com_kayh,5fa3b9594ddf782be2d9600c,Teachers' Weekly Check-in Form,ba16eb11-7c77-11ed-bcf5-df48f63ad78b,MIGRATED 21TH Teachers' Weekly Check-in Form sourcing project +STAGE,9b303e72-e45b-42b6-8ef6-163092144a6b,0fd600dd-0d56-4824-85c8-02477ed0a9e0,yash172,013476546243641344142,Sony,61ee884a-bbb2-421c-91b9-7583ec3c3d8f,bc9f4f4c-7e28-4b9c-9cb3-08a84b5828a3,sonydefect1yopmail.com_2u9k,bc9f4f4c-7e28-4b9c-9cb3-08a84b5828a3,sonydefect1yopmail.com_2u9k,5fae38794ddf782be2d9603f,MH01-Mantra4Change-APSWREIS School Leader Feedback,bab96ca1-7c77-11ed-bcf5-df48f63ad78b,MIGRATED 21TH MH01-Mantra4Change-APSWREIS School Leader Feedback sourcing project +STAGE,23e491b1-7c90-4497-823e-f01f104e81f1,cc33df21-c4e4-40f0-93bb-6e7d93220b81,nehal23,013496244294746112177,Roseland,07f7c48c-b368-4f9d-a5b8-b17828a94e90,47a90330-0e47-42b3-b569-4ee0565a589d,roseland10yopmail.com_8eey,47a90330-0e47-42b3-b569-4ee0565a589d,roseland10yopmail.com_8eey,5fb74511eff083796933d5a5,Baseline Data collection form- We Love Reading Program,bd05cda1-7c77-11ed-bcf5-df48f63ad78b,MIGRATED 21TH Baseline Data collection form- We Love Reading Program sourcing project +STAGE,3eeb8aef-b167-42ae-b2a3-515772a6e946,8defd75a-a28f-4bd7-be8a-82f29dbdd3f8,ayush213,0131602658018017280,AushaOrg,46e44ca6-7571-4599-aec7-228d3b1d557e,f149cf81-8b8f-4471-b20f-f3ef65df9243,demo3org,f149cf81-8b8f-4471-b20f-f3ef65df9243,demo3org,5fbb5b34eff083796933d5cf,Baseline Data collection form- We Love Reading Program,bd7a6160-7c77-11ed-bcf5-df48f63ad78b,MIGRATED 21TH Baseline Data collection form- We Love Reading Program sourcing project +STAGE,19a22722-9e35-4f80-9412-f2668d50ec3c,0a544b58-50f9-4da0-9c03-dbbb0b9dd486,vivek87,0126796199493140480,Staging Custodian Organization,c97b9d1f-1b0e-42ef-ac9e-1b711ef7bd11,7ff56e27-4eec-475e-bb77-8457e34244a2,upsmfadmin,7ff56e27-4eec-475e-bb77-8457e34244a2,upsmfadmin,5fc72c723e9df47967eed966,Digi-Saksham Baseline Survey,ce712f30-7c77-11ed-bcf5-df48f63ad78b,MIGRATED 21TH Digi-Saksham Baseline Survey sourcing project +STAGE,0f209f21-a093-421e-b867-561d85bc011b,1b216423-6a3c-4bcb-8ca1-07e3e2d2252e,aman56,013610958661779456198,Aloe,db2fc098-b963-4c96-b2c3-8fec2478fd3e,0a48c4dd-4662-47ca-a1fd-240ede6afabf,hdy1@yopmail.com,0a48c4dd-4662-47ca-a1fd-240ede6afabf,hdy1@yopmail.com,5fc8b5bcc9de717993cb4077,Need Assessment Form_Teacher Training,cf077bc1-7c77-11ed-bcf5-df48f63ad78b,MIGRATED 21TH Need Assessment Form_Teacher Training sourcing project +STAGE,d4a823ad-3adc-4658-9b32-6011021f3633,cb67ff46-e9d5-44f8-a241-485ef6c948af,shilpi232,013655711988719616228,UPSMF,a9736891-e436-4bb2-98dd-6f6e3903b9fb,f6a8fac1-ccb7-41b1-a622-32639276d116,upsmf_pfms,f6a8fac1-ccb7-41b1-a622-32639276d116,upsmf_pfms,5fc8d9823e9df47967eed9b4,Digi-Saksham Baseline Survey,d160ae00-7c77-11ed-bcf5-df48f63ad78b,MIGRATED 21TH Digi-Saksham Baseline Survey sourcing project +STAGE,1405f334-ee59-42fc-befb-51986221881e,1405f334-ee59-42fc-befb-51986221881e,orgadmin007,01269878797503692810,Tamil Nadu,7c5a96ca-bef8-4027-8736-4fa1ae6f9180,124da479-71a8-4241-b899-877df83bd7f3,May31,124da479-71a8-4241-b899-877df83bd7f3,May31,6026743a262f8023e110727f,Enrollment challenges in DIKSHA Courses-1613132857976,,Migrated Enrollment challenges in DIKSHA Courses-1613132857976) \ No newline at end of file diff --git a/scripts/creation-portal-migration/api-list/headers.js b/scripts/creation-portal-migration/api-list/headers.js index 60f4d80e..6d070fce 100644 --- a/scripts/creation-portal-migration/api-list/headers.js +++ b/scripts/creation-portal-migration/api-list/headers.js @@ -16,16 +16,20 @@ const genToken = async (url, body, type) => { logger.error(`Error while generateToken Error: ${JSON.stringify(err?.response?.data)}`) return err; }); - return res ? res.data.access_token : ""; + return res ? res?.data?.access_token : ""; } else { - const token = type === "ed" ? this.ed_token : this.creation_portal_token; + const token = this.ed_token; + // console.log("rbdsnbnsf", this.ed_token); + // type === "ed" ? this.ed_token : this.ed_token + // this.creation_portal_token; return token; } }; const validateToken = (type) => { - const token = type === "ed" ? this.ed_token : this.creation_portal_token; + const token = type === "ed" ? this.ed_token : this.ed_token; + // creation_portal_token; try { if (token) { @@ -44,15 +48,15 @@ const validateToken = (type) => { const generateToken = async (type) => { let url = ""; let body = {}; - + url = CONFIG.HOST.ed + CONFIG.APIS.token; switch (type) { case "ed": - url = CONFIG.HOST.ed + CONFIG.APIS.token; + // url = CONFIG.HOST.ed + CONFIG.APIS.token; body = querystring.stringify({ ...CONFIG.KEYS.ED.QUERY }); this.ed_token = await genToken(url, body, "ed"); return this.ed_token; case "creation_portal": - url = CONFIG.HOST.creation_portal + CONFIG.APIS.token; + // url = CONFIG.HOST.creation_portal + CONFIG.APIS.token; body = querystring.stringify({ ...CONFIG.KEYS.CREATION_PORTAL.QUERY }); this.creation_portal_token = await genToken(url, body, "creation_portal"); return this.creation_portal_token; diff --git a/scripts/creation-portal-migration/api-list/program.js b/scripts/creation-portal-migration/api-list/program.js index 7673945d..c10462c0 100644 --- a/scripts/creation-portal-migration/api-list/program.js +++ b/scripts/creation-portal-migration/api-list/program.js @@ -1,4 +1,5 @@ const { default: axios } = require("axios"); +const { request } = require("chai"); const { CONFIG } = require("../constant/config"); const { getHeaders } = require("./headers"); @@ -18,7 +19,8 @@ const createProgram = async (templateData) => { data: data, }; - const res = await axios(config); + const res = await axios(config).catch(err => {}); + return res?.data?.result?.program_id; }; @@ -56,7 +58,7 @@ const publishProgram = async (templateData) => { const res = await axios(config); return res.data; }; -const nominateProgram = async (program_id, author) => { +const nominateProgram = async (program_id, orgAdmin) => { const url = CONFIG.HOST.creation_portal + CONFIG.APIS.add_program_nomination; const data = { @@ -64,6 +66,7 @@ const nominateProgram = async (program_id, author) => { program_id: program_id, status: "Pending", collection_ids: [], + createdby: orgAdmin?.srcOrgAdminId, targetprimarycategories: [ { name: "Observation", @@ -77,8 +80,8 @@ const nominateProgram = async (program_id, author) => { }, ], content_types: [], - organisation_id: process.env.DEFAULT_PROGRAM_CREATOR_ORGANISATION_ID, - user_id: process.env.DEFAULT_USER_ID_TO_ADD_CONTRIBUTOR, + organisation_id: orgAdmin?.org_id, + user_id: orgAdmin?.mappedUserId|| process.env.DEFAULT_CONTRIBUTOR_ORG_ADMIN_ID, }, }; const config = { diff --git a/scripts/creation-portal-migration/api-list/user.js b/scripts/creation-portal-migration/api-list/user.js index 7442ac26..bdc21dc6 100644 --- a/scripts/creation-portal-migration/api-list/user.js +++ b/scripts/creation-portal-migration/api-list/user.js @@ -6,17 +6,18 @@ const { getHeaders } = require("./headers"); const readUser = async (userId) => { const params = "organisations,roles,locations,declarations,externalIds"; const url = - CONFIG.HOST.ed + - CONFIG.APIS.read_user + - userId + - "?fields=" + - params; - - const res = await axios - .get(url, await getHeaders(true, "ed")) - .catch((err) => { - logger.error(`Error while reading User: ${JSON.stringify(err?.response?.data)}`) - }); + CONFIG.HOST.ed + CONFIG.APIS.read_user + userId + "?fields=" + params; + + const config = { + method: "get", + url: url.trim(), + headers: await getHeaders(true, "ed"), + }; + + const res = await axios(config).catch((err) => { + logger.error(`Error while reading User: ${JSON.stringify(err)}`); + }); + return res.data?.result?.response; }; const searchUser = async (userId) => { @@ -31,12 +32,48 @@ const searchUser = async (userId) => { }; const res = await axios(config).catch((err) => { - logger.error(`Error while searching User: ${JSON.stringify(err?.response?.data)}`) + logger.error( + `Error while searching User: ${JSON.stringify(err?.response?.data)}` + ); }); - return res.data?.result?.response?.content; + return res?.data?.result?.response?.content; }; +const getOpenSaberUserOrgId = async () => { + const query = { + id: "open-saber.registry.search", + ver: "1.0", + ets: "11234", + params: { + did: "", + key: "", + msgid: "", + }, + request: { + entityType: ["User_Org"], + filters: {}, + limit: 10000, + offset: 0, + }, + }; + const url = CONFIG.HOST.creation_portal + CONFIG.APIS.open_saber_user_org_search; + const config = { + method: "post", + url: url, + headers: await getHeaders(true, "creation_portal"), + data: query + }; + + const res = await axios(config).catch((err) => { + logger.error( + `Error while searching User: ${JSON.stringify(err?.response?.data)}` + ); + }); + return res?.data?.result?.Org || [] +} + module.exports = { readUser, searchUser, + getOpenSaberUserOrgId }; diff --git a/scripts/creation-portal-migration/constant/config.js b/scripts/creation-portal-migration/constant/config.js index b5a2ceaf..548b4ff2 100644 --- a/scripts/creation-portal-migration/constant/config.js +++ b/scripts/creation-portal-migration/constant/config.js @@ -17,8 +17,9 @@ const CONFIG = { }, APIS: { token: process.env.GEN_TOKEN || "auth/realms/sunbird/protocol/openid-connect/token", - read_user: process.env.READ_USER || "api/user/v5/read/", + read_user: process.env.READ_USER || "api/user/v5/read", search_user: process.env.SEARCH_USER || "api/user/v3/search", + open_saber_user_org_search: process.env.OPEN_SABER_USER_ORG_SEARCH || "api/reg/search", create_questionset: process.env.CREATE_QUESTION_SET || "api/questionset/v1/create", update_hierarchy: process.env.UPDATE_QUESTION_SET_HIERARCHY || "api/questionset/v1/hierarchy/update", publish_questionset: process.env.PUBLISH_QUESTION_SET || "api/questionset/v1/publish", diff --git a/scripts/creation-portal-migration/db/dbConfig.js b/scripts/creation-portal-migration/db/dbConfig.js index dca169f3..1e2eb679 100644 --- a/scripts/creation-portal-migration/db/dbConfig.js +++ b/scripts/creation-portal-migration/db/dbConfig.js @@ -9,7 +9,10 @@ const connect = async () => { const Conn = mongoose.createConnection(); // connect to database console.log("MONGODB_URL", CONFIG.DB.DB_HOST) - this.database = await Conn.openUri(CONFIG.DB.DB_HOST); + this.database = await Conn.openUri(CONFIG.DB.DB_HOST, { + useNewUrlParser: true, + useUnifiedTopology: true, + }); Conn.on("error", console.error.bind(console, "connection error:")); } catch (err) { console.log("Error While connecting to DB", err); diff --git a/scripts/creation-portal-migration/logger.js b/scripts/creation-portal-migration/logger.js index fe9d25ab..8f25b11d 100644 --- a/scripts/creation-portal-migration/logger.js +++ b/scripts/creation-portal-migration/logger.js @@ -5,11 +5,15 @@ var Logger = require("bunyan"); const date = new Date().getDate() + "-" + - new Date().getMonth() + + (new Date().getMonth() + 1) + "-" + new Date().getFullYear(); -fs.existsSync("logs") || fs.mkdirSync("logs"); + var dir = __dirname + '/logs'; + + if (!fs.existsSync(dir)) { + fs.mkdirSync(dir, { recursive: true }); + } ; var logger = new Logger({ name: "creation-portal-migration", diff --git a/scripts/creation-portal-migration/orgDetails.js b/scripts/creation-portal-migration/orgDetails.js new file mode 100644 index 00000000..8aeb34cb --- /dev/null +++ b/scripts/creation-portal-migration/orgDetails.js @@ -0,0 +1,80 @@ +require("dotenv").config({ path: "./../../.env" }); +const { createDBInstance } = require("./db/dbConfig"); +const { findAll } = require("./db"); +const { CONFIG } = require("./constant/config"); +const { compact, uniq } = require("lodash"); +const fs = require("fs"); +const { searchUser, getOpenSaberUserOrgId } = require("./api-list/user"); + +const getUserIds = async () => { + try { + const db = await createDBInstance(); + + console.log("./../creation-portal-migration", db); + const data = await findAll(CONFIG.DB.TABLES.solutions, { + programId: { $exists: true }, + type: { $in: ["observation", "survey"] }, + }); + + const userIds = data.map((solution) => solution.author); + const solutions = data.map((solution) => { + return { + userId: solution.author, + solutionId: solution._id, + solutionName: solution.name, + programId: solution?.migrationReference?.sourcingProgramId || "", + programName: `Migrated ${solution.name}`, + }; + }); + const uniqUsers = compact([...new Set(userIds)]); + const usersList = await searchUser(uniqUsers); + const openSaberOrg = await getOpenSaberUserOrgId(); + const d = writeToCSVFile(usersList, uniqUsers, openSaberOrg, solutions); + console.log(`\n migratedCount userIds`, d); + } catch (err) { + console.log(`Error while migrating : ${err}`); + throw new Error("Error occured", err); + } +}; + +writeToCSVFile = (users, usersIdsInDb, openSaberOrg, solutions) => { + const filename = __dirname + "/SL-DataMapping.csv"; + fs.writeFile( + filename, + extractAsCSV(users, usersIdsInDb, openSaberOrg, solutions), + (err) => { + if (err) { + console.log("Error writing to csv file", err); + } else { + console.log(`saved as ${filename}`); + } + } + ); +}; + +const extractAsCSV = (users, usersIdsInDb, openSaberOrg, solutions) => { + const header = [ + `ENV,authorId,mappedUserId,userName,rootOrgId,rootOrgName,org_id,srcOrgAdminId,srcOrgAdminUserName,contributorOrgAdminId,contributorOrgAdminUserName,solutionId,solutionName,programId,programName`, + ]; + + let rows = usersIdsInDb.map((id) => { + const match = users.find((user) => user.userId === id); + const solution = solutions.find((s) => s.userId === id); + console.log("solutionsolution", solution); + const org = openSaberOrg.find((o) => o.createdBy === id); + return `STAGE,${id},${match?.userId || ""},${match?.userName || ""},${ + match?.rootOrgId || "" + },${match?.rootOrgName || ""},${org?.orgId || ""},${""},${""},${""},${""},${ + solution?.solutionId || "" + },${solution?.solutionName || ""},${solution?.programId || ""},${ + solution?.programName || "" + }`; + }); + + rows = uniq(rows); + rows = compact(rows); + const d = header.concat(rows).join("\n"); + return d; +}; + +getUserIds(); diff --git a/scripts/creation-portal-migration/template/config/question.js b/scripts/creation-portal-migration/template/config/question.js index 25564843..abdcb900 100644 --- a/scripts/creation-portal-migration/template/config/question.js +++ b/scripts/creation-portal-migration/template/config/question.js @@ -172,6 +172,7 @@ const questionTemplate = { name: "Migrated Question", code: "externalId", description: "", + showRemarks: "showRemarks", mimeType: "application/vnd.sunbird.question", primaryCategory: "Multiselect Multiple Choice Question", interactionTypes: ["choice"], diff --git a/scripts/creation-portal-migration/template/config/questionSet.js b/scripts/creation-portal-migration/template/config/questionSet.js deleted file mode 100644 index 4059b946..00000000 --- a/scripts/creation-portal-migration/template/config/questionSet.js +++ /dev/null @@ -1,76 +0,0 @@ -const questionSetTemplate = { - name: "name", - description: "description", - code: "externalId", - mimeType: "application/vnd.sunbird.questionset", - primaryCategory: "type", - entityType: "entityType", - language: "language", - keywords: "keywords", - startDate: "startDate", - endDate: "endDate", - createdBy: process.env.DEFAULT_CONTRIBUTOR_USER_ID, - organisationId: process.env.DEFAULT_PROGRAM_CREATOR_ORGANISATION_ID, - creator: process.env.DEFAULT_CONTRIBUTOR_USER_NAME, - createdFor: [process.env.DEFAULT_CONTRIBUTOR_USER_CHANNEL_ID], - channel: process.env.DEFAULT_CONTRIBUTOR_USER_CHANNEL_ID, - programId: "", - author: process.env.DEFAULT_CONTRIBUTOR_USER_NAME, - framework: process.env.DEFAULT_FRAMEWORK_ID, -}; - -const questionSetTemplateStatic = [ - "mimeType", - "createdBy", - "creator", - "author", - "organisationId", - "framework", - "channel", - "createdFor" -]; - -const program = { - name: `{solutionName} sourcing project`, - description: `{solutionName} sourcing project description`, - nomination_enddate: "", - shortlisting_enddate: "", - enddate: "", - content_submission_enddate: "", - rewards: null, - content_types: [], - target_collection_category: [], - sourcing_org_name: "", - type: "public", - target_type: "searchCriteria", - rootorg_id: process.env.DEFAULT_USER_CHANNEL_ID_TO_CREATE_PROGRAM, - createdby: process.env.DEFAULT_USER_ID_TO_CREATE_PROGRAM, - createdOn: "", - startdate: "", - slug: process.env.DEFAULT_SLUG, - status: "Draft", - program_id: "", - config: { - defaultContributeOrgReview: false, - }, -}; -const programTemplateStatic = [ - "rewards", - "content_types", - "target_collection_category", - "type", - "target_type", - "rootorg_id", - "createdby", - "slug", - "status", - "program_id", - "config", -]; - -module.exports = { - questionSetTemplate, - questionSetTemplateStatic, - program, - programTemplateStatic, -}; diff --git a/scripts/creation-portal-migration/template/generate/gProgram.js b/scripts/creation-portal-migration/template/generate/gProgram.js index 63d90a9b..50d6b3ea 100644 --- a/scripts/creation-portal-migration/template/generate/gProgram.js +++ b/scripts/creation-portal-migration/template/generate/gProgram.js @@ -1,4 +1,4 @@ -const { isEmpty } = require("lodash"); +const { isEmpty, pick, has } = require("lodash"); const { createProgram, updateProgram, @@ -6,8 +6,11 @@ const { nominateProgram, updateContributorToProgram, } = require("../../api-list/program"); -const { searchUser } = require("../../api-list/user"); const logger = require("../../logger"); +const { + getContributorAndSrcAdminData, + updateCsvFile, +} = require("../helpers/csvHelper"); const { updateSolutionById } = require("../helpers/questionsetHelper"); const getDate = (increment) => { @@ -19,17 +22,34 @@ const getDate = (increment) => { }; const createProgramTemplate = async (solution, program_id, migratedCount) => { - const userData = await searchUser(solution.author); + const userData = await getContributorAndSrcAdminData(solution, program_id); + + if (!userData?.srcOrgAdmin) { + return; + } + if (has(solution, 'migrationReference')) { + solution.migrationReference.sourcingProgramId = userData?.srcOrgAdmin?.programId.trim() ? userData?.srcOrgAdmin?.programId.trim() : solution?.migrationReference?.sourcingProgramId; + } else { + const programId = userData?.srcOrgAdmin?.programId.trim() ? userData?.srcOrgAdmin?.programId.trim() : solution?.migrationReference?.sourcingProgramId; + solution = { + ...solution, + migrationReference:{ + sourcingProgramId: programId + } + } + } - const userId = - userData?.length > 0 - ? solution.author - : process.env.DEFAULT_USER_ID_TO_CREATE_PROGRAM; - const rootOrgId = - userData?.length > 0 ? userData[0]?.rootOrgId : process.env.DEFAULT_CONTRIBUTOR_USER_CHANNEL_ID; + const id = `${solution._id}`; + const userId = userData?.srcOrgAdmin + ? userData?.srcOrgAdmin?.srcOrgAdminId + : process.env.DEFAULT_SRC_ORG_ADMIN_TO_CREATE_PROGRAM; + const rootOrgId = userData?.srcOrgAdmin + ? userData?.srcOrgAdmin?.rootOrgId + : process.env.DEFAULT_SRC_ORG_ADMIN_ROOT_ORG_ID; + const months = ["JAN", "FEB", "MAR", "APR", "MAY", "JUNE", "JULY", "AUG", "SEP", "OCT", "NOV", "DEC"]; const template = { - name: `Migrated ${solution?.name} sourcing project`, + name: `MIGRATED ${months[new Date().getMonth()]} ${new Date().getDate()} ${new Date().getFullYear()} ${solution?.name} sourcing project`, description: `${solution?.name} sourcing project description`, nomination_enddate: `${getDate(1)}`, rewards: null, @@ -40,7 +60,9 @@ const createProgramTemplate = async (solution, program_id, migratedCount) => { target_type: "searchCriteria", content_types: [], target_collection_category: [], - sourcing_org_name: process.env.DEFAULT_USER_SOURCING_ORG_NAME_TO_CREATE_PROGRAM, + sourcing_org_name: + userData?.srcOrgAdmin?.rootOrgName || + process.env.DEFAULT_USER_SRC_ORG_NAME_TO_CREATE_PROGRAM, rootorg_id: rootOrgId, createdby: userId, createdOn: `${getDate(0)}`, @@ -74,18 +96,29 @@ const createProgramTemplate = async (solution, program_id, migratedCount) => { let query = {}; if (!program_id) { - programId = await createProgram(template).catch((err) => { - logger.error(`Error while creating program for solution_id: ${ - solution?._id - } Error: - ${JSON.stringify(err.response.data)}`); - migratedCount.failed.program.migrated.count++; - if (!migratedCount.failed.program.migrated.ids.includes(id)) { - migratedCount.failed.program.migrated.ids.push(id); - } - // updateFailedCount(migratedCount, "migrated", solution?._id); - }); - if (!programId) { + if (isEmpty(programId)) { + programId = await createProgram(template).catch((err) => { + logger.error(`Error while creating program for solution_id: ${ + solution?._id + } Error: + ${JSON.stringify(err?.response?.data)}`); + migratedCount.failed.program.migrated.count++; + if (!migratedCount.failed.program.migrated.ids.includes(id)) { + migratedCount.failed.program.migrated.ids.push(id); + } + }); + programName = `` + programId && + (await updateCsvFile( + userData.csvData, + userData.srcOrgAdmin, + programId, + template?.name + )); + } else { + migratedCount.success.program.existing.migrated++; + } + if (isEmpty(programId)) { return; } logger.info( @@ -119,7 +152,7 @@ const createProgramTemplate = async (solution, program_id, migratedCount) => { } if (!solution?.migrationReference?.isSrcProgramPublished) { - const pub_res = await publishProgramTemplate(programId, solution?._id); + const pub_res = await publishProgramTemplate(programId, solution?._id, userData.srcOrgAdmin); if (!pub_res) { migratedCount.failed.program.published.count++; @@ -143,17 +176,20 @@ const createProgramTemplate = async (solution, program_id, migratedCount) => { } if (!solution?.migrationReference?.isNominated) { - const res = await nominateProgram(programId, userId).catch((err) => { - // updateFailedCount(migratedCount, "nominated", solution?._id); - migratedCount.failed.program.nominated.count++; - if (!migratedCount.failed.program.nominated.ids.includes(id)) { - migratedCount.failed.program.nominated.ids.push(id); + console.log(); + const res = await nominateProgram(programId, userData?.srcOrgAdmin).catch( + (err) => { + // updateFailedCount(migratedCount, "nominated", solution?._id); + migratedCount.failed.program.nominated.count++; + if (!migratedCount.failed.program.nominated.ids.includes(id)) { + migratedCount.failed.program.nominated.ids.push(id); + } + logger.error(`Error while nominating program for solution_id: ${ + solution?._id + } Error: + ${JSON.stringify(err?.response?.data)}`); } - logger.error(`Error while nominating program for solution_id: ${ - solution?._id - } Error: - ${JSON.stringify(err.response.data)}`); - }); + ); if (!res) { await updateSolutionDb(query, solution, migratedCount); return; @@ -170,9 +206,14 @@ const createProgramTemplate = async (solution, program_id, migratedCount) => { if (!solution?.migrationReference?.isContributorAdded) { const add_contri = { program_id: programId, - user_id: process.env.DEFAULT_USER_ID_TO_ADD_CONTRIBUTOR, + user_id: + userData?.srcOrgAdmin?.contributorOrgAdminId || + process.env.DEFAULT_CONTRIBUTOR_ORG_ADMIN_ID, rolemapping: { - CONTRIBUTOR: [process.env.DEFAULT_CONTRIBUTOR_USER_ID], + CONTRIBUTOR: [ + userData?.mappedUserId || + process.env.DEFAULT_CONTRIBUTOR_USER_ID, + ], }, }; @@ -186,7 +227,7 @@ const createProgramTemplate = async (solution, program_id, migratedCount) => { logger.error(`Error while adding contributor program for solution_id: ${ solution?._id } Error: - ${JSON.stringify(err.response.data)}`); + ${JSON.stringify(err?.response?.data)}`); } ); @@ -206,16 +247,18 @@ const createProgramTemplate = async (solution, program_id, migratedCount) => { if (!solution?.migrationReference?.isContributorAccepted) { const accept_contri = { program_id: programId, - user_id: process.env.DEFAULT_USER_ID_TO_ADD_CONTRIBUTOR, + user_id: + userData?.srcOrgAdmin?.mappedUserId || + process.env.DEFAULT_CONTRIBUTOR_ORG_ADMIN_ID, status: "Approved", updatedby: userId, }; - const update_nom = await updateContributorToProgram(accept_contri).catch( + const update_nom = await updateContributorToProgram(accept_contri).catch( (err) => { logger.error(`Error while accepting nomination to the program for solution_id: ${ solution?._id } Error: - ${JSON.stringify(err.response.data)}`); + ${JSON.stringify(err?.response?.data)}`); migratedCount.failed.program.accepted.count++; if (!migratedCount.failed.program.accepted.ids.includes(id)) { migratedCount.failed.program.accepted.ids.push(id); @@ -240,8 +283,7 @@ const createProgramTemplate = async (solution, program_id, migratedCount) => { if (!isEmpty(query)) { await updateSolutionDb(query, solution, migratedCount); } - - return programId; + return { programId, contributor: userData?.srcOrgAdmin }; }; const updateSolutionDb = async (query, solution, migratedCount) => { @@ -274,7 +316,6 @@ const updateSolutionDb = async (query, solution, migratedCount) => { } }; - const updateProgramTemplate = async (program_id, solution) => { const template = { config: { @@ -297,9 +338,9 @@ const updateProgramTemplate = async (program_id, solution) => { framework: [process.env.DEFAULT_FRAMEWORK_ID], frameworkObj: { code: process.env.DEFAULT_FRAMEWORK_ID, - name: process.env.DEFAULT_FRAMEWORK_ID, + name: process.env.DEFAULT_FRAMEWORK_NAME, type: process.env.DEFAULT_FRAMEWORK_TYPE, - identifier: process.env.DEFAULT_FRAMEWORK, + identifier: process.env.DEFAULT_FRAMEWORK_ID, }, sharedContext: [], }, @@ -323,20 +364,19 @@ const updateProgramTemplate = async (program_id, solution) => { logger.error(`Error while updating program for solution_id: ${ solution?._id } Error: - ${JSON.stringify(err.response.data)}`); + ${JSON.stringify(err?.response?.data)}`); }); return upd_res; }; -const publishProgramTemplate = async (program_id, id) => { +const publishProgramTemplate = async (program_id, id, contributor) => { const template = { - channel: "sunbird", + channel: process.env.DEFAULT_SLUG, program_id: program_id, }; - return await publishProgram(template).catch((err) => { logger.error(`Error while publishing program for solution_id: ${id} Error: - ${JSON.stringify(err.response.data)}`); + ${JSON.stringify(err?.response?.data)}`); }); }; diff --git a/scripts/creation-portal-migration/template/generate/gQuestion.js b/scripts/creation-portal-migration/template/generate/gQuestion.js index 9d51b893..65d7ddce 100644 --- a/scripts/creation-portal-migration/template/generate/gQuestion.js +++ b/scripts/creation-portal-migration/template/generate/gQuestion.js @@ -36,7 +36,9 @@ const getDateTemplate = (question) => { }; } else if (keyL === "interactions") { template[key] = { - validation: question["validation"], + validation: { + required: question["validation"]["required"] ? 'Yes' : 'No', + }, response1: { validation: { pattern: question["dateFormat"], @@ -80,7 +82,7 @@ const getSliderTemplate = (question) => { } else if (keyL === "interactions") { template[key] = { validation: { - required: question["validation"]["required"], + required: question["validation"]["required"] ? 'Yes' : 'No', }, response1: { validation: { @@ -150,7 +152,9 @@ const getMSMCQTemplate = (question) => { }; } else if (keyL === "interactions") { template[key] = { - validation: question["validation"], + validation: { + required: question["validation"]["required"] ? 'Yes' : 'No', + }, response1: { type: "choice", options: getOptions(question["options"]), @@ -174,8 +178,9 @@ const getMSMCQTemplate = (question) => { const getMCQTemplate = (question) => { const template = {}; - console.log("getMcq"); - + console.log(); + console.log("getMcq", JSON.stringify(questionTemplate.mcq)); + console.log(); for (let key in questionTemplate.mcq) { const keyL = key.toLowerCase(); @@ -192,7 +197,9 @@ const getMCQTemplate = (question) => { }; } else if (keyL === "interactions") { template[key] = { - validation: question["validation"], + validation: { + required: question["validation"]["required"] ? 'Yes' : 'No', + }, response1: { type: "choice", options: getOptions(question["options"]), @@ -234,7 +241,7 @@ const getTextTemplate = (question, type) => { } else if (keyL === "interactions") { template[key] = { validation: { - required: question["validation"]["required"], + required: question["validation"]["required"] ? 'Yes' : 'No', }, response1: { validation: { @@ -264,7 +271,6 @@ const getTextTemplate = (question, type) => { template[key] = question[questionTemplate.text[key]] || ""; } } - return template; }; diff --git a/scripts/creation-portal-migration/template/generate/gQuestionSet.js b/scripts/creation-portal-migration/template/generate/gQuestionSet.js index ada795ae..c477c402 100644 --- a/scripts/creation-portal-migration/template/generate/gQuestionSet.js +++ b/scripts/creation-portal-migration/template/generate/gQuestionSet.js @@ -12,35 +12,43 @@ const { createSection } = require("../migrate/matrix"); const { getNonMatrixQuestions } = require("../migrate/nonmatrix"); const getQuestionSetTemplates = async (solutions, migratedCount) => { - const data = Promise.all( - solutions.map(async (solution) => { + const data = []; + // solutions.map(async (solution) => { + for (let solution of solutions) { let programId = solution?.migrationReference?.sourcingProgramId; - programId = await createProgramTemplate( + const programData = await createProgramTemplate( solution, programId, migratedCount - ); + ).catch(error => { + console.log("Errror", error); + }); + programId = programData?.programId; + solution.author = programData?.contributor?.mappedUserId ? programData?.contributor?.mappedUserId : solution.author; logger.debug( `-----------------------sourcingProgramId---------------------- ${programId}` ); - - if (!programId) { - return; + // if (!programId) { + // return; + // } + // data.push(programId) + // return; + if (programId) { + data.push(await migrateQuestionset(solution, programId, migratedCount, programData?.contributor)); } - - return await migrateQuestionset(solution, programId, migratedCount); - }) - ); + // }) + } + return data; }; -const migrateQuestionset = async (solution, programId, migratedCount) => { +const migrateQuestionset = async (solution, programId, migratedCount, contributor) => { logger.debug( `-----------------------migrateQuestionset---------------------- ${programId}` ); - let templateData = setQuestionSetTemplate(solution, programId); + let templateData = setQuestionSetTemplate(solution, programId, contributor); const questionSetId = solution?._id.toString(); let questionSetMigratedId = solution.referenceQuestionSetId; @@ -50,11 +58,11 @@ const migrateQuestionset = async (solution, programId, migratedCount) => { } else { questionSetMigratedId = await createQuestionSet(templateData).catch( (err) => { - logger.error(`migrateQuestionset: Error while creating Questionset for solution_id: ${questionsetid} Error: + logger.error(`migrateQuestionset: Error while creating Questionset for solution_id: ${questionSetId} Error: ${JSON.stringify(err?.response?.data)}`); - if (!migratedCount.failed.questionSet.migrated.ids.includes(id)) { + if (!migratedCount.failed.questionSet.migrated.ids.includes(questionSetId)) { migratedCount.failed.questionSet.migrated.count++; - migratedCount.failed.questionSet.migrated.ids.push(id); + migratedCount.failed.questionSet.migrated.ids.push(questionSetId); } } ); diff --git a/scripts/creation-portal-migration/template/helpers/csvHelper.js b/scripts/creation-portal-migration/template/helpers/csvHelper.js new file mode 100644 index 00000000..a9d0a8e1 --- /dev/null +++ b/scripts/creation-portal-migration/template/helpers/csvHelper.js @@ -0,0 +1,95 @@ +const { pick } = require("lodash"); + +const { searchUser } = require("../../api-list/user"); + +const fs = require("fs"); +let { parse } = require("csv-parse"); +const csvtojson = require("csvtojson"); +const path = require("path"); +const { dirname } = require("path"); + + +const getCsvData = async (solution) => { + const filename = path.resolve(dirname("creation-portal-migration")) + "/SL-DataMapping.csv"; + console.log("fulasvnbsvfbndvfd", filename); + let srcOrgAdmin = []; + return new Promise(async (resolve, reject) => { + const data = []; + let index = 0; + fs.createReadStream(filename) + .pipe(parse()) + .on("error", (error) => { + reject(error); + }) + .on("data", (row) => { + index = data.length; + index === 0 && srcOrgAdmin.push(row); + data.push(row); + const csvUserIndex = data[0].indexOf("authorId"); + const csvUserId = row[csvUserIndex]; + const pIndex = data[0].indexOf("programId"); + + if (index > 0) { + if (solution.author === csvUserId) { + srcOrgAdmin.push(row); + if (!row[pIndex]) { + row[pIndex] = + solution?.migrationReference?.sourcingProgramId || ""; + } + } + } + }) + .on("end", () => { + const header = data; + const d = header.join("\n"); + srcOrgAdmin = srcOrgAdmin.join("\n"); + fs.writeFileSync(filename, d, (err) => { + if (err) { + reject(err); + } + }); + resolve({ data: d, srcOrgAdmin }); + }); + }); +}; + +const updateCsvFile = async (csvData = [], columnToUpdate, programId, programName) => { + const filename = __dirname + "/SL-DataMapping.csv"; + const data = await csvtojson().fromString(csvData); + + let header = Object.keys(data[0]); + const csvD = []; + data.forEach((d) => { + d?.rootOrgId === columnToUpdate?.rootOrgId && (d.programId = programId, d.programName=programName); + csvD.push(Object.values(d)); + }); + + csvD.unshift(header); + const d = csvD.join("\n"); + console.log(); + console.log(); + + fs.writeFileSync(filename, d, (err) => { + if (err) { + console.log("errororor", err); + } + }); +}; + +const getContributorAndSrcAdminData = async (solution, program_id) => { + // const userData = await searchUser(solution.author).catch((error) => { + // console.log("Error", error); + // }); + // const contributor = pick(userData[0], ["userId", "userName", "rootOrgId"]); + // contributor.rootOrgId = "01329314824202649627"; + // const csv = await getCsvData(solution, contributor?.rootOrgId); + const csv = await getCsvData(solution); + const srcOrgAdmin = await csvtojson().fromString(csv.srcOrgAdmin); + return { csvData: csv.data, srcOrgAdmin: srcOrgAdmin[0] }; +}; + +module.exports = { + getCsvData, + updateCsvFile, + getContributorAndSrcAdminData, +}; diff --git a/scripts/creation-portal-migration/template/helpers/hierarchyHelper.js b/scripts/creation-portal-migration/template/helpers/hierarchyHelper.js index 47352a41..c40a412a 100644 --- a/scripts/creation-portal-migration/template/helpers/hierarchyHelper.js +++ b/scripts/creation-portal-migration/template/helpers/hierarchyHelper.js @@ -6,6 +6,7 @@ const { find, omit, isArray, + uniq, } = require("lodash"); const { publishQuestionSet, @@ -82,9 +83,9 @@ const getPrecondition = (visible, parentId, parentQuestion) => { var: `${parentId}.response1.value`, type: "interactions", }, - findIndex(parentQuestion.options, { + [findIndex(parentQuestion.options, { value: isArray(visible?.value) ? visible?.value[0] : visible?.value, - }), + })], ], }, ], @@ -103,7 +104,8 @@ const updateHierarchyTemplate = async ( const branchingQuestionSetHierarchy = async (hierarchy, newCriterias) => { - logger.debug("branchingQuestionSetHierarchy", hierarchy) + logger.debug("branchingQuestionSetHierarchy", JSON.stringify(hierarchy)); + let questionSetHierarchy = {}; if (hierarchy.questionset && hierarchy.isHierarchyUpdated) { @@ -176,8 +178,8 @@ const branchingQuestionSetHierarchy = async (hierarchy, newCriterias) => { } } - console.log("branchingLogic", JSON.stringify(updateHierarchyData)) + updateHierarchyData.request.data.hierarchy[hierarchy.questionset].children = uniq(updateHierarchyData.request.data.hierarchy[hierarchy.questionset].children); return updateHierarchyData; }; @@ -230,9 +232,6 @@ const updateCriteriasList = async ( }, }; - console.log(); - console.log("5f3fcd6daf0a4decfa9a10bd5f3fcd6daf0a4decfa9a10bd", JSON.stringify(hierarchy)); - console.log(); let pageSections = {}; const newCriterias = []; @@ -352,13 +351,10 @@ const updateCriteriasList = async ( )}` ); - console.log(); - console.log("updateHierarchyTemplate", JSON.stringify(updateHierarchyData)); - console.log(); - const questionsetId = hierarchy.questionsetDbId; let query = {}; if (!hierarchy.isHierarchyUpdated) { + updateHierarchyData.request.data.hierarchy[hierarchy.questionset].children = uniq(updateHierarchyData.request.data.hierarchy[hierarchy.questionset].children); const result = await updateQuestionSetHierarchy(updateHierarchyData).catch( (err) => { logger.error(`Error while updating the questionset for solution_id: ${questionsetId} Error: @@ -418,7 +414,6 @@ const updateCriteriasList = async ( } ); - console.log("Branchinfnfnfnffn", result) if (!result) { await updateSolutionsDb(query, questionsetId, migratedCount); return; @@ -506,7 +501,6 @@ const updatePageData = ( if (pageName && isBranching) { const branching = criteria.branchingLogic[idToAdd]; - console.log("idtoAdddd", idToAdd, criteria?.name, criteria?.isMatrix, criteria.branchingLogic); if (!pageSections[pageName].branchingLogic.hasOwnProperty(idToAdd)) { pageSections[pageName].branchingLogic = { diff --git a/scripts/creation-portal-migration/template/helpers/questionsetHelper.js b/scripts/creation-portal-migration/template/helpers/questionsetHelper.js index aa406278..09e637b5 100644 --- a/scripts/creation-portal-migration/template/helpers/questionsetHelper.js +++ b/scripts/creation-portal-migration/template/helpers/questionsetHelper.js @@ -4,10 +4,6 @@ const { createQuestions, publishQuestion } = require("../../api-list/question"); const { CONFIG } = require("./../../constant/config"); const { updateById, findAll } = require("../../db"); const logger = require("../../logger"); -const { - questionSetTemplate, - questionSetTemplateStatic, -} = require("../config/questionSet"); const { getDateTemplate, getSliderTemplate, @@ -16,28 +12,33 @@ const { getTextTemplate, } = require("../generate/gQuestion"); -const setQuestionSetTemplate = (solution, programId) => { - let templateData = {}; - for (let key in questionSetTemplate) { - if (key === "programId") { - templateData[key] = programId; - } else if (questionSetTemplateStatic.includes(key)) { - templateData[key] = questionSetTemplate[key]; - } else if (key === "organisationId") { - if ( - solution[questionSetTemplate[key]] && - solution[questionSetTemplate[key]].length >= 1 - ) { - templateData[key] = solution[questionSetTemplate[key]][0]; - } else { - templateData[key] = solution[questionSetTemplate[key]]; - } - } else if (key.toLowerCase() === "entitytype") { - templateData[key] = capitalize(solution[questionSetTemplate[key]]) || ""; - } else { - templateData[key] = solution[questionSetTemplate[key]] || ""; - } - } +const setQuestionSetTemplate = (solution, programId, contributor) => { + let templateData = { + name: solution?.name, + description: solution?.description, + code: solution?.externalId, + mimeType: "application/vnd.sunbird.questionset", + primaryCategory: solution?.type, + entityType: capitalize(solution?.entityType), + language: solution?.language, + keywords: solution?.keywords, + startDate: solution?.startDate, + endDate: solution?.endDate, + createdBy: + solution?.author || process.env.DEFAULT_CONTRIBUTOR_USER_ID, + organisationId: + contributor?.org_id || process.env.DEFAULT_SRC_ORG_ADMIN_ORG_ID, + creator: contributor?.userName || process.env.DEFAULT_CONTRIBUTOR_USER_NAME, + createdFor: [ + contributor?.rootOrgId || process.env.DEFAULT_SRC_ORG_ADMIN_ROOT_ORG_ID, + ], + channel: + contributor?.rootOrgId || process.env.DEFAULT_SRC_ORG_ADMIN_ROOT_ORG_ID, + programId: programId, + author: contributor?.userName || process.env.DEFAULT_CONTRIBUTOR_USER_NAME, + framework: process.env.DEFAULT_FRAMEWORK_ID, + }; + return templateData; }; @@ -68,10 +69,11 @@ const createQuestionTemplate = async (question, migratedCount) => { } if (!isEmpty(questionToMigrate)) { - - referenceQuestionId = await createQuestions(questionToMigrate, question._id); + referenceQuestionId = await createQuestions( + questionToMigrate, + question._id + ); question.referenceQuestionId = referenceQuestionId; - } } diff --git a/scripts/creation-portal-migration/template/migrate/nonmatrix.js b/scripts/creation-portal-migration/template/migrate/nonmatrix.js index c0810472..7b7a127d 100644 --- a/scripts/creation-portal-migration/template/migrate/nonmatrix.js +++ b/scripts/creation-portal-migration/template/migrate/nonmatrix.js @@ -233,6 +233,18 @@ const nonMatrixChildrenAndNoVisibleIf = async ( matrixHierarchy = data.matrixHierarchy; questions = data.questions; } else { + const childWithHierarchy = updateNonMatrixHierarchyChildren( + child, + nonMatrixQuestions, + matrixQuestions, + hierarchy, + index, + criteriaId, + questions, + matrixHierarchy + ); + nonMatrixQuestions = childWithHierarchy.nonMatrixQuestions; + hierarchy = childWithHierarchy.hierarchy; hierarchy = updateHierarchyBranching( hierarchy,