From 02e438fd0fdf4c47af2b4cc7e546523b8110d529 Mon Sep 17 00:00:00 2001 From: Vedant Matanhelia Date: Fri, 22 Sep 2023 13:20:18 +0530 Subject: [PATCH] Fixes User Model: Unique email questions created new route get/dashboard (inherits from getRound) remade seeder given a test.py file to edit to add anything you want to seeders --- api/controllers/authController.js | 6 ++--- api/controllers/questions.js | 38 +++++++++++++++++++++++++++++++ api/models/User.js | 3 ++- api/routes/questionsRouter.js | 2 ++ api/seeders/questions_seeder.js | 10 ++++++-- api/seeders/test.py | 13 +++++++++++ 6 files changed, 66 insertions(+), 6 deletions(-) create mode 100644 api/seeders/test.py diff --git a/api/controllers/authController.js b/api/controllers/authController.js index aab5fcd..af5d2ea 100644 --- a/api/controllers/authController.js +++ b/api/controllers/authController.js @@ -21,11 +21,11 @@ const authController = { const accessToken = jwtController.signAccessToken( user.regNo, user.userRole, - user.tokenVersion + user.tokenVersion, ); const refreshToken = jwtController.signRefreshToken( user.regNo, - user.userRole + user.userRole, ); user.refreshToken = refreshToken; await user.save(); @@ -125,7 +125,7 @@ const authController = { const newAccessToken = jwtController.signAccessToken( user.regNo, user.userRole, - user.tokenVersion + user.tokenVersion, ); res.header("Authorization", `Bearer ${newAccessToken}`); res.json({ accessToken: newAccessToken }); diff --git a/api/controllers/questions.js b/api/controllers/questions.js index 21ffcf0..b37471e 100644 --- a/api/controllers/questions.js +++ b/api/controllers/questions.js @@ -153,6 +153,43 @@ async function createQuestion(req, res) { } } +async function dashboard(req, res) { + try { + let questionByRound; + const decoded = req.user; + if (decoded.userRole == "admin") { + questionByRound = await ques + .where("isActive") + .equals(true) + .populate("testCases"); + } else { + questionByRound = await ques + .where("isActive") + .equals(true) + .populate({ path: "testCases", match: { hidden: false } }); + } + if (questionByRound.length == 0) { + return res.status(404).json({ + message: "No questions found", + }); + } else { + const data = []; + questionByRound.forEach((element) => { + const obj = { + name: element.name, + points: element.points, + }; + data.push(obj); + }); + return res.status(201).json(data); + } + } catch (error) { + return res.status(500).json({ + message: error.message, + }); + } +} + module.exports = { createQuestion, getAll, @@ -160,4 +197,5 @@ module.exports = { getQuestionByID, updateQuestion, deleteQuestion, + dashboard, }; diff --git a/api/models/User.js b/api/models/User.js index 6367414..f87cc75 100644 --- a/api/models/User.js +++ b/api/models/User.js @@ -8,6 +8,7 @@ const userSchema = new mongoose.Schema( email: { type: String, required: true, + unique: true, }, regNo: { type: String, @@ -52,7 +53,7 @@ const userSchema = new mongoose.Schema( }, { timestamps: true, - } + }, ); const User = mongoose.model("User", userSchema); module.exports = User; diff --git a/api/routes/questionsRouter.js b/api/routes/questionsRouter.js index 28171b5..5c41d1c 100644 --- a/api/routes/questionsRouter.js +++ b/api/routes/questionsRouter.js @@ -11,6 +11,7 @@ const { getQuestionByID, updateQuestion, deleteQuestion, + dashboard, } = require("../controllers/questions"); router.use(verifyAccessToken); @@ -20,5 +21,6 @@ router.post("/getId", getQuestionByID); router.post("/getRound", getByRound); router.put("/updateQuestion/:id", verifyAdminToken, updateQuestion); router.delete("/deleteQuestion/:id", verifyAdminToken, deleteQuestion); +router.post("/get/dashboard", dashboard); module.exports = router; diff --git a/api/seeders/questions_seeder.js b/api/seeders/questions_seeder.js index cebb10a..999c45b 100644 --- a/api/seeders/questions_seeder.js +++ b/api/seeders/questions_seeder.js @@ -18,6 +18,9 @@ const addData = async (Questions) => { const questions = JSON.parse(JSON.stringify(Questions)); questions.forEach((question) => (question.testCases = [])); + await TestCaseModel.deleteMany({}); + await QuestionModel.deleteMany({}); + const insertedQuestions = await QuestionModel.insertMany(questions); const testCases = []; @@ -26,7 +29,6 @@ const addData = async (Questions) => { tc, ) => (tc.question = insertedQuestions[i]._id)); testCases.push(...Questions[i].testCases); - console.log(Questions[i].testCases); } for (let i = 0; i < testCases.length; i++) { @@ -42,9 +44,13 @@ fs.readFile("final_seeder.json", "utf8", (err, data) => { try { mongoose - .connect("mongodb://localhost:27017/mydb") + // .connect("mongodb://localhost:27017/mydb") + .connect( + "mongodb+srv://doadmin:18EA0xKN3IC7259W@db-mongodb-blr1-51446-2eb2b2b3.mongo.ondigitalocean.com/admin?tls=true&authSource=admin&replicaSet=db-mongodb-blr1-51446", + ) .then(() => console.log("Connection Successfull")) .then(() => addData(JSON.parse(data))) + .then(() => console.log("Done")) .catch("Error connecting"); } catch (parseError) { console.error(parseError.message); diff --git a/api/seeders/test.py b/api/seeders/test.py new file mode 100644 index 0000000..88672a6 --- /dev/null +++ b/api/seeders/test.py @@ -0,0 +1,13 @@ +import json + + +with open("final_seeder.json") as file: + global data + data = json.loads(file.read()) + +for question in data: + question["points"] = 10 + + +with open("final_seeder_new.json", "w") as file: + file.write(json.dumps(data))