Skip to content

Commit

Permalink
Fix - Fixed issues of different functionality (#33)
Browse files Browse the repository at this point in the history
* Update - updated postman collection

* Fixed issues of submit exam and start exam

* Fixed issue of email link verification

* Renamed quiz postman collection
  • Loading branch information
Faiz0developer authored Nov 6, 2023
1 parent 0eb2601 commit 88c7377
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"info": {
"_postman_id": "7988ba49-34ca-4223-a831-06de32da5423",
"name": "QuizClass Copy 3",
"_postman_id": "a5524660-6f23-4262-bf64-fe7ecee71c79",
"name": "QuizClass",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "23659079"
"_exporter_id": "29326444"
},
"item": [
{
Expand Down Expand Up @@ -1819,8 +1819,8 @@
]
}
},
"_postman_previewlanguage": null,
"header": null,
"_postman_previewlanguage": "Text",
"header": [],
"cookie": [],
"body": "{\r\n \"status\": \"success\",\r\n \"message\": \"OTP send successfully\",\r\n \"data\": {\r\n \"otp\": \"165558\"\r\n }\r\n}"
}
Expand Down
9 changes: 5 additions & 4 deletions backend/src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import otpGenerator from "otp-generator";
import OTP from "../models/OTP"

const secretKey = process.env.SECRET_KEY || "";
const SERVER_BASE_URL = process.env.BASE_URL;

//const registerUser:RequestHandler = async (req: Request, res: Response, next: NextFunction) => {
const registerUser: RequestHandler = async (req, res, next) => {
Expand Down Expand Up @@ -212,12 +213,12 @@ const generateEmail = async (name: string, temperoryKey: string, emailaddress: s
action: {
instructions: `If you believe that is by mistake here is your one time temporary key to activate your account after activating your account you can login once after this your account will be deactived for 24 hrs Note:<br><br>
If the button or link is not clickable kindly copy the link and paste it in the browser<br><br>
http://SERVER_BASE_URL/auth/activateaccount/${temperoryKey} <br><br>
http://${SERVER_BASE_URL}/auth/activateaccount/${temperoryKey} <br><br>
`,
button: {
color: '#22BC66', // Optional action button color
text: 'Confirm your account',
link: `http://SERVER_BASE_URL/auth/activateaccount/${temperoryKey}`
link: `http://${SERVER_BASE_URL}/auth/activateaccount/${temperoryKey}`
}
},
outro: "Discover your inner genius - Take the quiz now!"
Expand Down Expand Up @@ -290,12 +291,12 @@ const activateUser: RequestHandler = async (req, res, next) => {
action: {
instructions: `Click the button below to activate your user account. <br><br>
Note: If the button or link is not clickable kindly copy the link and paste it in the browser<br><br>
http://SERVER_BASE_URL/auth/activate/${emailToken}<br><br>
http://${SERVER_BASE_URL}/auth/activate/${emailToken}<br><br>
`,
button: {
color: '#22BC66', // Optional action button color
text: 'Activate Account',
link: `http://SERVER_BASE_URL/auth/activate/${emailToken}/`
link: `http://${SERVER_BASE_URL}/auth/activate/${emailToken}/`
},
},
outro: "Discover your inner genius - Take the quiz now!"
Expand Down
54 changes: 39 additions & 15 deletions backend/src/controllers/exam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import Report from "../models/report";
import { ReturnResponse } from "../utils/interfaces";

const startExam: RequestHandler = async (req, res, next) => {
const userId = req.userId;
try {
const quizId = req.params.quizId;
const quiz = await Quiz.findById(quizId, {
name: 1,
questionList: 1,
isPublished: 1,
createdBy: 1,
});

if (!quiz) {
Expand All @@ -26,6 +28,13 @@ const startExam: RequestHandler = async (req, res, next) => {
err.statusCode = 405;
throw err;
}

if (quiz.createdBy.toString() === userId) {
const err = new ProjectError("You can't attend your own quiz!");
err.statusCode = 405;
throw err;
}

const resp: ReturnResponse = {
status: "success",
message: "Quiz",
Expand All @@ -39,19 +48,28 @@ const startExam: RequestHandler = async (req, res, next) => {

const submitExam: RequestHandler = async (req, res, next) => {
try {
const userId = req.userId;
const quizId = req.body.quizId;
const attemptedQuestion = req.body.attemptedQuestion;

const quiz = await Quiz.findById(quizId, { answers: 1, passingPercentage:1 });
const quiz = await Quiz.findById(quizId, {
answers: 1,
passingPercentage: 1,
createdBy: 1,
});
if (!quiz) {
const err = new ProjectError("No quiz found!");
err.statusCode = 404;
throw err;
}
const answers = quiz.answers;
const passingPercentage=quiz.passingPercentage;

const userId = req.userId;
if (quiz.createdBy.toString() === userId) {
const err = new ProjectError("You can't submit your own quiz!");
err.statusCode = 405;
throw err;
}
const answers = quiz.answers;
const passingPercentage = quiz.passingPercentage;
const allQuestions = Object.keys(answers);
const total = allQuestions.length;

Expand All @@ -66,24 +84,30 @@ const submitExam: RequestHandler = async (req, res, next) => {
score = score + 1;
}
}

let result="";
let percentage=0;
percentage=score/total*100;

if(percentage>=passingPercentage){
result+="Pass";
}
else{
result+="Fail";
let result = "";
let percentage = 0;
percentage = (score / total) * 100;

if (percentage >= passingPercentage) {
result += "Pass";
} else {
result += "Fail";
}

const report = new Report({ userId, quizId, score, total,percentage, result });
const report = new Report({
userId,
quizId,
score,
total,
percentage,
result,
});
const data = await report.save();
const resp: ReturnResponse = {
status: "success",
message: "Quiz submitted",
data: { total, score,result, reportId: data._id },
data: { total, score, result, reportId: data._id },
};
res.status(200).send(resp);
} catch (error) {
Expand Down

0 comments on commit 88c7377

Please sign in to comment.