- Service notebook-organizer.service.ts : analyse IA des notes - Endpoint /api/ai/organize-notebook - Dialog avec 4 sections : 1. Résumé de l'état du carnet 2. Tags suggérés (cliquable pour appliquer) 3. Regroupements logiques par catégorie 4. Détection de doublons avec explication - Bouton 'Organiser' (Wand2) dans la barre du carnet - i18n FR/EN complet - Complète les 3 scénarios : Prof (wizard+exercices), Étudiant (wizard+planning), Ingénieur (organisateur)
58 lines
2.0 KiB
TypeScript
58 lines
2.0 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { auth } from '@/auth'
|
|
import prisma from '@/lib/prisma'
|
|
import { notebookOrganizerService } from '@/lib/ai/services/notebook-organizer.service'
|
|
import { checkEntitlementOrThrow, QuotaExceededError, incrementUsageAsync } from '@/lib/entitlements'
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const session = await auth()
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const { notebookId } = await request.json()
|
|
if (!notebookId) {
|
|
return NextResponse.json({ error: 'notebookId is required' }, { status: 400 })
|
|
}
|
|
|
|
try {
|
|
await checkEntitlementOrThrow(session.user.id, 'reformulate')
|
|
} 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 notes = await prisma.note.findMany({
|
|
where: { notebookId, trashedAt: null, userId: session.user.id },
|
|
select: { id: true, title: true, content: true },
|
|
orderBy: { order: 'asc' },
|
|
})
|
|
|
|
if (notes.length < 2) {
|
|
return NextResponse.json({ error: 'Need at least 2 notes to organize' }, { status: 400 })
|
|
}
|
|
|
|
const notesForAnalysis = notes.map(n => ({
|
|
id: n.id,
|
|
title: n.title || 'Sans titre',
|
|
contentPreview: n.content.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim().slice(0, 300),
|
|
}))
|
|
|
|
const result = await notebookOrganizerService.analyze(notesForAnalysis)
|
|
|
|
incrementUsageAsync(session.user.id, 'reformulate')
|
|
|
|
return NextResponse.json(result)
|
|
} catch (error: any) {
|
|
console.error('[Notebook Organizer] Error:', error)
|
|
return NextResponse.json({ error: error.message || 'Failed to organize notebook' }, { status: 500 })
|
|
}
|
|
}
|