Skip to content

Commit

Permalink
chore: Refactor WAMonitoringService instance retrieval and improve co…
Browse files Browse the repository at this point in the history
…de 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
  • Loading branch information
dgcode-tec committed Jun 26, 2024
1 parent b70ab5a commit d342a7b
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 90 deletions.
2 changes: 1 addition & 1 deletion src/api/controllers/instance.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class InstanceController {
chatwootLogo,
}: InstanceDto) {
try {
await this.authService.checkDuplicateToken(token);
if (token) await this.authService.checkDuplicateToken(token);

if (!token && integration === Integration.WHATSAPP_BUSINESS) {
throw new BadRequestException('token is required');
Expand Down
4 changes: 4 additions & 0 deletions src/api/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export class AuthService {
constructor(private readonly prismaRepository: PrismaRepository) {}

public async checkDuplicateToken(token: string) {
if (!token) {
return true;
}

const instances = await this.prismaRepository.instance.findMany({
where: { token },
});
Expand Down
2 changes: 0 additions & 2 deletions src/api/services/channels/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1831,8 +1831,6 @@ export class BaileysStartupService extends ChannelStartupService {
throw new NotFoundException('Group not found');
}

console.log('options', options);

if (options.mentionsEveryOne) {
mentions = group.participants.map((participant) => participant.id);
} else if (options.mentioned?.length) {
Expand Down
90 changes: 3 additions & 87 deletions src/api/services/monitor.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ export class WAMonitoringService {
throw new NotFoundException(`Instance "${instanceName}" not found`);
}

// const instances: any[] = [];
const where = instanceName ? { name: instanceName } : {};

const instances = await this.prismaRepository.instance.findMany({
where,
include: {
Chatwoot: true,
Proxy: true,
Expand All @@ -82,91 +83,6 @@ export class WAMonitoringService {
});

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) {
Expand Down Expand Up @@ -251,7 +167,7 @@ export class WAMonitoringService {

public async loadInstance() {
try {
if (this.providerSession.ENABLED) {
if (this.providerSession?.ENABLED) {
await this.loadInstancesFromProvider();
} else if (this.db.ENABLED && this.db.SAVE_DATA.INSTANCE) {
await this.loadInstancesFromDatabasePostgres();
Expand Down

0 comments on commit d342a7b

Please sign in to comment.