Files
Momento/memento-note/app/api/flashcards/[id]/review/route.ts
Antigravity 36336e6b0d
Some checks failed
CI / Lint, Test & Build (push) Failing after 32s
CI / Deploy production (on server) (push) Has been skipped
feat(flashcards): révision SM-2, génération IA et page /revision
Livre US-FLASHCARDS avec decks, session de révision, stats et migration Prisma. Finalise le Web Clipper (i18n 15 langues) et corrige les erreurs ESLint bloquant la CI.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-24 19:22:20 +00:00

66 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { NextRequest, NextResponse } from 'next/server'
import { auth } from '@/auth'
import prisma from '@/lib/prisma'
import { computeSm2Update } from '@/lib/flashcards/sm2'
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> },
) {
try {
const session = await auth()
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const { id: cardId } = await params
const body = await request.json()
const grade = typeof body.grade === 'number' ? body.grade : Number(body.grade)
if (!Number.isFinite(grade) || grade < 1 || grade > 4) {
return NextResponse.json({ error: 'Grade must be 14' }, { status: 400 })
}
const card = await prisma.flashcard.findFirst({
where: {
id: cardId,
deck: { userId: session.user.id },
},
})
if (!card) {
return NextResponse.json({ error: 'Card not found' }, { status: 404 })
}
const updated = computeSm2Update(grade, {
easinessFactor: card.easinessFactor,
interval: card.interval,
})
const [savedCard] = await prisma.$transaction([
prisma.flashcard.update({
where: { id: cardId },
data: {
easinessFactor: updated.easinessFactor,
interval: updated.interval,
nextReviewAt: updated.nextReviewAt,
},
}),
prisma.flashcardReview.create({
data: { cardId, grade: Math.round(grade) },
}),
])
return NextResponse.json({
card: {
id: savedCard.id,
interval: savedCard.interval,
easinessFactor: savedCard.easinessFactor,
nextReviewAt: savedCard.nextReviewAt.toISOString(),
},
})
} catch (error) {
console.error('[flashcards/[id]/review]', error)
return NextResponse.json({ error: 'Review failed' }, { status: 500 })
}
}