From 405690094f92f9bdce8904d67f9daaa5732ef9f2 Mon Sep 17 00:00:00 2001 From: flifloo Date: Wed, 30 Sep 2020 11:35:24 +0200 Subject: [PATCH] Add disabled option for sandwiches and departments --- locales/en.json | 3 ++- locales/fr.json | 3 ++- models/department.js | 5 +++++ models/sandwich.js | 5 +++++ routes/admin/departments/add.js | 2 +- routes/admin/departments/edit.js | 3 +++ routes/admin/index.js | 4 +--- routes/admin/orders/add.js | 4 ++-- routes/admin/sandwiches/add.js | 2 +- routes/admin/sandwiches/edit.js | 4 ++++ routes/admin/users/add.js | 2 +- routes/index.js | 4 ++-- routes/profile.js | 2 +- routes/register.js | 2 +- routes/utils/addOrder.js | 4 ++-- routes/utils/userCreate.js | 2 +- views/admin/departments/add.pug | 6 ++++++ views/admin/departments/edit.pug | 10 ++++++++-- views/admin/sandwiches/add.pug | 6 ++++++ views/admin/sandwiches/edit.pug | 6 ++++++ 20 files changed, 60 insertions(+), 19 deletions(-) diff --git a/locales/en.json b/locales/en.json index f50770d..4a3b832 100644 --- a/locales/en.json +++ b/locales/en.json @@ -74,7 +74,8 @@ "lastDate": "Last date", "paid": "Paid", "given": "Given", - "chooseUser": "Choose an user" + "chooseUser": "Choose an user", + "enable": "Enable" }, "payment": { "payment": "Payment", diff --git a/locales/fr.json b/locales/fr.json index 6b1b91e..264b352 100644 --- a/locales/fr.json +++ b/locales/fr.json @@ -74,7 +74,8 @@ "lastDate": "Dernière date", "paid": "Payé", "given": "Donné", - "chooseUser": "Choisissez un utilisateur" + "chooseUser": "Choisissez un utilisateur", + "enable": "Activer" }, "payment": { "payment": "Paiement", diff --git a/models/department.js b/models/department.js index c5f2c48..3002cd9 100644 --- a/models/department.js +++ b/models/department.js @@ -13,6 +13,11 @@ module.exports = (sequelize, DataTypes) => { name: { type: DataTypes.STRING, primaryKey: true + }, + enable: { + type: DataTypes.BOOLEAN, + allowNull: false, + defaultValue: true } }, { sequelize, diff --git a/models/sandwich.js b/models/sandwich.js index d4e07a0..d58108d 100644 --- a/models/sandwich.js +++ b/models/sandwich.js @@ -16,6 +16,11 @@ module.exports = (sequelize, DataTypes) => { price: { type: DataTypes.FLOAT, allowNull: false + }, + enable: { + type: DataTypes.BOOLEAN, + allowNull: false, + defaultValue: true } }, { sequelize, diff --git a/routes/admin/departments/add.js b/routes/admin/departments/add.js index 246d686..261d36e 100644 --- a/routes/admin/departments/add.js +++ b/routes/admin/departments/add.js @@ -15,7 +15,7 @@ router.get("/", sessionCheck(3), (req, res) => { if (await models.Department.findByPk(req.body.name)) return error(req, res, "Fail to add department", 400, "Name already used"); - await models.Department.create({name: req.body.name}); + await models.Department.create({name: req.body.name, enable: !!req.body.enable}); res.redirect("/admin/departments"); }); diff --git a/routes/admin/departments/edit.js b/routes/admin/departments/edit.js index 4ccf0f8..5aec58c 100644 --- a/routes/admin/departments/edit.js +++ b/routes/admin/departments/edit.js @@ -24,7 +24,10 @@ router.get("/", sessionCheck(3), async (req, res) => { if (!department) return error(req, res, "Fail to edit department !", 400, "Invalid department name"); + if (department.enable !== req.body.enable) + department.enable = !!req.body.enable; await department.save(); + if (req.body.newName && req.body.newName !== department.name) await models.Department.update({name: req.body.newName}, {where: {name: req.body.name}}); diff --git a/routes/admin/index.js b/routes/admin/index.js index 12aed57..b4eb50d 100644 --- a/routes/admin/index.js +++ b/routes/admin/index.js @@ -6,9 +6,7 @@ let models = require("../../models"); router.get("/", sessionCheck(3), async (req, res) => { res.render("admin/index", { - title: "SOD - Administration", - sandwiches: await models.Sandwich.findAll(), - users: await models.User.findAll() + title: "SOD - Administration" }); }) .use("/orders", require("./orders")) diff --git a/routes/admin/orders/add.js b/routes/admin/orders/add.js index e457e27..d71e930 100644 --- a/routes/admin/orders/add.js +++ b/routes/admin/orders/add.js @@ -8,8 +8,8 @@ const addOrder = require("../../utils/addOrder"); router.get("/", sessionCheck(3), async (req, res) => { res.render("admin/orders/add", { title: "SOD - Orders administration", - departments: await models.Department.findAll(), - sandwiches: await models.Sandwich.findAll(), + departments: await models.Department.findAll({where: {enable: true}}), + sandwiches: await models.Sandwich.findAll({where: {enable: true}}), users: await models.User.findAll() }); }).post("/", sessionCheck(3), async (req, res) => { diff --git a/routes/admin/sandwiches/add.js b/routes/admin/sandwiches/add.js index 2cd5e10..ba27af1 100644 --- a/routes/admin/sandwiches/add.js +++ b/routes/admin/sandwiches/add.js @@ -15,7 +15,7 @@ router.get("/", sessionCheck(3), (req, res) => { if (await models.Sandwich.findByPk(req.body.name)) return error(req, res, "Fail to add sandwich", 400, "Name already used"); - await models.Sandwich.create({name: req.body.name, price: req.body.price}); + await models.Sandwich.create({name: req.body.name, price: req.body.price, enable: !!req.body.enable}); res.redirect("/admin/sandwiches"); }); diff --git a/routes/admin/sandwiches/edit.js b/routes/admin/sandwiches/edit.js index a54666d..5d3fce4 100644 --- a/routes/admin/sandwiches/edit.js +++ b/routes/admin/sandwiches/edit.js @@ -26,9 +26,13 @@ router.get("/", sessionCheck(3), async (req, res) => { if (req.body.price && req.body.price !== sandwich.price) sandwich.price = req.body.price; + if(req.body.enable !== sandwich.enable) + sandwich.enable = !!req.body.enable; await sandwich.save(); + if (req.body.newName && req.body.newName !== sandwich.name) await models.Sandwich.update({name: req.body.newName}, {where: {name: req.body.name}}); + res.redirect("/admin/sandwiches"); }); diff --git a/routes/admin/users/add.js b/routes/admin/users/add.js index 1f31e37..0da679c 100644 --- a/routes/admin/users/add.js +++ b/routes/admin/users/add.js @@ -7,7 +7,7 @@ let userCreate = require("../../utils/userCreate"); router.get("/", sessionCheck(3), async (req, res) => { res.render("admin/users/add", { title: "SOD - Users administration", - departments: await models.Department.findAll() + departments: await models.Department.findAll({where: {enable: true}}) }); }).post("/", sessionCheck(3), async (req, res) => { let user = await userCreate(req, res); diff --git a/routes/index.js b/routes/index.js index 9b9e9a8..7e786f7 100644 --- a/routes/index.js +++ b/routes/index.js @@ -20,8 +20,8 @@ router.get("/", async (req, res) => { res.render("index", { title: "SOD - Home", - departments: await models.Department.findAll(), - sandwiches: await models.Sandwich.findAll(), + departments: await models.Department.findAll({where: {enable: true}}), + sandwiches: await models.Sandwich.findAll({where: {enable: true}}), date: { firstDate: firstDate, lastDate: lastDate diff --git a/routes/profile.js b/routes/profile.js index 4a366c8..f743d85 100644 --- a/routes/profile.js +++ b/routes/profile.js @@ -9,7 +9,7 @@ let error = require("./utils/error"); router.get("/", sessionCheck(0), async (req, res) => { res.render("profile", { title: "SOD - Profile", - departments: await models.Department.findAll(), + departments: await models.Department.findAll({where: {enable: true}}), orders: await models.Order.findAll({ where: {UserUsername: req.session.user.username, paid: true}, include: models.Sandwich, diff --git a/routes/register.js b/routes/register.js index 0628b85..76580eb 100644 --- a/routes/register.js +++ b/routes/register.js @@ -9,7 +9,7 @@ router.get("/", async (req, res) => { if (req.session.user) res.redirect("/"); else - res.render("register", {title: "SOD - register", departments: await models.Department.findAll()}); + res.render("register", {title: "SOD - register", departments: await models.Department.findAll({where: {enable: true}})}); }) .post("/", reCaptcha, async (req, res) => { let user = await userCreate(req, res); diff --git a/routes/utils/addOrder.js b/routes/utils/addOrder.js index 6b7214f..96b484e 100644 --- a/routes/utils/addOrder.js +++ b/routes/utils/addOrder.js @@ -6,7 +6,7 @@ module.exports = async (req, res, args, dateCheck = true) => { args.sandwiches.length !== args.dates.length) return error(req, res, "Invalid order !", 400, "Missing arguments"); - let department = await models.Department.findByPk(args.department); + let department = await models.Department.findByPk(args.department, {where: {enable: true}}); if (!department) return error(req, res, "Invalid order !", 400, "Invalid department"); @@ -20,7 +20,7 @@ module.exports = async (req, res, args, dateCheck = true) => { if (!args.dates[s]) return error(req, res, "Invalid order !", 400, "Sandwich without date"); - let sandwich = await models.Sandwich.findByPk(args.sandwiches[s]); + let sandwich = await models.Sandwich.findByPk(args.sandwiches[s], {where: {enable: true}}); if (!sandwich) return error(req, res, "Invalid order !", 400, "Invalid sandwich: "+args.sandwiches[s]); diff --git a/routes/utils/userCreate.js b/routes/utils/userCreate.js index 745deeb..5159fb8 100644 --- a/routes/utils/userCreate.js +++ b/routes/utils/userCreate.js @@ -18,7 +18,7 @@ module.exports = async (req, res) => { if (await models.User.findOne({where: {email: req.body.email}})) return error(req, res, "Invalid register !", 400, "Email already used"); - let department = await models.Department.findByPk(req.body.department); + let department = await models.Department.findByPk(req.body.department, {where: {enable: true}}); if (!department) return error(req, res, "Invalid register !", 400, "Invalid department"); diff --git a/views/admin/departments/add.pug b/views/admin/departments/add.pug index 769ebac..da1d1ed 100644 --- a/views/admin/departments/add.pug +++ b/views/admin/departments/add.pug @@ -9,6 +9,12 @@ block content div.input-field.col.s12 label(for="name")=__("admin.name")+":" input#name(type="text" name="name" required) + div.row + div.input-field.col.s12 + p + label(for="enable") + input#enable(type="checkbox" name="enable" checked) + span=__("admin.enable") div.row.center div.input-field.col.s12 +submit(__("admin.add")) diff --git a/views/admin/departments/edit.pug b/views/admin/departments/edit.pug index 0c96f69..edd8ede 100644 --- a/views/admin/departments/edit.pug +++ b/views/admin/departments/edit.pug @@ -8,8 +8,14 @@ block content input.hide(type="text" name="name" value=department.name required) div.row div.input-field.col.s12 - label(for="name")=__("admin.name")+":" - input#name(type="text" name="newName" value=department.name required) + input#name(type="text" name="newName" value=department.name required) + label(for="name")=__("admin.name")+":" + div.row + div.input-field.col.s12 + p + label(for="enable") + input#enable(type="checkbox" name="enable" checked=department.enable) + span=__("admin.enable") div.row.center div.input-field.col.s12 +submit(__("save")) diff --git a/views/admin/sandwiches/add.pug b/views/admin/sandwiches/add.pug index 1413344..0bf9b04 100644 --- a/views/admin/sandwiches/add.pug +++ b/views/admin/sandwiches/add.pug @@ -13,6 +13,12 @@ block content div.input-field.col.s12 label(for="price")=__("admin.price") + ":" input#price(type="number" min="0" step="any" name="price" value="0" required) + div.row + div.input-field.col.s12 + p + label(for="enable") + input#enable(type="checkbox" name="enable" checked) + span=__("admin.enable") div.row.center div.input-field.col.s12 +submit(__("admin.add")) diff --git a/views/admin/sandwiches/edit.pug b/views/admin/sandwiches/edit.pug index be9709f..1153a09 100644 --- a/views/admin/sandwiches/edit.pug +++ b/views/admin/sandwiches/edit.pug @@ -14,6 +14,12 @@ block content div.input-field.col.s12 label(for="price")=__("admin.price") + ":" input#price(type="number" min="0" step="any" name="price" value=sandwich.price required) + div.row + div.input-field.col.s12 + p + label(for="enable") + input#enable(type="checkbox" name="enable" checked=sandwich.enable) + span=__("admin.enable") div.row.center div.input-field.col.s12 +submit(__("save"))