use appError on socket io

This commit is contained in:
canove
2020-09-20 10:52:05 -03:00
parent b7484afd63
commit aa6f116514
2 changed files with 112 additions and 111 deletions

View File

@@ -1,5 +1,6 @@
import socketIo, { Server as SocketIO } from "socket.io"; import socketIo, { Server as SocketIO } from "socket.io";
import { Server } from "http"; import { Server } from "http";
import AppError from "../errors/AppError";
let io: SocketIO; let io: SocketIO;
@@ -9,7 +10,7 @@ export const initIO = (httpServer: Server): SocketIO => {
}; };
export const getIO = (): SocketIO => { export const getIO = (): SocketIO => {
if (!io) { if (!io) {
throw new Error("Socket IO not initialized"); throw new AppError("Socket IO not initialized");
} }
return io; return io;
}; };

View File

@@ -1,110 +1,110 @@
const qrCode = require("qrcode-terminal"); const qrCode = require("qrcode-terminal");
const { Client } = require("whatsapp-web.js"); const { Client } = require("whatsapp-web.js");
const Whatsapp = require("../models/Whatsapp"); const Whatsapp = require("../models/Whatsapp");
const { getIO } = require("../libs/socket"); const { getIO } = require("../libs/socket");
let sessions = []; let sessions = [];
module.exports = { module.exports = {
initWbot: async whatsapp => { initWbot: async whatsapp => {
try { try {
const io = getIO(); const io = getIO();
const sessionName = whatsapp.name; const sessionName = whatsapp.name;
let sessionCfg; let sessionCfg;
if (whatsapp && whatsapp.session) { if (whatsapp && whatsapp.session) {
sessionCfg = JSON.parse(whatsapp.session); sessionCfg = JSON.parse(whatsapp.session);
} }
const sessionIndex = sessions.findIndex(s => s.id === whatsapp.id); const sessionIndex = sessions.findIndex(s => s.id === whatsapp.id);
if (sessionIndex !== -1) { if (sessionIndex !== -1) {
sessions[sessionIndex].destroy(); sessions[sessionIndex].destroy();
sessions.splice(sessionIndex, 1); sessions.splice(sessionIndex, 1);
} }
const wbot = new Client({ const wbot = new Client({
session: sessionCfg, session: sessionCfg,
restartOnAuthFail: true, restartOnAuthFail: true
}); });
wbot.initialize(); wbot.initialize();
wbot.on("qr", async qr => { wbot.on("qr", async qr => {
console.log("Session:", sessionName); console.log("Session:", sessionName);
qrCode.generate(qr, { small: true }); qrCode.generate(qr, { small: true });
await whatsapp.update({ qrcode: qr, status: "qrcode" }); await whatsapp.update({ qrcode: qr, status: "qrcode" });
io.emit("whatsappSession", { io.emit("whatsappSession", {
action: "update", action: "update",
session: whatsapp, session: whatsapp
}); });
}); });
wbot.on("authenticated", async session => { wbot.on("authenticated", async session => {
console.log("Session:", sessionName, "AUTHENTICATED"); console.log("Session:", sessionName, "AUTHENTICATED");
await whatsapp.update({ await whatsapp.update({
session: JSON.stringify(session), session: JSON.stringify(session),
status: "authenticated", status: "authenticated"
}); });
io.emit("whatsappSession", { io.emit("whatsappSession", {
action: "update", action: "update",
session: whatsapp, session: whatsapp
}); });
}); });
wbot.on("auth_failure", async msg => { wbot.on("auth_failure", async msg => {
console.error("Session:", sessionName, "AUTHENTICATION FAILURE", msg); console.error("Session:", sessionName, "AUTHENTICATION FAILURE", msg);
await whatsapp.update({ session: "" }); await whatsapp.update({ session: "" });
}); });
wbot.on("ready", async () => { wbot.on("ready", async () => {
console.log("Session:", sessionName, "READY"); console.log("Session:", sessionName, "READY");
await whatsapp.update({ await whatsapp.update({
status: "CONNECTED", status: "CONNECTED",
qrcode: "", qrcode: ""
}); });
io.emit("whatsappSession", { io.emit("whatsappSession", {
action: "update", action: "update",
session: whatsapp, session: whatsapp
}); });
wbot.sendPresenceAvailable(); wbot.sendPresenceAvailable();
}); });
wbot.id = whatsapp.id; wbot.id = whatsapp.id;
sessions.push(wbot); sessions.push(wbot);
} catch (err) { } catch (err) {
console.log(err); console.log(err);
} }
return null; return null;
}, },
getWbot: whatsappId => { getWbot: whatsappId => {
const sessionIndex = sessions.findIndex(s => s.id === whatsappId); const sessionIndex = sessions.findIndex(s => s.id === whatsappId);
if (sessionIndex === -1) { if (sessionIndex === -1) {
console.log("This Wbot session is not initialized"); console.log("This Wbot session is not initialized");
return null; return null;
} }
return sessions[sessionIndex]; return sessions[sessionIndex];
}, },
removeWbot: whatsappId => { removeWbot: whatsappId => {
try { try {
const sessionIndex = sessions.findIndex(s => s.id === whatsappId); const sessionIndex = sessions.findIndex(s => s.id === whatsappId);
if (sessionIndex !== -1) { if (sessionIndex !== -1) {
sessions[sessionIndex].destroy(); sessions[sessionIndex].destroy();
sessions.splice(sessionIndex, 1); sessions.splice(sessionIndex, 1);
} }
} catch (err) { } catch (err) {
console.log(err); console.log(err);
} }
}, }
}; };