Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ const populateClosedDays = require('./schedule/populateClosedDays');
//Register focus routes
app.use('/', require('./endpoints/checkFocus/route')(databaseConnection));

//Register Quiz routes
app.use('/', require('./endpoints/hpcQuestion/route')(databaseConnection));

// Register report routes
app.use('/', require('./endpoints/report/routes')(databaseConnection));

Expand Down
41 changes: 41 additions & 0 deletions backend/db/mongodb-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,47 @@
"validationLevel": "moderate",
"validationAction": "error"
},
{
"name": "hpcQuestion",
"validator": {
"$jsonSchema": {
"bsonType": "object",
"required": [
"_id",
"question",
"options",
"correctAnswerIndex",
"explanation",
"active"
],
"properties": {
"_id": {
"bsonType": "objectId"
},
"question": {
"bsonType": "string"
},
"options": {
"bsonType": "array",
"items": {
"bsonType": "string"
}
},
"correctAnswerIndex": {
"bsonType": "int"
},
"explanation": {
"bsonType": "string"
},
"active": {
"bsonType": "bool"
}
}
}
},
"validationLevel": "moderate",
"validationAction": "error"
},
{
"name": "method",
"validator": {
Expand Down
112 changes: 112 additions & 0 deletions backend/endpoints/hpcQuestion/controller/checkAnswer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const { ObjectId } = require('mongodb');

/**
* @param {import('mongodb').Db} db
*/
module.exports = (db) => {
/**
* @param {import('express').Request} req
* @param {import('express').Response} res
* @returns {Promise<void>}
*/
return async (req, res) => {
try {
const { questionId, selectedAnswerIndex, optionOrder } = req.body || {};

if (!questionId) {
return res.status(400).json({
success: false,
error: 'Missing question id'
});
}

const sanitizedQuestionId = String(questionId).trim();

if (!ObjectId.isValid(sanitizedQuestionId)) {
return res.status(400).json({
success: false,
error: 'Invalid question id'
});
}

if (!Number.isInteger(selectedAnswerIndex)) {
return res.status(400).json({
success: false,
error: 'Selected answer index must be an integer'
});
}

if (!Array.isArray(optionOrder)) {
return res.status(400).json({
success: false,
error: 'Option order must be an array'
});
}

if (!optionOrder.every((value) => Number.isInteger(value))) {
return res.status(400).json({
success: false,
error: 'Option order must only contain integers'
});
}

const question = await db.collection('hpcQuestion').findOne({
_id: new ObjectId(sanitizedQuestionId),
active: true
});

if (!question) {
return res.status(404).json({
success: false,
error: 'HPC question does not exist'
});
}

if (selectedAnswerIndex < 0 || selectedAnswerIndex >= optionOrder.length) {
return res.status(400).json({
success: false,
error: 'Selected answer index is out of range'
});
}

if (optionOrder.length !== question.options.length) {
return res.status(400).json({
success: false,
error: 'Option order length does not match question options'
});
}

const originalSelectedAnswerIndex = optionOrder[selectedAnswerIndex];

if (
originalSelectedAnswerIndex < 0 ||
originalSelectedAnswerIndex >= question.options.length
) {
return res.status(400).json({
success: false,
error: 'Original selected answer index is out of range'
});
}

const correct = originalSelectedAnswerIndex === question.correctAnswerIndex;

const correctAnswerDisplayIndex = optionOrder.findIndex(
(originalIndex) => originalIndex === question.correctAnswerIndex
);

return res.status(200).json({
success: true,
body: {
correct,
correctAnswerIndex: correctAnswerDisplayIndex,
explanation: question.explanation
}
});
} catch (error) {
return res.status(500).json({
success: false,
error: error.message
});
}
};
};
61 changes: 61 additions & 0 deletions backend/endpoints/hpcQuestion/controller/getRandomQuestion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @param {import('mongodb').Db} db
*/
module.exports = (db) => {
/**
* @param {import('express').Request} req
* @param {import('express').Response} res
* @returns {Promise<void>}
*/
return async (req, res) => {
try {
if (Math.random() >= 0.35) {
return res.status(200).json({
success: true,
body: null
});
}

const [question] = await db
.collection('hpcQuestion')
.aggregate([{ $match: { active: true } }, { $sample: { size: 1 } }])
.toArray();

if (!question) {
return res.status(404).json({
success: false,
error: 'No active HPC questions found'
});
}

const optionOrder = question.options.map((_, index) => index);

for (let i = optionOrder.length - 1; i > 0; i--) {
const randomIndex = Math.floor(Math.random() * (i + 1));
const temporaryValue = optionOrder[i];

optionOrder[i] = optionOrder[randomIndex];
optionOrder[randomIndex] = temporaryValue;
}

const shuffledOptions = optionOrder.map(
(originalIndex) => question.options[originalIndex]
);

return res.status(200).json({
success: true,
body: {
id: question._id.toString(),
question: question.question,
options: shuffledOptions,
optionOrder
}
});
} catch (error) {
return res.status(500).json({
success: false,
error: error.message
});
}
};
};
8 changes: 8 additions & 0 deletions backend/endpoints/hpcQuestion/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* @param {import('mongodb').Db} db
*/
module.exports = (db) =>
require('express')
.Router()
.get('/hpc-question/random', require('./controller/getRandomQuestion')(db))
.post('/hpc-question/check', require('./controller/checkAnswer')(db));
Loading
Loading