Publication IA: - 4 templates (magazine, brief, essay, simple) avec CSS riche - Rewrite IA (article/exercises/tutorial/reference/mixed) - Modération avec timeout 12s + fallback safe - Quotas publish_enhance par tier (basic=2, pro=15, business=100) - Détection contenu stale (hash) - Migration DB publishedContent/publishedTemplate/publishedSourceHash Fixes: - cheerio v1.2: Element -> AnyNode (domhandler), decodeEntities cast - _isShared ajouté au type Note (champ virtuel serveur) - callout colors PDF export: extraction fonction pure testable - admin/published: guard note.userId null - Cmd+S fonctionne en mode dialog (pas seulement fullPage) i18n: - 23 clés publish* traduites dans les 15 locales - Extension Web Clipper: 13 locales mise à jour Tests: - callout-colors.test.ts (6 tests) - note-visible-in-view.test.ts (5 tests) - entitlements.test.ts + byok-entitlements.test.ts: mock usageLog + unstubAllEnvs - 199/199 tests passent Tracker: user-stories.md sync avec sprint-status.yaml
78 lines
2.7 KiB
TypeScript
78 lines
2.7 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { auth } from '@/auth'
|
|
import prisma from '@/lib/prisma'
|
|
import { notebookPublishAnalyzerService } from '@/lib/ai/services/notebook-publish-analyzer.service'
|
|
import { reserveUsageOrThrow, QuotaExceededError } from '@/lib/entitlements'
|
|
import { hasUserAiConsent, aiConsentForbiddenResponse } from '@/lib/consent/server-consent'
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const session = await auth()
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const { notebookId, language } = await request.json()
|
|
if (!notebookId) {
|
|
return NextResponse.json({ error: 'notebookId is required' }, { status: 400 })
|
|
}
|
|
|
|
if (!(await hasUserAiConsent())) return aiConsentForbiddenResponse()
|
|
|
|
try {
|
|
await reserveUsageOrThrow(session.user.id, 'publish_enhance')
|
|
} catch (err) {
|
|
if (err instanceof QuotaExceededError) {
|
|
const isTierLocked = err.currentQuota === 0
|
|
return NextResponse.json(
|
|
{ error: isTierLocked ? 'feature_locked' : 'quota_exceeded', errorKey: isTierLocked ? 'ai.featureLocked' : 'ai.quotaExceeded' },
|
|
{ status: 402 },
|
|
)
|
|
}
|
|
throw err
|
|
}
|
|
|
|
const notebook = await prisma.notebook.findFirst({
|
|
where: { id: notebookId, userId: session.user.id, trashedAt: null },
|
|
select: { id: true, name: true },
|
|
})
|
|
if (!notebook) return NextResponse.json({ error: 'Notebook not found' }, { status: 404 })
|
|
|
|
const notes = await prisma.note.findMany({
|
|
where: { notebookId, userId: session.user.id, trashedAt: null, isArchived: false },
|
|
select: { id: true, title: true, content: true },
|
|
orderBy: { order: 'asc' },
|
|
})
|
|
|
|
if (notes.length === 0) {
|
|
return NextResponse.json({ error: 'No notes found' }, { status: 400 })
|
|
}
|
|
|
|
const notesForService = notes.map(n => {
|
|
const stripped = (n.content || '').replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim()
|
|
const wordCount = stripped.split(' ').filter(Boolean).length
|
|
return {
|
|
id: n.id,
|
|
title: n.title || '',
|
|
wordCount,
|
|
preview: stripped.slice(0, 200),
|
|
}
|
|
})
|
|
|
|
const analysis = await notebookPublishAnalyzerService.analyze(
|
|
notebook.name,
|
|
notesForService,
|
|
language || 'fr',
|
|
)
|
|
|
|
return NextResponse.json({
|
|
notes: analysis.notes,
|
|
description: analysis.description,
|
|
noteTitles: Object.fromEntries(notes.map(n => [n.id, n.title || ''])),
|
|
})
|
|
} catch (error: any) {
|
|
console.error('[notebook-publish/analyze] Error:', error)
|
|
return NextResponse.json({ error: error.message || 'Failed to analyze' }, { status: 500 })
|
|
}
|
|
}
|