Files
Momento/memento-note/app/api/flashcards/save/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

109 lines
3.2 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { auth } from '@/auth'
import prisma from '@/lib/prisma'
import { getOrCreateDeckForNotebook } from '@/lib/flashcards/deck-utils'
import type { FlashcardStyle } from '@/lib/flashcards/generate-flashcards'
interface CardInput {
front: string
back: string
type?: string
}
export async function POST(request: NextRequest) {
try {
const session = await auth()
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const body = await request.json()
const noteId = typeof body.noteId === 'string' ? body.noteId : null
const deckIdInput = typeof body.deckId === 'string' ? body.deckId : null
const cards = Array.isArray(body.cards) ? body.cards as CardInput[] : []
if (cards.length === 0) {
return NextResponse.json({ error: 'No cards to save' }, { status: 400 })
}
let notebookId: string | null = null
let fallbackDeckName: string | undefined
if (noteId) {
const note = await prisma.note.findFirst({
where: { id: noteId, userId: session.user.id },
select: { notebookId: true, title: true },
})
if (!note) {
return NextResponse.json({ error: 'Note not found' }, { status: 404 })
}
notebookId = note.notebookId
if (!notebookId) {
fallbackDeckName = note.title?.trim() || 'General'
}
}
let deckId = deckIdInput
if (!deckId) {
if (noteId) {
const existingFromNote = await prisma.flashcard.findFirst({
where: { noteId, deck: { userId: session.user.id } },
select: { deckId: true },
})
if (existingFromNote) {
deckId = existingFromNote.deckId
}
}
if (!deckId) {
const deck = await getOrCreateDeckForNotebook({
userId: session.user.id,
notebookId,
manualName: fallbackDeckName,
})
deckId = deck.id
}
} else {
const deck = await prisma.flashcardDeck.findFirst({
where: { id: deckId, userId: session.user.id },
})
if (!deck) {
return NextResponse.json({ error: 'Deck not found' }, { status: 404 })
}
}
const sanitized = cards
.map((c) => ({
front: typeof c.front === 'string' ? c.front.trim().slice(0, 500) : '',
back: typeof c.back === 'string' ? c.back.trim().slice(0, 800) : '',
type: (['qa', 'cloze', 'concept'].includes(c.type || '') ? c.type : 'qa') as FlashcardStyle,
}))
.filter((c) => c.front && c.back)
if (sanitized.length === 0) {
return NextResponse.json({ error: 'No valid cards' }, { status: 400 })
}
await prisma.flashcard.createMany({
data: sanitized.map((c) => ({
deckId: deckId!,
noteId,
front: c.front,
back: c.back,
type: c.type,
})),
})
await prisma.flashcardDeck.update({
where: { id: deckId! },
data: { updatedAt: new Date() },
})
return NextResponse.json({
deckId,
savedCount: sanitized.length,
})
} catch (error) {
console.error('[flashcards/save]', error)
return NextResponse.json({ error: 'Failed to save flashcards' }, { status: 500 })
}
}