From ecd4b913b6ef5550b10a206d9fe69535492f032b Mon Sep 17 00:00:00 2001 From: Rafael Santana Date: Thu, 17 Oct 2024 16:42:52 -0500 Subject: [PATCH] feat: add support for fetching multiple instances by key This commit adds a new feature to fetch instances by key in the InstanceController. If the provided key does not match the environment key, the controller will search for instances with the matching token. If instances are found, the names are extracted and passed to the waMonitor to retrieve instance information. Also, update the waMonitor's instanceInfo method to accept an array of instance names instead of a single name. Fixes #990 --- src/api/controllers/instance.controller.ts | 18 +++++++--------- .../whatsapp/whatsapp.baileys.service.ts | 2 +- src/api/services/monitor.service.ts | 21 ++++++++++++++----- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/api/controllers/instance.controller.ts b/src/api/controllers/instance.controller.ts index b04edddd..7a4e2cb4 100644 --- a/src/api/controllers/instance.controller.ts +++ b/src/api/controllers/instance.controller.ts @@ -360,27 +360,25 @@ export class InstanceController { public async fetchInstances({ instanceName, instanceId, number }: InstanceDto, key: string) { const env = this.configService.get('AUTHENTICATION').API_KEY; - let name = instanceName; - // let arrayReturn = false; - if (env.KEY !== key) { - const instanceByKey = await this.prismaRepository.instance.findMany({ + const instancesByKey = await this.prismaRepository.instance.findMany({ where: { token: key, + name: instanceName || undefined, + id: instanceId || undefined, }, }); - if (instanceByKey) { - name = instanceByKey[0].name; - // arrayReturn = true; + if (instancesByKey.length > 0) { + const names = instancesByKey.map((instance) => instance.name); + + return this.waMonitor.instanceInfo(names); } else { throw new UnauthorizedException(); } } - if (name) { - return this.waMonitor.instanceInfo(name); - } else if (instanceId || number) { + if (instanceId || number) { return this.waMonitor.instanceInfoById(instanceId, number); } diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 2053b7a5..fc537a13 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -1699,7 +1699,7 @@ export class BaileysStartupService extends ChannelStartupService { website: business?.website?.shift(), }; } else { - const info: Instance = await waMonitor.instanceInfo(instanceName); + const info: Instance = await waMonitor.instanceInfo([instanceName]); const business = await this.fetchBusinessProfile(jid); return { diff --git a/src/api/services/monitor.service.ts b/src/api/services/monitor.service.ts index 0c3d8d50..cac8a45a 100644 --- a/src/api/services/monitor.service.ts +++ b/src/api/services/monitor.service.ts @@ -59,14 +59,25 @@ export class WAMonitoringService { } } - public async instanceInfo(instanceName?: string): Promise { - if (instanceName && !this.waInstances[instanceName]) { - throw new NotFoundException(`Instance "${instanceName}" not found`); + public async instanceInfo(instanceNames?: string[]): Promise { + 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`, + ); } const clientName = this.configService.get('DATABASE').CONNECTION.CLIENT_NAME; - const where = instanceName ? { name: instanceName, clientName } : { clientName }; + const where = instanceNames + ? { + name: { + in: instanceNames, + }, + clientName, + } + : { clientName }; const instances = await this.prismaRepository.instance.findMany({ where, @@ -112,7 +123,7 @@ export class WAMonitoringService { throw new NotFoundException(`Instance "${instanceName}" not found`); } - return this.instanceInfo(instanceName); + return this.instanceInfo([instanceName]); } public async cleaningUp(instanceName: string) {