Skip to content

Commit

Permalink
Add router to serve raw admin stats
Browse files Browse the repository at this point in the history
  • Loading branch information
VikramjeetD authored and AetherPrior committed Jun 13, 2020
1 parent cc91e43 commit 5ba4822
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 12 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dotenv": "^6.2.0",
"express": "^4.17.1",
"express-validator": "^6.4.0",
"moment": "^2.24.0",
"mongoose": "^5.9.6",
"node-sass": "^4.5.3",
"node-sass-middleware": "^0.11.0",
Expand Down
50 changes: 50 additions & 0 deletions routes/api/dashboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const express = require("express");
const router = express.Router();
const { check, query, validationResult } = require("express-validator");
const loggedIn = require("../../middleware/auth");
const TimeTable = require("../../models/TimeTable");
const Student = require("../../models/Student");
const Login = require("../../models/Login");

const mscBranches = ["BIO", "CHEM", "ECO", "MATH", "PHY"];

router.get("/", [], async (req, res) => {
try {
let nLogins = await Login.countDocuments();
let userLogins = await Login.find({}, "-_id -__v");
let nUniqueLogins = (await Login.find().distinct("userId")).length;
let timetablesCreated = await TimeTable.find({}, "date").populate(
"ownerId",
"-_id branch year"
);
timetablesCreated = timetablesCreated.map(function (tt) {
// null check
if (tt.ownerId) {
let branches = tt.ownerId.branch;

if (branches.length == 2) {
if (mscBranches.includes(branches[0])) {
tt.ownerId.branch = [branches[0]];
} else if (mscBranches.includes(branches[1])) {
tt.ownerId.branch = [branches[1]];
} else {
tt.ownerId.branch = [branches[0]];
}
}
}
return tt;
});

res.status(200).json({
nLogins: nLogins,
nUniqueLogin: nUniqueLogins,
userLogins: userLogins,
timetablesCreated: timetablesCreated
});
} catch (err) {
console.error(err.message);
res.status(500).send("Server error");
}
});

module.exports = router;
16 changes: 4 additions & 12 deletions routes/api/helForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ router.post(
"/submit",
[
loggedIn,
check("slotNumber", "Slot is required")
.not()
.isEmpty(),
check("slotNumber", "Slot is required").not().isEmpty(),
check("slotNumber", "Slot should be a number").isNumeric(),
check("humanitiesElectives").isArray()
],
Expand Down Expand Up @@ -68,17 +66,11 @@ router.post(
[
loggedIn,
[
check("email", "Email is required")
.not()
.isEmpty(),
check("email", "Email is required").not().isEmpty(),
check("email", "Invalid email").isEmail(),
check("studentBranch", "Invalid branch type").isArray(),
check("studentBranch", "Branch is required")
.not()
.isEmpty(),
check("year", "Year is required")
.not()
.isEmpty(),
check("studentBranch", "Branch is required").not().isEmpty(),
check("year", "Year is required").not().isEmpty(),
check("year", "Year should be a number").isNumeric()
]
],
Expand Down
3 changes: 3 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const auth = require("./routes/api/auth.js");
const helForm = require("./routes/api/helForm.js");
const helData = require("./routes/api/helData.js");
const timetable = require("./routes/api/timetable.js");
const dashboard = require("./routes/api/dashboard.js");

const configuration = require("./config/constants.js");

/* Express setup */
Expand Down Expand Up @@ -44,6 +46,7 @@ app.use("/api", auth);
app.use("/api/helform", helForm);
app.use("/api/helData", helData);
app.use("/api/timetable", timetable);
app.use("/api/dashboard", dashboard);
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));

Expand Down

0 comments on commit 5ba4822

Please sign in to comment.