Archived
1
0
Fork 0

Use middleware for user object on session and pass a test variable in case of... test

This commit is contained in:
Ethanell 2020-08-20 21:14:15 +02:00
parent 4201eae888
commit d98dcb0c07
17 changed files with 12 additions and 20 deletions

9
app.js
View file

@ -44,6 +44,12 @@ app.use(cookieParser());
app.use(session(sess));
app.use(express.static(path.join(__dirname, "public")));
app.use(i18n.init);
if(process.env.NODE_ENV === "test")
app.locals.test = true;
app.use((req, res, next) => {
res.locals.user = req.session.user;
next();
});
app.use("/", indexRouter);
app.use("/register", registerRouter);
@ -58,7 +64,7 @@ app.use("/admin", adminRouter);
// catch 404 and forward to error handler
app.use((req, res) => {
res.status(404);
res.render("error", {title: "SOD - Page not found", user: req.session.user, message: "Page not found", "error": {}})
res.render("error", {title: "SOD - Page not found", message: "Page not found", "error": {}})
});
// error handler
@ -66,7 +72,6 @@ app.use((err, req, res) => {
// render the error page
res.status(err.status || 500);
res.render("error", {
user: req.session.user,
message: err.message,
error: req.app.get("env") === "development" ? err : {}
});

View file

@ -6,8 +6,7 @@ let error = require("../../utils/error");
router.get("/", sessionCheck(3), (req, res) => {
res.render("admin/departments/add", {
title: "SOD - Departments administration",
user: req.session.user
title: "SOD - Departments administration"
});
}).post("/", sessionCheck(3), async (req, res) => {
if (!req.body.name)

View file

@ -14,7 +14,6 @@ router.get("/", sessionCheck(3), async (req, res) => {
res.render("admin/departments/edit", {
title: "SOD - Departments administration",
user: req.session.user,
department: department
});
}).post("/", sessionCheck(3), async (req, res) => {

View file

@ -8,7 +8,6 @@ let error = require("../../utils/error");
router.get("/", sessionCheck(3), async (req, res) => {
res.render("admin/departments/index", {
title: "SOD - Departments administration",
user: req.session.user,
departments: await models.Department.findAll()
});
})

View file

@ -7,7 +7,6 @@ let models = require("../../models");
router.get("/", sessionCheck(3), async (req, res) => {
res.render("admin/index", {
title: "SOD - Administration",
user: req.session.user,
sandwiches: await models.Sandwich.findAll(),
users: await models.User.findAll()
});

View file

@ -8,7 +8,6 @@ let error = require("../utils/error");
router.get("/", sessionCheck(3), async (req, res) => {
res.render("admin/orders", {
title: "SOD - Orders administration",
user: req.session.user,
orders: await models.Order.findAll({include: models.Sandwich, order: ["date"]})
});
}).post("/order/delete", sessionCheck(3), async (req, res) => {

View file

@ -6,8 +6,7 @@ let error = require("../../utils/error");
router.get("/", sessionCheck(3), (req, res) => {
res.render("admin/sandwiches/add", {
title: "SOD - Sandwiches administration",
user: req.session.user
title: "SOD - Sandwiches administration"
});
}).post("/", sessionCheck(3), async (req, res) => {
if (!req.body.name || !req.body.price)

View file

@ -14,7 +14,6 @@ router.get("/", sessionCheck(3), async (req, res) => {
res.render("admin/sandwiches/edit", {
title: "SOD - Sandwiches administration",
user: req.session.user,
sandwich: sandwich
});
}).post("/", sessionCheck(3), async (req, res) => {

View file

@ -8,7 +8,6 @@ let error = require("../../utils/error");
router.get("/", sessionCheck(3), async (req, res) => {
res.render("admin/sandwiches/index", {
title: "SOD - Sandwiches administration",
user: req.session.user,
sandwiches: await models.Sandwich.findAll()
});
})

View file

@ -7,7 +7,6 @@ let userCreate = require("../../utils/userCreate");
router.get("/", sessionCheck(3), async (req, res) => {
res.render("admin/users/add", {
title: "SOD - Users administration",
user: req.session.user,
departments: await models.Department.findAll()
});
}).post("/", sessionCheck(3), async (req, res) => {

View file

@ -15,7 +15,6 @@ router.get("/", sessionCheck(3), async (req, res) => {
res.render("admin/users/edit", {
title: "SOD - Users administration",
user: req.session.user,
targetUser: user,
departments: await models.Department.findAll()
});

View file

@ -8,7 +8,6 @@ let error = require("../../utils/error");
router.get("/", sessionCheck(3), async (req, res) => {
res.render("admin/users/index", {
title: "SOD - Users administration",
user: req.session.user,
users: await models.User.findAll()
});
})

View file

@ -5,7 +5,7 @@ let models = require("../models");
router.get("/", async (req, res) => {
let departments = await models.Department.findAll();
let sandwiches = await models.Sandwich.findAll();
res.render("index", { title: "SOD - Home", user: req.session.user, departments: departments, sandwiches: sandwiches });
res.render("index", { title: "SOD - Home", departments: departments, sandwiches: sandwiches });
});
module.exports = router;

View file

@ -23,7 +23,7 @@ router.get("/", sessionCheck(2), async (req, res) => {
orders[i.Order.DepartmentName][name][i.Order.id] = [];
orders[i.Order.DepartmentName][name][i.Order.id].push(i);
}
res.render("orders", {title: "SOD - Orders", user: req.session.user, orders: orders, date: date});
res.render("orders", {title: "SOD - Orders", orders: orders, date: date});
});
module.exports = router;

View file

@ -7,7 +7,6 @@ let userUpdate = require("./utils/userUpdate");
router.get("/", sessionCheck(0), async (req, res) => {
res.render("profile", {
title: "SOD - Profile",
user: req.session.user,
departments: await models.Department.findAll(),
orders: await models.Order.findAll({
where: {UserUsername: req.session.user.username},

View file

@ -10,7 +10,6 @@ router.get("/", sessionCheck(1), async (req, res) => {
res.render("sandwiches", {
title: "SOD - Sandwiches",
user: req.session.user,
sandwiches: await models.SandwichOrder.findAll({
attributes: ["SandwichName", [sequelize.fn("COUNT", sequelize.col("SandwichName")), "number"]],
where: {date: date},

View file

@ -1,4 +1,4 @@
module.exports = (req, res, message, status, subMessage) => {
res.status(status || 500);
res.render("error", {user: req.session.user, message: message, error: {status: subMessage || undefined}});
res.render("error", {message: message, error: {status: subMessage || undefined}});
};