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

@@ -6,17 +6,17 @@ const { getIO } = require("../libs/socket");
let sessions = [];
module.exports = {
initWbot: async dbSession => {
initWbot: async whatsapp => {
try {
const io = getIO();
const sessionName = dbSession.name;
const sessionName = whatsapp.name;
let sessionCfg;
if (dbSession && dbSession.session) {
sessionCfg = JSON.parse(dbSession.session);
if (whatsapp && whatsapp.session) {
sessionCfg = JSON.parse(whatsapp.session);
}
const sessionIndex = sessions.findIndex(s => s.id === dbSession.id);
const sessionIndex = sessions.findIndex(s => s.id === whatsapp.id);
if (sessionIndex !== -1) {
sessions[sessionIndex].destroy();
sessions.splice(sessionIndex, 1);
@@ -33,51 +33,51 @@ module.exports = {
qrCode.generate(qr, { small: true });
await dbSession.update({ id: 1, qrcode: qr, status: "qrcode" });
await whatsapp.update({ qrcode: qr, status: "qrcode" });
io.emit("session", {
io.emit("whatsappSession", {
action: "update",
session: dbSession,
session: whatsapp,
});
});
wbot.on("authenticated", async session => {
console.log("Session:", sessionName, "AUTHENTICATED");
await dbSession.update({
await whatsapp.update({
session: JSON.stringify(session),
status: "authenticated",
});
io.emit("session", {
io.emit("whatsappSession", {
action: "update",
session: dbSession,
session: whatsapp,
});
});
wbot.on("auth_failure", async msg => {
console.error("Session:", sessionName, "AUTHENTICATION FAILURE", msg);
await dbSession.update({ session: "" });
await whatsapp.update({ session: "" });
});
wbot.on("ready", async () => {
console.log("Session:", sessionName, "READY");
await dbSession.update({
await whatsapp.update({
status: "CONNECTED",
qrcode: "",
});
io.emit("session", {
io.emit("whatsappSession", {
action: "update",
session: dbSession,
session: whatsapp,
});
wbot.sendPresenceAvailable();
});
wbot.id = dbSession.id;
wbot.id = whatsapp.id;
sessions.push(wbot);
} catch (err) {
console.log(err);