From 23640a71b89ed5cca8461c058e420fb35815d9b6 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Tue, 29 Oct 2024 07:43:48 -0300 Subject: [PATCH] fix: fetch instances --- src/api/controllers/instance.controller.ts | 4 ++- .../whatsapp/whatsapp.baileys.service.ts | 3 +- src/api/services/monitor.service.ts | 28 +++++++++++-------- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/api/controllers/instance.controller.ts b/src/api/controllers/instance.controller.ts index a61fbe35..f68379e8 100644 --- a/src/api/controllers/instance.controller.ts +++ b/src/api/controllers/instance.controller.ts @@ -382,7 +382,9 @@ export class InstanceController { return this.waMonitor.instanceInfoById(instanceId, number); } - return this.waMonitor.instanceInfo([instanceName]); + const instanceNames = instanceName ? [instanceName] : null; + + return this.waMonitor.instanceInfo(instanceNames); } public async setPresence({ instanceName }: InstanceDto, data: SetPresenceDto) { diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 8a9b03df..3aac7a61 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -1762,7 +1762,8 @@ export class BaileysStartupService extends ChannelStartupService { website: business?.website?.shift(), }; } else { - const info: Instance = await waMonitor.instanceInfo([instanceName]); + const instanceNames = instanceName ? [instanceName] : null; + const info: Instance = await waMonitor.instanceInfo(instanceNames); const business = await this.fetchBusinessProfile(jid); return { diff --git a/src/api/services/monitor.service.ts b/src/api/services/monitor.service.ts index cac8a45a..c69e4fca 100644 --- a/src/api/services/monitor.service.ts +++ b/src/api/services/monitor.service.ts @@ -60,23 +60,25 @@ export class WAMonitoringService { } public async instanceInfo(instanceNames?: string[]): Promise { - const inexistentInstances = instanceNames ? instanceNames.filter((instance) => !this.waInstances[instance]) : []; + if (instanceNames && instanceNames.length > 0) { + const inexistentInstances = instanceNames ? instanceNames.filter((instance) => !this.waInstances[instance]) : []; - if (inexistentInstances.length > 0) { - throw new NotFoundException( - `Instance${inexistentInstances.length > 1 ? 's' : ''} "${inexistentInstances.join(', ')}" not found`, - ); + if (inexistentInstances.length > 0) { + throw new NotFoundException( + `Instance${inexistentInstances.length > 1 ? 's' : ''} "${inexistentInstances.join(', ')}" not found`, + ); + } } const clientName = this.configService.get('DATABASE').CONNECTION.CLIENT_NAME; - const where = instanceNames + const where = instanceNames && instanceNames.length > 0 ? { - name: { - in: instanceNames, - }, - clientName, - } + name: { + in: instanceNames, + }, + clientName, + } : { clientName }; const instances = await this.prismaRepository.instance.findMany({ @@ -123,7 +125,9 @@ export class WAMonitoringService { throw new NotFoundException(`Instance "${instanceName}" not found`); } - return this.instanceInfo([instanceName]); + const instanceNames = instanceName ? [instanceName] : null; + + return this.instanceInfo(instanceNames); } public async cleaningUp(instanceName: string) {