From fd46eade07fc9cde36ac261e7ae9f0116058261d Mon Sep 17 00:00:00 2001 From: Rafael Lopes Date: Fri, 19 Jun 2026 16:13:12 -0300 Subject: [PATCH] FIX: Mapeamento de chats e mensagens para o formato esperado pelo frontend --- .../repositories/mensagens.repository.ts | 26 ++++++++++------- src/modules/whatsapp/whatsapp.service.ts | 29 +++++++++++++++++-- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/src/modules/whatsapp/repositories/mensagens.repository.ts b/src/modules/whatsapp/repositories/mensagens.repository.ts index 0cd341f..31115f5 100644 --- a/src/modules/whatsapp/repositories/mensagens.repository.ts +++ b/src/modules/whatsapp/repositories/mensagens.repository.ts @@ -31,22 +31,28 @@ export class MensagensRepository { } async findByChatId(chatId: string) { - return this.database.query( + const result = await this.database.query( `SELECT * FROM mensagens WHERE chat_id = $1 ORDER BY timestamp ASC`, [chatId], ); + return result.rows; } async findChats() { - return this.database.query(` - SELECT DISTINCT ON (chat_id) - chat_id, - body AS preview, - from_me AS last_message_from_me, - sender_name, - timestamp - FROM mensagens - ORDER BY chat_id, timestamp DESC + const result = await this.database.query(` + SELECT * + FROM ( + SELECT DISTINCT ON (chat_id) + chat_id, + body AS preview, + from_me AS last_message_from_me, + sender_name, + timestamp + FROM mensagens + ORDER BY chat_id, timestamp DESC + ) latest + ORDER BY timestamp DESC `); + return result.rows; } } diff --git a/src/modules/whatsapp/whatsapp.service.ts b/src/modules/whatsapp/whatsapp.service.ts index 04ddc70..2364670 100644 --- a/src/modules/whatsapp/whatsapp.service.ts +++ b/src/modules/whatsapp/whatsapp.service.ts @@ -56,11 +56,36 @@ export class WhatsappService { } async getChats() { - return this.mensagensRepository.findChats(); + const rows = await this.mensagensRepository.findChats(); + return Promise.all(rows.map(async (row: any) => { + const chatId = row.chat_id; + const assignment = await this.assignmentService.getAssignment(chatId); + const contactProfile = await this.getCustomerContact(chatId); + return { + id: { _serialized: chatId, user: chatId, server: 'c.us' }, + name: contactProfile?.name || row.sender_name || chatId, + preview: row.preview || '', + timestamp: Number(row.timestamp), + unreadCount: 0, + lastMessageFromMe: Boolean(row.last_message_from_me), + contactProfile: contactProfile ?? { chat_id: chatId, phone: chatId, name: null, company: null, note: null }, + assignment: assignment || null, + }; + })); } async getChatMessages(chatId: string) { - return this.mensagensRepository.findByChatId(chatId); + 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 ?? '', + fromMe: Boolean(row.from_me), + timestamp: Number(row.timestamp), + hasMedia: row.type !== 'text', + media: null, + chatId: row.chat_id, + status: row.status, + })); } async getMessageMedia(_chatId: string, _messageId: string) {