From 85b0627737d136f8e477ab0f57dc2d485f2bbbee Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Thu, 6 Jun 2024 18:30:45 -0300 Subject: [PATCH] adjustis in instance controller --- .../channels/whatsapp.baileys.service.ts | 11 +++-- src/api/services/monitor.service.ts | 45 +++++++++++-------- 2 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/api/services/channels/whatsapp.baileys.service.ts b/src/api/services/channels/whatsapp.baileys.service.ts index 40430198..9683bea3 100644 --- a/src/api/services/channels/whatsapp.baileys.service.ts +++ b/src/api/services/channels/whatsapp.baileys.service.ts @@ -185,8 +185,6 @@ export class BaileysStartupService extends ChannelStartupService { ) return; - await this.forceUpdateGroupMetadataCache(); - setInterval(async () => { await this.forceUpdateGroupMetadataCache(); }, 3600000); @@ -208,6 +206,12 @@ export class BaileysStartupService extends ChannelStartupService { await this.client?.logout('Log out instance: ' + this.instanceName); this.client?.ws?.close(); + + await this.prismaRepository.session.delete({ + where: { + sessionId: this.instanceId, + }, + }); } public async getProfileName() { @@ -730,6 +734,7 @@ export class BaileysStartupService extends ChannelStartupService { private readonly chatHandle = { 'chats.upsert': async (chats: Chat[]) => { + console.log('chats.upsert', chats); const existingChatIds = await this.prismaRepository.chat.findMany({ where: { instanceId: this.instanceId }, select: { remoteJid: true }, @@ -3091,7 +3096,7 @@ export class BaileysStartupService extends ChannelStartupService { public async fetchAllGroups(getParticipants: GetParticipant) { try { - const fetch = Object.values(await this.client.groupFetchAllParticipating()); + const fetch = Object.values(await this?.client?.groupFetchAllParticipating()); let groups = []; for (const group of fetch) { const picture = await this.profilePicture(group.id); diff --git a/src/api/services/monitor.service.ts b/src/api/services/monitor.service.ts index dcac763c..f5136377 100644 --- a/src/api/services/monitor.service.ts +++ b/src/api/services/monitor.service.ts @@ -195,12 +195,14 @@ export class WAMonitoringService { public async cleaningUp(instanceName: string) { if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) { - await this.prismaRepository.instance.update({ + const instance = await this.prismaRepository.instance.update({ where: { name: instanceName }, data: { connectionStatus: 'close' }, }); - await this.prismaRepository.session.deleteMany({ where: { sessionId: instanceName } }); + if (!instance) this.logger.error('Instance not found'); + + await this.prismaRepository.session.deleteMany({ where: { sessionId: instance.id } }); return; } @@ -239,26 +241,31 @@ export class WAMonitoringService { return; } + const instance = await this.prismaRepository.instance.findFirst({ + where: { name: instanceName }, + }); - await this.prismaRepository.session.deleteMany({ where: { sessionId: instanceName } }); + rmSync(join(INSTANCE_DIR, instance.id), { recursive: true, force: true }); - await this.prismaRepository.chat.deleteMany({ where: { instanceId: instanceName } }); - await this.prismaRepository.contact.deleteMany({ where: { instanceId: instanceName } }); - await this.prismaRepository.messageUpdate.deleteMany({ where: { instanceId: instanceName } }); - await this.prismaRepository.message.deleteMany({ where: { instanceId: instanceName } }); + await this.prismaRepository.session.deleteMany({ where: { sessionId: instance.id } }); - await this.prismaRepository.integration.deleteMany({ where: { instanceId: instanceName } }); - await this.prismaRepository.auth.deleteMany({ where: { instanceId: instanceName } }); - await this.prismaRepository.webhook.deleteMany({ where: { instanceId: instanceName } }); - await this.prismaRepository.chatwoot.deleteMany({ where: { instanceId: instanceName } }); - await this.prismaRepository.proxy.deleteMany({ where: { instanceId: instanceName } }); - await this.prismaRepository.rabbitmq.deleteMany({ where: { instanceId: instanceName } }); - await this.prismaRepository.sqs.deleteMany({ where: { instanceId: instanceName } }); - await this.prismaRepository.typebotSession.deleteMany({ where: { instanceId: instanceName } }); - await this.prismaRepository.typebot.deleteMany({ where: { instanceId: instanceName } }); - await this.prismaRepository.websocket.deleteMany({ where: { instanceId: instanceName } }); - await this.prismaRepository.setting.deleteMany({ where: { instanceId: instanceName } }); - await this.prismaRepository.label.deleteMany({ where: { instanceId: instanceName } }); + await this.prismaRepository.chat.deleteMany({ where: { instanceId: instance.id } }); + await this.prismaRepository.contact.deleteMany({ where: { instanceId: instance.id } }); + await this.prismaRepository.messageUpdate.deleteMany({ where: { instanceId: instance.id } }); + await this.prismaRepository.message.deleteMany({ where: { instanceId: instance.id } }); + + await this.prismaRepository.integration.deleteMany({ where: { instanceId: instance.id } }); + await this.prismaRepository.auth.deleteMany({ where: { instanceId: instance.id } }); + await this.prismaRepository.webhook.deleteMany({ where: { instanceId: instance.id } }); + await this.prismaRepository.chatwoot.deleteMany({ where: { instanceId: instance.id } }); + await this.prismaRepository.proxy.deleteMany({ where: { instanceId: instance.id } }); + await this.prismaRepository.rabbitmq.deleteMany({ where: { instanceId: instance.id } }); + await this.prismaRepository.sqs.deleteMany({ where: { instanceId: instance.id } }); + await this.prismaRepository.typebotSession.deleteMany({ where: { instanceId: instance.id } }); + await this.prismaRepository.typebot.deleteMany({ where: { instanceId: instance.id } }); + await this.prismaRepository.websocket.deleteMany({ where: { instanceId: instance.id } }); + await this.prismaRepository.setting.deleteMany({ where: { instanceId: instance.id } }); + await this.prismaRepository.label.deleteMany({ where: { instanceId: instance.id } }); await this.prismaRepository.instance.delete({ where: { name: instanceName } }); }