@@ -18,6 +18,16 @@ import { Db } from 'mongodb';
1818import { initInstance } from '../whatsapp.module' ;
1919import { RedisCache } from '../../db/redis.client' ;
2020import { execSync } from 'child_process' ;
21+ import { dbserver } from '../../db/db.connect' ;
22+ import mongoose from 'mongoose' ;
23+ import {
24+ AuthModel ,
25+ ChatwootModel ,
26+ ContactModel ,
27+ MessageModel ,
28+ MessageUpModel ,
29+ WebhookModel ,
30+ } from '../models' ;
2131
2232export class WAMonitoringService {
2333 constructor (
@@ -45,6 +55,8 @@ export class WAMonitoringService {
4555
4656 private dbInstance : Db ;
4757
58+ private dbStore = dbserver ;
59+
4860 private readonly logger = new Logger ( WAMonitoringService . name ) ;
4961 public readonly waInstances : Record < string , WAStartupService > = { } ;
5062
@@ -218,11 +230,8 @@ export class WAMonitoringService {
218230 }
219231
220232 public async cleaningStoreFiles ( instanceName : string ) {
221- this . logger . verbose ( 'cleaning store files instance: ' + instanceName ) ;
222-
223233 if ( ! this . db . ENABLED ) {
224- const instance = this . waInstances [ instanceName ] ;
225-
234+ this . logger . verbose ( 'cleaning store files instance: ' + instanceName ) ;
226235 rmSync ( join ( INSTANCE_DIR , instanceName ) , { recursive : true , force : true } ) ;
227236
228237 execSync ( `rm -rf ${ join ( STORE_DIR , 'chats' , instanceName ) } ` ) ;
@@ -233,7 +242,21 @@ export class WAMonitoringService {
233242 execSync ( `rm -rf ${ join ( STORE_DIR , 'auth' , 'apikey' , instanceName + '.json' ) } ` ) ;
234243 execSync ( `rm -rf ${ join ( STORE_DIR , 'webhook' , instanceName + '.json' ) } ` ) ;
235244 execSync ( `rm -rf ${ join ( STORE_DIR , 'chatwoot' , instanceName + '*' ) } ` ) ;
245+
246+ return ;
236247 }
248+
249+ this . logger . verbose ( 'cleaning store database instance: ' + instanceName ) ;
250+
251+ await AuthModel . deleteMany ( { owner : instanceName } ) ;
252+ await ContactModel . deleteMany ( { owner : instanceName } ) ;
253+ await MessageModel . deleteMany ( { owner : instanceName } ) ;
254+ await MessageUpModel . deleteMany ( { owner : instanceName } ) ;
255+ await AuthModel . deleteMany ( { _id : instanceName } ) ;
256+ await WebhookModel . deleteMany ( { _id : instanceName } ) ;
257+ await ChatwootModel . deleteMany ( { _id : instanceName } ) ;
258+
259+ return ;
237260 }
238261
239262 public async loadInstance ( ) {
0 commit comments