Files
remotelink-docker/app/api/sessions/[id]/route.ts
2026-04-10 15:36:33 -07:00

54 lines
1.4 KiB
TypeScript

import { auth } from '@/auth'
import { db } from '@/lib/db'
import { sessions } from '@/lib/db/schema'
import { eq, and } from 'drizzle-orm'
import { NextRequest, NextResponse } from 'next/server'
export async function GET(
_request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const session = await auth()
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const { id } = await params
const result = await db
.select()
.from(sessions)
.where(eq(sessions.id, id))
.limit(1)
if (!result[0]) {
return NextResponse.json({ error: 'Session not found' }, { status: 404 })
}
return NextResponse.json({ session: result[0] })
}
export async function PATCH(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
const session = await auth()
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const { id } = await params
const body = await request.json()
const updates: Record<string, unknown> = {}
if (body.endedAt !== undefined) updates.endedAt = body.endedAt ? new Date(body.endedAt) : new Date()
if (body.durationSeconds !== undefined) updates.durationSeconds = body.durationSeconds
await db
.update(sessions)
.set(updates)
.where(and(eq(sessions.id, id), eq(sessions.viewerUserId, session.user.id)))
return NextResponse.json({ success: true })
}