mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-24 09:28:39 -06:00
chore: Refactor WAMonitoringService instance retrieval and improve code readability
Refactored WAMonitoringService to retrieve instances using Prisma's `findMany` method with a dynamic `where` clause. This change simplifies the code and makes it more maintainable. It also removes the deprecated `for...of` loop and the unnecessary instantiation of objects. The new implementation provides better performance and is more aligned with the current best practices. Modified files: - src/api/services/monitor.service.ts
This commit is contained in:
parent
b70ab5a4c3
commit
d342a7b621
@ -89,7 +89,7 @@ export class InstanceController {
|
|||||||
chatwootLogo,
|
chatwootLogo,
|
||||||
}: InstanceDto) {
|
}: InstanceDto) {
|
||||||
try {
|
try {
|
||||||
await this.authService.checkDuplicateToken(token);
|
if (token) await this.authService.checkDuplicateToken(token);
|
||||||
|
|
||||||
if (!token && integration === Integration.WHATSAPP_BUSINESS) {
|
if (!token && integration === Integration.WHATSAPP_BUSINESS) {
|
||||||
throw new BadRequestException('token is required');
|
throw new BadRequestException('token is required');
|
||||||
|
@ -5,6 +5,10 @@ export class AuthService {
|
|||||||
constructor(private readonly prismaRepository: PrismaRepository) {}
|
constructor(private readonly prismaRepository: PrismaRepository) {}
|
||||||
|
|
||||||
public async checkDuplicateToken(token: string) {
|
public async checkDuplicateToken(token: string) {
|
||||||
|
if (!token) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
const instances = await this.prismaRepository.instance.findMany({
|
const instances = await this.prismaRepository.instance.findMany({
|
||||||
where: { token },
|
where: { token },
|
||||||
});
|
});
|
||||||
|
@ -1831,8 +1831,6 @@ export class BaileysStartupService extends ChannelStartupService {
|
|||||||
throw new NotFoundException('Group not found');
|
throw new NotFoundException('Group not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('options', options);
|
|
||||||
|
|
||||||
if (options.mentionsEveryOne) {
|
if (options.mentionsEveryOne) {
|
||||||
mentions = group.participants.map((participant) => participant.id);
|
mentions = group.participants.map((participant) => participant.id);
|
||||||
} else if (options.mentioned?.length) {
|
} else if (options.mentioned?.length) {
|
||||||
|
@ -68,9 +68,10 @@ export class WAMonitoringService {
|
|||||||
throw new NotFoundException(`Instance "${instanceName}" not found`);
|
throw new NotFoundException(`Instance "${instanceName}" not found`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// const instances: any[] = [];
|
const where = instanceName ? { name: instanceName } : {};
|
||||||
|
|
||||||
const instances = await this.prismaRepository.instance.findMany({
|
const instances = await this.prismaRepository.instance.findMany({
|
||||||
|
where,
|
||||||
include: {
|
include: {
|
||||||
Chatwoot: true,
|
Chatwoot: true,
|
||||||
Proxy: true,
|
Proxy: true,
|
||||||
@ -82,91 +83,6 @@ export class WAMonitoringService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return instances;
|
return instances;
|
||||||
|
|
||||||
// for await (const [key, value] of Object.entries(this.waInstances)) {
|
|
||||||
// if (value) {
|
|
||||||
// let chatwoot: any;
|
|
||||||
// const urlServer = this.configService.get<HttpServer>('SERVER').URL;
|
|
||||||
|
|
||||||
// if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) {
|
|
||||||
// const findChatwoot = await this.waInstances[key].findChatwoot();
|
|
||||||
|
|
||||||
// if (findChatwoot && findChatwoot.enabled) {
|
|
||||||
// chatwoot = {
|
|
||||||
// ...findChatwoot,
|
|
||||||
// webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(key)}`,
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// const findIntegration = {
|
|
||||||
// integration: this.waInstances[key].integration,
|
|
||||||
// token: this.waInstances[key].token,
|
|
||||||
// number: this.waInstances[key].number,
|
|
||||||
// };
|
|
||||||
|
|
||||||
// let integration: any;
|
|
||||||
// if (this.waInstances[key].integration === Integration.WHATSAPP_BUSINESS) {
|
|
||||||
// integration = {
|
|
||||||
// ...findIntegration,
|
|
||||||
// webhookWaBusiness: `${urlServer}/webhook/whatsapp/${encodeURIComponent(key)}`,
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
|
|
||||||
// const expose = this.configService.get<Auth>('AUTHENTICATION').EXPOSE_IN_FETCH_INSTANCES;
|
|
||||||
|
|
||||||
// if (value.connectionStatus.state === 'open') {
|
|
||||||
// const instanceData = {
|
|
||||||
// instance: {
|
|
||||||
// instanceName: key,
|
|
||||||
// instanceId: this.waInstances[key].instanceId,
|
|
||||||
// owner: value.wuid,
|
|
||||||
// profileName: (await value.getProfileName()) || 'not loaded',
|
|
||||||
// profilePictureUrl: value.profilePictureUrl,
|
|
||||||
// profileStatus: (await value.getProfileStatus()) || '',
|
|
||||||
// status: value.connectionStatus.state,
|
|
||||||
// },
|
|
||||||
// };
|
|
||||||
|
|
||||||
// if (expose) {
|
|
||||||
// instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;
|
|
||||||
|
|
||||||
// instanceData.instance['token'] = this.waInstances[key].token;
|
|
||||||
|
|
||||||
// if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) instanceData.instance['chatwoot'] = chatwoot;
|
|
||||||
|
|
||||||
// instanceData.instance['integration'] = integration;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// instances.push(instanceData);
|
|
||||||
// } else {
|
|
||||||
// const instanceData = {
|
|
||||||
// instance: {
|
|
||||||
// instanceName: key,
|
|
||||||
// instanceId: this.waInstances[key].instanceId,
|
|
||||||
// status: value.connectionStatus.state,
|
|
||||||
// },
|
|
||||||
// };
|
|
||||||
|
|
||||||
// if (expose) {
|
|
||||||
// instanceData.instance['serverUrl'] = this.configService.get<HttpServer>('SERVER').URL;
|
|
||||||
|
|
||||||
// instanceData.instance['token'] = this.waInstances[key].token;
|
|
||||||
|
|
||||||
// if (this.configService.get<Chatwoot>('CHATWOOT').ENABLED) instanceData.instance['chatwoot'] = chatwoot;
|
|
||||||
|
|
||||||
// instanceData.instance['integration'] = integration;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// instances.push(instanceData);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if (arrayReturn) {
|
|
||||||
// return [instances.find((i) => i.instance.instanceName === instanceName) ?? instances];
|
|
||||||
// }
|
|
||||||
// return instances.find((i) => i.instance.instanceName === instanceName) ?? instances;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async instanceInfoById(instanceId?: string, number?: string) {
|
public async instanceInfoById(instanceId?: string, number?: string) {
|
||||||
@ -251,7 +167,7 @@ export class WAMonitoringService {
|
|||||||
|
|
||||||
public async loadInstance() {
|
public async loadInstance() {
|
||||||
try {
|
try {
|
||||||
if (this.providerSession.ENABLED) {
|
if (this.providerSession?.ENABLED) {
|
||||||
await this.loadInstancesFromProvider();
|
await this.loadInstancesFromProvider();
|
||||||
} else if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
|
} else if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
|
||||||
await this.loadInstancesFromDatabasePostgres();
|
await this.loadInstancesFromDatabasePostgres();
|
||||||
|
Loading…
Reference in New Issue
Block a user