2020-05-30 15:43:24 +02:00
|
|
|
const models = require("../models");
|
|
|
|
const utils = require("./utils");
|
2020-05-29 19:44:12 +02:00
|
|
|
|
|
|
|
module.exports = socket => {
|
|
|
|
return async (data) => {
|
|
|
|
try {
|
|
|
|
let o = await models.Command.findOne({
|
|
|
|
where: {date: {[models.Sequelize.Op.eq]: new Date()}},
|
|
|
|
order: [["number", "DESC"]]
|
|
|
|
});
|
|
|
|
let c = await models.Command.create({
|
|
|
|
number: o ? o.number + 1 : 1,
|
2020-05-30 16:48:50 +02:00
|
|
|
price: await utils.price(data)
|
2020-05-29 19:44:12 +02:00
|
|
|
});
|
|
|
|
if (data.client)
|
|
|
|
await c.setClient(await models.User.findByPk(data.client));
|
|
|
|
if (data.pc)
|
|
|
|
await c.setPc(await models.User.findByPk(data.pc));
|
|
|
|
if (data.dish)
|
|
|
|
await c.setDish(await models.Dish.findByPk(data.dish));
|
|
|
|
if (data.ingredient)
|
2020-05-30 16:48:50 +02:00
|
|
|
for (let i of data.ingredient)
|
2020-05-29 19:44:12 +02:00
|
|
|
await c.addIngredient(await models.Ingredient.findByPk(i));
|
|
|
|
if (data.sauce)
|
2020-05-30 16:48:50 +02:00
|
|
|
for (let s of data.sauce)
|
2020-05-29 19:44:12 +02:00
|
|
|
await c.addSauce(await models.Sauce.findByPk(s));
|
|
|
|
if (data.drink)
|
|
|
|
await c.addDrink(await models.Drink.findByPk(data.drink));
|
|
|
|
if (data.dessert)
|
2020-05-30 16:48:50 +02:00
|
|
|
await c.setDessert(await models.Dessert.findByPk(data.dessert));
|
2020-05-29 21:56:35 +02:00
|
|
|
|
|
|
|
let send = utils.commandExport(c);
|
2020-05-29 19:44:12 +02:00
|
|
|
socket.emit("new command", send);
|
|
|
|
socket.broadcast.emit("new command", send);
|
|
|
|
} catch (e) {
|
2020-05-30 15:43:24 +02:00
|
|
|
socket.emit("internal error");
|
2020-05-29 19:44:12 +02:00
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|