Archived
1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
SOD/routes/utils/addOrder.js

80 lines
2.8 KiB
JavaScript

const models = require("../../models");
const error = require("./error");
module.exports = async (req, res, args, dateCheck = true) => {
if (!args.department || !args.firstName || !args.lastName || args.sandwiches.length < 1 || args.dates.length < 1 ||
args.sandwiches.length !== args.dates.length)
return error(req, res, "Invalid order !", 400, "Missing arguments");
let department = await models.Department.findByPk(args.department);
if (!department)
return error(req, res, "Invalid order !", 400, "Invalid department");
let user = null;
if (args.username)
user = await models.User.findOne({where: {username: args.username}});
let sandwiches = [];
let price = 0;
for (let s in args.sandwiches) {
if (!args.dates[s])
return error(req, res, "Invalid order !", 400, "Sandwich without date");
let sandwich = await models.Sandwich.findByPk(args.sandwiches[s]);
if (!sandwich)
return error(req, res, "Invalid order !", 400, "Invalid sandwich: "+args.sandwiches[s]);
let date = new Date(args.dates[s]);
if (dateCheck) {
let [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");
}
}
let give = false;
if (args.give && args.give[s])
give = args.give[s];
try {
sandwiches.push([sandwich.name, date.toISOString().substring(0, 10), give]);
} catch {
return error(req, res, "Invalid order !", 400, "Invalid date");
}
price += sandwich.price;
}
let order = await models.Order.create({
firstName: args.firstName,
lastName: args.lastName,
paid: Boolean(args.paid),
price: price
});
if (user)
await order.setUser(user);
await order.setDepartment(department);
for (let data of sandwiches)
try {
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);
throw e;
}
return order;
};