Skip to content

Commit

Permalink
Merge pull request #49 from Black-Sirius69/master
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
Mr-Emerald-Wolf committed Sep 22, 2023
2 parents e6909ed + 02e438f commit 6c03d22
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 6 deletions.
6 changes: 3 additions & 3 deletions api/controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 });
Expand Down
38 changes: 38 additions & 0 deletions api/controllers/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,49 @@ 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,
getByRound,
getQuestionByID,
updateQuestion,
deleteQuestion,
dashboard,
};
3 changes: 2 additions & 1 deletion api/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const userSchema = new mongoose.Schema(
email: {
type: String,
required: true,
unique: true,
},
regNo: {
type: String,
Expand Down Expand Up @@ -52,7 +53,7 @@ const userSchema = new mongoose.Schema(
},
{
timestamps: true,
}
},
);
const User = mongoose.model("User", userSchema);
module.exports = User;
2 changes: 2 additions & 0 deletions api/routes/questionsRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
getQuestionByID,
updateQuestion,
deleteQuestion,
dashboard,
} = require("../controllers/questions");

router.use(verifyAccessToken);
Expand All @@ -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;
10 changes: 8 additions & 2 deletions api/seeders/questions_seeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand All @@ -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++) {
Expand All @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions api/seeders/test.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 6c03d22

Please sign in to comment.