Archived
1
0
Fork 0

Switch give state from order to SandwichOrder

This commit is contained in:
Ethanell 2020-09-14 11:16:06 +02:00
parent 78c8def258
commit 73a9aae4e4
11 changed files with 36 additions and 25 deletions

View file

@ -33,11 +33,6 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
},
give: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
}
}, {
sequelize,

View file

@ -17,6 +17,11 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.DATEONLY,
defaultValue: DataTypes.NOW,
allowNull: false
},
give: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false
}
}, {
sequelize,

View file

@ -21,7 +21,11 @@ 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="dates[${id}]" required>
<input id="day${id}" type="date" name="dates[${id}]" required>
</div>
<div class="field">
<label for="give${id}">${locals.day}</label>
<input id="give${id}" type="checkbox" name="give[${id}]">
</div>
</div>`);
document.getElementById("order"+lastOrderId()).scrollIntoView({behavior: "smooth"});

View file

@ -248,7 +248,7 @@ p.before-link a::before {
width: min-content;
}
input::-webkit-calendar-picker-indicator {
input[type="list"]::-webkit-calendar-picker-indicator {
display: none;/* remove default arrow */
}

View file

@ -56,8 +56,12 @@ router.get("/", sessionCheck(3), async (req, res) => {
let date = new Date(req.body.dates[s]);
let give = false;
if (req.body.give && req.body.give[s])
give = req.body.give[s];
try {
sandwiches.push([sandwich.name, date.toISOString().substring(0, 10)]);
sandwiches.push([sandwich.name, date.toISOString().substring(0, 10), give]);
} catch {
return error(req, res, "Invalid order edit !", 400, "Invalid date");
}
@ -73,13 +77,11 @@ router.get("/", sessionCheck(3), async (req, res) => {
order.lastName = req.body.lastName;
if (req.body.paid !== order.paid)
order.paid = Boolean(req.body.paid);
if (req.body.give !== order.give)
order.give = Boolean(req.body.give);
await order.removeSandwiches(order.Sandwiches);
for (let data of sandwiches)
try {
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], give: data[2]});
} catch (e) {
await order.destroy();
error(req, res, "Invalid order !");

View file

@ -10,11 +10,11 @@ router.get("/", sessionCheck(2), async (req, res) => {
let orders = {};
for (let o of await models.Order.findAll({
where: {paid: true, give: false},
where: {paid: true},
include: [{
model: models.Sandwich,
through: {
where: {date: date}
where: {date: date, give: false}
},
required: true
}]
@ -34,9 +34,9 @@ router.get("/", sessionCheck(2), async (req, res) => {
if (!req.body.id)
return error(req, res, "Missing arg !", 400);
let order = await models.Order.findByPk(req.body.id, {where: {paid: true, give: false}});
let order = await models.SandwichOrder.findByPk(req.body.id, {where: {give: false}});
if (!order)
return error(req, res, "Invalid command id !", 400);
return error(req, res, "Invalid order id !", 400);
order.give = true;
await order.save();

View file

@ -44,8 +44,12 @@ module.exports = async (req, res, args, dateCheck = true) => {
}
}
let give = false;
if (args.give && args.give[s])
give = args.give[s];
try {
sandwiches.push([sandwich.name, date.toISOString().substring(0, 10)]);
sandwiches.push([sandwich.name, date.toISOString().substring(0, 10), give]);
} catch {
return error(req, res, "Invalid order !", 400, "Invalid date");
}
@ -56,7 +60,6 @@ module.exports = async (req, res, args, dateCheck = true) => {
firstName: args.firstName,
lastName: args.lastName,
paid: Boolean(args.paid),
give: Boolean(args.give),
price: price
});
@ -66,7 +69,7 @@ module.exports = async (req, res, args, dateCheck = true) => {
for (let data of sandwiches)
try {
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], give: data[2]});
} catch (e) {
await order.destroy();
error(req, res, "Invalid order !", 500);

View file

@ -30,6 +30,9 @@ block content
div.field
label(for="day1")=__("index.day") + ":"
input#day1(type="date" name="dates[1]" required)
div.field
label(for="given1")=__("admin.given")
input#given1(type="checkbox" name="give[1]")
div#order-action
a#add-order +
@ -38,8 +41,6 @@ block content
div.field
label(for="paid")=__("admin.paid")
input#paid(type="checkbox" name="paid")
label(for="given")=__("admin.given")
input#given(type="checkbox" name="give")
div.field
input#send(type="submit" value=__("admin.add"))

View file

@ -34,6 +34,9 @@ block content
div.field
label(for="day"+i)=__("index.day") + ":"
input(id="#day"+i type="date" name="dates["+i+"]" value=sandwich.SandwichOrder.date required)
div.field
label(for="given"+i)=__("admin.given")
input(id="given"+i type="checkbox" name="give["+i+"]" checked=sandwich.SandwichOrder.give)
div#order-action
a#add-order +
@ -45,11 +48,9 @@ block content
div.field
label(for="paid")=__("admin.paid")
input#paid(type="checkbox" name="paid" checked=order.paid)
label(for="given")=__("admin.given")
input#given(type="checkbox" name="give" checked=order.give)
div.field
input#send(type="submit" value=__("admin.add"))
input#send(type="submit" value=__("save"))
datalist#sandwich-list
each sandwich in sandwiches

View file

@ -2,6 +2,6 @@ mixin submit(value)
div.recaptcha.recaptcha-cb
input(type="submit" value!=value)
mixin list(id, list, name, value="", autocomplete="on", required="false")
mixin list(id, list, name, value="", autocomplete="on", required=false)
span.list_arrow
input(id=id type="list" list=list name=name value=value autocomplete=autocomplete required=required)

View file

@ -18,7 +18,7 @@ block content
each sandwich in order
div.sandwich
form.give(method="POST" action="/orders/give")
input(type="hidden" name="id" value=id)
input(type="hidden" name="id" value=sandwich.SandwichOrder.id)
input.give(type="submit" value="v")
h4= sandwich.name