Archived
1
0
Fork 0

Add date management for orders

This commit is contained in:
Ethanell 2020-08-24 23:23:46 +02:00
parent 95bfdf5068
commit 0edafd6be2
12 changed files with 145 additions and 15 deletions

View file

@ -60,7 +60,9 @@
"departmentsManagement": "Departments management", "departmentsManagement": "Departments management",
"manageDepartments": "Manage departments", "manageDepartments": "Manage departments",
"user": "User", "user": "User",
"permissions": "Permissions" "permissions": "Permissions",
"firstDate": "First date",
"lastDate": "Last date"
}, },
"firstName": "First name", "firstName": "First name",
"lastName": "Last name", "lastName": "Last name",

View file

@ -60,7 +60,9 @@
"departmentsManagement": "Gestion des départements", "departmentsManagement": "Gestion des départements",
"manageDepartments": "Gérer les départements", "manageDepartments": "Gérer les départements",
"user": "Utilisateur", "user": "Utilisateur",
"permissions": "Autorisations" "permissions": "Autorisations",
"firstDate": "Première date",
"lastDate": "Dernière date"
}, },
"firstName": "Prénom", "firstName": "Prénom",
"lastName": "Nom de famille", "lastName": "Nom de famille",

View file

@ -7,7 +7,8 @@ function sessionCheck(permission) {
req.session.save(() => res.redirect("/login")); req.session.save(() => res.redirect("/login"));
} else if (req.session.user.permissions < permission) } else if (req.session.user.permissions < permission)
return error(req, res, "Permission denied !", 403); return error(req, res, "Permission denied !", 403);
next(); else
next();
} }
} }

24
models/data.js Normal file
View file

@ -0,0 +1,24 @@
"use strict";
const {
Model
} = require("sequelize");
module.exports = (sequelize, DataTypes) => {
class Data extends Model {
static associate(models) {
}
}
Data.init({
key: {
type: DataTypes.STRING,
primaryKey: true
},
value: {
type: DataTypes.STRING,
allowNull: false
}
}, {
sequelize,
modelName: "Data",
});
return Data;
};

View file

