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.
kfet/sockets/WIPCommand.js

41 lines
1.4 KiB
JavaScript
Raw Permalink Normal View History

const models = require("../models");
module.exports = socket => {
return async (data) => {
try {
let sandwich;
let c = await models.Command.findOne({where: {number: {[models.Sequelize.Op.eq]: data}, date: {[models.Sequelize.Op.eq]: new Date()}}});
let s = await models.Service.findOne({where:{date:{[models.Sequelize.Op.eq]: new Date()}}, include: ["sandwich1", "sandwich2", "sandwich3"]});
if (!c)
throw new Error("Command not found")
else if (!s)
throw new Error("Service not found");
for (let sn of ["sandwich1", "sandwich2", "sandwich3"]) {
2020-06-01 17:32:49 +02:00
await s.reload();
if (!c.WIP && s[sn] && !s[sn + "Busy"]) {
sandwich = sn;
break;
}
}
if (sandwich) {
2020-06-01 17:32:49 +02:00
s[sandwich + "Busy"] = true;
await c.setSandwich(s[sandwich]);
c.WIP = true;
await s.save();
2020-06-01 17:32:49 +02:00
await c.save();
let send = {
number: data,
2020-06-01 17:32:49 +02:00
sandwich: s[sandwich].username
}
socket.emit("WIP command", send);
socket.broadcast.emit("WIP command", send);
}
} catch (e) {
socket.emit("internal error");
console.error(e);
}
}
}