diff --git a/public/javascripts/commands.js b/public/javascripts/commands.js index 37b7422..8fb87bc 100644 --- a/public/javascripts/commands.js +++ b/public/javascripts/commands.js @@ -177,13 +177,7 @@ function error(e) { } function price() { - let p = 0; - for (let i in current.price) { - p += current.price[i] - } - p = p.toFixed(2); - document.querySelector("#resume h2").innerHTML = p+"€"; - return p; + socket.emit("price", current); } function addUser() { @@ -205,7 +199,6 @@ function addUser() { function addCommand() { current.pc = null; - current.price = price(); socket.emit("add command", current); clearSelections(); } @@ -351,6 +344,10 @@ socket.on("add user", data => { addCommand(); }); +socket.on("price", data => { + document.querySelector("#resume h2").innerHTML = data+"€"; +}) + socket.on("fail add user", () => { alert("User creation fail !"); addUser(); diff --git a/sockets/addCommand.js b/sockets/addCommand.js index cc47783..b9761f7 100644 --- a/sockets/addCommand.js +++ b/sockets/addCommand.js @@ -10,7 +10,7 @@ module.exports = socket => { }); let c = await models.Command.create({ number: o ? o.number + 1 : 1, - price: data.price + price: await utils.price(data) }); if (data.client) await c.setClient(await models.User.findByPk(data.client)); @@ -19,15 +19,15 @@ module.exports = socket => { if (data.dish) await c.setDish(await models.Dish.findByPk(data.dish)); if (data.ingredient) - for (let i in data.ingredient) + for (let i of data.ingredient) await c.addIngredient(await models.Ingredient.findByPk(i)); if (data.sauce) - for (let s in data.sauce) + for (let s of data.sauce) await c.addSauce(await models.Sauce.findByPk(s)); if (data.drink) await c.addDrink(await models.Drink.findByPk(data.drink)); if (data.dessert) - await c.setDessert(await models.Drink.findByPk(data.dessert)); + await c.setDessert(await models.Dessert.findByPk(data.dessert)); let send = utils.commandExport(c); socket.emit("new command", send); diff --git a/sockets/index.js b/sockets/index.js index 60decac..bd67cb5 100644 --- a/sockets/index.js +++ b/sockets/index.js @@ -10,6 +10,7 @@ const addUSer = require("./addUser") const giveCommand = require("./giveCommand"); const errorCommand = require("./errorCommand"); const clearCommand = require("./clearCommand"); +const price = require("./price") module.exports = socket => { socket.on("list command", listCommand(socket)); @@ -25,6 +26,7 @@ module.exports = socket => { socket.on("give command", giveCommand(socket)); socket.on("error command", errorCommand(socket)); socket.on("clear command", clearCommand(socket)); + socket.on("price", price(socket)); console.log("New connection !"); socket.emit("connected"); } diff --git a/sockets/price.js b/sockets/price.js new file mode 100644 index 0000000..b60f8f5 --- /dev/null +++ b/sockets/price.js @@ -0,0 +1,12 @@ +const utils = require("./utils"); + +module.exports = socket => { + return async (data) => { + try { + socket.emit("price", await utils.price(data)); + } catch (e) { + socket.emit("internal error"); + console.error(e); + } + } +} diff --git a/sockets/utils.js b/sockets/utils.js index 330c6d3..93322f1 100644 --- a/sockets/utils.js +++ b/sockets/utils.js @@ -1,3 +1,5 @@ +const models = require("../models"); + function commandExport (c ) { return { number: c.number, @@ -23,5 +25,26 @@ function resetService(c, service) { } } +async function price(data) { + let price = 0; + if (data.dish) + price += (await models.Dish.findByPk(data.dish)).price; + if (data.ingredient) + for (let i of data.ingredient) + price += (await models.Ingredient.findByPk(i)).price; + if (data.sauce) + for (let s of data.sauce) + price += (await models.Sauce.findByPk(s)).price; + if (data.drink) + price += (await models.Drink.findByPk(data.drink)).price; + if (data.dessert) + price += (await models.Dessert.findByPk(data.dessert)).price; + + if (data.dish && data.ingredient && data.sauce && data.drink && data.dessert) + price -= 0.3; + return price; +} + module.exports.commandExport = commandExport; module.exports.resetService = resetService; +module.exports.price = price;