diff --git a/backend/src/controllers/WbotMonitorController.ts b/backend/src/controllers/WbotMonitorController.ts index 25ee67c..294f2a5 100644 --- a/backend/src/controllers/WbotMonitorController.ts +++ b/backend/src/controllers/WbotMonitorController.ts @@ -3,6 +3,7 @@ import { getIO } from "../libs/socket"; import ShowWhatsAppService from "../services/WhatsappService/ShowWhatsAppService"; import { logger } from "../utils/logger"; import * as Sentry from "@sentry/node"; +import Whatsapp from "../models/Whatsapp"; // type IndexQuery = { @@ -15,18 +16,23 @@ export const wbotMonitorRemote = async (req: Request, res: Response): Promise ACTION: ', req.body['action']) - let whatsapp = await ShowWhatsAppService(whatsappId) + // let whatsapp = await ShowWhatsAppService(whatsappId) + + const whatsapp: any = await Whatsapp.findByPk(whatsappId, { raw: true }) + + if (whatsapp) { + if (action === 'disconnected') { + logger.info(`Disconnected session: ${whatsapp.name}, reason: ${reason}`); + } + + const io = getIO(); + io.emit("whatsappSession", { + action: "update", + session: whatsapp + }); - if (action === 'disconnected') { - logger.info(`Disconnected session: ${whatsapp.name}, reason: ${reason}`); } - const io = getIO(); - io.emit("whatsappSession", { - action: "update", - session: whatsapp - }); - return res.status(200).json({ "message": "Ok" }); }; @@ -39,55 +45,37 @@ export const wbotMonitorQrcodeRemote = async (req: Request, res: Response): Prom console.log('-----------> whatsappId: ', req.body['whatsappId']) - let whatsapp = await ShowWhatsAppService(whatsappId) - - const io = getIO(); - io.emit("whatsappSession", { - action: "update", - session: whatsapp - }); + const whatsapp: any = await Whatsapp.findByPk(whatsappId, { raw: true }) + // let whatsapp = await ShowWhatsAppService(whatsappId) + if (whatsapp) { + const io = getIO(); + io.emit("whatsappSession", { + action: "update", + session: whatsapp + }); + } return res.status(200).json({ "message": "Ok" }); }; -// export const wbotMonitorNumberRead = async (req: Request, res: Response): Promise => { +export const wbotMonitorQrcodeNumberRead = async (req: Request, res: Response): Promise => { -// const { whatsappId } = req.body + const { number } = req.body -// console.log('-----------> whatsappId: ', req.body['whatsappId']) + console.log('-----------> number read: ', number) + + const io = getIO(); + io.emit("whatsappSession", { + action: "error", + msg: `Numero lido: ${number} \nEssa sessão de whatsapp foi desconectada porque o numero que esta descrito no nome dessa sessão não corresponde ao numero lido!` + }); -// let whatsapp = await ShowWhatsAppService(whatsappId) - -// const io = getIO(); -// io.emit("whatsappSession", { -// action: "update", -// session: whatsapp -// }); - - -// return res.status(200).json({ "message": "Ok" }); -// }; + return res.status(200).json({ "message": "Ok" }); +}; -// export const DeleteWhatsAppMessage = async (req: Request, res: Response): Promise => { - -// const { whatsappId } = req.body - -// console.log('-----------> whatsappId: ', req.body['whatsappId']) - -// let whatsapp = await ShowWhatsAppService(whatsappId) - -// const io = getIO(); -// io.emit("whatsappSession", { -// action: "update", -// session: whatsapp -// }); - - - -// return res.status(200).json({ "message": "Ok" }); -// }; \ No newline at end of file + \ No newline at end of file diff --git a/backend/src/controllers/WhatsAppSessionController.ts b/backend/src/controllers/WhatsAppSessionController.ts index af41482..52ed16b 100644 --- a/backend/src/controllers/WhatsAppSessionController.ts +++ b/backend/src/controllers/WhatsAppSessionController.ts @@ -15,6 +15,8 @@ import { setRestoreControll, getRestoreControll, shifRestoreControll } from "../ import autoRestore from "../helpers/AutoRestore"; import axios from "axios"; +import Whatsapp from "../models/Whatsapp"; +import endPointQuery from "../helpers/EndPointQuery"; // let lstRestore: any = [] @@ -68,7 +70,28 @@ const restart = async (req: Request, res: Response): Promise => { } - await autoRestore(whatsappId, 'human') + let whatsapp: any = await Whatsapp.findByPk(whatsappId) + + if (whatsapp) { + + await whatsapp.update({ status: 'OPENING' }); + + const io = getIO(); + io.emit("whatsappSession", { + action: "update", + session: whatsapp + }); + + whatsapp = JSON.parse(JSON.stringify(whatsapp)) + + console.log('whatsapp url: ', whatsapp.url) + + await endPointQuery(`${whatsapp.url}/api/restore`, {}) + } + + + + autoRestore(whatsappId) return res.status(200).json({ message: "Starting session." }); }; diff --git a/backend/src/helpers/AutoRestore.ts b/backend/src/helpers/AutoRestore.ts index b0707b0..1eaf583 100644 --- a/backend/src/helpers/AutoRestore.ts +++ b/backend/src/helpers/AutoRestore.ts @@ -14,9 +14,7 @@ import { number } from "yargs"; const autoRestore = async (whatsappId: string | number, started_action_by: string = '') => { - return - - const whatsapp = await ShowWhatsAppService(whatsappId); + /*const whatsapp = await ShowWhatsAppService(whatsappId); // console.log('>>>>>>>>>>> Whatsapp.id: ', whatsappId) @@ -42,7 +40,7 @@ const autoRestore = async (whatsappId: string | number, started_action_by: strin if (started_action_by.trim().length > 0) fs.writeFileSync(`${sourcePath}/${timestamp}_restore_${number}_triggered_by_${started_action_by}_id_${whatsappId}.txt`, `Whatsapp id: ${whatsapp.id} \nDate: ${dateToday.fullDate} ${dateToday.fullTime}`, (error: any) => { console.log(error) }); - } + }*/ setRestoreControll({ 'id': +whatsappId, 'disabled': true }) diff --git a/backend/src/routes/wbotMonitorRoutes.ts b/backend/src/routes/wbotMonitorRoutes.ts index fdce4d7..8e80888 100644 --- a/backend/src/routes/wbotMonitorRoutes.ts +++ b/backend/src/routes/wbotMonitorRoutes.ts @@ -7,6 +7,6 @@ const userRoutes = Router(); userRoutes.post("/whatsapp/connection/monitor", WbotMonitorController.wbotMonitorRemote); userRoutes.post("/whatsapp/connection/qrcode", WbotMonitorController.wbotMonitorQrcodeRemote); -// userRoutes.post("/whatsapp/delete/message", WbotMonitorController.DeleteWhatsAppMessage); +userRoutes.post("/whatsapp/connection/number", WbotMonitorController.wbotMonitorQrcodeNumberRead); export default userRoutes;