From de4877b31f922daabb074049999d96982f573eb0 Mon Sep 17 00:00:00 2001 From: adriano Date: Sun, 13 Feb 2022 22:39:33 -0300 Subject: [PATCH] =?UTF-8?q?Adi=C3=A7=C3=A3o=20do=20m=C3=A9todo=20respons?= =?UTF-8?q?=C3=A1vel=20por=20excluir=20os=20diret=C3=B3rios=20das=20sessio?= =?UTF-8?q?ns=20do=20whatsapp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/controllers/WhatsAppController.ts | 9 +++++++ .../controllers/WhatsAppSessionController.ts | 12 ++++++++++ backend/src/helpers/DeleteDirectory.ts | 24 +++++++++++++++++++ backend/src/libs/wbot.ts | 13 +++++----- .../WbotServices/StartWhatsAppSession.ts | 5 +++- 5 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 backend/src/helpers/DeleteDirectory.ts diff --git a/backend/src/controllers/WhatsAppController.ts b/backend/src/controllers/WhatsAppController.ts index 66ca018..b712c13 100644 --- a/backend/src/controllers/WhatsAppController.ts +++ b/backend/src/controllers/WhatsAppController.ts @@ -3,6 +3,8 @@ import { getIO } from "../libs/socket"; import { removeWbot } from "../libs/wbot"; import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession"; +import { removeDir } from "../helpers/DeleteDirectory"; + import CreateWhatsAppService from "../services/WhatsappService/CreateWhatsAppService"; import DeleteWhatsAppService from "../services/WhatsappService/DeleteWhatsAppService"; import ListWhatsAppsService from "../services/WhatsappService/ListWhatsAppsService"; @@ -11,6 +13,8 @@ import UpdateWhatsAppService from "../services/WhatsappService/UpdateWhatsAppSer import AppError from "../errors/AppError"; +import path from 'path'; + interface WhatsappData { name: string; queueIds: number[]; @@ -115,13 +119,18 @@ export const remove = async ( const { whatsappId } = req.params; await DeleteWhatsAppService(whatsappId); + removeWbot(+whatsappId); + console.log('Deleteou o whatsapp service com id = ', whatsappId) + const io = getIO(); io.emit("whatsapp", { action: "delete", whatsappId: +whatsappId }); + removeDir(path.join(process.cwd(),'WWebJS', `session-bd_${whatsappId}`)) + return res.status(200).json({ message: "Whatsapp deleted." }); }; diff --git a/backend/src/controllers/WhatsAppSessionController.ts b/backend/src/controllers/WhatsAppSessionController.ts index 045aa20..6b5c613 100644 --- a/backend/src/controllers/WhatsAppSessionController.ts +++ b/backend/src/controllers/WhatsAppSessionController.ts @@ -1,9 +1,14 @@ import { Request, Response } from "express"; import { getWbot } from "../libs/wbot"; +import { removeDir } from "../helpers/DeleteDirectory"; import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService"; import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSession"; 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 => { const { whatsappId } = req.params; const whatsapp = await ShowWhatsAppService(whatsappId); @@ -32,6 +37,13 @@ const remove = async (req: Request, res: Response): Promise => { 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(); return res.status(200).json({ message: "Session disconnected." }); diff --git a/backend/src/helpers/DeleteDirectory.ts b/backend/src/helpers/DeleteDirectory.ts new file mode 100644 index 0000000..b577a16 --- /dev/null +++ b/backend/src/helpers/DeleteDirectory.ts @@ -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) + + } + +} \ No newline at end of file diff --git a/backend/src/libs/wbot.ts b/backend/src/libs/wbot.ts index a650466..cfe6d1f 100644 --- a/backend/src/libs/wbot.ts +++ b/backend/src/libs/wbot.ts @@ -45,10 +45,8 @@ export const initWbot = async (whatsapp: Whatsapp): Promise => { // sessionCfg = JSON.parse(whatsapp.session); // } - // const wbot: Session = new Client({ - // session: sessionCfg, - // puppeteer: { - // executablePath: process.env.CHROME_BIN || undefined + // const wbot: Session = new Client({session: sessionCfg, + // puppeteer: {executablePath: process.env.CHROME_BIN || undefined // } // }); @@ -58,13 +56,16 @@ export const initWbot = async (whatsapp: Whatsapp): Promise => { let sessionCfg if(fs.existsSync(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.on("qr", async qr => { + + console.log('************** whatsapp.id: ',whatsapp.id) + logger.info("Session:", sessionName); qrCode.generate(qr, { small: true }); await whatsapp.update({ qrcode: qr, status: "qrcode", retries: 0 }); diff --git a/backend/src/services/WbotServices/StartWhatsAppSession.ts b/backend/src/services/WbotServices/StartWhatsAppSession.ts index 074f5fa..f9e006c 100644 --- a/backend/src/services/WbotServices/StartWhatsAppSession.ts +++ b/backend/src/services/WbotServices/StartWhatsAppSession.ts @@ -8,7 +8,7 @@ import { logger } from "../../utils/logger"; export const StartWhatsAppSession = async ( whatsapp: Whatsapp ): Promise => { - await whatsapp.update({ status: "OPENING" }); + await whatsapp.update({ status: "OPENING" }); const io = getIO(); io.emit("whatsappSession", { @@ -21,6 +21,9 @@ export const StartWhatsAppSession = async ( wbotMessageListener(wbot); wbotMonitor(wbot, whatsapp); } catch (err) { + + console.error(err ) logger.error(err); + } };