2020-08-14 20:08:42 +02:00
|
|
|
let express = require("express");
|
|
|
|
let router = express.Router();
|
2020-08-15 20:17:36 +02:00
|
|
|
let models = require("../models");
|
2020-08-20 12:14:38 +02:00
|
|
|
let error = require("./utils/error");
|
2020-08-14 20:08:42 +02:00
|
|
|
|
2020-08-15 20:17:36 +02:00
|
|
|
|
|
|
|
router.post("/", async (req, res) => {
|
2020-08-20 12:14:38 +02:00
|
|
|
if (!req.body.department || !req.body.firstName || !req.body.lastName || !req.body.sandwich1 || !req.body.date1)
|
|
|
|
return error(req, res, "Invalid order !", 400, "Missing arguments");
|
2020-08-15 20:17:36 +02:00
|
|
|
|
|
|
|
let department = await models.Department.findByPk(req.body.department);
|
2020-08-20 12:14:38 +02:00
|
|
|
if (!department)
|
|
|
|
return error(req, res, "Invalid order !", 400, "Invalid department");
|
2020-08-15 20:17:36 +02:00
|
|
|
|
2020-08-16 12:48:43 +02:00
|
|
|
let sandwiches = [];
|
2020-08-15 20:17:36 +02:00
|
|
|
let price = 0;
|
|
|
|
for (let i = 1; req.body["sandwich" + i] !== undefined; i++) {
|
2020-08-20 12:14:38 +02:00
|
|
|
if (req.body["date" + i] === undefined)
|
|
|
|
return error(req, res, "Invalid order !", 400, "Sandwich without date");
|
2020-08-15 20:17:36 +02:00
|
|
|
|
|
|
|
let sandwich = await models.Sandwich.findByPk(req.body["sandwich" + i]);
|
2020-08-20 12:14:38 +02:00
|
|
|
if (!sandwich)
|
|
|
|
return error(req, res, "Invalid order !", 400, "Invalid sandwich: "+req.body["sandwich" + i]);
|
|
|
|
|
2020-08-24 23:23:46 +02:00
|
|
|
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");
|
|
|
|
}
|
2020-08-15 20:17:36 +02:00
|
|
|
price += sandwich.price;
|
|
|
|
}
|
|
|
|
|
2020-08-18 17:54:00 +02:00
|
|
|
let order = await models.Order.create({
|
2020-08-15 20:17:36 +02:00
|
|
|
firstName: req.body.firstName,
|
|
|
|
lastName: req.body.lastName,
|
|
|
|
price: price
|
|
|
|
});
|
2020-08-17 17:10:21 +02:00
|
|
|
let user = await models.User.findOne({where: {firstName: req.body.firstName, lastName: req.body.lastName}});
|
|
|
|
if (user)
|
2020-08-18 17:54:00 +02:00
|
|
|
await order.setUser(user);
|
|
|
|
await order.setDepartment(department);
|
2020-08-16 12:48:43 +02:00
|
|
|
for (let data of sandwiches)
|
2020-08-15 20:17:36 +02:00
|
|
|
try {
|
2020-08-18 17:54:00 +02:00
|
|
|
await models.SandwichOrder.create({OrderId: order.id, SandwichName: data[0], date: data[1]});
|
2020-08-15 20:17:36 +02:00
|
|
|
} catch (e) {
|
2020-08-18 17:54:00 +02:00
|
|
|
await order.destroy();
|
2020-08-24 23:23:46 +02:00
|
|
|
error(req, res, "Invalid order !", 500);
|
2020-08-15 20:17:36 +02:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
res.send("Ok");
|
2020-08-14 20:08:42 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|