route.ts
891 bytes
| 1 | import { NextResponse } from 'next/server'; |
|---|---|
| 2 | import { currentUserId } from '@/auth'; |
| 3 | import { getDebateOwnership } from '@/db/repositories'; |
| 4 | import { cancelDebate } from '@/lib/debate-registry'; |
| 5 | |
| 6 | export const runtime = 'nodejs'; |
| 7 | |
| 8 | type Params = { params: Promise<{ id: string }> }; |
| 9 | |
| 10 | /** POST: abort a running debate server-side (distinct from just disconnecting). */ |
| 11 | export async function POST(_request: Request, { params }: Params) { |
| 12 | const { id } = await params; |
| 13 | const ownership = await getDebateOwnership(id); |
| 14 | if (!ownership) return NextResponse.json({ error: 'Not found' }, { status: 404 }); |
| 15 | |
| 16 | const userId = await currentUserId(); |
| 17 | const canCancel = ownership.userId === null || ownership.userId === userId; |
| 18 | if (!canCancel) return NextResponse.json({ error: 'Forbidden' }, { status: 403 }); |
| 19 | |
| 20 | const cancelled = cancelDebate(id); |
| 21 | return NextResponse.json({ ok: cancelled }); |
| 22 | } |
| 23 | |