|
| 1 | +import connectDB from "@/database/db"; |
| 2 | +import Session from "@/database/sessionSchema"; |
| 3 | +import { NextRequest, NextResponse } from "next/server"; |
| 4 | +import mongoose from "mongoose"; |
| 5 | +// GET and PATCH routes for sessions |
| 6 | + |
| 7 | +/** |
| 8 | + * GET /api/sessions/:sessionId |
| 9 | + * Fetch a session by ID |
| 10 | + */ |
| 11 | +export async function GET(request: NextRequest, { params }: { params: { sessionId: string } }) { |
| 12 | + try { |
| 13 | + await connectDB(); |
| 14 | + |
| 15 | + const { sessionId } = params; |
| 16 | + |
| 17 | + if (!mongoose.Types.ObjectId.isValid(sessionId)) { |
| 18 | + return NextResponse.json({ error: "Invalid session ID" }, { status: 400 }); |
| 19 | + } |
| 20 | + |
| 21 | + const session = await Session.findById(sessionId); |
| 22 | + |
| 23 | + if (!session) { |
| 24 | + return NextResponse.json({ error: "Session not found" }, { status: 404 }); |
| 25 | + } |
| 26 | + |
| 27 | + return NextResponse.json({ |
| 28 | + _id: session._id, |
| 29 | + anonUserId: session.anonUserId, |
| 30 | + startedAt: session.startedAt, |
| 31 | + endedAt: session.endedAt, |
| 32 | + durationMs: session.durationMs, |
| 33 | + }); |
| 34 | + } catch (error) { |
| 35 | + console.error("GET /api/sessions/:sessionId error:", error); |
| 36 | + return NextResponse.json({ error: "Failed to fetch session" }, { status: 500 }); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * PATCH /api/sessions/:sessionId |
| 42 | + * Update a session (e.g., end it by setting endedAt and durationMs) |
| 43 | + * Body: { endedAt?: Date, durationMs?: number } or empty to auto-end |
| 44 | + */ |
| 45 | +export async function PATCH(request: NextRequest, { params }: { params: { sessionId: string } }) { |
| 46 | + try { |
| 47 | + await connectDB(); |
| 48 | + |
| 49 | + const { sessionId } = params; |
| 50 | + |
| 51 | + if (!mongoose.Types.ObjectId.isValid(sessionId)) { |
| 52 | + return NextResponse.json({ error: "Invalid session ID" }, { status: 400 }); |
| 53 | + } |
| 54 | + |
| 55 | + const session = await Session.findById(sessionId); |
| 56 | + |
| 57 | + if (!session) { |
| 58 | + return NextResponse.json({ error: "Session not found" }, { status: 404 }); |
| 59 | + } |
| 60 | + |
| 61 | + const body = await request.json().catch(() => ({})); |
| 62 | + |
| 63 | + // If body is empty or only endedAt/durationMs, allow update |
| 64 | + const update: { endedAt?: Date; durationMs?: number } = {}; |
| 65 | + |
| 66 | + if (body.endedAt !== undefined) { |
| 67 | + update.endedAt = typeof body.endedAt === "string" ? new Date(body.endedAt) : body.endedAt; |
| 68 | + } else if (Object.keys(body).length === 0 || body.endSession) { |
| 69 | + // Auto-end: set endedAt to now and calculate duration |
| 70 | + const endedAt = new Date(); |
| 71 | + update.endedAt = endedAt; |
| 72 | + update.durationMs = endedAt.getTime() - session.startedAt.getTime(); |
| 73 | + } |
| 74 | + |
| 75 | + if (body.durationMs !== undefined) { |
| 76 | + update.durationMs = Number(body.durationMs); |
| 77 | + } |
| 78 | + |
| 79 | + const updatedSession = await Session.findByIdAndUpdate(sessionId, { $set: update }, { new: true }); |
| 80 | + |
| 81 | + return NextResponse.json({ |
| 82 | + _id: updatedSession!._id, |
| 83 | + anonUserId: updatedSession!.anonUserId, |
| 84 | + startedAt: updatedSession!.startedAt, |
| 85 | + endedAt: updatedSession!.endedAt, |
| 86 | + durationMs: updatedSession!.durationMs, |
| 87 | + }); |
| 88 | + } catch (error) { |
| 89 | + console.error("PATCH /api/sessions/:sessionId error:", error); |
| 90 | + return NextResponse.json({ error: "Failed to update session" }, { status: 500 }); |
| 91 | + } |
| 92 | +} |
0 commit comments