Add date management for orders
This commit is contained in:
parent
95bfdf5068
commit
0edafd6be2
12 changed files with 145 additions and 15 deletions
|
@ -60,7 +60,9 @@
|
|||
"departmentsManagement": "Departments management",
|
||||
"manageDepartments": "Manage departments",
|
||||
"user": "User",
|
||||
"permissions": "Permissions"
|
||||
"permissions": "Permissions",
|
||||
"firstDate": "First date",
|
||||
"lastDate": "Last date"
|
||||
},
|
||||
"firstName": "First name",
|
||||
"lastName": "Last name",
|
||||
|
|
|
@ -60,7 +60,9 @@
|
|||
"departmentsManagement": "Gestion des départements",
|
||||
"manageDepartments": "Gérer les départements",
|
||||
"user": "Utilisateur",
|
||||
"permissions": "Autorisations"
|
||||
"permissions": "Autorisations",
|
||||
"firstDate": "Première date",
|
||||
"lastDate": "Dernière date"
|
||||
},
|
||||
"firstName": "Prénom",
|
||||
"lastName": "Nom de famille",
|
||||
|
|
|
@ -7,6 +7,7 @@ function sessionCheck(permission) {
|
|||
req.session.save(() => res.redirect("/login"));
|
||||
} else if (req.session.user.permissions < permission)
|
||||
return error(req, res, "Permission denied !", 403);
|
||||
else
|
||||
next();
|
||||
}
|
||||
}
|
||||
|
|
24
models/data.js
Normal file
24
models/data.js
Normal 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;
|
||||
};
|
|
@ -5,6 +5,7 @@ const locals = {
|
|||
sandwich: document.querySelector("label[for='sandwich1']").innerHTML,
|
||||
day: document.querySelector("label[for='day1']").innerHTML
|
||||
};
|
||||
const [min, max] = [document.getElementById("day1").min, document.getElementById("day1").max];
|
||||
|
||||
function lastOrderId() {
|
||||
let list = document.querySelectorAll("div.order h2");
|
||||
|
@ -21,7 +22,7 @@ document.getElementById("add-order").addEventListener("click", () => {
|
|||
</div>
|
||||
<div class="field">
|
||||
<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>`);
|
||||
document.getElementById("order"+lastOrderId()).scrollIntoView({behavior: "smooth"});
|
||||
|
|
43
routes/admin/orders/date.js
Normal file
43
routes/admin/orders/date.js
Normal 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;
|
|
@ -1,8 +1,8 @@
|
|||
let express = require("express");
|
||||
let router = express.Router();
|
||||
let sessionCheck = require("../../middlewares/sessionCheck");
|
||||
let models = require("../../models");
|
||||
let error = require("../utils/error");
|
||||
let sessionCheck = require("../../../middlewares/sessionCheck");
|
||||
let models = require("../../../models");
|
||||
let error = require("../../utils/error");
|
||||
|
||||
|
||||
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 !");
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
})
|
||||
.use("/date", require("./date"));
|
||||
|
||||
module.exports = router;
|
|
@ -3,9 +3,27 @@ let router = express.Router();
|
|||
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", departments: departments, sandwiches: sandwiches });
|
||||
let now = new Date();
|
||||
let firstDate = await models.Data.findByPk("firstDate");
|
||||
|
||||
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;
|
||||
|
|
|
@ -22,7 +22,31 @@ router.post("/", async (req, res) => {
|
|||
if (!sandwich)
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -40,7 +64,7 @@ router.post("/", async (req, res) => {
|
|||
await models.SandwichOrder.create({OrderId: order.id, SandwichName: data[0], date: data[1]});
|
||||
} catch (e) {
|
||||
await order.destroy();
|
||||
error(req, res, "Invalid order !", 400, "Invalid date");
|
||||
error(req, res, "Invalid order !", 500);
|
||||
throw e;
|
||||
}
|
||||
res.send("Ok");
|
||||
|
|
14
views/admin/orders/date.pug
Normal file
14
views/admin/orders/date.pug
Normal 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"))
|
|
@ -1,4 +1,4 @@
|
|||
extends ../layout
|
||||
extends ../../layout
|
||||
|
||||
block content
|
||||
div.card#ordersManagement
|
|
@ -26,7 +26,7 @@ block content
|
|||
input#sandwich1(type="list" list="sandwich-list" name="sandwich1" autocomplete="off" required)
|
||||
div.field
|
||||
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
|
||||
a#add-order +
|
||||
|
|
Reference in a new issue