2020-08-18 17:54:00 +02:00
|
|
|
let express = require("express");
|
|
|
|
let router = express.Router();
|
|
|
|
let sessionCheck = require("../middlewares/sessionCheck");
|
|
|
|
let models = require("../models");
|
2020-09-11 13:38:09 +02:00
|
|
|
let error = require("./utils/error");
|
2020-08-18 17:54:00 +02:00
|
|
|
|
|
|
|
|
|
|
|
router.get("/", sessionCheck(2), async (req, res) => {
|
|
|
|
let date = req.query.date ? req.query.date : (new Date()).toISOString().substring(0,10);
|
|
|
|
|
|
|
|
let orders = {};
|
2020-09-11 12:59:47 +02:00
|
|
|
for (let o of await models.Order.findAll({
|
2020-09-11 13:38:09 +02:00
|
|
|
where: {paid: true, give: false},
|
2020-09-11 12:59:47 +02:00
|
|
|
include: [{
|
|
|
|
model: models.Sandwich,
|
|
|
|
through: {
|
|
|
|
where: {date: date}
|
|
|
|
},
|
|
|
|
required: true
|
|
|
|
}]
|
|
|
|
})) {
|
|
|
|
let name = o.firstName + " " + o.lastName;
|
2020-08-18 17:54:00 +02:00
|
|
|
|
2020-09-11 12:59:47 +02:00
|
|
|
if (!(o.DepartmentName in orders))
|
|
|
|
orders[o.DepartmentName] = {};
|
2020-08-18 17:54:00 +02:00
|
|
|
|
2020-09-11 12:59:47 +02:00
|
|
|
if (!(name in orders[o.DepartmentName]))
|
|
|
|
orders[o.DepartmentName][name] = {};
|
2020-08-18 17:54:00 +02:00
|
|
|
|
2020-09-11 12:59:47 +02:00
|
|
|
orders[o.DepartmentName][name][o.id] = o.Sandwiches;
|
2020-08-18 17:54:00 +02:00
|
|
|
}
|
2020-08-20 21:14:15 +02:00
|
|
|
res.render("orders", {title: "SOD - Orders", orders: orders, date: date});
|
2020-09-11 13:38:09 +02:00
|
|
|
}).post("/give", sessionCheck(2), async (req, res) => {
|
|
|
|
if (!req.body.id)
|
|
|
|
return error(req, res, "Missing arg !", 400);
|
|
|
|
|
|
|
|
let order = await models.Order.findByPk(req.body.id, {where: {paid: true, give: false}});
|
|
|
|
if (!order)
|
|
|
|
return error(req, res, "Invalid command id !", 400);
|
|
|
|
|
|
|
|
order.give = true;
|
|
|
|
await order.save();
|
|
|
|
|
|
|
|
res.redirect("/orders")
|
2020-08-18 17:54:00 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|