Skip to content

Instantly share code, notes, and snippets.

@shark-h
Created September 4, 2019 10:21
Show Gist options
  • Save shark-h/6981d91a7d1f5bb8c6e27d4a9d5077da to your computer and use it in GitHub Desktop.
Save shark-h/6981d91a7d1f5bb8c6e27d4a9d5077da to your computer and use it in GitHub Desktop.
// INCREMENT USER SCORE IF THE ANSWER IS CORRECT
exports.checkAnswer = functions.https.onRequest( async (request, response) => {
const answerID = request.body.event.data.new.answer_id;
const userID = request.body.event.data.new.user_id;
const answerQuery = `
queryAnswer($answerID: uuid!) {
question_answers(where: {id: {_eq: $answerID}}) {
is_correct
}
}`;
const incrementMutation = `
mutationScore($userID: String!) {
update_users(where: {id: {_eq: $userID}}, _inc: {score: 10}) {
affected_rows
}
}`;
try {
const data = await client.request(answerQuery, {
answerID: answerID,
})
const isCorrect = data["question_answers"][0]["is_correct"];
console.log(isCorrect);
if (!isCorrect) {
response.send("correct");
return;
} else {
await client.request(incrementMutation, { userID: userID })
response.send("correct");
}
} catch (e) {
throw new functions.https.HttpsError(JSON.stringify(e, undefined, 2));
}
});
@benfgit
Copy link

benfgit commented Feb 11, 2021

Why line #28 is "correct"? Is that a typo?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment