From 6ddad8e85a4ed0b1fa2752b93ab3753f96e3a058 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Thu, 19 Dec 2024 11:12:11 -0300 Subject: [PATCH] refactor: Simplify queue name generation and update AMQP publish method - Refactored the queue name generation logic in `channel.service.ts` to improve readability by using a conditional operator. - Updated the `bindQueue` method to use `queueName` instead of `event` for better clarity in `channel.service.ts`. - Commented out unused phone number parsing logic in `whatsapp.baileys.service.ts` to clean up the code and improve maintainability. - Made `isLatest` property optional in the type definition for better flexibility. These changes enhance code clarity and maintainability across the services. --- src/api/services/channel.service.ts | 12 +-- .../channels/whatsapp.baileys.service.ts | 98 ++++++++----------- 2 files changed, 49 insertions(+), 61 deletions(-) diff --git a/src/api/services/channel.service.ts b/src/api/services/channel.service.ts index 12b2556d..49ea9c3a 100644 --- a/src/api/services/channel.service.ts +++ b/src/api/services/channel.service.ts @@ -789,10 +789,9 @@ export class ChannelStartupService { autoDelete: false, }); - const queueName = - prefixKey !== '' - ? `${prefixKey}.${event.replace(/_/g, '.').toLowerCase()}` - : `${event.replace(/_/g, '.').toLowerCase()}`; + const queueName = prefixKey + ? `${prefixKey}.${event.replace(/_/g, '.').toLowerCase()}` + : event.replace(/_/g, '.').toLowerCase(); await amqp.assertQueue(queueName, { durable: true, @@ -802,7 +801,7 @@ export class ChannelStartupService { }, }); - await amqp.bindQueue(queueName, exchangeName, event); + await amqp.bindQueue(queueName, exchangeName, queueName); const message = { event, @@ -816,7 +815,8 @@ export class ChannelStartupService { if (expose && instanceApikey) { message['apikey'] = instanceApikey; } - await amqp.publish(exchangeName, event, Buffer.from(JSON.stringify(message))); + + await amqp.publish(exchangeName, queueName, Buffer.from(JSON.stringify(message))); if (this.configService.get('LOG').LEVEL.includes('WEBHOOKS')) { const logData = { diff --git a/src/api/services/channels/whatsapp.baileys.service.ts b/src/api/services/channels/whatsapp.baileys.service.ts index f342fed4..190a3c8d 100644 --- a/src/api/services/channels/whatsapp.baileys.service.ts +++ b/src/api/services/channels/whatsapp.baileys.service.ts @@ -26,7 +26,6 @@ import makeWASocket, { MessageUpsertType, MiscMessageGenerationOptions, ParticipantAction, - PHONENUMBER_MCC, prepareWAMessageMedia, proto, useMultiFileAuthState, @@ -45,7 +44,6 @@ import { isBase64, isURL } from 'class-validator'; import EventEmitter2 from 'eventemitter2'; // import ffmpeg from 'fluent-ffmpeg'; import fs, { existsSync, readFileSync } from 'fs'; -import { parsePhoneNumber } from 'libphonenumber-js'; import Long from 'long'; import NodeCache from 'node-cache'; import { getMIMEType } from 'node-mime-types'; @@ -676,61 +674,51 @@ export class BaileysStartupService extends ChannelStartupService { } private async sendMobileCode() { - const { registration } = this.client.authState.creds || null; - - let phoneNumber = registration.phoneNumber || this.phoneNumber; - - if (!phoneNumber.startsWith('+')) { - phoneNumber = '+' + phoneNumber; - } - - if (!phoneNumber) { - this.logger.error('Phone number not found'); - return; - } - - const parsedPhoneNumber = parsePhoneNumber(phoneNumber); - - if (!parsedPhoneNumber?.isValid()) { - this.logger.error('Phone number invalid'); - return; - } - - registration.phoneNumber = parsedPhoneNumber.format('E.164'); - registration.phoneNumberCountryCode = parsedPhoneNumber.countryCallingCode; - registration.phoneNumberNationalNumber = parsedPhoneNumber.nationalNumber; - - const mcc = await PHONENUMBER_MCC[parsedPhoneNumber.countryCallingCode]; - if (!mcc) { - this.logger.error('MCC not found'); - return; - } - - registration.phoneNumberMobileCountryCode = mcc; - registration.method = 'sms'; - - try { - const response = await this.client.requestRegistrationCode(registration); - - if (['ok', 'sent'].includes(response?.status)) { - this.logger.verbose('Registration code sent successfully'); - - return response; - } - } catch (error) { - this.logger.error(error); - } + // const { registration } = this.client.authState.creds || null; + // let phoneNumber = registration.phoneNumber || this.phoneNumber; + // if (!phoneNumber.startsWith('+')) { + // phoneNumber = '+' + phoneNumber; + // } + // if (!phoneNumber) { + // this.logger.error('Phone number not found'); + // return; + // } + // const parsedPhoneNumber = parsePhoneNumber(phoneNumber); + // if (!parsedPhoneNumber?.isValid()) { + // this.logger.error('Phone number invalid'); + // return; + // } + // registration.phoneNumber = parsedPhoneNumber.format('E.164'); + // registration.phoneNumberCountryCode = parsedPhoneNumber.countryCallingCode; + // registration.phoneNumberNationalNumber = parsedPhoneNumber.nationalNumber; + // const mcc = await PHONENUMBER_MCC[parsedPhoneNumber.countryCallingCode]; + // if (!mcc) { + // this.logger.error('MCC not found'); + // return; + // } + // registration.phoneNumberMobileCountryCode = mcc; + // registration.method = 'sms'; + // try { + // const response = await this.client.requestRegistrationCode(registration); + // if (['ok', 'sent'].includes(response?.status)) { + // this.logger.verbose('Registration code sent successfully'); + // return response; + // } + // } catch (error) { + // this.logger.error(error); + // } } public async receiveMobileCode(code: string) { - await this.client - .register(code.replace(/["']/g, '').trim().toLowerCase()) - .then(async () => { - this.logger.verbose('Registration code received successfully'); - }) - .catch((error) => { - this.logger.error(error); - }); + console.log(code); + // await this.client + // .register(code.replace(/["']/g, '').trim().toLowerCase()) + // .then(async () => { + // this.logger.verbose('Registration code received successfully'); + // }) + // .catch((error) => { + // this.logger.error(error); + // }); } public async reloadConnection(): Promise { @@ -897,7 +885,7 @@ export class BaileysStartupService extends ChannelStartupService { chats: Chat[]; contacts: Contact[]; messages: proto.IWebMessageInfo[]; - isLatest: boolean; + isLatest?: boolean; }, database: Database, ) => {