route.ts
1,511 bytes
| 1 | import { NextResponse } from 'next/server'; |
|---|---|
| 2 | import { currentUserId } from '@/auth'; |
| 3 | import { deleteDebate, getDebateOwnership, loadDebateResult } from '@/db/repositories'; |
| 4 | |
| 5 | export const runtime = 'nodejs'; |
| 6 | |
| 7 | type Params = { params: Promise<{ id: string }> }; |
| 8 | |
| 9 | /** GET a full debate snapshot for replay/history. */ |
| 10 | export async function GET(_request: Request, { params }: Params) { |
| 11 | const { id } = await params; |
| 12 | const ownership = await getDebateOwnership(id); |
| 13 | if (!ownership) return NextResponse.json({ error: 'Not found' }, { status: 404 }); |
| 14 | |
| 15 | const userId = await currentUserId(); |
| 16 | // Readable if: a demo, an unlisted anon debate (no owner), or you own it. |
| 17 | const canRead = ownership.isDemo || ownership.userId === null || ownership.userId === userId; |
| 18 | if (!canRead) return NextResponse.json({ error: 'Forbidden' }, { status: 403 }); |
| 19 | |
| 20 | const result = await loadDebateResult(id); |
| 21 | if (!result) return NextResponse.json({ error: 'Not found' }, { status: 404 }); |
| 22 | return NextResponse.json(result); |
| 23 | } |
| 24 | |
| 25 | /** DELETE a debate you own. */ |
| 26 | export async function DELETE(_request: Request, { params }: Params) { |
| 27 | const { id } = await params; |
| 28 | const ownership = await getDebateOwnership(id); |
| 29 | if (!ownership) return NextResponse.json({ error: 'Not found' }, { status: 404 }); |
| 30 | const userId = await currentUserId(); |
| 31 | if (!userId || ownership.userId !== userId) { |
| 32 | return NextResponse.json({ error: 'Forbidden' }, { status: 403 }); |
| 33 | } |
| 34 | await deleteDebate(id); |
| 35 | return NextResponse.json({ ok: true }); |
| 36 | } |
| 37 | |