Skip to content

Commit ecd4b91

Browse files
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
1 parent ec2b7f9 commit ecd4b91

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

src/api/controllers/instance.controller.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -360,27 +360,25 @@ export class InstanceController {
360360
public async fetchInstances({ instanceName, instanceId, number }: InstanceDto, key: string) {
361361
const env = this.configService.get<Auth>('AUTHENTICATION').API_KEY;
362362

363-
let name = instanceName;
364-
// let arrayReturn = false;
365-
366363
if (env.KEY !== key) {
367-
const instanceByKey = await this.prismaRepository.instance.findMany({
364+
const instancesByKey = await this.prismaRepository.instance.findMany({
368365
where: {
369366
token: key,
367+
name: instanceName || undefined,
368+
id: instanceId || undefined,
370369
},
371370
});
372371

373-
if (instanceByKey) {
374-
name = instanceByKey[0].name;
375-
// arrayReturn = true;
372+
if (instancesByKey.length > 0) {
373+
const names = instancesByKey.map((instance) => instance.name);
374+
375+
return this.waMonitor.instanceInfo(names);
376376
} else {
377377
throw new UnauthorizedException();
378378
}
379379
}
380380

381-
if (name) {
382-
return this.waMonitor.instanceInfo(name);
383-
} else if (instanceId || number) {
381+
if (instanceId || number) {
384382
return this.waMonitor.instanceInfoById(instanceId, number);
385383
}
386384

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1699,7 +1699,7 @@ export class BaileysStartupService extends ChannelStartupService {
16991699
website: business?.website?.shift(),
17001700
};
17011701
} else {
1702-
const info: Instance = await waMonitor.instanceInfo(instanceName);
1702+
const info: Instance = await waMonitor.instanceInfo([instanceName]);
17031703
const business = await this.fetchBusinessProfile(jid);
17041704

17051705
return {

src/api/services/monitor.service.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,25 @@ export class WAMonitoringService {
5959
}
6060
}
6161

62-
public async instanceInfo(instanceName?: string): Promise<any> {
63-
if (instanceName && !this.waInstances[instanceName]) {
64-
throw new NotFoundException(`Instance "${instanceName}" not found`);
62+
public async instanceInfo(instanceNames?: string[]): Promise<any> {
63+
const inexistentInstances = instanceNames ? instanceNames.filter((instance) => !this.waInstances[instance]) : [];
64+
65+
if (inexistentInstances.length > 0) {
66+
throw new NotFoundException(
67+
`Instance${inexistentInstances.length > 1 ? 's' : ''} "${inexistentInstances.join(', ')}" not found`,
68+
);
6569
}
6670

6771
const clientName = this.configService.get<Database>('DATABASE').CONNECTION.CLIENT_NAME;
6872

69-
const where = instanceName ? { name: instanceName, clientName } : { clientName };
73+
const where = instanceNames
74+
? {
75+
name: {
76+
in: instanceNames,
77+
},
78+
clientName,
79+
}
80+
: { clientName };
7081

7182
const instances = await this.prismaRepository.instance.findMany({
7283
where,
@@ -112,7 +123,7 @@ export class WAMonitoringService {
112123
throw new NotFoundException(`Instance "${instanceName}" not found`);
113124
}
114125

115-
return this.instanceInfo(instanceName);
126+
return this.instanceInfo([instanceName]);
116127
}
117128

118129
public async cleaningUp(instanceName: string) {

0 commit comments

Comments
 (0)