Merge branch 'testeA'

pull/1/head
adriano 2022-02-13 22:40:04 -03:00
commit 441fc8120c
5 changed files with 56 additions and 7 deletions

View File

@ -3,6 +3,8 @@ import { getIO } from "../libs/socket";
import { removeWbot } from "../libs/wbot"; import { removeWbot } from "../libs/wbot";
import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession"; import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession";
import { removeDir } from "../helpers/DeleteDirectory";
import CreateWhatsAppService from "../services/WhatsappService/CreateWhatsAppService"; import CreateWhatsAppService from "../services/WhatsappService/CreateWhatsAppService";
import DeleteWhatsAppService from "../services/WhatsappService/DeleteWhatsAppService"; import DeleteWhatsAppService from "../services/WhatsappService/DeleteWhatsAppService";
import ListWhatsAppsService from "../services/WhatsappService/ListWhatsAppsService"; import ListWhatsAppsService from "../services/WhatsappService/ListWhatsAppsService";
@ -11,6 +13,8 @@ import UpdateWhatsAppService from "../services/WhatsappService/UpdateWhatsAppSer
import AppError from "../errors/AppError"; import AppError from "../errors/AppError";
import path from 'path';
interface WhatsappData { interface WhatsappData {
name: string; name: string;
queueIds: number[]; queueIds: number[];
@ -115,13 +119,18 @@ export const remove = async (
const { whatsappId } = req.params; const { whatsappId } = req.params;
await DeleteWhatsAppService(whatsappId); await DeleteWhatsAppService(whatsappId);
removeWbot(+whatsappId); removeWbot(+whatsappId);
console.log('Deleteou o whatsapp service com id = ', whatsappId)
const io = getIO(); const io = getIO();
io.emit("whatsapp", { io.emit("whatsapp", {
action: "delete", action: "delete",
whatsappId: +whatsappId whatsappId: +whatsappId
}); });
removeDir(path.join(process.cwd(),'WWebJS', `session-bd_${whatsappId}`))
return res.status(200).json({ message: "Whatsapp deleted." }); return res.status(200).json({ message: "Whatsapp deleted." });
}; };

View File

@ -1,9 +1,14 @@
import { Request, Response } from "express"; import { Request, Response } from "express";
import { getWbot } from "../libs/wbot"; import { getWbot } from "../libs/wbot";
import { removeDir } from "../helpers/DeleteDirectory";
import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService"; import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService";
import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession"; import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession";
import UpdateWhatsAppService from "../services/WhatsappService/UpdateWhatsAppService"; import UpdateWhatsAppService from "../services/WhatsappService/UpdateWhatsAppService";
import path from 'path';
// import { WWebJsw } from "../../WWebJS/session-bd_40"
const store = async (req: Request, res: Response): Promise<Response> => { const store = async (req: Request, res: Response): Promise<Response> => {
const { whatsappId } = req.params; const { whatsappId } = req.params;
const whatsapp = await ShowWhatsAppService(whatsappId); const whatsapp = await ShowWhatsAppService(whatsappId);
@ -32,6 +37,13 @@ const remove = async (req: Request, res: Response): Promise<Response> => {
const wbot = getWbot(whatsapp.id); const wbot = getWbot(whatsapp.id);
console.log(
'Desconectou s whatsapp.id: ', whatsapp.id,
' | PATH TO DELETE',path.join(process.cwd(),'WWebJS', `session-bd_${whatsapp.id}`)
)
removeDir(path.join(process.cwd(),'WWebJS', `session-bd_${whatsapp.id}`))
wbot.logout(); wbot.logout();
return res.status(200).json({ message: "Session disconnected." }); return res.status(200).json({ message: "Session disconnected." });

View File

@ -0,0 +1,24 @@
const fsPromises = require("fs/promises");
const fs = require('fs')
// Delete a directory and its children
export const removeDir = async (dirPath:string) => {
if (fs.existsSync(dirPath)){
try {
await fsPromises.rm(dirPath, { recursive: true });
console.log("Directory removed!");
}
catch (err) {
console.log('An error occurred while removing the directory: ',err);
}
}
else{
console.log('Directory not found to remove: ',dirPath)
}
}

View File

@ -45,10 +45,8 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
// sessionCfg = JSON.parse(whatsapp.session); // sessionCfg = JSON.parse(whatsapp.session);
// } // }
// const wbot: Session = new Client({ // const wbot: Session = new Client({session: sessionCfg,
// session: sessionCfg, // puppeteer: {executablePath: process.env.CHROME_BIN || undefined
// puppeteer: {
// executablePath: process.env.CHROME_BIN || undefined
// } // }
// }); // });
@ -58,13 +56,16 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => {
let sessionCfg let sessionCfg
if(fs.existsSync(SESSION_FILE_PATH)){ if(fs.existsSync(SESSION_FILE_PATH)){
sessionCfg = require(SESSION_FILE_PATH) sessionCfg = require(SESSION_FILE_PATH)
} }
const wbot: Session = new Client({ puppeteer: { headless: true }, clientId: 'bd_'+whatsapp.id})
const wbot: Session = new Client({ puppeteer: { headless: true }, clientId: 'bd_'+whatsapp.id})
wbot.initialize(); wbot.initialize();
wbot.on("qr", async qr => { wbot.on("qr", async qr => {
console.log('************** whatsapp.id: ',whatsapp.id)
logger.info("Session:", sessionName); logger.info("Session:", sessionName);
qrCode.generate(qr, { small: true }); qrCode.generate(qr, { small: true });
await whatsapp.update({ qrcode: qr, status: "qrcode", retries: 0 }); await whatsapp.update({ qrcode: qr, status: "qrcode", retries: 0 });

View File

@ -8,7 +8,7 @@ import { logger } from "../../utils/logger";
export const StartWhatsAppSession = async ( export const StartWhatsAppSession = async (
whatsapp: Whatsapp whatsapp: Whatsapp
): Promise<void> => { ): Promise<void> => {
await whatsapp.update({ status: "OPENING" }); await whatsapp.update({ status: "OPENING" });
const io = getIO(); const io = getIO();
io.emit("whatsappSession", { io.emit("whatsappSession", {
@ -21,6 +21,9 @@ export const StartWhatsAppSession = async (
wbotMessageListener(wbot); wbotMessageListener(wbot);
wbotMonitor(wbot, whatsapp); wbotMonitor(wbot, whatsapp);
} catch (err) { } catch (err) {
console.error(err )
logger.error(err); logger.error(err);
} }
}; };