From b9ba035fa30a3f16a7816644bf673bb28596e2eb Mon Sep 17 00:00:00 2001 From: flifloo Date: Tue, 25 Aug 2020 14:19:20 +0200 Subject: [PATCH] Fix dates check and management when nothing is set in database --- routes/admin/orders/date.js | 4 +++- routes/index.js | 9 ++++++--- views/index.pug | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/routes/admin/orders/date.js b/routes/admin/orders/date.js index ebd829b..765076b 100644 --- a/routes/admin/orders/date.js +++ b/routes/admin/orders/date.js @@ -11,10 +11,12 @@ router.get("/", sessionCheck(3), async (req, res) => { if ((!firstDate || !firstDate.value) || (!lastDate || !lastDate.value)) [firstDate, lastDate] = [undefined, undefined]; + else + [firstDate, lastDate] = [firstDate.value, lastDate.value]; res.render("admin/orders/date", { title: "SOD - Orders administration", - date: {firstDate: firstDate.value, lastDate: lastDate.value} + date: {firstDate: firstDate, lastDate: lastDate} }); }).post("/", sessionCheck(3), async (req, res) => { if (!req.body.firstDate || ! req.body.lastDate) diff --git a/routes/index.js b/routes/index.js index 902d16b..9b9e9a8 100644 --- a/routes/index.js +++ b/routes/index.js @@ -4,10 +4,13 @@ let models = require("../models"); router.get("/", async (req, res) => { let now = new Date(); - let firstDate = await models.Data.findByPk("firstDate"); + let [firstDate, lastDate] = [await models.Data.findByPk("firstDate"), + await models.Data.findByPk("lastDate")]; - if (firstDate && firstDate.value) { + if (firstDate && lastDate && firstDate.value && lastDate.value) { + lastDate = lastDate.value; firstDate = new Date(firstDate.value); + if (firstDate.getTime() < now.getTime()) firstDate = now; } else @@ -21,7 +24,7 @@ router.get("/", async (req, res) => { sandwiches: await models.Sandwich.findAll(), date: { firstDate: firstDate, - lastDate: await models.Data.findByPk("lastDate") + lastDate: lastDate } }); }); diff --git a/views/index.pug b/views/index.pug index 352aea7..f698338 100644 --- a/views/index.pug +++ b/views/index.pug @@ -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" min=date ? date.firstDate : "" max=date ? date.lastDate.value : "" name="date1" required) + input#day1(type="date" min=date.firstDate ? date.firstDate : "" max=date.lastDate ? date.lastDate : "" name="date1" required) div#order-action a#add-order +