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,22 +31,28 @@ export class MensagensRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async findByChatId(chatId: string) {
|
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`,
|
`SELECT * FROM mensagens WHERE chat_id = $1 ORDER BY timestamp ASC`,
|
||||||
[chatId],
|
[chatId],
|
||||||
);
|
);
|
||||||
|
return result.rows;
|
||||||
}
|
}
|
||||||
|
|
||||||
async findChats() {
|
async findChats() {
|
||||||
return this.database.query(`
|
const result = await this.database.query(`
|
||||||
SELECT DISTINCT ON (chat_id)
|
SELECT *
|
||||||
chat_id,
|
FROM (
|
||||||
body AS preview,
|
SELECT DISTINCT ON (chat_id)
|
||||||
from_me AS last_message_from_me,
|
chat_id,
|
||||||
sender_name,
|
body AS preview,
|
||||||
timestamp
|
from_me AS last_message_from_me,
|
||||||
FROM mensagens
|
sender_name,
|
||||||
ORDER BY chat_id, timestamp DESC
|
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() {
|
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) {
|
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) {
|
async getMessageMedia(_chatId: string, _messageId: string) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user