Created
September 4, 2019 10:21
-
-
Save shark-h/6981d91a7d1f5bb8c6e27d4a9d5077da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why line #28 is "correct"? Is that a typo?