78 lines
2.7 KiB
JavaScript
78 lines
2.7 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.MessageRepository = void 0;
|
|
const prisma_1 = require("../../infra/database/prisma");
|
|
class MessageRepository {
|
|
/**
|
|
* Histórico paginado por cursor (timestamp).
|
|
* Retorna `limit` mensagens anteriores ao cursor dado.
|
|
* Se cursor não for fornecido, retorna as mais recentes.
|
|
*/
|
|
async findByChatPaginated(chatId, opts = {}) {
|
|
const { limit = 40, before, after } = opts;
|
|
const where = { chatId };
|
|
if (before)
|
|
where['timestamp'] = { lt: new Date(before) };
|
|
if (after)
|
|
where['timestamp'] = { gt: new Date(after) };
|
|
// Initial load (sem cursor) e scroll-up (before): busca DESC e inverte
|
|
// para retornar as mensagens mais recentes em ordem cronológica.
|
|
// Scroll-down (after): busca ASC naturalmente.
|
|
const needsReverse = !after;
|
|
const messages = await prisma_1.prisma.message.findMany({
|
|
where,
|
|
orderBy: { timestamp: needsReverse ? 'desc' : 'asc' },
|
|
take: limit,
|
|
include: {
|
|
replyTo: {
|
|
select: {
|
|
id: true,
|
|
messageId: true,
|
|
body: true,
|
|
fromMe: true,
|
|
type: true,
|
|
mediaUrl: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
// Inverte para ordem cronológica (mais antiga → mais recente)
|
|
if (needsReverse)
|
|
messages.reverse();
|
|
const hasMore = messages.length === limit;
|
|
return { messages, hasMore };
|
|
}
|
|
async findById(id) {
|
|
return prisma_1.prisma.message.findUnique({ where: { id } });
|
|
}
|
|
async search(tenantId, instanceId, query, limit = 20) {
|
|
return prisma_1.prisma.message.findMany({
|
|
where: {
|
|
tenantId,
|
|
instanceId,
|
|
body: { contains: query, mode: 'insensitive' },
|
|
},
|
|
include: {
|
|
chat: {
|
|
select: {
|
|
jid: true,
|
|
contact: { select: { name: true, notify: true, avatarUrl: true } },
|
|
},
|
|
},
|
|
},
|
|
orderBy: { timestamp: 'desc' },
|
|
take: limit,
|
|
});
|
|
}
|
|
async updateStatus(messageId, instanceId, status) {
|
|
return prisma_1.prisma.message.updateMany({
|
|
where: { messageId, instanceId },
|
|
data: { status: status },
|
|
});
|
|
}
|
|
async countByChat(chatId) {
|
|
return prisma_1.prisma.message.count({ where: { chatId } });
|
|
}
|
|
}
|
|
exports.MessageRepository = MessageRepository;
|