From 2c6b6c7309dcc29062c46c74a4f02aec5915571e Mon Sep 17 00:00:00 2001 From: adriano Date: Fri, 23 Dec 2022 13:35:07 -0300 Subject: [PATCH] =?UTF-8?q?Altera=C3=A7=C3=A3o=20no=20cache?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/helpers/AutoRestore.ts | 2 +- backend/src/helpers/CreationTime.ts | 24 +++++++++++++++ backend/src/helpers/RestoreControll.ts | 2 +- backend/src/helpers/WhatsCache.ts | 1 + .../WbotServices/StartWhatsAppSession.ts | 29 +++++++++++-------- 5 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 backend/src/helpers/CreationTime.ts diff --git a/backend/src/helpers/AutoRestore.ts b/backend/src/helpers/AutoRestore.ts index 595c3ea..e4abf5c 100644 --- a/backend/src/helpers/AutoRestore.ts +++ b/backend/src/helpers/AutoRestore.ts @@ -18,7 +18,7 @@ const autoRestore = async (whatsappId: string | number, started_action_by: strin // console.log('>>>>>>>>>>> Whatsapp.id: ', whatsappId) - restartWhatsSession(whatsapp, true) + await restartWhatsSession(whatsapp, true) const sourcePath = path.join(__dirname, `../../.wwebjs_auth/sessions/log`) diff --git a/backend/src/helpers/CreationTime.ts b/backend/src/helpers/CreationTime.ts new file mode 100644 index 0000000..7207413 --- /dev/null +++ b/backend/src/helpers/CreationTime.ts @@ -0,0 +1,24 @@ +// require fs module +const fs = require("fs"); + + +// Delete a directory and its children +export const creationTime = (sourcePath: string) => { + + if (fs.existsSync(sourcePath)) { + + try { + + const { birthtime } = fs.statSync(sourcePath) + + return birthtime + + } catch (error) { + console.log(`There was an error on trying get de file/directory creation info from ${sourcePath}: `, error) + } + + } + + return + +} \ No newline at end of file diff --git a/backend/src/helpers/RestoreControll.ts b/backend/src/helpers/RestoreControll.ts index fb6e456..8f4908c 100644 --- a/backend/src/helpers/RestoreControll.ts +++ b/backend/src/helpers/RestoreControll.ts @@ -142,7 +142,7 @@ export const getRestoreControll = () => { export const _restore = async (whatsapp: Whatsapp, msg_file_title: string) => { - if (whatsapp.status != 'RESTORING') { + if (whatsapp.status != 'RESTORING' && whatsapp.status != 'qrcode') { console.log('THE WHATSAAP ID: ', whatsapp.id, ' WILL BE RESTORED SOON!') diff --git a/backend/src/helpers/WhatsCache.ts b/backend/src/helpers/WhatsCache.ts index 4a6559b..c972f2a 100644 --- a/backend/src/helpers/WhatsCache.ts +++ b/backend/src/helpers/WhatsCache.ts @@ -117,6 +117,7 @@ async function searchWhatsappCache(id: string, status: string) { const number_cache: any = await redis.hgetall(`whatsapp:${id}`) if(Object.entries(number_cache).length == 0){ + redis.quit() return [] } diff --git a/backend/src/services/WbotServices/StartWhatsAppSession.ts b/backend/src/services/WbotServices/StartWhatsAppSession.ts index 68bdb3b..60f9162 100644 --- a/backend/src/services/WbotServices/StartWhatsAppSession.ts +++ b/backend/src/services/WbotServices/StartWhatsAppSession.ts @@ -10,19 +10,24 @@ import { getRestoreControll, setRestoreControll, shifRestoreControll } from "../ import ShowWhatsAppService from "../WhatsappService/ShowWhatsAppService"; import { restartWhatsSession } from "../../helpers/RestartWhatsSession"; +import dir from 'path'; + import autoRestore from "../../helpers/AutoRestore"; +import { creationTime } from "../../helpers/CreationTime"; +import { splitDateTime } from "../../helpers/SplitDateTime"; + +import { format } from "date-fns"; +import ptBR from 'date-fns/locale/pt-BR'; let lstAutoRestore: any = [] export const StartWhatsAppSession = async (whatsapp: Whatsapp, backupSession: boolean = false): Promise => { - await whatsapp.update({ status: "OPENING" }); + await whatsapp.update({ status: "OPENING" }); await insertOrUpeateWhatsCache(`whatsapp:${whatsapp.id}`, { status: "OPENING", }) - - try { let lstRestore: any = getRestoreControll() @@ -34,7 +39,7 @@ export const StartWhatsAppSession = async (whatsapp: Whatsapp, backupSession: bo } else { - lstAutoRestore.push({ 'whatsappId': +whatsapp.id }) + lstAutoRestore.push({ 'whatsappId': +whatsapp.id }) setTimeout(async () => { @@ -46,22 +51,22 @@ export const StartWhatsAppSession = async (whatsapp: Whatsapp, backupSession: bo let autoR = lstAutoRestore.shift() - console.log('----------------> autoR: ', autoR) + console.log('----------------> autoR: ', autoR) - if (autoR && autoR.whatsappId) { + 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 120 seconds: ', autoR) + console.log('----------------> autoR exec after 120 seconds: ', autoR) - let whatsappStatus = ["CONFLICT", + let whatsappStatus = ["CONFLICT", "DEPRECATED_VERSION", "OPENING", "PROXYBLOCK", @@ -74,13 +79,13 @@ export const StartWhatsAppSession = async (whatsapp: Whatsapp, backupSession: bo if (_whatsapp?.status) { - if (whatsappStatus.includes(_whatsapp.status)) { + if (whatsappStatus.includes(_whatsapp.status)) { - await autoRestore(autoR.whatsappId, 'auto_monit') + await autoRestore(autoR.whatsappId, 'auto_monit') } - } + } }