FIX: Mapeamento de chats e mensagens para o formato esperado pelo frontend
Some checks are pending
Deploy Dev / deploy (push) Waiting to run
Some checks are pending
Deploy Dev / deploy (push) Waiting to run
This commit is contained in:
parent
ee7b94a699
commit
fd46eade07
@ -31,14 +31,17 @@ 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(`
|
||||
const result = await this.database.query(`
|
||||
SELECT *
|
||||
FROM (
|
||||
SELECT DISTINCT ON (chat_id)
|
||||
chat_id,
|
||||
body AS preview,
|
||||
@ -47,6 +50,9 @@ export class MensagensRepository {
|
||||
timestamp
|
||||
FROM mensagens
|
||||
ORDER BY chat_id, timestamp DESC
|
||||
) latest
|
||||
ORDER BY timestamp DESC
|
||||
`);
|
||||
return result.rows;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user