mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-25 01:48:39 -06:00
fix: find intance by number
This commit is contained in:
parent
a931ee7afb
commit
7e65cb1d19
@ -642,13 +642,13 @@ export class InstanceController {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public async fetchInstances({ instanceName, instanceId }: InstanceDto) {
|
public async fetchInstances({ instanceName, instanceId, number }: InstanceDto) {
|
||||||
if (instanceName) {
|
if (instanceName) {
|
||||||
this.logger.verbose('requested fetchInstances from ' + instanceName + ' instance');
|
this.logger.verbose('requested fetchInstances from ' + instanceName + ' instance');
|
||||||
this.logger.verbose('instanceName: ' + instanceName);
|
this.logger.verbose('instanceName: ' + instanceName);
|
||||||
return this.waMonitor.instanceInfo(instanceName);
|
return this.waMonitor.instanceInfo(instanceName);
|
||||||
} else if (instanceId) {
|
} else if (instanceId || number) {
|
||||||
return this.waMonitor.instanceInfoById(instanceId);
|
return this.waMonitor.instanceInfoById(instanceId, number);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.logger.verbose('requested fetchInstances (all instances)');
|
this.logger.verbose('requested fetchInstances (all instances)');
|
||||||
|
@ -5,10 +5,14 @@ import { Auth, ConfigService } from '../../config/env.config';
|
|||||||
import { Logger } from '../../config/logger.config';
|
import { Logger } from '../../config/logger.config';
|
||||||
import { AUTH_DIR } from '../../config/path.config';
|
import { AUTH_DIR } from '../../config/path.config';
|
||||||
import { IInsert, Repository } from '../abstract/abstract.repository';
|
import { IInsert, Repository } from '../abstract/abstract.repository';
|
||||||
import { AuthRaw, IAuthModel } from '../models';
|
import { AuthRaw, IAuthModel, IntegrationModel } from '../models';
|
||||||
|
|
||||||
export class AuthRepository extends Repository {
|
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);
|
super(configService);
|
||||||
this.auth = configService.get<Auth>('AUTHENTICATION');
|
this.auth = configService.get<Auth>('AUTHENTICATION');
|
||||||
}
|
}
|
||||||
@ -110,4 +114,22 @@ export class AuthRepository extends Repository {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async findInstanceNameByNumber(number: string): Promise<string | null> {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -107,7 +107,7 @@ export class WAMonitoringService {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
const findIntegration = await this.repository.integration.find(key);
|
const findIntegration = await this.waInstances[key].findIntegration();
|
||||||
const integration = {
|
const integration = {
|
||||||
...findIntegration,
|
...findIntegration,
|
||||||
webhook_wa_business: `${urlServer}/webhook/whatsapp/${encodeURIComponent(key)}`,
|
webhook_wa_business: `${urlServer}/webhook/whatsapp/${encodeURIComponent(key)}`,
|
||||||
@ -170,9 +170,21 @@ export class WAMonitoringService {
|
|||||||
return instances.find((i) => i.instance.instanceName === instanceName) ?? instances;
|
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');
|
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) {
|
if (!instanceName) {
|
||||||
throw new NotFoundException(`Instance "${instanceId}" not found`);
|
throw new NotFoundException(`Instance "${instanceId}" not found`);
|
||||||
}
|
}
|
||||||
@ -181,85 +193,7 @@ export class WAMonitoringService {
|
|||||||
throw new NotFoundException(`Instance "${instanceName}" not found`);
|
throw new NotFoundException(`Instance "${instanceName}" not found`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const instances: any[] = [];
|
return this.instanceInfo(instanceName);
|
||||||
|
|
||||||
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<HttpServer>('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<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
|
|
||||||
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('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<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES) {
|
|
||||||
instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private delInstanceFiles() {
|
private delInstanceFiles() {
|
||||||
|
@ -83,7 +83,7 @@ const sqsRepository = new SqsRepository(SqsModel, configService);
|
|||||||
const integrationRepository = new IntegrationRepository(IntegrationModel, configService);
|
const integrationRepository = new IntegrationRepository(IntegrationModel, configService);
|
||||||
const chatwootRepository = new ChatwootRepository(ChatwootModel, configService);
|
const chatwootRepository = new ChatwootRepository(ChatwootModel, configService);
|
||||||
const settingsRepository = new SettingsRepository(SettingsModel, 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);
|
const labelRepository = new LabelRepository(LabelModel, configService);
|
||||||
|
|
||||||
export const repository = new RepositoryBroker(
|
export const repository = new RepositoryBroker(
|
||||||
|
Loading…
Reference in New Issue
Block a user