Server-side price calculation
This commit is contained in:
parent
df5cba87ad
commit
14369ba8bc
5 changed files with 46 additions and 12 deletions
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
|
12
sockets/price.js
Normal file
12
sockets/price.js
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
|
Reference in a new issue