@@ -2,22 +2,19 @@ import { execSync } from 'child_process';
22import EventEmitter2 from 'eventemitter2' ;
33import { opendirSync , readdirSync , rmSync } from 'fs' ;
44import { Db } from 'mongodb' ;
5+ import { Collection } from 'mongoose' ;
56import { join } from 'path' ;
67
78import { Auth , ConfigService , Database , DelInstance , HttpServer , Redis } from '../../config/env.config' ;
89import { Logger } from '../../config/logger.config' ;
910import { INSTANCE_DIR , STORE_DIR } from '../../config/path.config' ;
1011import { NotFoundException } from '../../exceptions' ;
11- import { dbserver } from '../../libs/db.connect' ;
1212import { RedisCache } from '../../libs/redis.client' ;
1313import {
1414 AuthModel ,
1515 ChamaaiModel ,
16- // ChatModel,
1716 ChatwootModel ,
18- // ContactModel,
19- // MessageModel,
20- // MessageUpModel,
17+ ContactModel ,
2118 ProxyModel ,
2219 RabbitmqModel ,
2320 SettingsModel ,
@@ -41,7 +38,6 @@ export class WAMonitoringService {
4138
4239 this . removeInstance ( ) ;
4340 this . noConnection ( ) ;
44- // this.delInstanceFiles();
4541
4642 Object . assign ( this . db , configService . get < Database > ( 'DATABASE' ) ) ;
4743 Object . assign ( this . redis , configService . get < Redis > ( 'REDIS' ) ) ;
@@ -56,8 +52,6 @@ export class WAMonitoringService {
5652
5753 private dbInstance : Db ;
5854
59- private dbStore = dbserver ;
60-
6155 private readonly logger = new Logger ( WAMonitoringService . name ) ;
6256 public readonly waInstances : Record < string , WAStartupService > = { } ;
6357
@@ -326,11 +320,6 @@ export class WAMonitoringService {
326320
327321 this . logger . verbose ( 'cleaning store database instance: ' + instanceName ) ;
328322
329- // await ChatModel.deleteMany({ owner: instanceName });
330- // await ContactModel.deleteMany({ owner: instanceName });
331- // await MessageUpModel.deleteMany({ owner: instanceName });
332- // await MessageModel.deleteMany({ owner: instanceName });
333-
334323 await AuthModel . deleteMany ( { _id : instanceName } ) ;
335324 await WebhookModel . deleteMany ( { _id : instanceName } ) ;
336325 await ChatwootModel . deleteMany ( { _id : instanceName } ) ;
@@ -340,6 +329,7 @@ export class WAMonitoringService {
340329 await TypebotModel . deleteMany ( { _id : instanceName } ) ;
341330 await WebsocketModel . deleteMany ( { _id : instanceName } ) ;
342331 await SettingsModel . deleteMany ( { _id : instanceName } ) ;
332+ await ContactModel . deleteMany ( { owner : instanceName } ) ;
343333
344334 return ;
345335 }
@@ -393,7 +383,7 @@ export class WAMonitoringService {
393383 this . logger . verbose ( 'Database enabled' ) ;
394384 await this . repository . dbServer . connect ( ) ;
395385 const collections : any [ ] = await this . dbInstance . collections ( ) ;
396-
386+ await this . deleteTempInstances ( collections ) ;
397387 if ( collections . length > 0 ) {
398388 this . logger . verbose ( 'Reading collections and setting instances' ) ;
399389 await Promise . all ( collections . map ( ( coll ) => this . setInstance ( coll . namespace . replace ( / ^ [ \w - ] + \. / , '' ) ) ) ) ;
@@ -482,4 +472,22 @@ export class WAMonitoringService {
482472 }
483473 } ) ;
484474 }
475+
476+ private async deleteTempInstances ( collections : Collection < Document > [ ] ) {
477+ this . logger . verbose ( 'Cleaning up temp instances' ) ;
478+ const auths = await this . repository . auth . list ( ) ;
479+ if ( auths . length === 0 ) {
480+ this . logger . verbose ( 'No temp instances found' ) ;
481+ return ;
482+ }
483+ let tempInstances = 0 ;
484+ auths . forEach ( ( auth ) => {
485+ if ( collections . find ( ( coll ) => coll . namespace . replace ( / ^ [ \w - ] + \. / , '' ) === auth . _id ) ) {
486+ return ;
487+ }
488+ tempInstances ++ ;
489+ this . eventEmitter . emit ( 'remove.instance' , auth . _id , 'inner' ) ;
490+ } ) ;
491+ this . logger . verbose ( 'Temp instances removed: ' + tempInstances ) ;
492+ }
485493}
0 commit comments