Skip to content

Commit

Permalink
🐛 FIX: Validate duplicate entries during submit quiz
Browse files Browse the repository at this point in the history
  • Loading branch information
justEhmadSaeed committed Mar 9, 2021
1 parent bcffc1b commit e0ffbda
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions backend/src/Routes/DB.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const DBStart = async () => {
'mongodb://quizdom:[email protected]:27017,cluster0-shard-00-01.lecax.mongodb.net:27017,cluster0-shard-00-02.lecax.mongodb.net:27017/quizdom-project?ssl=true&replicaSet=atlas-hmlbn7-shard-0&authSource=admin&retryWrites=true&w=majority',
{
useNewUrlParser: true,
useUnifiedTopology: true,
useUnifiedTopology: true
}
)
console.log('DB Connected Successfully.')
Expand All @@ -35,7 +35,7 @@ const createUser = async (uid, name, email, res) => {
name,
email,
createdQuiz: [],
attemptedQuiz: [],
attemptedQuiz: []
})
res.status(200).json({ message: 'User Created successfully.' })
} else {
Expand All @@ -51,12 +51,12 @@ createQuiz = async (quiz, res) => {
const result = await db.collection('quizzes').insertOne(quiz)
res.status(200).json({
message: 'Quiz created successfully',
quizId: result.insertedId,
quizId: result.insertedId
})
console.log('quiz ID', result.insertedId)
const query = { uid: quiz.uid }
const addQuiz = {
$push: { createdQuiz: result.insertedId },
$push: { createdQuiz: result.insertedId }
}
await db.collection('users').updateOne(query, addQuiz)
console.log('Quiz Added to Creator Document: ', result.insertedId)
Expand All @@ -70,6 +70,24 @@ createQuiz = async (quiz, res) => {
submitQuiz = async (submittedQuiz, res) => {
withDB(async (db) => {
try {
// Check whether the user has already submitted the Quiz
const validationCursor = db.collection('users').find({
$and: [
{ uid: submittedQuiz.uid },
{ attemptedQuiz: ObjectId(submittedQuiz.quizId) }
]
})

const quizData = await validationCursor.toArray()

console.log({ quizData })
// If the quiz is already submitted, DONOT submit it.
if (quizData[0]) {
console.log('in quiz already attempted')
return res.status(200).json({
error: 'ERR:QUIZ_ALREADY_ATTEMPTED'
})
}
const cursor = db
.collection('quizzes')
.find({ _id: new ObjectId(submittedQuiz.quizId) })
Expand All @@ -87,17 +105,17 @@ submitQuiz = async (submittedQuiz, res) => {
{ _id: new ObjectId(submittedQuiz.quizId) },
{
$push: {
responses: { uid: submittedQuiz.uid, score: score },
},
responses: { uid: submittedQuiz.uid, score: score }
}
}
)
// Update user's attempted quizzes
await db.collection('users').updateOne(
{ uid: submittedQuiz.uid },
{
$push: {
attemptedQuiz: ObjectId(submittedQuiz.quizId),
},
attemptedQuiz: ObjectId(submittedQuiz.quizId)
}
}
)
} catch (error) {
Expand Down Expand Up @@ -129,7 +147,7 @@ const getResponses = (obj, res) => {
finalResponse.push({
name: data.name,
email: data.email,
score: responses[index].score,
score: responses[index].score
})
})
res.status(200).json({ finalResponse })
Expand Down

0 comments on commit e0ffbda

Please sign in to comment.