import { getQuery } from "h3"; import { prisma } from "../../utils/prisma"; import { getAuthContext } from "../../utils/auth"; export default defineEventHandler(async (event) => { const auth = await getAuthContext(event); const q = getQuery(event); const threadId = typeof q.threadId === "string" ? q.threadId : ""; if (!threadId) throw createError({ statusCode: 400, statusMessage: "threadId is required" }); const thread = await prisma.omniThread.findFirst({ where: { id: threadId, teamId: auth.teamId, channel: "TELEGRAM" }, }); if (!thread) throw createError({ statusCode: 404, statusMessage: "thread not found" }); const items = await prisma.omniMessage.findMany({ where: { teamId: auth.teamId, threadId: thread.id, channel: "TELEGRAM" }, orderBy: { occurredAt: "asc" }, take: 200, }); return { thread: { id: thread.id, contactId: thread.contactId, externalChatId: thread.externalChatId, businessConnectionId: thread.businessConnectionId, title: thread.title, updatedAt: thread.updatedAt, }, items: items.map((m) => ({ id: m.id, direction: m.direction, status: m.status, text: m.text, providerMessageId: m.providerMessageId, occurredAt: m.occurredAt, })), }; });