- Centralize AI consent 403 responses in server-consent helpers - Return human-readable message + code + i18n errorKey from all AI APIs - Update dashboard and AI clients to detect code/errorKey and translate - Add consent.ai.requiredToast i18n keys (fr/en)
63 lines
2.0 KiB
TypeScript
63 lines
2.0 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { auth } from '@/auth'
|
|
import { withAiQuota, handleQuotaHttpError } from '@/lib/ai-quota'
|
|
import { hasUserAiConsent, aiConsentForbiddenResponse } from '@/lib/consent/server-consent'
|
|
import { generateSlideDeck } from '@/lib/ai/services/slide-deck-generator.service'
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const session = await auth()
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
if (!(await hasUserAiConsent())) {
|
|
return aiConsentForbiddenResponse()
|
|
}
|
|
const userId = session.user.id
|
|
|
|
const body = await request.json()
|
|
const { notebookId, theme, purpose, audience, slideCount, language } = body
|
|
|
|
if (!notebookId || typeof notebookId !== 'string') {
|
|
return NextResponse.json({ error: 'notebookId is required' }, { status: 400 })
|
|
}
|
|
|
|
const intent =
|
|
purpose || audience || slideCount
|
|
? {
|
|
purpose: purpose || 'auto',
|
|
audience: audience || 'auto',
|
|
slideCount: typeof slideCount === 'number' ? slideCount : undefined,
|
|
template: 'auto' as const,
|
|
}
|
|
: null
|
|
|
|
const result = await withAiQuota(
|
|
userId,
|
|
'slide_generate',
|
|
() =>
|
|
generateSlideDeck({
|
|
userId,
|
|
sourceNotebookId: notebookId,
|
|
theme: theme && theme !== 'auto' ? theme : null,
|
|
lang: language === 'en' ? 'en' : 'fr',
|
|
intent,
|
|
}),
|
|
{ lane: 'chat' },
|
|
)
|
|
|
|
if (!result.success) {
|
|
return NextResponse.json({ error: result.error || 'Failed to generate slides' }, { status: 500 })
|
|
}
|
|
|
|
return NextResponse.json(result)
|
|
} catch (error: unknown) {
|
|
const quotaResp = handleQuotaHttpError(error)
|
|
if (quotaResp) return quotaResp
|
|
console.error('[NotebookSlides] Error:', error)
|
|
const message = error instanceof Error ? error.message : 'Failed to generate slides'
|
|
return NextResponse.json({ error: message }, { status: 500 })
|
|
}
|
|
}
|