From 7d9dd64303ae42a10edd7ce8c1e3bf054ac8e7f1 Mon Sep 17 00:00:00 2001 From: Wender Teixeira Date: Mon, 4 Sep 2023 15:06:18 -0300 Subject: [PATCH] Update monitor.service.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Isso permite que as operações assíncronas sejam executadas em paralelo --- src/whatsapp/services/monitor.service.ts | 101 +++++++++-------------- 1 file changed, 41 insertions(+), 60 deletions(-) diff --git a/src/whatsapp/services/monitor.service.ts b/src/whatsapp/services/monitor.service.ts index 69aae5aa..e0bdfd73 100644 --- a/src/whatsapp/services/monitor.service.ts +++ b/src/whatsapp/services/monitor.service.ts @@ -74,81 +74,62 @@ export class WAMonitoringService { } } - public async instanceInfo(instanceName?: string) { + public async instanceInfo(instanceName?: string) { this.logger.verbose('get instance info'); - if (instanceName && !this.waInstances[instanceName]) { - throw new NotFoundException(`Instance "${instanceName}" not found`); - } - const instances: any[] = []; + const urlServer = this.configService.get('SERVER').URL; - for await (const [key, value] of Object.entries(this.waInstances)) { - if (value) { - this.logger.verbose('get instance info: ' + key); - let chatwoot: any; - - const urlServer = this.configService.get('SERVER').URL; - - const findChatwoot = await this.waInstances[key].findChatwoot(); - - if (findChatwoot && findChatwoot.enabled) { - chatwoot = { - ...findChatwoot, - webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`, + const instances: any[] = await Promise.all( + Object.entries(this.waInstances).map(async ([key, value]) => { + if (!value || !value.connectionStatus || value.connectionStatus.state !== 'open') { + return { + instance: { + instanceName: key, + status: value?.connectionStatus?.state || 'unknown', + }, }; } - if (value.connectionStatus.state === 'open') { - this.logger.verbose('instance: ' + key + ' - connectionStatus: open'); + this.logger.verbose('instance: ' + key + ' - connectionStatus: open'); - const instanceData = { - instance: { - instanceName: key, - owner: value.wuid, - profileName: (await value.getProfileName()) || 'not loaded', - profilePictureUrl: value.profilePictureUrl, - profileStatus: (await value.getProfileStatus()) || '', - status: value.connectionStatus.state, - }, - }; + const instanceData: any = { + instance: { + instanceName: key, + owner: value.wuid, + profileName: (await value.getProfileName()) || 'not loaded', + profilePictureUrl: value.profilePictureUrl, + profileStatus: (await value.getProfileStatus()) || '', + status: 'open', + }, + }; - if (this.configService.get('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) { - instanceData.instance['serverUrl'] = this.configService.get('SERVER').URL; + if (this.configService.get('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) { + instanceData.instance.serverUrl = urlServer; + instanceData.instance.apikey = (await this.repository.auth.find(key))?.apikey; - instanceData.instance['apikey'] = (await this.repository.auth.find(key))?.apikey; - - instanceData.instance['chatwoot'] = chatwoot; + const findChatwoot = await this.waInstances[key].findChatwoot(); + if (findChatwoot && findChatwoot.enabled) { + instanceData.instance.chatwoot = { + ...findChatwoot, + webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`, + }; } - - instances.push(instanceData); - } else { - this.logger.verbose('instance: ' + key + ' - connectionStatus: ' + value.connectionStatus.state); - - const instanceData = { - instance: { - instanceName: key, - status: value.connectionStatus.state, - }, - }; - - if (this.configService.get('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) { - instanceData.instance['serverUrl'] = this.configService.get('SERVER').URL; - - instanceData.instance['apikey'] = (await this.repository.auth.find(key))?.apikey; - - instanceData.instance['chatwoot'] = chatwoot; - } - - instances.push(instanceData); } - } - } + + return instanceData; + }), + ); this.logger.verbose('return instance info: ' + instances.length); - return instances.find((i) => i.instance.instanceName === instanceName) ?? instances; - } + if (instanceName) { + const instance = instances.find((i) => i.instance.instanceName === instanceName); + return instance || []; + } + return instances; + } + private delInstanceFiles() { this.logger.verbose('cron to delete instance files started'); setInterval(async () => {