diff --git a/backend/src/libs/socket.ts b/backend/src/libs/socket.ts index ec82552..cf5bcbf 100644 --- a/backend/src/libs/socket.ts +++ b/backend/src/libs/socket.ts @@ -1,5 +1,6 @@ import socketIo, { Server as SocketIO } from "socket.io"; import { Server } from "http"; +import AppError from "../errors/AppError"; let io: SocketIO; @@ -9,7 +10,7 @@ export const initIO = (httpServer: Server): SocketIO => { }; export const getIO = (): SocketIO => { if (!io) { - throw new Error("Socket IO not initialized"); + throw new AppError("Socket IO not initialized"); } return io; }; diff --git a/backend/src/libs/wbot.js b/backend/src/libs/wbot.js index 86ee3a0..6ba443d 100644 --- a/backend/src/libs/wbot.js +++ b/backend/src/libs/wbot.js @@ -1,110 +1,110 @@ -const qrCode = require("qrcode-terminal"); -const { Client } = require("whatsapp-web.js"); -const Whatsapp = require("../models/Whatsapp"); -const { getIO } = require("../libs/socket"); - -let sessions = []; - -module.exports = { - initWbot: async whatsapp => { - try { - const io = getIO(); - const sessionName = whatsapp.name; - let sessionCfg; - - if (whatsapp && whatsapp.session) { - sessionCfg = JSON.parse(whatsapp.session); - } - - const sessionIndex = sessions.findIndex(s => s.id === whatsapp.id); - if (sessionIndex !== -1) { - sessions[sessionIndex].destroy(); - sessions.splice(sessionIndex, 1); - } - - const wbot = new Client({ - session: sessionCfg, - restartOnAuthFail: true, - }); - wbot.initialize(); - - wbot.on("qr", async qr => { - console.log("Session:", sessionName); - - qrCode.generate(qr, { small: true }); - - await whatsapp.update({ qrcode: qr, status: "qrcode" }); - - io.emit("whatsappSession", { - action: "update", - session: whatsapp, - }); - }); - - wbot.on("authenticated", async session => { - console.log("Session:", sessionName, "AUTHENTICATED"); - - await whatsapp.update({ - session: JSON.stringify(session), - status: "authenticated", - }); - - io.emit("whatsappSession", { - action: "update", - session: whatsapp, - }); - }); - - wbot.on("auth_failure", async msg => { - console.error("Session:", sessionName, "AUTHENTICATION FAILURE", msg); - - await whatsapp.update({ session: "" }); - }); - - wbot.on("ready", async () => { - console.log("Session:", sessionName, "READY"); - - await whatsapp.update({ - status: "CONNECTED", - qrcode: "", - }); - - io.emit("whatsappSession", { - action: "update", - session: whatsapp, - }); - - wbot.sendPresenceAvailable(); - }); - - wbot.id = whatsapp.id; - sessions.push(wbot); - } catch (err) { - console.log(err); - } - - return null; - }, - - getWbot: whatsappId => { - const sessionIndex = sessions.findIndex(s => s.id === whatsappId); - - if (sessionIndex === -1) { - console.log("This Wbot session is not initialized"); - return null; - } - return sessions[sessionIndex]; - }, - - removeWbot: whatsappId => { - try { - const sessionIndex = sessions.findIndex(s => s.id === whatsappId); - if (sessionIndex !== -1) { - sessions[sessionIndex].destroy(); - sessions.splice(sessionIndex, 1); - } - } catch (err) { - console.log(err); - } - }, -}; +const qrCode = require("qrcode-terminal"); +const { Client } = require("whatsapp-web.js"); +const Whatsapp = require("../models/Whatsapp"); +const { getIO } = require("../libs/socket"); + +let sessions = []; + +module.exports = { + initWbot: async whatsapp => { + try { + const io = getIO(); + const sessionName = whatsapp.name; + let sessionCfg; + + if (whatsapp && whatsapp.session) { + sessionCfg = JSON.parse(whatsapp.session); + } + + const sessionIndex = sessions.findIndex(s => s.id === whatsapp.id); + if (sessionIndex !== -1) { + sessions[sessionIndex].destroy(); + sessions.splice(sessionIndex, 1); + } + + const wbot = new Client({ + session: sessionCfg, + restartOnAuthFail: true + }); + wbot.initialize(); + + wbot.on("qr", async qr => { + console.log("Session:", sessionName); + + qrCode.generate(qr, { small: true }); + + await whatsapp.update({ qrcode: qr, status: "qrcode" }); + + io.emit("whatsappSession", { + action: "update", + session: whatsapp + }); + }); + + wbot.on("authenticated", async session => { + console.log("Session:", sessionName, "AUTHENTICATED"); + + await whatsapp.update({ + session: JSON.stringify(session), + status: "authenticated" + }); + + io.emit("whatsappSession", { + action: "update", + session: whatsapp + }); + }); + + wbot.on("auth_failure", async msg => { + console.error("Session:", sessionName, "AUTHENTICATION FAILURE", msg); + + await whatsapp.update({ session: "" }); + }); + + wbot.on("ready", async () => { + console.log("Session:", sessionName, "READY"); + + await whatsapp.update({ + status: "CONNECTED", + qrcode: "" + }); + + io.emit("whatsappSession", { + action: "update", + session: whatsapp + }); + + wbot.sendPresenceAvailable(); + }); + + wbot.id = whatsapp.id; + sessions.push(wbot); + } catch (err) { + console.log(err); + } + + return null; + }, + + getWbot: whatsappId => { + const sessionIndex = sessions.findIndex(s => s.id === whatsappId); + + if (sessionIndex === -1) { + console.log("This Wbot session is not initialized"); + return null; + } + return sessions[sessionIndex]; + }, + + removeWbot: whatsappId => { + try { + const sessionIndex = sessions.findIndex(s => s.id === whatsappId); + if (sessionIndex !== -1) { + sessions[sessionIndex].destroy(); + sessions.splice(sessionIndex, 1); + } + } catch (err) { + console.log(err); + } + } +};