diff --git a/src/modules/whatsapp/whatsapp.service.ts b/src/modules/whatsapp/whatsapp.service.ts index e3fdc15..41ab2cf 100644 --- a/src/modules/whatsapp/whatsapp.service.ts +++ b/src/modules/whatsapp/whatsapp.service.ts @@ -55,14 +55,23 @@ 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: chatId, user: chatId, server: 'c.us' }, + id: { _serialized: waId, user: chatId, server: 'c.us' }, name: contactProfile?.name || row.sender_name || chatId, preview: row.preview || '', timestamp: Number(row.timestamp), @@ -75,7 +84,8 @@ export class WhatsappService { } async getChatMessages(chatId: string) { - const rows = await this.mensagensRepository.findByChatId(chatId); + const cleanId = this.cleanPhone(chatId); + const rows = await this.mensagensRepository.findByChatId(cleanId); return rows.map((row: any) => ({ id: { _serialized: row.wamid, user: row.wamid, server: 'c.us' }, body: row.body ?? '', @@ -83,7 +93,7 @@ export class WhatsappService { timestamp: Number(row.timestamp), hasMedia: row.type !== 'text', media: null, - chatId: row.chat_id, + chatId: this.toWaId(row.chat_id), status: row.status, })); } @@ -112,6 +122,7 @@ 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'; @@ -133,7 +144,7 @@ export class WhatsappService { body: JSON.stringify({ messaging_product: 'whatsapp', recipient_type: 'individual', - to, + to: cleanTo, type: 'text', text: { preview_url: false, body }, }), @@ -142,18 +153,18 @@ export class WhatsappService { if (!response.ok) { const error = await response.json().catch(() => ({})); - this.logger.error(`[Meta] Falha ao enviar mensagem para ${to}:`, error); + this.logger.error(`[Meta] Falha ao enviar mensagem para ${cleanTo}:`, 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 ${to}: ${wamid}`); + this.logger.log(`[Meta] Mensagem enviada para ${cleanTo}: ${wamid}`); if (wamid) { await this.mensagensRepository.save({ wamid, - chatId: to, + chatId: cleanTo, fromMe: true, type: 'text', body: body, @@ -163,7 +174,7 @@ export class WhatsappService { }); } - await this.assignmentService.clearTransferNote(to); + await this.assignmentService.clearTransferNote(cleanTo); return result; } @@ -257,7 +268,7 @@ export class WhatsappService { }); this.gateway.emitNewMessage({ - from, + from: this.toWaId(from), body: messageBody, timestamp, isGroupMsg: false,