Skip to content

Commit 897f816

Browse files
committed
fix: Now when deleting the instance, the data referring to it in mongodb is also deleted
1 parent 796287a commit 897f816

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* Fix in update settings that needed to restart after updated
66
* Correction in the use of the api with mongodb
77
* Adjustments to search endpoint for contacts, chats, messages and Status messages
8+
* Now when deleting the instance, the data referring to it in mongodb is also deleted
89

910
# 1.3.1 (2023-07-20 07:48)
1011

src/whatsapp/services/monitor.service.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ import { Db } from 'mongodb';
1818
import { initInstance } from '../whatsapp.module';
1919
import { RedisCache } from '../../db/redis.client';
2020
import { 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

2232
export 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

Comments
 (0)