forked from HarishGangula/quizServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadQuestions.js
37 lines (37 loc) · 1.26 KB
/
loadQuestions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const request = require("request-promise"); // 'request' npm package with Promise support
const getQuestions = async allQuestionIds => {
const questions = allQuestionIds.map(async questionId => {
const options = {
method: "GET",
url: `https://dev.sunbirded.org/action/assessment/v3/items/read/${questionId}`,
json: true
};
const response = await request(options);
return response.result.assessment_item;
});
return await Promise.all(questions);
};
app.use("/get/:id", async (req, res) => {
const options = {
method: "GET",
url: `https://dev.sunbirded.org/api/content/v1/read/${req.params.id}?fields=questions`,
json: true
};
let allQuestions;
try {
allQuestions = await request(options);
const allQuestionIds = [];
allQuestions.result.content.questions.forEach(question => {
allQuestionIds.push(question.identifier);
});
questionMeta = await getQuestions(allQuestionIds);
res.json(questionMeta);
} catch (error) {
console.log(error);
if (error.error) {
res.status(404).json(error.error);
} else {
res.status(404).json("unhandled erro");
}
}
});