This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
handler.js
64 lines (59 loc) · 1.85 KB
/
handler.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"use strict";
const fs = require("fs");
const articleService = require("./lib/services/articleService");
const capiService = require("./lib/services/capi");
const { filterArticles } = require("./lib/services/filter");
let questions = fs.readdirSync("./lib/questionTypes").map(fileName => {
const questionType = fileName.split(".")[0];
return {
questionType,
file: require(`./lib/questionTypes/${questionType}`)
};
});
module.exports.ftlabsQuiz = async (event, context, callback) => {
try {
const queryStringParameters = event.queryStringParameters;
if (queryStringParameters && queryStringParameters.questionTypes) {
questions = questions.filter(question => {
const questionType = queryStringParameters.questionTypes
.split(",")
.find(
requestedQuestion => requestedQuestion === question.questionType
);
if (questionType) {
return true;
} else {
console.error(
"One of the inputted question types was invalid, it has been ignored"
);
return false;
}
});
}
let articles = await articleService.get();
articles = filterArticles(articles.data.topArticleViews);
let articleDetails = await Promise.all(
articles.map(async article => {
const capiData = await capiService.getArticle(article.key);
return { capiData, lanternData: article };
})
);
const results = await Promise.all(
questions.map(
async question => await question.file.getQuestion(articleDetails)
)
);
const response = {
statusCode: 200,
headers: { "Access-Control-Allow-Origin": "*" },
body: JSON.stringify({
message: results,
input: event
})
};
callback(null, response);
} catch (err) {
console.error(err);
callback(null, err);
}
};