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/PassportMiddleware.js b/middleware/PassportMiddleware.js index 15a901a..8eeb281 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" || @@ -69,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/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/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 1446a55..f78c7a9 100644 --- a/routes/api/auth.js +++ b/routes/api/auth.js @@ -3,13 +3,14 @@ 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(); router.get( "/google", passport.authenticate("google", { - scope: ["profile", "email"] + scope: ["profile", "email"], }) ); @@ -19,99 +20,71 @@ 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("/"); - } - }); - } 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"); + } }); - - // 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("/"); - // } - // }); - // } - // }); - // } - // }); } ); @@ -121,12 +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==="superuser"){ + } 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 }, { $pull: { accessToken: { $in: [req.session.token] } } } diff --git a/routes/api/mess.js b/routes/api/mess.js new file mode 100644 index 0000000..193eb63 --- /dev/null +++ b/routes/api/mess.js @@ -0,0 +1,30 @@ +const express = require("express"); +const bodyParser = require("body-parser"); +const router = express.Router(); +const auth = require("../../middleware/authmess"); +const Mess = require("../../models/Mess"); +const Leave = require("../../models/Leave"); +const moment=require("moment"); +router.use(bodyParser.urlencoded({ extended: true })); + +router.get("/dashboard",auth,async (req,res)=>{ + + 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 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'); -%> +