Commit 0fbf9d47 authored by Bharat Kashyap's avatar Bharat Kashyap
Browse files

Merge branch 'release-4.2.0' of github.com:Sunbird-Ed/program-service into...

Merge branch 'release-4.2.0' of github.com:Sunbird-Ed/program-service into feat/question-paper-csv-docx
Showing with 876 additions and 10 deletions
+876 -10
const createError = require("http-errors");
const fileupload = require('express-fileupload');
express = require("express");
path = require("path");
http = require("http");
......@@ -13,6 +14,7 @@ const telemetryService = require("./service/telemetryService");
const sb_logger = require("sb_logger_util_v2");
const logLevel = process.env.sunbird_service_log_level || "info";
var logFilePath = path.join(__dirname, "./logs/microservice.log");
const qumlConsumerService = require("./service/kafkaQumlConsumerService");
const createAppServer = () => {
const app = express();
......@@ -32,6 +34,7 @@ const createAppServer = () => {
else next();
});
app.use(bodyParser.json({ limit: "1mb" }));
app.use(fileupload());
app.use(logger("dev"));
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: false }));
......@@ -40,6 +43,7 @@ const createAppServer = () => {
require("./routes/configurationRoutes")(app);
require("./routes/programFeedRoutes")(app);
require("./routes/print")(app);
require("./routes/qumlBulkRoutes")(app);
app.use(cookieParser());
module.exports = app;
return app;
......@@ -53,5 +57,6 @@ app.listen(port, () => {
console.log(
`program-service is running in test env on port ${port} with ${process.pid} pid`
);
qumlConsumerService.qumlConsumer();
telemetryService.initializeTelemetryService();
});
......@@ -16,6 +16,9 @@ const envVariables = {
DOCK_REDIS_HOST: process.env.dock_redis_host,
DOCK_REDIS_PORT: process.env.dock_redis_port || 6379,
SUNBIRD_AUTO_CREATION_TOPIC: process.env.sunbird_auto_creation_topic,
SUNBIRD_QUESTION_BULKUPLOAD_TOPIC: process.env.sunbird_question_bulkupload_topic,
SUNBIRD_KAFKA_BULKUPLOAD_CONSUMER_GROUP_ID:process.env.sunbird_kafka_bulkupload_consumer_group_id,
SUNBIRD_ASSESSMENT_SERVICE_BASE_URL : process.env.sunbird_assessment_service_base_url,
CORE_INGRESS_GATEWAY_IP: process.env.CORE_INGRESS_GATEWAY_IP,
config: {
user: process.env.sunbird_program_db_user || "postgres",
......
......@@ -37,6 +37,22 @@ const KafkaService = {
logger.info({msg: 'Kafka record', additionalInfo: {record}})
// Send record to Kafka and log result/error
producer.send(record, callback)
},
sendRecordWithTopic: (data,topicName, callback = () => { }) => {
if (_.isEmpty(data)) {
logger.error({msg: 'Data must be provided to send Record', additionalInfo: {data}})
return callback(new Error('Event Data must be provided.'))
}
// Create a new payload
const record = [
{
topic: topicName,
messages: JSON.stringify(data)
}
]
logger.info({msg: 'Kafka record', additionalInfo: {record}})
// Send record to Kafka and log result/error
producer.send(record, callback)
}
}
......
......@@ -239,7 +239,7 @@ class HierarchyService {
primaryCategory: data.primaryCategory,
children: _.compact(
_.map(data.children, function(child) {
if (child.mimeType === "application/vnd.ekstep.content-collection")
if (child.mimeType === "application/vnd.ekstep.content-collection" && child.visibility === 'Parent')
{
return child.identifier;
}
......@@ -249,7 +249,7 @@ class HierarchyService {
};
}
_.forEach(data.children, child => {
if (child.mimeType === "application/vnd.ekstep.content-collection") {
if (child.mimeType === "application/vnd.ekstep.content-collection" && child.visibility === 'Parent') {
instance.getFlatHierarchyObj(child, additionalMetaData, children);
}
});
......@@ -318,7 +318,7 @@ class HierarchyService {
}
_.forEach(data.children, child => {
if (child.mimeType === "application/vnd.ekstep.content-collection") {
if (child.mimeType === "application/vnd.ekstep.content-collection" && child.visibility === 'Parent') {
instance.getFlatNodesModified(child, additionalMetaData, children);
}
});
......
......@@ -1246,6 +1246,14 @@
"resolved": "https://registry.npmjs.org/buffers/-/buffers-0.1.1.tgz",
"integrity": "sha1-skV5w77U1tOWru5tmorn9Ugqt7s="
},
"busboy": {
"version": "0.3.1",
"resolved": "https://registry.npmjs.org/busboy/-/busboy-0.3.1.tgz",
"integrity": "sha512-y7tTxhGKXcyBxRKAni+awqx8uqaJKrSFSNFSeRG5CsWNdmy2BIK+6VGWEW7TZnIO/533mtMEA4rOevQV815YJw==",
"requires": {
"dicer": "0.3.0"
}
},
"bytes": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz",
......@@ -2063,6 +2071,14 @@
"resolved": "https://registry.npmjs.org/di/-/di-0.0.1.tgz",
"integrity": "sha1-gGZJMmzqp8qjMG112YXqJ0i6kTw="
},
"dicer": {
"version": "0.3.0",
"resolved": "https://registry.npmjs.org/dicer/-/dicer-0.3.0.tgz",
"integrity": "sha512-MdceRRWqltEG2dZqO769g27N/3PXfcKl04VhYnBlo2YhH7zPi88VebsjTKclaOyiuMaGU72hTfw3VkUitGcVCA==",
"requires": {
"streamsearch": "0.1.2"
}
},
"diff": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/diff/-/diff-5.0.0.tgz",
......@@ -2907,6 +2923,14 @@
"resolved": "https://registry.npmjs.org/express-cluster/-/express-cluster-0.0.5.tgz",
"integrity": "sha512-Wo0i4YFdj1341nkCxXO9zcO6ZGgiTiFEi+KMwE8bZexGg1sY2GmeDyHHEUDI+zSUJsQDSlT1EQSBnh5XaPIFVQ=="
},
"express-fileupload": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/express-fileupload/-/express-fileupload-1.2.1.tgz",
"integrity": "sha512-fWPNAkBj+Azt9Itmcz/Reqdg3LeBfaXptDEev2JM8bCC0yDptglCnlizhf0YZauyU5X/g6v7v4Xxqhg8tmEfEA==",
"requires": {
"busboy": "^0.3.1"
}
},
"express-validator": {
"version": "6.9.2",
"resolved": "https://registry.npmjs.org/express-validator/-/express-validator-6.9.2.tgz",
......@@ -7168,6 +7192,11 @@
}
}
},
"streamsearch": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/streamsearch/-/streamsearch-0.1.2.tgz",
"integrity": "sha1-gIudDlb8Jz2Am6VzOOkpkZoanxo="
},
"string-width": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz",
......
......@@ -25,6 +25,7 @@
"express": "~4.16.1",
"express-cassandra": "^2.3.2",
"express-cluster": "0.0.5",
"express-fileupload": "^1.2.1",
"express-validator": "^6.6.1",
"helmet": "^3.23.1",
"http-errors": "^1.6.3",
......
[
{
"userId": "",
"publisherId": "publisher",
"organizationId": "orgid",
"question": {
"name": "question_11",
"code": "question",
"mimeType": "application/vnd.sunbird.question",
"primaryCategory": "Practice Question Set",
"body": "",
"editorState": "abc"
}
},
{
"userId": "something",
"publisherId": "publisher",
"organizationId": "orgid",
"question": {
"name": "question_12",
"code": "question",
"mimeType": "application/vnd.sunbird.question",
"primaryCategory": "Practice Question Set",
"body": "<div class='question-body'><div class='question-title'><p>If iam a apple</div><div data-choice-interaction='response1' class='mcq-vertical'></div></div>",
"solutions": [
"Easy solution"
],
"interactions": {
"response1": {
"type": "choice",
"options": [
{
"body": "good",
"value": 0
},
{
"body": "Bad",
"value": 1
},
{
"body": "ok",
"value": 2
},
{
"body": "not okay",
"value": 3
}
]
}
}
}
},
{
"userId": "",
"publisherId": "publisher",
"organizationId": "orgid",
"question": {
"name": "question_13",
"code": "question",
"mimeType": "application/vnd.sunbird.question",
"primaryCategory": "Practice Question Set",
"body": "",
"editorState": "abc"
}
},
{
"userId": "",
"publisherId": "publisher",
"organizationId": "orgid",
"question": {
"name": "question_14",
"code": "question",
"mimeType": "application/vnd.sunbird.question",
"primaryCategory": "Practice Question Set",
"body": "",
"editorState": "abc"
}
},
{
"userId": "something",
"publisherId": "publisher",
"organizationId": "orgid",
"question": {
"name": "question_15",
"code": "question",
"mimeType": "application/vnd.sunbird.question",
"primaryCategory": "Practice Question Set",
"body": "<div class='question-body'><div class='question-title'><p>If iam a apple</div><div data-choice-interaction='response1' class='mcq-vertical'></div></div>",
"solutions": [
"Easy solution"
],
"interactions": {
"response1": {
"type": "choice",
"options": [
{
"body": "good",
"value": 0
},
{
"body": "Bad",
"value": 1
},
{
"body": "ok",
"value": 2
},
{
"body": "not okay",
"value": 3
}
]
}
}
}
},
{
"userId": "",
"publisherId": "publisher",
"organizationId": "orgid",
"question": {
"name": "question_16",
"code": "question",
"mimeType": "application/vnd.sunbird.question",
"primaryCategory": "Practice Question Set",
"body": "",
"editorState": "abc"
}
},
{
"userId": "something",
"publisherId": "publisher",
"organizationId": "orgid",
"question": {
"name": "question_17",
"code": "question",
"mimeType": "application/vnd.sunbird.question",
"primaryCategory": "Practice Question Set",
"body": "<div class='question-body'><div class='question-title'><p>If iam a apple</div><div data-choice-interaction='response1' class='mcq-vertical'></div></div>",
"solutions": [
"Easy solution"
],
"interactions": {
"response1": {
"type": "choice",
"options": [
{
"body": "good",
"value": 0
},
{
"body": "Bad",
"value": 1
},
{
"body": "ok",
"value": 2
},
{
"body": "not okay",
"value": 3
}
]
}
}
}
},
{
"userId": "",
"publisherId": "publisher",
"organizationId": "orgid",
"question": {
"name": "question_18",
"code": "question",
"mimeType": "application/vnd.sunbird.question",
"primaryCategory": "Practice Question Set",
"body": "",
"editorState": "abc"
}
}
]
\ No newline at end of file
const qumlBulkUpload = require("../service/qumlBulkService");
const BASE_URL = '/question/v1';
module.exports = function (app) {
app.route(BASE_URL + '/bulkUpload')
.post(qumlBulkUpload.bulkUpload)
app.route(BASE_URL + '/bulkUploadStatus')
.post(qumlBulkUpload.qumlSearch)
}
\ No newline at end of file
var kafka = require("kafka-node");
const fetch = require("node-fetch");
const logger = require("sb_logger_util_v2");
const loggerService = require("./loggerService");
const messageUtils = require("../service/messageUtil");
const responseCode = messageUtils.RESPONSE_CODE;
const errorCodes = messageUtils.ERRORCODES;
const envVariables = require("../envVariables");
const rspObj = {};
const qumlConsumer = () => {
try {
Consumer = kafka.Consumer;
ConsumerGroup = kafka.ConsumerGroup;
client = new kafka.KafkaClient({
kafkaHost: envVariables.SUNBIRD_KAFKA_HOST,
});
payload = [
{
topic: envVariables.SUNBIRD_QUESTION_BULKUPLOAD_TOPIC,
partition: 1,
},
];
var options = {
kafkaHost: envVariables.SUNBIRD_KAFKA_HOST,
groupId: envVariables.SUNBIRD_KAFKA_BULKUPLOAD_CONSUMER_GROUP_ID,
fromOffset: "latest",
};
var consumerGroup = new ConsumerGroup(options, [
envVariables.SUNBIRD_QUESTION_BULKUPLOAD_TOPIC
]);
consumerGroup
.on("message", function (message) {
const qumlArr = JSON.parse(message.value);
logger.info({ message: "Entered into the consumer service" });
let parsedJsonValue = JSON.parse(qumlArr);
let createApiData = {
request: {
question: {
code: parsedJsonValue.question.code,
mimeType: parsedJsonValue.question.mimeType,
name: parsedJsonValue.question.name,
editorState: parsedJsonValue.question.editorState,
primaryCategory: parsedJsonValue.question.primaryCategory,
body: parsedJsonValue.question.body,
processId: parsedJsonValue.question.processId,
questionFileRefId: parsedJsonValue.question.questionFileRefId,
},
},
};
//fetch call for creating a question.
fetch(`${envVariables.SUNBIRD_ASSESSMENT_SERVICE_BASE_URL}/question/v1/create`, {
method: "POST", // or 'PUT'
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(createApiData),
})
.then((response) => response.json())
.then((createResponseData) => {
let updateApiData = createResponseData;
parsedJsonValue.question["versionKey"] =
updateApiData.result.versionKey;
delete parsedJsonValue.question.mimeType;
delete parsedJsonValue.question.code;
delete parsedJsonValue.question.processId;
delete parsedJsonValue.question.questionId;
let updateData = { request: parsedJsonValue };
//if success fetch call for updating question.
if (
createResponseData.responseCode.toLowerCase() ===
"OK".toLowerCase()
) {
fetch(
`${envVariables.SUNBIRD_ASSESSMENT_SERVICE_BASE_URL}/question/v1/update/${updateApiData.result.identifier}`,
{
method: "PATCH", // or 'PUT'
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(updateData),
}
)
.then((response) => response.json())
.then((updateResponseData) => {
let reviewData = { request: { question: {} } };
//if success fetch call for reviewing question.
if (
updateResponseData.responseCode.toLowerCase() ===
"OK".toLowerCase()
) {
fetch(
`${envVariables.SUNBIRD_ASSESSMENT_SERVICE_BASE_URL}/question/v1/review/${updateApiData.result.identifier}`,
{
method: "POST", // or 'PUT'
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(reviewData),
}
)
.then((response) => response.json())
.then((reviewResponseData) => {
let publishApiData = { request: { question: {} } };
//if success fetch call for publishing question.
if (
reviewResponseData.responseCode.toLowerCase() ===
"OK".toLowerCase()
) {
fetch(
`${envVariables.SUNBIRD_ASSESSMENT_SERVICE_BASE_URL}/question/v1/publish/${updateApiData.result.identifier}`,
{
method: "POST", // or 'PUT'
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(publishApiData),
}
)
.then((response) => response.json())
.then((publishResponseData) => {
if (
publishResponseData.responseCode.toLowerCase() ===
"OK".toLowerCase()
) {
updateResponse(
updateApiData.result.identifier,
`Successfully uploaded the question for the identifier:${updateApiData.result.identifier}`,
createResponseData.result.versionKey
);
} else {
logger.error({
message:
"Something went wrong while Publishing the question",
});
updateResponse(
updateApiData.result.identifier,
`Something went wrong while Publishing the question`,
createResponseData.result.versionKey
);
}
})
.catch((error) => {
console.error("Error:", error);
updateResponse(
updateApiData.result.identifier,
`Something went wrong while Publishing the question`,
createResponseData.result.versionKey
);
});
} else {
logger.error({
message:
"Something Went wrong while reviewing the questions",
});
updateResponse(
updateApiData.result.identifier,
`Something Went wrong while reviewing the questions`,
createResponseData.result.versionKey
);
}
})
.catch((error) => {
console.error("Error:", error);
logger.error({
message:
"Something Went wrong while reviewing the questions",
});
updateResponse(
updateApiData.result.identifier,
`Something Went wrong while reviewing the questions: ${error}`,
createResponseData.result.versionKey
);
});
} else {
logger.error({
message:
"Something Went Wrong While Updating the question",
});
updateResponse(
updateApiData.result.identifier,
`Something Went Wrong While Updating the question:`,
createResponseData.result.versionKey
);
}
})
.catch((error) => {
console.error("Error:", error);
logger.error({
message: "Something Went Wrong While Updating the question",
});
updateResponse(
updateApiData.result.identifier,
`Something Went Wrong While Updating the question: ${error}`,
createResponseData.result.versionKey
);
});
} else {
logger.error({
message: "Something Went Wrong While Creating the question",
});
updateResponse(
updateApiData.result.identifier,
`Something Went Wrong While Creating the question:`,
createResponseData.result.versionKey
);
}
})
.catch((error) => {
console.error("Error:", error);
logger.error({
message: "Something Went Wrong While Creating the question",
});
updateResponse(
updateApiData.result.identifier,
`Something Went Wrong While Creating the question: ${error}`,
createResponseData.result.versionKey
);
});
})
.on("error", function (message) {
consumer.close();
client.close();
});
} catch (error) {
logger.error(
{
message: "Something Went Wrong While Creating the question",
},
error
);
}
};
//function to update the status of all other fetch calls mentioned above using question update.
const updateResponse = (updateData, updateMessage, versionKey) => {
const updateNewData = {
request: {
question: {
versionKey: versionKey,
questionUploadStatus: updateMessage,
},
},
};
fetch(`${envVariables.SUNBIRD_ASSESSMENT_SERVICE_BASE_URL}/question/v1/update/${updateData}`, {
method: "POST", // or 'PUT'
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(updateNewData),
})
.then((response) => response.json())
.then((updateResult) => {
rspObj.responseCode = "OK";
rspObj.result = {
questionStatus: `Successfully updated the question data for the identifier: ${updateData}`,
};
logger.info({
message: "Successfully updated the question data",
rspObj,
});
})
.catch((error) => {
rspObj.errMsg = "Something Went wrong while updating question data";
rspObj.responseCode = responseCode.SERVER_ERROR;
logger.error(
{
message: "Something Went wrong while updating question data",
errorData: error,
rspObj,
},
errorCodes.CODE2
);
});
};
module.exports = {
qumlConsumer,
updateResponse
};
......@@ -733,6 +733,22 @@ exports.PROGRAM = {
FAILED_MESSAGE: 'Unable to generate the content report',
INFO: 'Generate the content report'
},
QUML_BULKUPLOAD: {
EXCEPTION_CODE: 'QUMLBLK',
MISSING_CODE: 'ERR_QUML_BULKUPLOAD',
MISSING_MESSAGE: 'Required fields like user_id or publisher_id or organization_id is missing',
FAILED_CODE: 'ERR_QUML_BULKUPLOAD_FAILED',
FAILED_MESSAGE: 'Unable to perform quml bulk upload',
INFO: 'Quml bulk upload'
},
QUML_BULKSTATUS: {
EXCEPTION_CODE: 'QUMLSTS',
MISSING_CODE: 'ERR_QUML_BULKSTATUS',
MISSING_MESSAGE: 'Err while fetching data',
FAILED_CODE: 'ERR_QUML_BULKSTATUS_FAILED',
FAILED_MESSAGE: 'Unable to perform quml bulk upload status check',
INFO: 'Quml bulk upload status check'
},
CONTENT_PUBLISH: {
EXCEPTION_CODE: 'CNTPUB',
MISSING_CODE: 'ERR_CONTENT_PUBLISH',
......@@ -756,8 +772,7 @@ exports.PROGRAM = {
MISSING_MESSAGE: 'Required fields like email id or phone to send nomination notificiation missing',
FAILED_CODE: 'ERR_TO_SEND_NOMINATION_NOTIFICATION_FAILED',
FAILED_MESSAGE: 'Unable to send nomination notification',
EMAIL_SUBJECT: 'VidyaDaan: Your nomination for {PROGRAM_NAME} project is accepted',
SMS:'VidyaDaan: Your nomination for {PROGRAM_NAME} is accepted. Please login to {smsURL} to start contributing content'
EMAIL_SUBJECT: 'VidyaDaan: Your nomination for {PROGRAM_NAME} project is accepted'
},
READ: {
EXCEPTION_CODE: 'NOMRED',
......
......@@ -4,6 +4,7 @@ const axios = require('axios');
const _ = require("lodash");
const messageUtils = require('../service/messageUtil');
const programMessages = messageUtils.PROGRAM;
const model = require('../models');
class NotificationService {
async sendNotification(req, reqData) {
......@@ -68,16 +69,25 @@ class NotificationService {
return;
}
const result = await model.configuration.findAll({
where: {
key: 'smsNominationAccept',
status: 'active'
}
});
const projectName = _.truncate(program.name, { length: 25 });
let smsYouAreNominated = programMessages.NOMINATION.NOTIFY.SMS.replace('{PROGRAM_NAME}', projectName);
smsYouAreNominated = smsYouAreNominated.replace('{smsURL}', envVariables.baseURL);
let smsBody = _.get(_.first(result),'dataValues.value');
smsBody = smsBody.replace('$projectName', projectName);
smsBody = smsBody.replace('$url', 'https://vdn.diksha.gov.in');
const request = {
mode: 'sms',
subject: 'VidyaDaan',
body: smsYouAreNominated,
body: smsBody,
recipientUserIds: userIdsToSms
};
return this.sendNotification(req, request);
}
}
......
......@@ -160,7 +160,6 @@ function create(data, paperData) {
arr.push(Marks(question));
question[0].Questions.map((item) => {
// console.log(item)
arr.push(createSAObject(item, count++));
});
arr.push(
......@@ -306,7 +305,6 @@ function Marks(data) {
});
}
}
function createFTBObject(data) {
const arr = [];
......
const fs = require("fs");
const fetch = require("node-fetch");
const { v4: uuidv4 } = require("uuid");
const KafkaService = require("../helpers/kafkaUtil");
const logger = require("sb_logger_util_v2");
const loggerService = require("./loggerService");
const messageUtils = require("./messageUtil");
const responseCode = messageUtils.RESPONSE_CODE;
const programMessages = messageUtils.PROGRAM;
const errorCodes = messageUtils.ERRORCODES;
const envVariables = require("../envVariables");
const rspObj = {};
const bulkUpload = async (req, res) => {
const logObject = {
traceId: req.headers["x-request-id"] || "",
message: programMessages.QUML_BULKUPLOAD.INFO,
};
let totalQuestionLength = 0;
let errorArray = [];
let pId = uuidv4();
let successArray = [];
let qumlData;
const fileType = req.files.File.mimetype;
const fileName = req.files.File.name;
loggerService.entryLog("Api to upload questions in bulk", logObject);
//validating the file whether the incoming file is json or not
if (fileType !== "application/json") {
rspObj.errMsg = "The File is not in JSON format!!";
rspObj.responseCode = responseCode.SERVER_ERROR;
logger.error({ message: "The File is not in JSON format!!" });
res
.status(400)
.send(
{ message: "The File is not in JSON format!!", rspObj },
errorCodes.CODE2
);
} else {
const AppendData = req.files.File.data.toString("utf8");
fs.writeFile(`${fileName}.json`, AppendData, (err) => {
if (err) {
rspObj.errMsg = "Something Went Wrong While Writing the file";
rspObj.responseCode = responseCode.SERVER_ERROR;
logger.error(
{ message: "Something Went Wrong While Writing the file", rspObj },
errorCodes.CODE2
);
res
.status(400)
.send(
{ message: "Something Went Wrong While Writing the file", rspObj },
errorCodes.CODE2
);
} else {
logger.info({ message: "File has been written Successfully" });
}
});
await readfile(fileName)
.then((ele) => {
qumlData = ele;
})
.catch((err) => {
rspObj.errMsg = "Something went Wrong while file reading";
rspObj.responseCode = responseCode.SERVER_ERROR;
logger.error({
message: "Something went Wrong while file reading",
rspObj,
});
res
.status(400)
.send(
{
message: "Something went Wrong while file reading",
errorData: err,
rspObj,
},
errorCodes.CODE2
);
});
totalQuestionLength = qumlData.length;
//validating whether the userId,publisherId and organizationId is empty or not;
for (let i = 0; i < qumlData.length; i++) {
if (qumlData[i].userId === "") {
errorArray.push(
`${programMessages.QUML_BULKUPLOAD.MISSING_MESSAGE}: ${JSON.stringify(
qumlData[i]
)}`
);
} else if (qumlData[i].publisherId === "") {
errorArray.push(
`${programMessages.QUML_BULKUPLOAD.MISSING_MESSAGE}:${JSON.stringify(
qumlData[i]
)}`
);
} else if (qumlData[i].organizationId === "") {
errorArray.push(
`${programMessages.QUML_BULKUPLOAD.MISSING_MESSAGE}: ${JSON.stringify(
qumlData[i]
)}`
);
} else {
qumlData[i].question["processId"] = pId;
qumlData[i].question["questionFileRefId"] = uuidv4();
successArray.push(`${JSON.stringify(qumlData[i])}`);
//calling the kafka producer here
KafkaService.sendRecordWithTopic(
qumlData[i],
envVariables.SUNBIRD_QUESTION_BULKUPLOAD_TOPIC,
function (err, response) {
if (err) {
logger.error(
{
message: "Something Went wrong while producing kafka",
errorData: err,
},
errorCodes.CODE2
);
}
}
);
}
}
fs.unlink(`${fileName}.json`, function (err, res) {
if (err) {
logger.error({
message: "Something Went wrong while performing unlink of the file ",
errorData: err,
});
} else {
logger.info({
message: "Successfully unlinked the file",
resData: res,
});
}
//Do whatever else you need to do here
});
}
rspObj.responseCode = "OK";
rspObj.result = {
questionStatus: `Bulk Upload process has started successfully for the process Id : ${pId}`,
data: {
"Total no of questions": totalQuestionLength,
"No of questions getting processed": successArray.length,
"No of questions With issues": errorArray.length,
"Questions With wrong message": errorArray,
},
};
logger.info({
message: "Bulk Upload process has started successfully for the process Id",
pId,
});
loggerService.exitLog(
`Bulk Upload process has started successfully for the process Id : ${pId}`,
rspObj
);
res
.status(200)
.send(
{message: `Bulk Upload process has started successfully for the process Id : ${pId}`,rspObj}
);
};
const readfile = (filename) => {
return new Promise((resolve, reject) => {
fs.readFile(`${filename}.json`, "utf8", (err, jsonString) => {
if (err) {
reject(err);
} else {
var qumlJsonData = JSON.parse(jsonString);
resolve(qumlJsonData);
}
});
});
};
//question search API function;
const qumlSearch = (req, res) => {
const searchData = req.body;
const logObject = {
traceId: req.headers["x-request-id"] || "",
message: programMessages.QUML_BULKSTATUS.INFO,
};
loggerService.entryLog("Api to check the status of bulk upload question", logObject);
fetch(`${envVariables.baseURL}/action/composite/v3/search`, {
method: "POST", // or 'PUT'
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(searchData),
})
.then((response) => response.json())
.then((resData) => {
rspObj.responseCode = "OK";
rspObj.result = {
questionStatus: `Successfully fetched the data for the given request: ${searchData}`,
};
logger.info({ message: "Successfully Fetched the data", rspObj });
res
.status(200)
.send({
message: "Successfully got the Questions",
rspObj,
data: resData,
});
loggerService.exitLog(
"Successfully got the Questions",
rspObj,
);
})
.catch((error) => {
rspObj.errMsg = "Something went wrong while fetching the data";
rspObj.responseCode = responseCode.SERVER_ERROR;
logger.error(
{
message: "Something went wrong while fetching the data",
errorData: error,
rspObj,
},
errorCodes.CODE2
);
res
.status(400)
.send(
{
message: "Something went wrong while fetching the data",
errorData: error,
rspObj,
},
errorCodes.CODE2
);
});
};
module.exports = {
bulkUpload,
qumlSearch,
readfile
};
process.env.NODE_ENV = "test";
const app = require("../../app");
const envVariables = require("../../envVariables");
const chai = require("chai");
const chaiHttp = require("chai-http");
chai.use(chaiHttp);
const { expect } = chai;
chai.use(require("chai-sorted"));
const _ = require("lodash");
const {readfile} = require("../../service/qumlBulkService.js");
const {updateResponse} = require("../../service/kafkaQumlConsumerService.js");
// const host = 'http://localhost:5000'
const BASE_URL = '/question/v1';
describe("QUML BULK UPLOAD SERVICE",async () => {
it("it should give the questions related to the request data", (done) => {
const program = {"filters": {"objectType": "collection", "identifier": "do_1133273110340894721318"}};
chai
.request(app)
.post(BASE_URL + "/bulkUploadStatus")
.set("Accept", "application/json")
.send(program)
.end((err, res) => {
console.log("test case",res.body)
expect(res.status).to.equal(200);
expect(res.body.data).to.have.property("responseCode");
done();
},3000);
});
it("it should produce kafka data", (done) => {
chai
.request(app)
.post(BASE_URL + "/bulkUpload")
.set("Accept", "application/json")
.attach(
"File",
fs.readFileSync("/home/navadhiti/Documents/example_2.json"),
"/home/navadhiti/Documents/example_2.json"
)
.end((err, res) => {
expect(res.status).to.equal(200);
},3000).catch(done());
});
it("read file function test case", (done) => {
readfile("qumlTestData")
.then((response) => {
expect(response[0]).to.have.property('userId');
expect(response[0]).to.have.property('publisherId');
expect(response[0]).to.have.property('organizationId');
done();
})
.catch((e) => {
done(e);
});
});
it("update response function to update data", (done) => {
updateResponse("do_113351214093565952173","test case response","1629786995450")
.then((response) => {
expect(response.status).to.equal(200);
done();
})
.catch((e) => {
done(e);
});
});
});
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment