From 59005d8d6e003e65e8290319ec79336ded831df9 Mon Sep 17 00:00:00 2001 From: flifloo Date: Fri, 11 Sep 2020 12:59:47 +0200 Subject: [PATCH] Filter orders and sandwiches to have those paying --- routes/orders.js | 27 ++++++++++++++++----------- routes/profile.js | 2 +- routes/sandwiches.js | 15 +++++++++++---- views/orders.pug | 2 +- views/sandwiches.pug | 2 +- 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/routes/orders.js b/routes/orders.js index 94369a4..fa2d2d8 100644 --- a/routes/orders.js +++ b/routes/orders.js @@ -8,20 +8,25 @@ router.get("/", sessionCheck(2), async (req, res) => { let date = req.query.date ? req.query.date : (new Date()).toISOString().substring(0,10); let orders = {}; - for (let i of await models.SandwichOrder.findAll({where: {date: date}})) { - i.Order = await models.Order.findByPk(i.OrderId); - i.Sandwich = await models.Sandwich.findByPk(i.SandwichName); - let name = i.Order.firstName + " " + i.Order.lastName; + for (let o of await models.Order.findAll({ + where: {paid: true}, + include: [{ + model: models.Sandwich, + through: { + where: {date: date} + }, + required: true + }] + })) { + let name = o.firstName + " " + o.lastName; - if (!(i.Order.DepartmentName in orders)) - orders[i.Order.DepartmentName] = {}; + if (!(o.DepartmentName in orders)) + orders[o.DepartmentName] = {}; - if (!(name in orders[i.Order.DepartmentName])) - orders[i.Order.DepartmentName][name] = {}; + if (!(name in orders[o.DepartmentName])) + orders[o.DepartmentName][name] = {}; - if (!(i.Order.id in orders[i.Order.DepartmentName][name])) - orders[i.Order.DepartmentName][name][i.Order.id] = []; - orders[i.Order.DepartmentName][name][i.Order.id].push(i); + orders[o.DepartmentName][name][o.id] = o.Sandwiches; } res.render("orders", {title: "SOD - Orders", orders: orders, date: date}); }); diff --git a/routes/profile.js b/routes/profile.js index 28058a8..4a366c8 100644 --- a/routes/profile.js +++ b/routes/profile.js @@ -11,7 +11,7 @@ router.get("/", sessionCheck(0), async (req, res) => { title: "SOD - Profile", departments: await models.Department.findAll(), orders: await models.Order.findAll({ - where: {UserUsername: req.session.user.username}, + where: {UserUsername: req.session.user.username, paid: true}, include: models.Sandwich, order: ["date"] }) diff --git a/routes/sandwiches.js b/routes/sandwiches.js index c097c73..9960013 100644 --- a/routes/sandwiches.js +++ b/routes/sandwiches.js @@ -10,10 +10,17 @@ router.get("/", sessionCheck(1), async (req, res) => { res.render("sandwiches", { title: "SOD - Sandwiches", - sandwiches: await models.SandwichOrder.findAll({ - attributes: ["SandwichName", [sequelize.fn("COUNT", sequelize.col("SandwichName")), "number"]], - where: {date: date}, - group: "SandwichName" + sandwiches: await models.Sandwich.findAll({ + attributes: ["name", [sequelize.fn("COUNT", sequelize.col("name")), "number"]], + include: [{ + model: models.Order, + where: {paid: true}, + through: { + where: {date: date} + }, + required: true + }], + group: "name" }), date: date}); }); diff --git a/views/orders.pug b/views/orders.pug index 30d8f24..421f454 100644 --- a/views/orders.pug +++ b/views/orders.pug @@ -17,6 +17,6 @@ block content h4 #{__("order")} N°#{id} each sandwich in order div.sandwich - h4= sandwich.Sandwich.name + h4= sandwich.name script(src="/javascripts/orders.js") diff --git a/views/sandwiches.pug b/views/sandwiches.pug index 90f3ff1..356e561 100644 --- a/views/sandwiches.pug +++ b/views/sandwiches.pug @@ -8,6 +8,6 @@ block content h1=__("sandwiches") each sandwich in sandwiches div.sandwich - h2 #{sandwich.SandwichName}: #{sandwich.dataValues.number} + h2 #{sandwich.name}: #{sandwich.dataValues.number} script(src="/javascripts/sandwiches.js")