From 3347378cf52358d4862fd7d2bc4fa9bb36a9a4e7 Mon Sep 17 00:00:00 2001 From: Zebratic <48886580+Zebratic@users.noreply.github.com> Date: Thu, 12 Mar 2026 04:43:20 +0100 Subject: [PATCH] feat: add sessions API endpoints Add GET endpoints for listing and retrieving session details from debug_logs collection. --- .../dashboard/sessions/[sessionId]/route.ts | 20 ++++++++++++ src/app/api/dashboard/sessions/route.ts | 31 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/app/api/dashboard/sessions/[sessionId]/route.ts create mode 100644 src/app/api/dashboard/sessions/route.ts diff --git a/src/app/api/dashboard/sessions/[sessionId]/route.ts b/src/app/api/dashboard/sessions/[sessionId]/route.ts new file mode 100644 index 0000000..3a10ab7 --- /dev/null +++ b/src/app/api/dashboard/sessions/[sessionId]/route.ts @@ -0,0 +1,20 @@ +import { getSession } from "@/lib/auth-helpers"; +import { getMongoDb } from "@/lib/mongo"; +import { NextResponse } from "next/server"; + +export async function GET(request: Request, { params }: { params: Promise<{ sessionId: string }> }) { + const { sessionId } = await params; + const session = await getSession(); + if (!session?.user?.id) { + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + } + + const db = getMongoDb(); + + const logs = await db.collection("debug_logs") + .find({ userId: session.user.id, sessionId }) + .sort({ timestamp: 1 }) + .toArray(); + + return NextResponse.json({ data: logs }); +} diff --git a/src/app/api/dashboard/sessions/route.ts b/src/app/api/dashboard/sessions/route.ts new file mode 100644 index 0000000..11460bd --- /dev/null +++ b/src/app/api/dashboard/sessions/route.ts @@ -0,0 +1,31 @@ +import { getSession } from "@/lib/auth-helpers"; +import { getMongoDb } from "@/lib/mongo"; +import { NextResponse } from "next/server"; + +export async function GET() { + const session = await getSession(); + if (!session?.user?.id) { + return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); + } + + const db = getMongoDb(); + + // Group by sessionId and get session summaries + const sessions = await db.collection("debug_logs").aggregate([ + { $match: { userId: session.user.id, sessionId: { $exists: true, $ne: null } } }, + { $sort: { timestamp: -1 } }, + { + $group: { + _id: "$sessionId", + firstRequest: { $first: "$timestamp" }, + lastRequest: { $last: "$timestamp" }, + requestCount: { $sum: 1 }, + model: { $first: "$originalModel" }, + } + }, + { $sort: { lastRequest: -1 } }, + { $limit: 50 } + ]).toArray(); + + return NextResponse.json({ data: sessions }); +}