From 7e65cb1d197a80ed60e116f510e60464dc65c203 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Sun, 18 Feb 2024 08:11:24 -0300 Subject: [PATCH] fix: find intance by number --- .../controllers/instance.controller.ts | 6 +- src/whatsapp/repository/auth.repository.ts | 26 ++++- src/whatsapp/services/monitor.service.ts | 98 +++---------------- src/whatsapp/whatsapp.module.ts | 2 +- 4 files changed, 44 insertions(+), 88 deletions(-) diff --git a/src/whatsapp/controllers/instance.controller.ts b/src/whatsapp/controllers/instance.controller.ts index 196dad42..60e44281 100644 --- a/src/whatsapp/controllers/instance.controller.ts +++ b/src/whatsapp/controllers/instance.controller.ts @@ -642,13 +642,13 @@ export class InstanceController { }; } - public async fetchInstances({ instanceName, instanceId }: InstanceDto) { + public async fetchInstances({ instanceName, instanceId, number }: 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); + } else if (instanceId || number) { + return this.waMonitor.instanceInfoById(instanceId, number); } this.logger.verbose('requested fetchInstances (all instances)'); diff --git a/src/whatsapp/repository/auth.repository.ts b/src/whatsapp/repository/auth.repository.ts index fa520a16..1e4bbb81 100644 --- a/src/whatsapp/repository/auth.repository.ts +++ b/src/whatsapp/repository/auth.repository.ts @@ -5,10 +5,14 @@ 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 { AuthRaw, IAuthModel } from '../models'; +import { AuthRaw, IAuthModel, IntegrationModel } from '../models'; export class AuthRepository extends Repository { - constructor(private readonly authModel: IAuthModel, readonly configService: ConfigService) { + constructor( + private readonly authModel: IAuthModel, + private readonly integrationModel: IntegrationModel, + readonly configService: ConfigService, + ) { super(configService); this.auth = configService.get('AUTHENTICATION'); } @@ -110,4 +114,22 @@ export class AuthRepository extends Repository { return null; } } + + public async findInstanceNameByNumber(number: string): Promise { + try { + this.logger.verbose('finding auth by number'); + if (this.dbSettings.ENABLED) { + this.logger.verbose('finding auth in db'); + const instance = await this.integrationModel.findOne({ number }); + + const response = await this.authModel.findOne({ _id: instance._id }); + + return response._id; + } + + this.logger.verbose('finding auth in store is not supported'); + } catch (error) { + return null; + } + } } diff --git a/src/whatsapp/services/monitor.service.ts b/src/whatsapp/services/monitor.service.ts index bc983766..ca1a79df 100644 --- a/src/whatsapp/services/monitor.service.ts +++ b/src/whatsapp/services/monitor.service.ts @@ -107,7 +107,7 @@ export class WAMonitoringService { }; } - const findIntegration = await this.repository.integration.find(key); + const findIntegration = await this.waInstances[key].findIntegration(); const integration = { ...findIntegration, webhook_wa_business: `${urlServer}/webhook/whatsapp/${encodeURIComponent(key)}`, @@ -170,9 +170,21 @@ export class WAMonitoringService { return instances.find((i) => i.instance.instanceName === instanceName) ?? instances; } - public async instanceInfoById(instanceId?: string) { + public async instanceInfoById(instanceId?: string, number?: string) { this.logger.verbose('get instance info'); - const instanceName = await this.repository.auth.findInstanceNameById(instanceId); + let instanceName: string; + if (instanceId) { + instanceName = await this.repository.auth.findInstanceNameById(instanceId); + if (!instanceName) { + throw new NotFoundException(`Instance "${instanceId}" not found`); + } + } else if (number) { + instanceName = await this.repository.auth.findInstanceNameByNumber(number); + if (!instanceName) { + throw new NotFoundException(`Instance "${number}" not found`); + } + } + if (!instanceName) { throw new NotFoundException(`Instance "${instanceId}" not found`); } @@ -181,85 +193,7 @@ export class WAMonitoringService { 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)}`, - }; - } - - const findIntegration = await this.repository.integration.find(key); - const integration = { - ...findIntegration, - webhook_wa_business: `${urlServer}/webhook/whatsapp/${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; - - instanceData.instance['integration'] = integration; - } - - 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; - - instanceData.instance['integration'] = integration; - } - - instances.push(instanceData); - } - } - } - - this.logger.verbose('return instance info: ' + instances.length); - - return instances.find((i) => i.instance.instanceName === instanceName) ?? instances; + return this.instanceInfo(instanceName); } private delInstanceFiles() { diff --git a/src/whatsapp/whatsapp.module.ts b/src/whatsapp/whatsapp.module.ts index 3b195524..c3fe2665 100644 --- a/src/whatsapp/whatsapp.module.ts +++ b/src/whatsapp/whatsapp.module.ts @@ -83,7 +83,7 @@ const sqsRepository = new SqsRepository(SqsModel, configService); const integrationRepository = new IntegrationRepository(IntegrationModel, configService); const chatwootRepository = new ChatwootRepository(ChatwootModel, configService); const settingsRepository = new SettingsRepository(SettingsModel, configService); -const authRepository = new AuthRepository(AuthModel, configService); +const authRepository = new AuthRepository(AuthModel, IntegrationModel, configService); const labelRepository = new LabelRepository(LabelModel, configService); export const repository = new RepositoryBroker(