feat: finished multiple whatsapps handle in frontend

This commit is contained in:
canove
2020-09-06 12:09:12 -03:00
parent f423dd60bc
commit b0f28c16ad
17 changed files with 595 additions and 511 deletions

View File

@@ -0,0 +1,86 @@
const Whatsapp = require("../models/Whatsapp");
const { getIO } = require("../libs/socket");
const { getWbot, initWbot, removeWbot } = require("../libs/wbot");
const wbotMessageListener = require("../services/wbotMessageListener");
const wbotMonitor = require("../services/wbotMonitor");
exports.index = async (req, res) => {
const whatsapp = await Whatsapp.findAll();
return res.status(200).json(whatsapp);
};
exports.store = async (req, res) => {
const io = getIO();
const whatsapp = await Whatsapp.create(req.body);
if (!whatsapp) {
return res.status(400).json({ error: "Cannot create whatsapp session." });
}
initWbot(whatsapp)
.then(() => {
wbotMessageListener(whatsapp);
wbotMonitor(whatsapp);
})
.catch(err => console.log(err));
io.emit("whatsapp", {
action: "update",
whatsapp: whatsapp,
});
return res.status(200).json(whatsapp);
};
exports.show = async (req, res) => {
const { whatsappId } = req.params;
const whatsapp = await Whatsapp.findByPk(whatsappId);
if (!whatsapp) {
return res.status(200).json({ message: "Session not found" });
}
return res.status(200).json(whatsapp);
};
exports.update = async (req, res) => {
const io = getIO();
const { whatsappId } = req.params;
const whatsapp = await Whatsapp.findByPk(whatsappId);
if (!whatsapp) {
return res.status(404).json({ message: "Whatsapp not found" });
}
await whatsapp.update(req.body);
io.emit("whatsapp", {
action: "update",
whatsapp: whatsapp,
});
return res.status(200).json({ message: "Whatsapp updated" });
};
exports.delete = async (req, res) => {
const io = getIO();
const { whatsappId } = req.params;
const whatsapp = await Whatsapp.findByPk(whatsappId);
if (!whatsapp) {
return res.status(404).json({ message: "Whatsapp not found" });
}
await whatsapp.destroy();
removeWbot(whatsapp.id);
io.emit("whatsapp", {
action: "delete",
whatsappId: whatsapp.id,
});
return res.status(200).json({ message: "Whatsapp deleted." });
};

View File

@@ -1,91 +1,32 @@
const Whatsapp = require("../models/Whatsapp");
const { getIO } = require("../libs/socket");
const { getWbot, initWbot, removeWbot } = require("../libs/wbot");
const wbotMessageListener = require("../services/wbotMessageListener");
const wbotMonitor = require("../services/wbotMonitor");
// const Whatsapp = require("../models/Whatsapp");
// const { getIO } = require("../libs/socket");
// const { getWbot, initWbot, removeWbot } = require("../libs/wbot");
// const wbotMessageListener = require("../services/wbotMessageListener");
// const wbotMonitor = require("../services/wbotMonitor");
exports.index = async (req, res) => {
const dbSession = await Whatsapp.findAll();
// exports.show = async (req, res) => {
// const { whatsappId } = req.params;
// const dbSession = await Whatsapp.findByPk(whatsappId);
return res.status(200).json(dbSession);
};
// if (!dbSession) {
// return res.status(200).json({ message: "Session not found" });
// }
exports.store = async (req, res) => {
const io = getIO();
const dbSession = await Whatsapp.create(req.body);
// return res.status(200).json(dbSession);
// };
if (!dbSession) {
return res.status(400).json({ error: "Cannot create whatsapp session." });
}
// exports.delete = async (req, res) => {
// const { whatsappId } = req.params;
initWbot(dbSession)
.then(() => {
wbotMessageListener(dbSession);
wbotMonitor(dbSession);
})
.catch(err => console.log(err));
// const dbSession = await Whatsapp.findByPk(whatsappId);
io.emit("session", {
action: "update",
session: dbSession,
});
// if (!dbSession) {
// return res.status(404).json({ message: "Session not found" });
// }
return res.status(200).json(dbSession);
};
// const wbot = getWbot(dbSession.id);
exports.show = async (req, res) => {
const { sessionId } = req.params;
const dbSession = await Whatsapp.findByPk(sessionId);
// wbot.logout();
if (!dbSession) {
return res.status(200).json({ message: "Session not found" });
}
return res.status(200).json(dbSession);
};
exports.update = async (req, res) => {
const io = getIO();
const { sessionId } = req.params;
const dbSession = await Whatsapp.findByPk(sessionId);
if (!dbSession) {
return res.status(404).json({ message: "Session not found" });
}
const wbot = getWbot(dbSession.id);
wbot.logout();
await dbSession.update(req.body);
io.emit("session", {
action: "update",
session: dbSession,
});
return res.status(200).json({ message: "Session updated" });
};
exports.delete = async (req, res) => {
const io = getIO();
const { sessionId } = req.params;
const dbSession = await Whatsapp.findByPk(sessionId);
if (!dbSession) {
return res.status(404).json({ message: "Session not found" });
}
const wbot = getWbot(dbSession.id);
await dbSession.destroy();
removeWbot(dbSession.id);
io.emit("session", {
action: "delete",
sessionId: dbSession.id,
});
return res.status(200).json({ message: "Session deleted." });
};
// return res.status(200).json({ message: "Session disconnected." });
// };