diff --git a/src/whatsapp/controllers/instance.controller.ts b/src/whatsapp/controllers/instance.controller.ts index 275a1554..a4517955 100644 --- a/src/whatsapp/controllers/instance.controller.ts +++ b/src/whatsapp/controllers/instance.controller.ts @@ -591,11 +591,13 @@ export class InstanceController { }; } - public async fetchInstances({ instanceName }: InstanceDto) { + public async fetchInstances({ instanceName, instanceId }: InstanceDto) { if (instanceName) { this.logger.verbose('requested fetchInstances from ' + instanceName + ' instance'); this.logger.verbose('instanceName: ' + instanceName); return this.waMonitor.instanceInfo(instanceName); + } else if (instanceId) { + return this.waMonitor.instanceInfoById(instanceId); } this.logger.verbose('requested fetchInstances (all instances)'); diff --git a/src/whatsapp/repository/auth.repository.ts b/src/whatsapp/repository/auth.repository.ts index 5720cccb..47fc579e 100644 --- a/src/whatsapp/repository/auth.repository.ts +++ b/src/whatsapp/repository/auth.repository.ts @@ -5,6 +5,7 @@ import { Auth, ConfigService } from '../../config/env.config'; import { Logger } from '../../config/logger.config'; import { AUTH_DIR } from '../../config/path.config'; import { IInsert, Repository } from '../abstract/abstract.repository'; +import { InstanceDto } from '../dto/instance.dto'; import { AuthRaw, IAuthModel } from '../models'; export class AuthRepository extends Repository { @@ -63,4 +64,20 @@ export class AuthRepository extends Repository { return {}; } } + + public async findInstanceNameById(instanceId: string): Promise { + try { + this.logger.verbose('finding auth by instanceId'); + if (this.dbSettings.ENABLED) { + this.logger.verbose('finding auth in db'); + const response = await this.authModel.findOne({ instanceId }); + + return response._id; + } + + this.logger.verbose('finding auth in store is not supported'); + } catch (error) { + return ''; + } + } } diff --git a/src/whatsapp/services/monitor.service.ts b/src/whatsapp/services/monitor.service.ts index 54193664..78c83c85 100644 --- a/src/whatsapp/services/monitor.service.ts +++ b/src/whatsapp/services/monitor.service.ts @@ -159,6 +159,84 @@ export class WAMonitoringService { return instances.find((i) => i.instance.instanceName === instanceName) ?? instances; } + public async instanceInfoById(instanceId?: string) { + this.logger.verbose('get instance info'); + const instanceName = await this.repository.auth.findInstanceNameById(instanceId); + if (instanceName && !this.waInstances[instanceName]) { + throw new NotFoundException(`Instance "${instanceName}" not found`); + } + + const instances: any[] = []; + + 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)}`, + }; + } + + if (value.connectionStatus.state === 'open') { + this.logger.verbose('instance: ' + key + ' - connectionStatus: open'); + + const instanceData = { + instance: { + instanceName: key, + instanceId: (await this.repository.auth.find(key))?.instanceId, + owner: value.wuid, + profileName: (await value.getProfileName()) || 'not loaded', + profilePictureUrl: value.profilePictureUrl, + profileStatus: (await value.getProfileStatus()) || '', + 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); + } else { + this.logger.verbose('instance: ' + key + ' - connectionStatus: ' + value.connectionStatus.state); + + const instanceData = { + instance: { + instanceName: key, + instanceId: (await this.repository.auth.find(key))?.instanceId, + 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); + } + } + } + + this.logger.verbose('return instance info: ' + instances.length); + + return instances.find((i) => i.instance.instanceName === instanceName) ?? instances; + } + private delInstanceFiles() { this.logger.verbose('cron to delete instance files started'); setInterval(async () => {