From 19f79a5fdc5443fc27c0d529f64bee15f02cb494 Mon Sep 17 00:00:00 2001 From: flifloo Date: Fri, 29 May 2020 21:56:35 +0200 Subject: [PATCH] Some code refactor --- sockets/addCommand.js | 17 +++-------------- sockets/clearCommand.js | 8 ++------ sockets/giveCommand.js | 8 ++------ sockets/listCommand.js | 16 ++-------------- sockets/utils.js | 27 +++++++++++++++++++++++++++ 5 files changed, 36 insertions(+), 40 deletions(-) create mode 100644 sockets/utils.js diff --git a/sockets/addCommand.js b/sockets/addCommand.js index 71d2aae..9f03db4 100644 --- a/sockets/addCommand.js +++ b/sockets/addCommand.js @@ -1,4 +1,5 @@ const models = require("../models") +const utils = require("./utils") module.exports = socket => { return async (data) => { @@ -27,20 +28,8 @@ module.exports = socket => { await c.addDrink(await models.Drink.findByPk(data.drink)); if (data.dessert) await c.setDessert(await models.Drink.findByPk(data.dessert)); - let send = { - number: c.number, - sandwich: c.sandwich ? c.sandwich.username : null, - client: c.client ? c.client.firstName + " " + c.client.lastName : null, - dish: c.Dish ? c.Dish.name : null, - ingredients: c.Ingredients ? c.Ingredients.map(i => i.name) : null, - sauces: c.Sauces ? c.Sauces.map(s => s.name) : null, - drink: c.Drink ? c.Drink.name : null, - dessert: c.Dessert ? c.Dessert.name : null, - error: c.error, - give: c.give, - done: c.done, - WIP: c.WIP - } + + let send = utils.commandExport(c); socket.emit("new command", send); socket.broadcast.emit("new command", send); } catch (e) { diff --git a/sockets/clearCommand.js b/sockets/clearCommand.js index e1de5f8..7f75613 100644 --- a/sockets/clearCommand.js +++ b/sockets/clearCommand.js @@ -1,4 +1,5 @@ const models = require("../models") +const utils = require("./utils") module.exports = socket => { return async (data) => { @@ -10,12 +11,7 @@ module.exports = socket => { c.give = null; c.error = false; - let service = models.Service.findOne({where:{date:{[models.Sequelize.Op.eq]: new Date()}}}); - if (c.WIP && service) { - let sandwichs = [service.sandwich1Id, service.sandwich2Id, service.sandwich3Id] - if (c.sandwichId in sandwichs) - service["sandwich"+sandwichs.indexOf(c.sandwichId)+1] = false; - } + utils.resetService(c, await models.Service.findOne({where:{date:{[models.Sequelize.Op.eq]: new Date()}}})); c.WIP = false; await c.save(); diff --git a/sockets/giveCommand.js b/sockets/giveCommand.js index d42001c..ed16c50 100644 --- a/sockets/giveCommand.js +++ b/sockets/giveCommand.js @@ -1,4 +1,5 @@ const models = require("../models") +const utils = require("./utils") module.exports = socket => { return async (data) => { @@ -9,12 +10,7 @@ module.exports = socket => { c.give = new Date() - let service = models.Service.findOne({where:{date:{[models.Sequelize.Op.eq]: new Date()}}}); - if (c.WIP && service) { - let sandwiches = [service.sandwich1Id, service.sandwich2Id, service.sandwich3Id] - if (c.sandwichId in sandwiches) - service["sandwich"+sandwiches.indexOf(c.sandwichId)+1] = false; - } + utils.resetService(c, await models.Service.findOne({where:{date:{[models.Sequelize.Op.eq]: new Date()}}})); c.WIP = false; await c.save(); diff --git a/sockets/listCommand.js b/sockets/listCommand.js index 3214422..9c26deb 100644 --- a/sockets/listCommand.js +++ b/sockets/listCommand.js @@ -1,4 +1,5 @@ const models = require("../models") +const utils = require("./utils") module.exports = socket => { return async () => { @@ -12,20 +13,7 @@ module.exports = socket => { order: ["number"], include: [models.Dish, models.Ingredient, models.Sauce, models.Drink, models.Dessert, "client", "pc", "sandwich"] })) { - commands.push({ - number: c.number, - sandwich: c.sandwich ? c.sandwich.username : null, - client: c.client ? c.client.firstName + " " + c.client.lastName : null, - dish: c.Dish ? c.Dish.name : null, - ingredients: c.Ingredients ? c.Ingredients.map(i => i.name) : null, - sauces: c.Sauces ? c.Sauces.map(s => s.name) : null, - drink: c.Drink ? c.Drink.name : null, - dessert: c.Dessert ? c.Dessert.name : null, - error: c.error, - give: c.give, - done: c.done, - WIP: c.WIP - }); + commands.push(utils.commandExport(c)); } socket.emit("list command", commands); } diff --git a/sockets/utils.js b/sockets/utils.js new file mode 100644 index 0000000..330c6d3 --- /dev/null +++ b/sockets/utils.js @@ -0,0 +1,27 @@ +function commandExport (c ) { + return { + number: c.number, + sandwich: c.sandwich ? c.sandwich.username : null, + client: c.client ? c.client.firstName + " " + c.client.lastName : null, + dish: c.Dish ? c.Dish.name : null, + ingredients: c.Ingredients ? c.Ingredients.map(i => i.name) : null, + sauces: c.Sauces ? c.Sauces.map(s => s.name) : null, + drink: c.Drink ? c.Drink.name : null, + dessert: c.Dessert ? c.Dessert.name : null, + error: c.error, + give: c.give, + done: c.done, + WIP: c.WIP + } +} + +function resetService(c, service) { + if (c.WIP && service) { + let sandwiches = [service.sandwich1Id, service.sandwich2Id, service.sandwich3Id] + if (c.sandwichId in sandwiches) + service["sandwich"+sandwiches.indexOf(c.sandwichId)+1] = false; + } +} + +module.exports.commandExport = commandExport; +module.exports.resetService = resetService;