import { prisma } from "../../utils/prisma"; import { getAuthContext } from "../../utils/auth"; export default defineEventHandler(async (event) => { const auth = await getAuthContext(event); const threads = await prisma.omniThread.findMany({ where: { teamId: auth.teamId, channel: "TELEGRAM" }, orderBy: { updatedAt: "desc" }, take: 50, include: { contact: true, messages: { orderBy: { occurredAt: "desc" }, take: 1 }, }, }); return { items: threads.map((t) => ({ id: t.id, contact: { id: t.contact.id, name: t.contact.name }, externalChatId: t.externalChatId, businessConnectionId: t.businessConnectionId, title: t.title, updatedAt: t.updatedAt, lastMessage: t.messages[0] ? { id: t.messages[0].id, direction: t.messages[0].direction, status: t.messages[0].status, text: t.messages[0].text, occurredAt: t.messages[0].occurredAt, } : null, })), }; });