@ -5,6 +5,7 @@ const locals = {
sandwich: document.querySelector("label[for='sandwich1']").innerHTML, sandwich: document.querySelector("label[for='sandwich1']").innerHTML,
day: document.querySelector("label[for='day1']").innerHTML day: document.querySelector("label[for='day1']").innerHTML
}; };
const [min, max] = [document.getElementById("day1").min, document.getElementById("day1").max];
function lastOrderId() { function lastOrderId() {
let list = document.querySelectorAll("div.order h2"); let list = document.querySelectorAll("div.order h2");
@ -21,7 +22,7 @@ document.getElementById("add-order").addEventListener("click", () => {
</div> </div>
<div class="field"> <div class="field">
<label for="day${id}">${locals.day}</label> <label for="day${id}">${locals.day}</label>
<input id="da${id}y" type="date" name="date${id}" required> <input id="da${id}y" type="date" min="${min}" max="${max}" name="date${id}" required>
</div> </div>
</div>`); </div>`);
document.getElementById("order"+lastOrderId()).scrollIntoView({behavior: "smooth"}); document.getElementById("order"+lastOrderId()).scrollIntoView({behavior: "smooth"});

View file

@ -0,0 +1,43 @@
let express = require("express");
let router = express.Router();
let sessionCheck = require("../../../middlewares/sessionCheck");
let models = require("../../../models");
let error = require("../../utils/error");
router.get("/", sessionCheck(3), async (req, res) => {
let firstDate = await models.Data.findByPk("firstDate");
let lastDate = await models.Data.findByPk("lastDate");
if ((!firstDate || !firstDate.value) || (!lastDate || !lastDate.value))
[firstDate, lastDate] = [undefined, undefined];
res.render("admin/orders/date", {
title: "SOD - Orders administration",
date: {firstDate: firstDate.value, lastDate: lastDate.value}
});
}).post("/", sessionCheck(3), async (req, res) => {
if (!req.body.firstDate || ! req.body.lastDate)
return error(req, res, "Fail to set date !", 400, "Missing args");
let firstDate = await models.Data.findByPk("firstDate");
let lastDate = await models.Data.findByPk("lastDate");
if (!firstDate)
firstDate = await models.Data.create({key: "firstDate", value: ""});
if (!lastDate)
lastDate = await models.Data.create({key: "lastDate", value: ""});
try {
[firstDate.value, lastDate.value] = [(new Date(req.body.firstDate)).toISOString().substring(0,10),
(new Date(req.body.lastDate)).toISOString().substring(0,10)];
await firstDate.save();
await lastDate.save();
} catch {
return error(req, res, "Fail to set date !", 400, "Invalid date");
}
res.redirect("/admin");
});
module.exports = router;

View file

@ -1,8 +1,8 @@
let express = require("express"); let express = require("express");
let router = express.Router(); let router = express.Router();
let sessionCheck = require("../../middlewares/sessionCheck"); let sessionCheck = require("../../../middlewares/sessionCheck");
let models = require("../../models"); let models = require("../../../models");
let error = require("../utils/error"); let error = require("../../utils/error");
router.get("/", sessionCheck(3), async (req, res) => { router.get("/", sessionCheck(3), async (req, res) => {
@ -33,6 +33,7 @@ router.get("/", sessionCheck(3), async (req, res) => {
error(req, res, "Fail to remove sandwich !"); error(req, res, "Fail to remove sandwich !");
throw e; throw e;
} }
}); })
.use("/date", require("./date"));
module.exports = router; module.exports = router;

View file

@ -3,9 +3,27 @@ let router = express.Router();
let models = require("../models"); let models = require("../models");
router.get("/", async (req, res) => { router.get("/", async (req, res) => {
let departments = await models.Department.findAll(); let now = new Date();
let sandwiches = await models.Sandwich.findAll(); let firstDate = await models.Data.findByPk("firstDate");
res.render("index", { title: "SOD - Home", departments: departments, sandwiches: sandwiches });
if (firstDate && firstDate.value) {
firstDate = new Date(firstDate.value);
if (firstDate.getTime() < now.getTime())
firstDate = now;
} else
firstDate = now;
firstDate = firstDate.toISOString().substring(0, 10);
res.render("index", {
title: "SOD - Home",
departments: await models.Department.findAll(),
sandwiches: await models.Sandwich.findAll(),
date: {
firstDate: firstDate,
lastDate: await models.Data.findByPk("lastDate")
}
});
}); });
module.exports = router; module.exports = router;

View file

@ -22,7 +22,31 @@ router.post("/", async (req, res) => {
if (!sandwich) if (!sandwich)
return error(req, res, "Invalid order !", 400, "Invalid sandwich: "+req.body["sandwich" + i]); return error(req, res, "Invalid order !", 400, "Invalid sandwich: "+req.body["sandwich" + i]);
sandwiches.push([sandwich.name, req.body["date" + i]]); let date = new Date(req.body["date" + i]);
let firstDate, lastDate;
[firstDate, lastDate] = [await models.Data.findByPk("firstDate"),
await models.Data.findByPk("lastDate")];
let now = new Date();
now.setUTCHours(0,0,0,0);
if (firstDate && firstDate.value && lastDate && lastDate.value) {
[firstDate, lastDate] = [new Date(firstDate.value), new Date(lastDate.value)];
firstDate.setUTCHours(0,0,0,0);
lastDate.setUTCHours(0,0,0,0);
if (now.getTime() > date.getTime() ||
firstDate.getTime() > date.getTime() ||
lastDate.getTime() < date.getTime())
return error(req, res, "Invalid order !", 400, "Date not available");
}
try {
sandwiches.push([sandwich.name, date.toISOString().substring(0, 10)]);
} catch {
return error(req, res, "Invalid order !", 400, "Invalid date");
}
price += sandwich.price; price += sandwich.price;
} }
@ -40,7 +64,7 @@ router.post("/", async (req, res) => {
await models.SandwichOrder.create({OrderId: order.id, SandwichName: data[0], date: data[1]}); await models.SandwichOrder.create({OrderId: order.id, SandwichName: data[0], date: data[1]});
} catch (e) { } catch (e) {
await order.destroy(); await order.destroy();
error(req, res, "Invalid order !", 400, "Invalid date"); error(req, res, "Invalid order !", 500);
throw e; throw e;
} }
res.send("Ok"); res.send("Ok");

View file

@ -0,0 +1,14 @@
extends ../../layout
block content
div.card#ordersManagement
h1=__("admin.ordersDate")
form(action="/admin/orders/date" method="POST")
div.field
label(for="firstDate")=__("admin.firstDate")
input#firstDate(type="date" name="firstDate" value=date? date.firstDate: "" required)
div.field
label(for="lastDate")=__("admin.lastDate")
input#lastDate(type="date" name="lastDate" value=date? date.lastDate: "" required)
div.field
input(type="submit" value=__("save"))

View file

@ -1,4 +1,4 @@
extends ../layout extends ../../layout
block content block content
div.card#ordersManagement div.card#ordersManagement

View file

@ -26,7 +26,7 @@ block content
input#sandwich1(type="list" list="sandwich-list" name="sandwich1" autocomplete="off" required) input#sandwich1(type="list" list="sandwich-list" name="sandwich1" autocomplete="off" required)
div.field div.field
label(for="day1")=__("index.day")+":" label(for="day1")=__("index.day")+":"
input#day1(type="date" name="date1" required) input#day1(type="date" min=date ? date.firstDate : "" max=date ? date.lastDate.value : "" name="date1" required)
div#order-action div#order-action
a#add-order + a#add-order +