FIX: IDs no formato @c.us para compatibilidade com o frontend
O frontend exige IDs no formato 5511999@c.us (verifica includes('@') antes de carregar mensagens e usa split('@')[0] para exibir o nome). Adicionados helpers cleanPhone/toWaId: DB continua com números limpos, interface pública expõe @c.us. Corrige mensagens não carregando ao abrir um chat e socket real-time mapeando o contato correto. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
876ab6ec8b
commit
224594918d
@ -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,
|
||||
|
||||
Loading…
Reference in New Issue
Block a user