Files
Momento/memento-note/app/api/flashcards/save/route.ts
Antigravity 0fa8978395
Some checks failed
CI / Lint, Unit Tests & Build (push) Failing after 1m32s
CI / Deploy production (on server) (push) Has been skipped
feat: mobile app complet + flashcards fixes + drag handle améliorations
Mobile app:
- Révision flashcards : liste decks, session flip-card SM-2, couleurs harmonisées web
- Génération flashcards depuis note (FlashcardSheet + route /api/mobile/flashcards/generate)
- Audio Whisper : hook useAudioRecorder reécrit, MicButton avec erreurs
- IA : AISheet (améliorer/clarifier/résumer), TitleSheet (titre automatique)
- Suppression note (soft delete + confirmation Alert)
- Note du jour : titre lisible + HTML (plus JSON TipTap brut)
- Parser TipTap→HTML côté mobile (tipTapToHtml)
- Icône 🎓 dans header note → génération flashcards
- Endpoint flashcardGenerate dans config.ts

Web fixes:
- Bug flashcards groupées par carnet → deck par note (migration + schema)
- Bug filtre 'cartes dues' ignoré (suppression fallback buildSessionQueue)
- Suppression UI création deck manuelle (inutile)
- Fix setViewType is not defined dans home-client.tsx

Drag handle menu:
- Fix : clearNodes() avant transformation (heading→liste/code/citation)
- Ajout : option 'Texte' (paragraphe) dans Transformer en
- Ajout : Monter / Descendre le bloc
- Ajout : Copier le contenu du bloc
- Fix : sous-menu hover stable (délai 200ms)
- Fix : Supprimer en rouge via classe --danger (plus :first-child)
- i18n : nouvelles clés dans 15 locales

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-29 18:49:40 +00:00

123 lines
3.9 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { Prisma } from '@prisma/client'
import { auth } from '@/auth'
import { prisma } from '@/lib/prisma'
import type { FlashcardStyle } from '@/lib/flashcards/generate-flashcards'
interface CardInput {
front: string
back: string
type?: string
}
export async function POST(request: NextRequest) {
try {
if (!prisma.flashcard || !prisma.flashcardDeck) {
return NextResponse.json(
{ error: 'Flashcards client outdated. Restart the app after prisma generate.', errorKey: 'flashcards.schemaMissing' },
{ status: 503 },
)
}
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 noteName = 'Sans titre'
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
noteName = note.title?.trim() || 'Sans titre'
}
let deckId = deckIdInput
if (!deckId) {
if (noteId) {
// Chercher un deck déjà créé pour CETTE note spécifique
const existingFromNote = await prisma.flashcard.findFirst({
where: { noteId, deck: { userId: session.user.id } },
select: { deckId: true },
})
if (existingFromNote) {
deckId = existingFromNote.deckId
}
}
if (!deckId) {
// Créer un nouveau deck nommé d'après la note (pas le carnet)
const deck = await prisma.flashcardDeck.create({
data: { userId: session.user.id, notebookId, name: noteName },
})
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)
if (error instanceof Prisma.PrismaClientKnownRequestError) {
if (error.code === 'P2021' || error.code === 'P2022') {
return NextResponse.json(
{ error: 'Flashcards schema not migrated. Run prisma migrate deploy.', errorKey: 'flashcards.schemaMissing' },
{ status: 503 },
)
}
}
const message = error instanceof Error ? error.message : 'Failed to save flashcards'
return NextResponse.json({ error: message }, { status: 500 })
}
}