trivia: avoid adding duplicate questiosn to the db

This commit is contained in:
Ayden Jahola 2024-09-08 18:38:08 +01:00
parent 83fc8d207e
commit eb6445b0f9
No known key found for this signature in database
GPG key ID: 71DD90AE4AE92742

View file

@ -100,6 +100,12 @@ const fetchTriviaQuestion = async (categoryId, categoryName) => {
`https://opentdb.com/api.php?amount=1&category=${categoryId}&token=${sessionToken}`
);
const retryApiQuestion = retryResponse.data.results[0];
triviaQuestion = await TriviaQuestion.findOne({
question: decode(retryApiQuestion.question),
category: categoryName,
});
if (!triviaQuestion) {
await TriviaQuestion.create({
question: decode(retryApiQuestion.question),
correct_answer: decode(retryApiQuestion.correct_answer),
@ -112,7 +118,14 @@ const fetchTriviaQuestion = async (categoryId, categoryName) => {
question: decode(retryApiQuestion.question),
category: categoryName,
});
}
} else {
triviaQuestion = await TriviaQuestion.findOne({
question: decode(apiQuestion.question),
category: categoryName,
});
if (!triviaQuestion) {
await TriviaQuestion.create({
question: decode(apiQuestion.question),
correct_answer: decode(apiQuestion.correct_answer),
@ -126,6 +139,7 @@ const fetchTriviaQuestion = async (categoryId, categoryName) => {
category: categoryName,
});
}
}
LAST_API_CALL.time = Date.now(); // Update the last API call time
} else {