Skip to content

Commit

Permalink
fix: corrigdo fluxo na gestão da sessão
Browse files Browse the repository at this point in the history
  • Loading branch information
ldurans committed Nov 14, 2022
1 parent c105543 commit d9435f3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 57 deletions.
56 changes: 1 addition & 55 deletions backend/src/libs/wbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,8 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
});
});

wbot.on("authenticated", async session => {
wbot.on("authenticated", async () => {
logger.info(`Session: ${sessionName} AUTHENTICATED`);
if (session) {
await whatsapp.update({
session: JSON.stringify(session)
});
}
});

wbot.on("auth_failure", async msg => {
Expand Down Expand Up @@ -126,10 +121,6 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
wbot.on("ready", async () => {
logger.info(`Session: ${sessionName}-READY`);

// if (process.env.NODE_ENV === "prod") {
// logger.info("Iniciando sincronização de contatos.");
// syncContacts(wbot, tenantId);
// }
const info: any = wbot?.info;
await whatsapp.update({
status: "CONNECTED",
Expand Down Expand Up @@ -162,51 +153,6 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
resolve(wbot);
});

wbot.on("disconnected", async reason => {
logger.info(`disconnected wbot ${reason}`);

try {
if (reason === "UNPAIRED") {
logger.info(
`Disconnected(UNPAIRED) session DEstroy: ${sessionName} | ${reason}`
);
const sessionIndex = sessions.findIndex(s => s.id === whatsapp.id);
if (sessionIndex === -1) {
wbot.id = whatsapp.id;
sessions.push(wbot);
} else {
wbot.id = whatsapp.id;
sessions[sessionIndex] = wbot;
}
await whatsapp.update({
status: "DESTROYED",
retries: 0,
session: ""
});
await apagarPastaSessao(whatsapp.id);
await wbot.logout();
await wbot.destroy();
} else if (reason === "CONFLICT") {
await whatsapp.update({ status: "DISCONNECTED", retries: 0 });
} else {
await whatsapp.update({
status: "DESTROYED",
retries: 0,
session: ""
});
await apagarPastaSessao(whatsapp.id);
await wbot.logout();
await wbot.destroy();
}
} catch (err) {
logger.error(`wbot: update: disconnected.Error: ${err}`);
}

io.emit(`${whatsapp.tenantId}:whatsappSession`, {
action: "update",
session: whatsapp
});
});
setInterval(
checkMessages,
+(process.env.CHECK_INTERVAL || 5000),
Expand Down
9 changes: 7 additions & 2 deletions backend/src/services/WbotServices/wbotMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Client } from "whatsapp-web.js";
import { getIO } from "../../libs/socket";
import Whatsapp from "../../models/Whatsapp";
import { logger } from "../../utils/logger";
import { StartWhatsAppSession } from "./StartWhatsAppSession";
// import { StartWhatsAppSession } from "./StartWhatsAppSession";

interface Session extends Client {
Expand Down Expand Up @@ -61,12 +62,16 @@ const wbotMonitor = async (

wbot.on("disconnected", async reason => {
logger.info(`Disconnected session: ${sessionName} | Reason: ${reason}`);
try {
await whatsapp.update({ status: "OPENING", session: "" });
} catch (err) {
logger.error(err);
}
io.emit(`${whatsapp.tenantId}:whatsappSession`, {
action: "update",
session: whatsapp
});

// setTimeout(() => StartWhatsAppSession(whatsapp), 2000);
setTimeout(() => StartWhatsAppSession(whatsapp), 2000);
});
} catch (err) {
logger.error(err);
Expand Down

0 comments on commit d9435f3

Please sign in to comment.