Adição do método responsável por excluir os diretórios das sessions do whatsapp
							parent
							
								
									737d6f6a46
								
							
						
					
					
						commit
						de4877b31f
					
				|  | @ -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." }); | ||||
| }; | ||||
|  |  | |||
|  | @ -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<Response> => { | ||||
|   const { whatsappId } = req.params; | ||||
|   const whatsapp = await ShowWhatsAppService(whatsappId); | ||||
|  | @ -32,6 +37,13 @@ const remove = async (req: Request, res: Response): Promise<Response> => { | |||
| 
 | ||||
|   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." }); | ||||
|  |  | |||
|  | @ -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) | ||||
|          | ||||
|     } | ||||
| 
 | ||||
| } | ||||
|  | @ -45,10 +45,8 @@ export const initWbot = async (whatsapp: Whatsapp): Promise<Session> => { | |||
|       //   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<Session> => { | |||
|       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 }); | ||||
|  |  | |||
|  | @ -8,7 +8,7 @@ import { logger } from "../../utils/logger"; | |||
| export const StartWhatsAppSession = async ( | ||||
|   whatsapp: Whatsapp | ||||
| ): Promise<void> => { | ||||
|   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); | ||||
|      | ||||
|   } | ||||
| }; | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue