From 5ba48226c97b471bd20c21f608fc67eda740f4a1 Mon Sep 17 00:00:00 2001 From: Nighthawx <41290641+VikramjeetD@users.noreply.github.com> Date: Mon, 20 Apr 2020 15:54:25 +0530 Subject: [PATCH] Add router to serve raw admin stats --- package.json | 1 + routes/api/dashboard.js | 50 +++++++++++++++++++++++++++++++++++++++++ routes/api/helForm.js | 16 ++++--------- server.js | 3 +++ 4 files changed, 58 insertions(+), 12 deletions(-) create mode 100644 routes/api/dashboard.js diff --git a/package.json b/package.json index f12d671..ba1092f 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/routes/api/dashboard.js b/routes/api/dashboard.js new file mode 100644 index 0000000..5dba634 --- /dev/null +++ b/routes/api/dashboard.js @@ -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; diff --git a/routes/api/helForm.js b/routes/api/helForm.js index 74dd69d..636a9f5 100644 --- a/routes/api/helForm.js +++ b/routes/api/helForm.js @@ -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() ], @@ -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() ] ], diff --git a/server.js b/server.js index 2c79ca6..0f52739 100644 --- a/server.js +++ b/server.js @@ -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 */ @@ -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"));