FIX: Mapeamento de chats e mensagens para o formato esperado pelo frontend
Some checks are pending
Deploy Dev / deploy (push) Waiting to run

This commit is contained in:
Rafael Alves Lopes 2026-06-19 16:13:12 -03:00
parent ee7b94a699
commit fd46eade07
2 changed files with 43 additions and 12 deletions

View File

@ -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;
}
}

View File

@ -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) {