diff --git a/src/modules/whatsapp/whatsapp.service.ts b/src/modules/whatsapp/whatsapp.service.ts index 41ab2cf..e3fdc15 100644 --- a/src/modules/whatsapp/whatsapp.service.ts +++ b/src/modules/whatsapp/whatsapp.service.ts @@ -55,23 +55,14 @@ export class WhatsappService { return 'CONNECTED'; } - private cleanPhone(id: string): string { - return id.replace(/@c\.us$/, ''); - } - - private toWaId(phone: string): string { - return phone.includes('@') ? phone : `${phone}@c.us`; - } - async getChats() { const rows = await this.mensagensRepository.findChats(); return Promise.all(rows.map(async (row: any) => { const chatId = row.chat_id; - const waId = this.toWaId(chatId); const assignment = await this.assignmentService.getAssignment(chatId); const contactProfile = await this.getCustomerContact(chatId); return { - id: { _serialized: waId, user: chatId, server: 'c.us' }, + id: { _serialized: chatId, user: chatId, server: 'c.us' }, name: contactProfile?.name || row.sender_name || chatId, preview: row.preview || '', timestamp: Number(row.timestamp), @@ -84,8 +75,7 @@ export class WhatsappService { } async getChatMessages(chatId: string) { - const cleanId = this.cleanPhone(chatId); - const rows = await this.mensagensRepository.findByChatId(cleanId); + const rows = await this.mensagensRepository.findByChatId(chatId); return rows.map((row: any) => ({ id: { _serialized: row.wamid, user: row.wamid, server: 'c.us' }, body: row.body ?? '', @@ -93,7 +83,7 @@ export class WhatsappService { timestamp: Number(row.timestamp), hasMedia: row.type !== 'text', media: null, - chatId: this.toWaId(row.chat_id), + chatId: row.chat_id, status: row.status, })); } @@ -122,7 +112,6 @@ export class WhatsappService { } async sendMessage(to: string, message: string, _media?: { data: string; mimetype: string; filename?: string }, senderName?: string) { - const cleanTo = this.cleanPhone(to); const token = process.env.META_ACCESS_TOKEN; const phoneNumberId = process.env.META_PHONE_NUMBER_ID; const apiVersion = process.env.META_API_VERSION || 'v25.0'; @@ -144,7 +133,7 @@ export class WhatsappService { body: JSON.stringify({ messaging_product: 'whatsapp', recipient_type: 'individual', - to: cleanTo, + to, type: 'text', text: { preview_url: false, body }, }), @@ -153,18 +142,18 @@ export class WhatsappService { if (!response.ok) { const error = await response.json().catch(() => ({})); - this.logger.error(`[Meta] Falha ao enviar mensagem para ${cleanTo}:`, error); + this.logger.error(`[Meta] Falha ao enviar mensagem para ${to}:`, error); throw new Error(`Meta API erro ${response.status}: ${JSON.stringify(error)}`); } const result = await response.json() as any; const wamid = result?.messages?.[0]?.id; - this.logger.log(`[Meta] Mensagem enviada para ${cleanTo}: ${wamid}`); + this.logger.log(`[Meta] Mensagem enviada para ${to}: ${wamid}`); if (wamid) { await this.mensagensRepository.save({ wamid, - chatId: cleanTo, + chatId: to, fromMe: true, type: 'text', body: body, @@ -174,7 +163,7 @@ export class WhatsappService { }); } - await this.assignmentService.clearTransferNote(cleanTo); + await this.assignmentService.clearTransferNote(to); return result; } @@ -268,7 +257,7 @@ export class WhatsappService { }); this.gateway.emitNewMessage({ - from: this.toWaId(from), + from, body: messageBody, timestamp, isGroupMsg: false,