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 all commits
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} 🔥 `));
9 changes: 4 additions & 5 deletions middleware/PassportMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" ||
Expand Down Expand Up @@ -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 {
Expand Down
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);
8 changes: 4 additions & 4 deletions models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand Down
168 changes: 73 additions & 95 deletions routes/api/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
})
);

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

Expand All @@ -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] } } }
Expand Down
30 changes: 30 additions & 0 deletions routes/api/mess.js
Original file line number Diff line number Diff line change
@@ -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;
Loading