Skip to content
This repository has been archived by the owner on Dec 27, 2020. It is now read-only.

Mess Manager Functionality #40

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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} 🔥 `));
29 changes: 29 additions & 0 deletions middleware/authmess.js
Original file line number Diff line number Diff line change
@@ -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 ;
1 change: 1 addition & 0 deletions models/Admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ const AdminSchema = new mongoose.Schema({

const Admin = mongoose.model("admin", AdminSchema);
module.exports = Admin;

17 changes: 17 additions & 0 deletions models/Mess.js
Original file line number Diff line number Diff line change
@@ -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);
77 changes: 28 additions & 49 deletions routes/api/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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) {
Expand Down Expand Up @@ -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("/");
// }
// });
// }
// });
// }
// });
}
);

Expand All @@ -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 },
Expand Down
14 changes: 14 additions & 0 deletions routes/api/mess.js
Original file line number Diff line number Diff line change
@@ -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;
36 changes: 33 additions & 3 deletions routes/api/super_user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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;
13 changes: 13 additions & 0 deletions views/mess_manager.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<%- include('partials/header'); -%>
<div class="container">
<div class="jumbotron">
<h5>
Hello Everyone I m Mess Manager
</h5>

<h2>
Number of Students present
</h2>
</div>
</div>
<%- include('partials/footer'); -%>
2 changes: 1 addition & 1 deletion views/partials/header.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<%}else{%>
<li><a class="signed-as" href="#">Signed in as <%=currentUser.name%></a></li>
<li>
<%if(clientType == "admin"|| clientType=="superuser"){%>
<%if(clientType == "admin"|| clientType=="superuser"||clientType=="mess"){%>
<!-- <a href="#">Edit Profile </a> -->
<%}else{%>
<a href="/user/dashboard/profile/<%=currentUser.googleId%>">
Expand Down
46 changes: 44 additions & 2 deletions views/super_user.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div class="container">
<div class="jumbotron">
<h2>Add Admins Here</h2>
<form action="/superuser/dashboard" method="POST">
<form action="/superuser/admin/add" method="POST">
<div class="form-group">
<label>Name:</label><br />
<input
Expand All @@ -33,17 +33,59 @@
</div>
</div>


<div class="container">
<div class="jumbotron">
<h2>Add Mess Managers Here</h2>
<form action="/superuser/mess/add" method="POST">
<div class="form-group">
<label>Name:</label><br />
<input
type="text"
name="mains[name]"
/>
</div>

<div class="form-group">
<label>Email:</label><br />
<input
type="text"
name="mains[email]"
/>
</div>

<div class="form-group">
<br />
<button class="btn btn-primary blue-grey darken-1">Submit!</button>
</div>

</form>
</div>
</div>


<div class="container">
<div class="jumbotron">
<h3>Adjust Admins</h3>
<%admins.forEach(function(admin){%>
<div><%=admin.name%></div>
<div><%=admin.email%></div>
<a href="/superuser/remove/<%=admin.email%>">Delete</a>
<a href="/superuser/remove/admin/<%=admin.email%>">Delete</a>
<br>
<%})%>
</div>
</div>

<div class="container">
<div class="jumbotron">
<h3>Adjust Mess Managers</h3>
<%messes.forEach(function(mess){%>
<div><%=mess.name%></div>
<div><%=mess.email%></div>
<a href="/superuser/remove/mess/<%=mess.email%>">Delete</a>
<br>
<%})%>
</div>
</div>

<%- include('partials/footer'); -%>