From 35787bf11b5e953caf11ffb096787ecb1d41caf1 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Fri, 10 Jan 2025 18:41:27 -0300 Subject: [PATCH] fix: fetch instances - Modified instanceInfo method to accept an optional array of instance names, improving flexibility in instance retrieval. - Updated related controller and service methods to utilize the new instanceInfo signature, ensuring consistent handling of instance names. - Enhanced error handling for non-existent instances when provided with specific names. --- src/api/controllers/instance.controller.ts | 3 +- .../whatsapp/whatsapp.baileys.service.ts | 3 +- src/api/services/monitor.service.ts | 32 +++++++++++-------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/api/controllers/instance.controller.ts b/src/api/controllers/instance.controller.ts index 3fa87c5d..2fd2face 100644 --- a/src/api/controllers/instance.controller.ts +++ b/src/api/controllers/instance.controller.ts @@ -275,7 +275,8 @@ export class InstanceController { return this.waMonitor.instanceInfoById(instanceId, number); } - return this.waMonitor.instanceInfo(); + 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 b917d266..d58f29cd 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -1410,7 +1410,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 13bc8fb3..3b7d4a0b 100644 --- a/src/api/services/monitor.service.ts +++ b/src/api/services/monitor.service.ts @@ -58,24 +58,27 @@ 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 - ? { - name: { - in: instanceNames, - }, - clientName, - } - : { clientName }; + const where = + instanceNames && instanceNames.length > 0 + ? { + name: { + in: instanceNames, + }, + clientName, + } + : { clientName }; const instances = await this.prismaRepository.instance.findMany({ where, @@ -121,7 +124,8 @@ 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) {