Criação do restoreMonit

pull/21/head
adriano 2023-01-04 12:17:59 -03:00
parent 10d15930da
commit 72087f8adb
3 changed files with 95 additions and 85 deletions

View File

@ -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)
}
}

View File

@ -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<void> => {
await whatsapp.update({ status: "OPENING" });
await whatsapp.update({ status: "OPENING" });
const io = getIO();
io.emit("whatsappSession", {

View File

@ -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 => {