import { Request, Response } from "express"; 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"; import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService"; import UpdateWhatsAppService from "../services/WhatsappService/UpdateWhatsAppService"; import AppError from "../errors/AppError"; import path from 'path'; interface WhatsappData { name: string; queueIds: number[]; greetingMessage?: string; farewellMessage?: string; status?: string; isDefault?: boolean; } export const index = async (req: Request, res: Response): Promise => { const whatsapps = await ListWhatsAppsService(); return res.status(200).json(whatsapps); }; export const store = async (req: Request, res: Response): Promise => { const { name, status, isDefault, greetingMessage, farewellMessage, queueIds }: WhatsappData = req.body; if (req.user.profile !== "master") { throw new AppError("ERR_NO_PERMISSION", 403); } const { whatsapp, oldDefaultWhatsapp } = await CreateWhatsAppService({ name, status, isDefault, greetingMessage, farewellMessage, queueIds }); StartWhatsAppSession(whatsapp); const io = getIO(); io.emit("whatsapp", { action: "update", whatsapp }); if (oldDefaultWhatsapp) { io.emit("whatsapp", { action: "update", whatsapp: oldDefaultWhatsapp }); } return res.status(200).json(whatsapp); }; export const show = async (req: Request, res: Response): Promise => { const { whatsappId } = req.params; const whatsapp = await ShowWhatsAppService(whatsappId); return res.status(200).json(whatsapp); }; export const update = async ( req: Request, res: Response ): Promise => { const { whatsappId } = req.params; const whatsappData = req.body; const { whatsapp, oldDefaultWhatsapp } = await UpdateWhatsAppService({ whatsappData, whatsappId }); const io = getIO(); io.emit("whatsapp", { action: "update", whatsapp }); if (oldDefaultWhatsapp) { io.emit("whatsapp", { action: "update", whatsapp: oldDefaultWhatsapp }); } return res.status(200).json(whatsapp); }; export const remove = async ( req: Request, res: Response ): Promise => { if (req.user.profile !== "master") { throw new AppError("ERR_NO_PERMISSION", 403); } 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." }); };