From 446678ee5100c4f00d7c4b7d3e0b8baebb3cb8ba Mon Sep 17 00:00:00 2001 From: lav-joshi Date: Tue, 30 Jun 2020 06:02:32 +0530 Subject: [PATCH 1/3] Mess Manager Functionality --- app.js | 2 + middleware/authmess.js | 29 +++++++++++++++ models/Admin.js | 1 + models/Mess.js | 17 +++++++++ routes/api/auth.js | 77 ++++++++++++++------------------------- routes/api/mess.js | 14 +++++++ routes/api/super_user.js | 36 ++++++++++++++++-- views/mess_manager.ejs | 13 +++++++ views/partials/header.ejs | 2 +- views/super_user.ejs | 46 ++++++++++++++++++++++- 10 files changed, 182 insertions(+), 55 deletions(-) create mode 100644 middleware/authmess.js create mode 100644 models/Mess.js create mode 100644 routes/api/mess.js create mode 100644 views/mess_manager.ejs diff --git a/app.js b/app.js index b98b9b3..0be484a 100644 --- a/app.js +++ b/app.js @@ -6,6 +6,7 @@ const authRoutes = require("./routes/api/auth"); const user = require("./routes/api/user"); const admin = require("./routes/api/admin"); const superuser = require("./routes/api/super_user"); +const mess = require("./routes/api/mess"); const passport = require("passport"); const Admin = require("./models/Admin"); const Super_User = require("./models/Super_User"); @@ -67,6 +68,7 @@ app.use("/auth", authRoutes); app.use("/user", user); app.use("/admin", admin); app.use("/superuser", superuser); +app.use("/mess",mess); const PORT = process.env.PORT || 3000; app.listen(PORT, () => console.log(`Server up on port ${PORT} 🔥 `)); diff --git a/middleware/authmess.js b/middleware/authmess.js new file mode 100644 index 0000000..75bffaa --- /dev/null +++ b/middleware/authmess.js @@ -0,0 +1,29 @@ +const Mess = require("../models/Mess"); + +const auth = async (req, res, next) => { + try { + if (req.session.token) { + Mess.findOne({ email: req.user.email }, function (err, mess) { + if (mess) { + var x = mess.accessToken.indexOf(req.session.token); + if (x !== -1) { + next(); + } else { + res.cookie("token", ""); + res.redirect("/auth/logout"); + } + } else if (err) { + res.cookie("token", ""); + res.redirect("/"); + } + }); + } else { + res.cookie("token", ""); + res.redirect("/"); + } + } catch (e) { + res.redirect("/"); + } + }; + +module.exports = auth ; diff --git a/models/Admin.js b/models/Admin.js index d8bb540..c752c35 100644 --- a/models/Admin.js +++ b/models/Admin.js @@ -8,3 +8,4 @@ const AdminSchema = new mongoose.Schema({ const Admin = mongoose.model("admin", AdminSchema); module.exports = Admin; + diff --git a/models/Mess.js b/models/Mess.js new file mode 100644 index 0000000..70410c9 --- /dev/null +++ b/models/Mess.js @@ -0,0 +1,17 @@ +const mongoose = require("mongoose"); + +const MessSchema = new mongoose.Schema({ + name: { + type: String, + trim: true, + required: true + }, + email: { + type: String, + trim: true, + required: true + }, + accessToken: [String] +}); + +module.exports = mongoose.model("messman", MessSchema); diff --git a/routes/api/auth.js b/routes/api/auth.js index 1446a55..b357ed7 100644 --- a/routes/api/auth.js +++ b/routes/api/auth.js @@ -3,6 +3,7 @@ const passport = require("passport"); const Admin = require("../../models/Admin"); const User = require("../../models/User"); const Super_User = require("../../models/Super_User"); +const Mess = require("../../models/Mess"); const router = express.Router(); @@ -19,23 +20,36 @@ router.get( (req, res) => { req.session.token = req.user.accessToken[req.user.accessToken.length - 1]; res.cookie("token", req.session.token); - // console.log(req.session.token); Super_User.findOne({email:req.user.email},async (err,superuser)=>{ if(err) Error(err); if(!superuser){ Admin.findOne({ email: req.user.email }, function (err, admin) { if (err) Error(err); if (!admin) { - User.findOne({ email: req.user.email }, function (err, user) { - if (err) Error(err); - var x = user.accessToken.indexOf(req.session.token); - if (x !== -1) { - req.session.client = "user"; - res.redirect("/user/dashboard"); - } else { - res.redirect("/"); + Mess.findOne({},async (err,mess)=>{ + if(err) Error(err); + if(!mess){ + User.findOne({ email: req.user.email }, function (err, user) { + if (err) Error(err); + var x = user.accessToken.indexOf(req.session.token); + if (x !== -1) { + req.session.client = "user"; + res.redirect("/user/dashboard"); + } else { + res.redirect("/"); + } + }); + }else{ + mess.accessToken.push(req.session.token); + await mess.save(); + User.deleteOne({email:req.user.email},(err,user)=>{ + if(err) res.redirect("/"); + }); + req.session.client = "mess"; + res.redirect("/mess/dashboard"); } }); + } else { User.deleteOne({ email: req.user.email }, function (err, user) { if (err) { @@ -72,46 +86,6 @@ router.get( res.redirect("/superuser/dashboard"); } }); - - // Admin.findOne({ email: req.user.email }, function (err, admin) { - // if (err) Error(err); - // if (!admin) { - // User.findOne({ email: req.user.email }, function (err, user) { - // if (err) Error(err); - // var x = user.accessToken.indexOf(req.session.token); - // if (x !== -1) { - // req.session.client = "user"; - // res.redirect("/user/dashboard"); - // } else { - // res.redirect("/"); - // } - // }); - // } else { - // User.deleteOne({ email: req.user.email }, function (err, user) { - // if (err) { - // res.redirect("/"); - // } else if (user) { - // Admin.findOne({ email: req.user.email }, async function ( - // err, - // admin - // ) { - // if (err) Error(err); - // admin.accessToken.push(req.session.token); - // await admin.save(); - // console.log("hello2"); - // var x = admin.accessToken.indexOf(req.session.token); - // if (x !== -1) { - // req.session.status = "applied"; - // req.session.client = "admin"; - // res.redirect("/admin/dashboard"); - // } else { - // res.redirect("/"); - // } - // }); - // } - // }); - // } - // }); } ); @@ -126,6 +100,11 @@ router.get("/logout", async (req, res) => { { email: req.user.email }, { $pull: { accessToken: { $in: [req.session.token] } } } ); + }else if(req.session.client==="mess"){ + await Mess.updateOne( + { email: req.user.email }, + { $pull: { accessToken: { $in: [req.session.token] } } } + ); }else if(req.session.client==="superuser"){ await Super_User.updateOne( { email: req.user.email }, diff --git a/routes/api/mess.js b/routes/api/mess.js new file mode 100644 index 0000000..4b624cd --- /dev/null +++ b/routes/api/mess.js @@ -0,0 +1,14 @@ +const express = require("express"); +const bodyParser = require("body-parser"); +const router = express.Router(); +const auth = require("../../middleware/authmess"); +const Mess = require("../../models/Mess"); +const Leaves = require("../../models/Leave"); + +router.use(bodyParser.urlencoded({ extended: true })); + +router.get("/dashboard",auth,async (req,res)=>{ + res.render("mess_manager",{currentUser:req.user,clientType:req.session.client}); +}); + +module.exports = router; \ No newline at end of file diff --git a/routes/api/super_user.js b/routes/api/super_user.js index 4025b38..dc673c6 100644 --- a/routes/api/super_user.js +++ b/routes/api/super_user.js @@ -4,16 +4,25 @@ const auth = require("../../middleware/authsuperuser"); const Admin = require("../../models/Admin"); const { reset } = require("nodemon"); const router = express.Router(); +const Mess = require("../../models/Mess"); router.use(bodyParser.urlencoded({ extended: true })); router.get("/dashboard",auth,async (req,res)=>{ Admin.find({},(err,admins)=>{ - res.render("super_user",{currentUser:req.user,clientType:req.session.client,admins:admins}); + if(err) Error(err); + else{ + Mess.find({},(err,messes)=>{ + if(err) Error(err); + else{ + res.render("super_user",{currentUser:req.user,clientType:req.session.client,admins:admins,messes:messes}); + } + }); + } }); }); -router.post("/dashboard",auth,async(req,res)=>{ +router.post("/admin/add",auth,async(req,res)=>{ Admin.create(req.body.mains,(err,admin)=>{ if(err){ res.redirect("/superuser/dashboard"); @@ -23,7 +32,18 @@ router.post("/dashboard",auth,async(req,res)=>{ }); }); -router.get("/remove/:email",auth,async (req,res)=>{ +router.post("/mess/add",auth,async(req,res)=>{ + Mess.create(req.body.mains,(err,mess)=>{ + if(err){ + res.redirect("/superuser/dashboard"); + }else{ + res.redirect("/superuser/dashboard"); + } + }); +}); + + +router.get("/remove/admin/:email",auth,async (req,res)=>{ Admin.deleteOne({email:req.params.email},(err,admin)=>{ if(err){ res.redirect("/superuser/dashboard"); @@ -33,4 +53,14 @@ router.get("/remove/:email",auth,async (req,res)=>{ }); }); +router.get("/remove/mess/:email",auth,async (req,res)=>{ + Mess.deleteOne({email:req.params.email},(err,mess)=>{ + if(err){ + res.redirect("/superuser/dashboard"); + }else{ + res.redirect("/superuser/dashboard"); + } + }); +}); + module.exports = router; \ No newline at end of file diff --git a/views/mess_manager.ejs b/views/mess_manager.ejs new file mode 100644 index 0000000..942c422 --- /dev/null +++ b/views/mess_manager.ejs @@ -0,0 +1,13 @@ +<%- include('partials/header'); -%> +
+
+
+ Hello Everyone I m Mess Manager +
+ +

+ Number of Students present +

+
+
+<%- include('partials/footer'); -%> diff --git a/views/partials/header.ejs b/views/partials/header.ejs index 7428de8..e23c997 100644 --- a/views/partials/header.ejs +++ b/views/partials/header.ejs @@ -44,7 +44,7 @@ <%}else{%>
  • Signed in as <%=currentUser.name%>
  • - <%if(clientType == "admin"|| clientType=="superuser"){%> + <%if(clientType == "admin"|| clientType=="superuser"||clientType=="mess"){%> <%}else{%> diff --git a/views/super_user.ejs b/views/super_user.ejs index 0aa84af..e4b35f2 100644 --- a/views/super_user.ejs +++ b/views/super_user.ejs @@ -7,7 +7,7 @@

    Add Admins Here

    -
    +

    + +
    +
    +

    Add Mess Managers Here

    + +
    +
    + +
    + +
    +
    + +
    + +
    +
    + +
    + + +
    +
    + +
    +
    +
    +

    Adjust Mess Managers

    + <%messes.forEach(function(mess){%> +
    <%=mess.name%>
    +
    <%=mess.email%>
    + Delete +
    + <%})%> +
    +
    <%- include('partials/footer'); -%> From 3e7b77d100b809554574fba286d040dece2399ed Mon Sep 17 00:00:00 2001 From: lav-joshi Date: Mon, 20 Jul 2020 00:31:18 +0530 Subject: [PATCH 2/3] Improve --- middleware/PassportMiddleware.js | 1 - routes/api/auth.js | 2 +- routes/api/mess.js | 22 +++++++++++++++++++--- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/middleware/PassportMiddleware.js b/middleware/PassportMiddleware.js index 15a901a..8da9208 100644 --- a/middleware/PassportMiddleware.js +++ b/middleware/PassportMiddleware.js @@ -26,7 +26,6 @@ passport.use( var str = profile.emails[0].value; var res = str.split("@"); - if ( (res[1] === "iiitl.ac.in" && str[0] === "c") || str[0] === "l" || diff --git a/routes/api/auth.js b/routes/api/auth.js index b357ed7..b004091 100644 --- a/routes/api/auth.js +++ b/routes/api/auth.js @@ -26,7 +26,7 @@ router.get( Admin.findOne({ email: req.user.email }, function (err, admin) { if (err) Error(err); if (!admin) { - Mess.findOne({},async (err,mess)=>{ + Mess.findOne({email:req.user.email},async (err,mess)=>{ if(err) Error(err); if(!mess){ User.findOne({ email: req.user.email }, function (err, user) { diff --git a/routes/api/mess.js b/routes/api/mess.js index 4b624cd..193eb63 100644 --- a/routes/api/mess.js +++ b/routes/api/mess.js @@ -3,12 +3,28 @@ const bodyParser = require("body-parser"); const router = express.Router(); const auth = require("../../middleware/authmess"); const Mess = require("../../models/Mess"); -const Leaves = require("../../models/Leave"); - +const Leave = require("../../models/Leave"); +const moment=require("moment"); router.use(bodyParser.urlencoded({ extended: true })); router.get("/dashboard",auth,async (req,res)=>{ - res.render("mess_manager",{currentUser:req.user,clientType:req.session.client}); + + Leave.find({},function(err,leaves){ + if(err) Error(err); + else{ + let arr=[]; + leaves.forEach((s,i)=>{ + if( moment(Date.now()).format("ddd MMM D, YYYY") <= moment(s.Return).format("ddd MMM D, YYYY") && moment(Date.now()).format("ddd MMM D, YYYY")>= moment(s.Leave).format("ddd MMM D, YYYY") && s.Approve===true) + arr.push(s); + }); + console.log(arr); + res.render("mess_manager",{currentUser:req.user,clientType:req.session.client}); + } + }); + // if(moment(Date.now()).format("ddd MMM D, YYYY")>= moment("2020-06-16T18:30:00.000+00:00").format("ddd MMM D, YYYY") ) + // console.log("ass"); + // else + // console.log("fuck"); }); module.exports = router; \ No newline at end of file From d79d6c76a9bd7fd490411638da0edfddd0a70c17 Mon Sep 17 00:00:00 2001 From: blurry-x-face Date: Mon, 20 Jul 2020 01:13:30 +0530 Subject: [PATCH 3/3] remove required field from year,rollNumber --- middleware/PassportMiddleware.js | 8 +- models/User.js | 8 +- routes/api/auth.js | 137 +++++++++++++++---------------- 3 files changed, 76 insertions(+), 77 deletions(-) diff --git a/middleware/PassportMiddleware.js b/middleware/PassportMiddleware.js index 8da9208..8eeb281 100644 --- a/middleware/PassportMiddleware.js +++ b/middleware/PassportMiddleware.js @@ -68,10 +68,10 @@ passport.use( name: profile.displayName, email: profile.emails[0].value, displayPicture: profile.photos[0].value, - branch: branch, - year: year, - course: course, - rollNo: rollNo + branch: branch || "", + year: year || "", + course: course || "", + rollNo: rollNo || "" }); user.accessToken.push(accessToken); try { diff --git a/models/User.js b/models/User.js index 52e11f1..7e2a329 100644 --- a/models/User.js +++ b/models/User.js @@ -15,22 +15,22 @@ const UserSchema = new mongoose.Schema({ branch: { type: String, trim: true, - required: true + // required: true }, year: { type: String, trim: true, - required: true + // required: true }, course: { type: String, trim: true, - required: true + // required: true }, rollNo: { type: String, trim: true, - required: true, + // required: true, index: true, unique: true }, diff --git a/routes/api/auth.js b/routes/api/auth.js index b004091..f78c7a9 100644 --- a/routes/api/auth.js +++ b/routes/api/auth.js @@ -10,7 +10,7 @@ const router = express.Router(); router.get( "/google", passport.authenticate("google", { - scope: ["profile", "email"] + scope: ["profile", "email"], }) ); @@ -20,71 +20,70 @@ router.get( (req, res) => { req.session.token = req.user.accessToken[req.user.accessToken.length - 1]; res.cookie("token", req.session.token); - Super_User.findOne({email:req.user.email},async (err,superuser)=>{ - if(err) Error(err); - if(!superuser){ - Admin.findOne({ email: req.user.email }, function (err, admin) { - if (err) Error(err); - if (!admin) { - Mess.findOne({email:req.user.email},async (err,mess)=>{ - if(err) Error(err); - if(!mess){ - User.findOne({ email: req.user.email }, function (err, user) { - if (err) Error(err); - var x = user.accessToken.indexOf(req.session.token); - if (x !== -1) { - req.session.client = "user"; - res.redirect("/user/dashboard"); - } else { - res.redirect("/"); - } - }); - }else{ - mess.accessToken.push(req.session.token); - await mess.save(); - User.deleteOne({email:req.user.email},(err,user)=>{ - if(err) res.redirect("/"); - }); - req.session.client = "mess"; - res.redirect("/mess/dashboard"); - } - }); - - } else { - User.deleteOne({ email: req.user.email }, function (err, user) { - if (err) { - res.redirect("/"); - } else if (user) { - Admin.findOne({ email: req.user.email }, async function ( - err, - admin - ) { - if (err) Error(err); - admin.accessToken.push(req.session.token); - await admin.save(); - console.log("hello2"); - var x = admin.accessToken.indexOf(req.session.token); - if (x !== -1) { - req.session.status = "applied"; - req.session.client = "admin"; - res.redirect("/admin/dashboard"); - } else { - res.redirect("/"); - } - }); - } - }); - } - }); - }else{ - superuser.accessToken.push(req.session.token); - await superuser.save(); - User.deleteOne({email:req.user.email},(err,user)=>{ - if(err) res.redirect("/"); - }); - req.session.client = "superuser"; - res.redirect("/superuser/dashboard"); - } + Super_User.findOne({ email: req.user.email }, async (err, superuser) => { + if (err) Error(err); + if (!superuser) { + Admin.findOne({ email: req.user.email }, function(err, admin) { + if (err) Error(err); + if (!admin) { + Mess.findOne({ email: req.user.email }, async (err, mess) => { + if (err) Error(err); + if (!mess) { + User.findOne({ email: req.user.email }, function(err, user) { + if (err) Error(err); + var x = user.accessToken.indexOf(req.session.token); + if (x !== -1) { + req.session.client = "user"; + res.redirect("/user/dashboard"); + } else { + res.redirect("/"); + } + }); + } else { + mess.accessToken.push(req.session.token); + await mess.save(); + User.deleteOne({ email: req.user.email }, (err, user) => { + if (err) res.redirect("/"); + }); + req.session.client = "mess"; + res.redirect("/mess/dashboard"); + } + }); + } else { + User.deleteOne({ email: req.user.email }, function(err, user) { + if (err) { + res.redirect("/"); + } else if (user) { + Admin.findOne({ email: req.user.email }, async function( + err, + admin + ) { + if (err) Error(err); + admin.accessToken.push(req.session.token); + await admin.save(); + console.log("hello2"); + var x = admin.accessToken.indexOf(req.session.token); + if (x !== -1) { + req.session.status = "applied"; + req.session.client = "admin"; + res.redirect("/admin/dashboard"); + } else { + res.redirect("/"); + } + }); + } + }); + } + }); + } else { + superuser.accessToken.push(req.session.token); + await superuser.save(); + User.deleteOne({ email: req.user.email }, (err, user) => { + if (err) res.redirect("/"); + }); + req.session.client = "superuser"; + res.redirect("/superuser/dashboard"); + } }); } ); @@ -95,17 +94,17 @@ router.get("/logout", async (req, res) => { { email: req.user.email }, { $pull: { accessToken: { $in: [req.session.token] } } } ); - } else if(req.session.client==="admin"){ + } else if (req.session.client === "admin") { await Admin.updateOne( { email: req.user.email }, { $pull: { accessToken: { $in: [req.session.token] } } } ); - }else if(req.session.client==="mess"){ + } else if (req.session.client === "mess") { await Mess.updateOne( { email: req.user.email }, { $pull: { accessToken: { $in: [req.session.token] } } } ); - }else if(req.session.client==="superuser"){ + } else if (req.session.client === "superuser") { await Super_User.updateOne( { email: req.user.email }, { $pull: { accessToken: { $in: [req.session.token] } } }