From 897f8164b90af48202e4b9d696429afef2855a97 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Fri, 21 Jul 2023 12:13:03 -0300 Subject: [PATCH] fix: Now when deleting the instance, the data referring to it in mongodb is also deleted --- CHANGELOG.md | 1 + src/whatsapp/services/monitor.service.ts | 31 +++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 875d7ac4..2311edac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Fix in update settings that needed to restart after updated * Correction in the use of the api with mongodb * Adjustments to search endpoint for contacts, chats, messages and Status messages +* Now when deleting the instance, the data referring to it in mongodb is also deleted # 1.3.1 (2023-07-20 07:48) diff --git a/src/whatsapp/services/monitor.service.ts b/src/whatsapp/services/monitor.service.ts index 8fdac88a..6aa43b70 100644 --- a/src/whatsapp/services/monitor.service.ts +++ b/src/whatsapp/services/monitor.service.ts @@ -18,6 +18,16 @@ import { Db } from 'mongodb'; import { initInstance } from '../whatsapp.module'; import { RedisCache } from '../../db/redis.client'; import { execSync } from 'child_process'; +import { dbserver } from '../../db/db.connect'; +import mongoose from 'mongoose'; +import { + AuthModel, + ChatwootModel, + ContactModel, + MessageModel, + MessageUpModel, + WebhookModel, +} from '../models'; export class WAMonitoringService { constructor( @@ -45,6 +55,8 @@ export class WAMonitoringService { private dbInstance: Db; + private dbStore = dbserver; + private readonly logger = new Logger(WAMonitoringService.name); public readonly waInstances: Record = {}; @@ -218,11 +230,8 @@ export class WAMonitoringService { } public async cleaningStoreFiles(instanceName: string) { - this.logger.verbose('cleaning store files instance: ' + instanceName); - if (!this.db.ENABLED) { - const instance = this.waInstances[instanceName]; - + this.logger.verbose('cleaning store files instance: ' + instanceName); rmSync(join(INSTANCE_DIR, instanceName), { recursive: true, force: true }); execSync(`rm -rf ${join(STORE_DIR, 'chats', instanceName)}`); @@ -233,7 +242,21 @@ export class WAMonitoringService { execSync(`rm -rf ${join(STORE_DIR, 'auth', 'apikey', instanceName + '.json')}`); execSync(`rm -rf ${join(STORE_DIR, 'webhook', instanceName + '.json')}`); execSync(`rm -rf ${join(STORE_DIR, 'chatwoot', instanceName + '*')}`); + + return; } + + this.logger.verbose('cleaning store database instance: ' + instanceName); + + await AuthModel.deleteMany({ owner: instanceName }); + await ContactModel.deleteMany({ owner: instanceName }); + await MessageModel.deleteMany({ owner: instanceName }); + await MessageUpModel.deleteMany({ owner: instanceName }); + await AuthModel.deleteMany({ _id: instanceName }); + await WebhookModel.deleteMany({ _id: instanceName }); + await ChatwootModel.deleteMany({ _id: instanceName }); + + return; } public async loadInstance() {