feat: add sessions API endpoints

Add GET endpoints for listing and retrieving session details from debug_logs collection.
This commit is contained in:
Zebratic committed 2026-03-12 04:43:20 +01:00
1 parent 34d3a7c473
commit 3347378cf5
2 files changed
+51

No files matched your search

@@ -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 });
}
+31
View File
@@ -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 });
}