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
This commit is contained in:
Rafael Santana 2024-10-17 16:42:52 -05:00
parent ec2b7f94f8
commit ecd4b913b6
3 changed files with 25 additions and 16 deletions

View File

@ -360,27 +360,25 @@ export class InstanceController {
public async fetchInstances({ instanceName, instanceId, number }: InstanceDto, key: string) { public async fetchInstances({ instanceName, instanceId, number }: InstanceDto, key: string) {
const env = this.configService.get<Auth>('AUTHENTICATION').API_KEY; const env = this.configService.get<Auth>('AUTHENTICATION').API_KEY;
let name = instanceName;
// let arrayReturn = false;
if (env.KEY !== key) { if (env.KEY !== key) {
const instanceByKey = await this.prismaRepository.instance.findMany({ const instancesByKey = await this.prismaRepository.instance.findMany({
where: { where: {
token: key, token: key,
name: instanceName || undefined,
id: instanceId || undefined,
}, },
}); });
if (instanceByKey) { if (instancesByKey.length > 0) {
name = instanceByKey[0].name; const names = instancesByKey.map((instance) => instance.name);
// arrayReturn = true;
return this.waMonitor.instanceInfo(names);
} else { } else {
throw new UnauthorizedException(); throw new UnauthorizedException();
} }
} }
if (name) { if (instanceId || number) {
return this.waMonitor.instanceInfo(name);
} else if (instanceId || number) {
return this.waMonitor.instanceInfoById(instanceId, number); return this.waMonitor.instanceInfoById(instanceId, number);
} }

View File

@ -1699,7 +1699,7 @@ export class BaileysStartupService extends ChannelStartupService {
website: business?.website?.shift(), website: business?.website?.shift(),
}; };
} else { } else {
const info: Instance = await waMonitor.instanceInfo(instanceName); const info: Instance = await waMonitor.instanceInfo([instanceName]);
const business = await this.fetchBusinessProfile(jid); const business = await this.fetchBusinessProfile(jid);
return { return {

View File

@ -59,14 +59,25 @@ export class WAMonitoringService {
} }
} }
public async instanceInfo(instanceName?: string): Promise<any> { public async instanceInfo(instanceNames?: string[]): Promise<any> {
if (instanceName && !this.waInstances[instanceName]) { const inexistentInstances = instanceNames ? instanceNames.filter((instance) => !this.waInstances[instance]) : [];
throw new NotFoundException(`Instance "${instanceName}" not found`);
if (inexistentInstances.length > 0) {
throw new NotFoundException(
`Instance${inexistentInstances.length > 1 ? 's' : ''} "${inexistentInstances.join(', ')}" not found`,
);
} }
const clientName = this.configService.get<Database>('DATABASE').CONNECTION.CLIENT_NAME; const clientName = this.configService.get<Database>('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({ const instances = await this.prismaRepository.instance.findMany({
where, where,
@ -112,7 +123,7 @@ export class WAMonitoringService {
throw new NotFoundException(`Instance "${instanceName}" not found`); throw new NotFoundException(`Instance "${instanceName}" not found`);
} }
return this.instanceInfo(instanceName); return this.instanceInfo([instanceName]);
} }
public async cleaningUp(instanceName: string) { public async cleaningUp(instanceName: string) {