Supprimé: - memento-note/memento-note/ (dossier fantôme, 7 erreurs TS) - tiptap-subpage-extension.tsx + toutes ses références (feature retirée) Corrigé: - tiptap-columns-extension.tsx: PMNode import type → import value - study-plan/route.ts: title null → string conversion - csv/route.ts: paramètre implicit any Ajouté: - Section wizard.* complète (33 clés) dans fr.json + en.json - generalContinue + structuredViewsTagApplied dans fr/en
72 lines
2.4 KiB
TypeScript
72 lines
2.4 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { auth } from '@/auth'
|
|
import prisma from '@/lib/prisma'
|
|
import { studyPlannerService } from '@/lib/ai/services/study-planner.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, examDate } = await request.json()
|
|
if (!notebookId || !examDate) {
|
|
return NextResponse.json({ error: 'notebookId and examDate are 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 },
|
|
orderBy: { order: 'asc' },
|
|
}) as Array<{ id: string; title: string | null }>
|
|
|
|
if (notes.length === 0) {
|
|
return NextResponse.json({ error: 'No notes found in notebook' }, { status: 400 })
|
|
}
|
|
|
|
const userLang = await prisma.user.findUnique({
|
|
where: { id: session.user.id },
|
|
select: { theme: true },
|
|
})
|
|
|
|
const notesForService = notes.map(n => ({ id: n.id, title: n.title ?? '' }))
|
|
const plan = await studyPlannerService.generate(notesForService, examDate)
|
|
|
|
// Set reminders on notes based on the plan
|
|
for (const day of plan.days) {
|
|
for (const noteId of day.noteIds) {
|
|
try {
|
|
const reminderDate = new Date(day.date)
|
|
reminderDate.setHours(9, 0, 0, 0)
|
|
await prisma.note.update({
|
|
where: { id: noteId },
|
|
data: { reminder: reminderDate },
|
|
})
|
|
} catch {}
|
|
}
|
|
}
|
|
|
|
incrementUsageAsync(session.user.id, 'reformulate')
|
|
|
|
return NextResponse.json(plan)
|
|
} catch (error: any) {
|
|
console.error('[Study Planner] Error:', error)
|
|
return NextResponse.json({ error: error.message || 'Failed to generate study plan' }, { status: 500 })
|
|
}
|
|
}
|