route.ts
1,188 bytes
| 1 | import { NextResponse } from 'next/server'; |
|---|---|
| 2 | import { currentUserId } from '@/auth'; |
| 3 | import { getDebateOwnership, loadDebateResult } from '@/db/repositories'; |
| 4 | import { debateToMarkdown } from '@/lib/export-markdown'; |
| 5 | |
| 6 | export const runtime = 'nodejs'; |
| 7 | |
| 8 | type Params = { params: Promise<{ id: string }> }; |
| 9 | |
| 10 | /** GET ?format=md - download a Markdown deliberation report. */ |
| 11 | export async function GET(_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 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 | |
| 23 | const markdown = debateToMarkdown(result); |
| 24 | return new Response(markdown, { |
| 25 | headers: { |
| 26 | 'Content-Type': 'text/markdown; charset=utf-8', |
| 27 | 'Content-Disposition': `attachment; filename="roundtable-${id}.md"`, |
| 28 | }, |
| 29 | }); |
| 30 | } |
| 31 | |