diff --git a/backend/src/helpers/RestoreMonit.ts b/backend/src/helpers/RestoreMonit.ts new file mode 100644 index 0000000..365702e --- /dev/null +++ b/backend/src/helpers/RestoreMonit.ts @@ -0,0 +1,86 @@ +import Whatsapp from "../models/Whatsapp" +import autoRestore from "./AutoRestore" +import { getRestoreControll } from "./RestoreControll" + +let lstAutoRestore: any = [] + +// Delete a directory and its children +export const restoreMonit = (whatsappId: string | number) => { + + try { + + let lstRestore: any = getRestoreControll() + + console.log('ENTROU NO AUTO RESTORE..................................') + + if (Object.keys(lstRestore.filter((e: any) => +e.id == +whatsappId)).length) { + + console.log('Restore executed by human whatasappId: ', whatsappId) + + } + else { + + lstAutoRestore.push({ 'whatsappId': +whatsappId }) + + setTimeout(async () => { + + lstRestore = getRestoreControll() + + let count = lstAutoRestore.length + + for (let i = 0; i < count; i++) { + + let autoR = lstAutoRestore.shift() + + console.log('----------------> autoR: ', autoR) + + if (autoR && autoR.whatsappId) { + + if (Object.keys(lstRestore.filter((e: any) => +e.id == +autoR.whatsappId)).length) { + + console.log(' EXECUTING RESTORING autoR: ', autoR) + + continue + } + + const _whatsapp = await Whatsapp.findOne({ where: { id: autoR.whatsappId } }); + + console.log('----------------> autoR exec after 180 seconds: ', autoR) + + let whatsappStatus = ["CONFLICT", + "DEPRECATED_VERSION", + "OPENING", + "PAIRING", + "PROXYBLOCK", + "SMB_TOS_BLOCK", + "TIMEOUT", + "TOS_BLOCK", + "UNLAUNCHED", + "UNPAIRED", + "UNPAIRED_IDLE"] + + if (_whatsapp?.status) { + + if (whatsappStatus.includes(_whatsapp.status)) { + + await autoRestore(autoR.whatsappId, 'auto_monit') + + } + + } + + } + + } + + console.log('888888888888888888 ENTROU NO SET TIMEOUT -----------> ') + + }, 180000); + + } + + } catch (error) { + console.log('There was an error on try execute AUTO-RESTORE: ', error) + } + +} \ No newline at end of file diff --git a/backend/src/services/WbotServices/StartWhatsAppSession.ts b/backend/src/services/WbotServices/StartWhatsAppSession.ts index d682494..0a281fb 100644 --- a/backend/src/services/WbotServices/StartWhatsAppSession.ts +++ b/backend/src/services/WbotServices/StartWhatsAppSession.ts @@ -19,16 +19,11 @@ import { splitDateTime } from "../../helpers/SplitDateTime"; import { format } from "date-fns"; import ptBR from 'date-fns/locale/pt-BR'; import path from "path"; -const fs = require('fs') - - - - -let lstAutoRestore: any = [] - +const fs = require('fs') + export const StartWhatsAppSession = async (whatsapp: Whatsapp, backupSession: boolean = false): Promise => { - await whatsapp.update({ status: "OPENING" }); + await whatsapp.update({ status: "OPENING" }); const io = getIO(); io.emit("whatsappSession", { diff --git a/backend/src/services/WbotServices/wbotMonitor.ts b/backend/src/services/WbotServices/wbotMonitor.ts index 1cbc227..c7216c6 100644 --- a/backend/src/services/WbotServices/wbotMonitor.ts +++ b/backend/src/services/WbotServices/wbotMonitor.ts @@ -24,6 +24,7 @@ import { splitDateTime } from "../../helpers/SplitDateTime"; import { format } from "date-fns"; import ptBR from 'date-fns/locale/pt-BR'; import path from "path"; +import { restoreMonit } from "../../helpers/RestoreMonit"; const fs = require('fs') @@ -44,82 +45,7 @@ const wbotMonitor = async ( wbot.on("change_state", async newState => { logger.info(`Monitor session: ${sessionName}, ${newState}`); - console.log('>>>>>>>>>>>>>> change_state wbotMonitor.ts MOBILE NUMBER: ', wbot.info["wid"]["user"]) - - - try { - - let lstRestore: any = getRestoreControll() - - if (Object.keys(lstRestore.filter((e: any) => +e.id == +whatsapp.id)).length) { - - console.log('Restore executed by human whatasappId: ', whatsapp.id) - - } - else { - - lstAutoRestore.push({ 'whatsappId': +whatsapp.id }) - - setTimeout(async () => { - - lstRestore = getRestoreControll() - - let count = lstAutoRestore.length - - for (let i = 0; i < count; i++) { - - let autoR = lstAutoRestore.shift() - - console.log('----------------> autoR: ', autoR) - - if (autoR && autoR.whatsappId) { - - if (Object.keys(lstRestore.filter((e: any) => +e.id == +autoR.whatsappId)).length) { - - console.log(' EXECUTING RESTORING autoR: ', autoR) - - continue - } - - const _whatsapp = await Whatsapp.findOne({ where: { id: autoR.whatsappId } }); - - console.log('----------------> autoR exec after 180 seconds: ', autoR) - - let whatsappStatus = ["CONFLICT", - "DEPRECATED_VERSION", - "OPENING", - "PAIRING", - "PROXYBLOCK", - "SMB_TOS_BLOCK", - "TIMEOUT", - "TOS_BLOCK", - "UNLAUNCHED", - "UNPAIRED", - "UNPAIRED_IDLE"] - - if (_whatsapp?.status) { - - if (whatsappStatus.includes(_whatsapp.status)) { - - await autoRestore(autoR.whatsappId, 'auto_monit') - - } - - } - - } - - } - - console.log('888888888888888888 ENTROU NO SET TIMEOUT -----------> ') - - }, 180000); - - } - - } catch (error) { - console.log('There was an error on try execute AUTO-RESTORE: ', error) - } + console.log('>>>>>>>>>>>>>> change_state wbotMonitor.ts MOBILE NUMBER: ', wbot.info["wid"]["user"]) try { await whatsapp.update({ status: newState }); @@ -128,11 +54,14 @@ const wbotMonitor = async ( Sentry.captureException(err); logger.error(err); } - + io.emit("whatsappSession", { action: "update", session: whatsapp }); + + restoreMonit(whatsapp.id) + }); wbot.on("change_battery", async batteryInfo => {