Centralise la réserve via ai-quota, corrige admin unavailable (-1), brancher les routes sans quota et le host-pays brainstorm, avec usage-meter élargi, noms de clusters, MCP et ajustements dashboard/insights. Co-authored-by: Cursor <cursoragent@cursor.com>
90 lines
3.0 KiB
TypeScript
90 lines
3.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 { withAiQuota, handleQuotaHttpError } from '@/lib/ai-quota'
|
|
import { syncNoteLabels } from '@/app/actions/notes'
|
|
|
|
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 })
|
|
}
|
|
|
|
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 withAiQuota(
|
|
session.user.id,
|
|
'reformulate',
|
|
() => notebookOrganizerService.analyze(notesForAnalysis),
|
|
{ lane: 'chat' },
|
|
)
|
|
|
|
return NextResponse.json(result)
|
|
} catch (error: unknown) {
|
|
const quotaResp = handleQuotaHttpError(error)
|
|
if (quotaResp) return quotaResp
|
|
console.error('[Notebook Organizer] Error:', error)
|
|
const message = error instanceof Error ? error.message : 'Failed to organize notebook'
|
|
return NextResponse.json({ error: message }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export async function PATCH(request: NextRequest) {
|
|
try {
|
|
const session = await auth()
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const { action, tagName, noteIds, notebookId } = await request.json()
|
|
|
|
if (action === 'apply_tag' && tagName && Array.isArray(noteIds)) {
|
|
// Fetch existing labels for each note, merge with new tag
|
|
for (const noteId of noteIds) {
|
|
const note = await prisma.note.findUnique({
|
|
where: { id: noteId },
|
|
select: { labels: true, notebookId: true },
|
|
})
|
|
if (!note) continue
|
|
|
|
let existingLabels: string[] = []
|
|
try { existingLabels = note.labels ? JSON.parse(note.labels) : [] } catch {}
|
|
|
|
if (!existingLabels.includes(tagName)) {
|
|
existingLabels.push(tagName)
|
|
}
|
|
|
|
await syncNoteLabels(noteId, existingLabels, note.notebookId, session.user.id)
|
|
}
|
|
|
|
return NextResponse.json({ success: true, applied: noteIds.length })
|
|
}
|
|
|
|
return NextResponse.json({ error: 'Invalid action' }, { status: 400 })
|
|
} catch (error: any) {
|
|
console.error('[Notebook Organizer PATCH] Error:', error)
|
|
return NextResponse.json({ error: error.message }, { status: 500 })
|
|
}
|
|
}
|