feat: added routes to handle whatsapp session

This commit is contained in:
canove
2020-10-26 19:55:07 -03:00
parent 4e9d9f7d15
commit 73cb08a557
13 changed files with 118 additions and 96 deletions

View File

@@ -0,0 +1,11 @@
import Whatsapp from "../../models/Whatsapp";
import { StartWhatsAppSession } from "./StartWhatsAppSession";
export const StartAllWhatsAppsSessions = async (): Promise<void> => {
const whatsapps = await Whatsapp.findAll();
if (whatsapps.length > 0) {
whatsapps.forEach(whatsapp => {
StartWhatsAppSession(whatsapp);
});
}
};

View File

@@ -0,0 +1,16 @@
import { initWbot } from "../../libs/wbot";
import Whatsapp from "../../models/Whatsapp";
import wbotMessageListener from "./wbotMessageListener";
import wbotMonitor from "./wbotMonitor";
export const StartWhatsAppSession = async (
whatsapp: Whatsapp
): Promise<void> => {
try {
const wbot = await initWbot(whatsapp);
wbotMessageListener(wbot);
wbotMonitor(wbot, whatsapp);
} catch (err) {
console.log(err);
}
};

View File

@@ -1,19 +0,0 @@
import { initWbot } from "../../libs/wbot";
import Whatsapp from "../../models/Whatsapp";
import wbotMessageListener from "./wbotMessageListener";
import wbotMonitor from "./wbotMonitor";
export const StartWhatsAppSessions = async (): Promise<void> => {
const whatsapps = await Whatsapp.findAll();
if (whatsapps.length > 0) {
whatsapps.forEach(async whatsapp => {
try {
const wbot = await initWbot(whatsapp);
wbotMessageListener(wbot);
wbotMonitor(wbot, whatsapp);
} catch (err) {
console.log(err);
}
});
}
};

View File

@@ -1,11 +1,9 @@
import * as Sentry from "@sentry/node";
import { Client } from "whatsapp-web.js";
import wbotMessageListener from "./wbotMessageListener";
import { getIO } from "../../libs/socket";
import { initWbot } from "../../libs/wbot";
import Whatsapp from "../../models/Whatsapp";
import { StartWhatsAppSession } from "./StartWhatsAppSession";
interface Session extends Client {
id?: number;
@@ -56,7 +54,7 @@ const wbotMonitor = async (
wbot.on("disconnected", async reason => {
console.log("Disconnected session:", sessionName, reason);
try {
await whatsapp.update({ status: "disconnected" });
await whatsapp.update({ status: "DISCONNECTED", session: "" });
} catch (err) {
Sentry.captureException(err);
console.log(err);
@@ -67,26 +65,8 @@ const wbotMonitor = async (
session: whatsapp
});
// to be removed after adding buttons to rebuild session on frontend
setTimeout(
() =>
initWbot(whatsapp)
.then(() => {
wbotMessageListener(wbot);
wbotMonitor(wbot, whatsapp);
})
.catch(err => {
Sentry.captureException(err);
console.log(err);
}),
2000
);
setTimeout(() => StartWhatsAppSession(whatsapp), 2000);
});
// setInterval(() => {
// wbot.resetState();
// }, 20000);
} catch (err) {
Sentry.captureException(err);
console.log(err);

View File

@@ -1,9 +1,7 @@
import Whatsapp from "../../models/Whatsapp";
import AppError from "../../errors/AppError";
const ShowWhatsAppService = async (
id: string | number
): Promise<Whatsapp | undefined> => {
const ShowWhatsAppService = async (id: string | number): Promise<Whatsapp> => {
const whatsapp = await Whatsapp.findByPk(id);
if (!whatsapp) {