Files
Momento/memento-note/app/api/mobile/flashcards/generate/route.ts
Antigravity ee70e74bf5
All checks were successful
CI / Lint, Unit Tests & Build (push) Successful in 5m39s
CI / Deploy production (on server) (push) Successful in 22s
fix: 5 bugs critiques de l'éditeur (Phase 1 audit)
1. replaceAll (Find & Replace) — une seule transaction ProseMirror
   au lieu d'un forEach cassé. Tous les matchs sont maintenant remplacés.

2. Link Preview unwrap — deleteNode() au lieu de clearer les attrs
   qui laissaient un nœud fantôme invisible dans le document.

3. Conversion Markdown → richtext — breaks: true dans marked.parse()
   Les simple newlines sont maintenant convertis en <br>.
   + préserve les blocs custom (toggle, callout, math, columns,
   outline, link-preview) en commentaires HTML lors de l'export MD.

4. emitNoteChange exercices — shape corrigée (type:'created' attend
   un objet Note, pas noteId/notebookId séparés).

5. Raccourcis clavier sans conflit :
   Cmd+Shift+C → Cmd+Alt+C (callout, avant: copier)
   Cmd+Shift+O → Cmd+Alt+O (outline, avant: historique/signets)
   Cmd+Shift+L → Cmd+Alt+L (colonnes, avant: lock screen macOS)
2026-06-20 15:48:18 +00:00

84 lines
3.0 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import prisma from '@/lib/prisma'
import { getMobileUserId } from '@/lib/mobile-auth'
import { generateFlashcardsFromNote, type FlashcardStyle } from '@/lib/flashcards/generate-flashcards'
import { stripHtmlToText } from '@/lib/flashcards/deck-utils'
import { reserveUsageOrThrow, QuotaExceededError } from '@/lib/entitlements'
export async function POST(req: NextRequest) {
const userId = getMobileUserId(req)
if (!userId) return NextResponse.json({ error: 'Non autorisé' }, { status: 401 })
const body = await req.json().catch(() => ({}))
const noteId = typeof body.noteId === 'string' ? body.noteId : null
const count = typeof body.count === 'number' ? Math.min(body.count, 20) : 10
const styleRaw = typeof body.style === 'string' ? body.style : 'qa'
const style: FlashcardStyle = ['qa', 'cloze', 'concept'].includes(styleRaw) ? (styleRaw as FlashcardStyle) : 'qa'
if (!noteId) return NextResponse.json({ error: 'noteId requis' }, { status: 400 })
const note = await prisma.note.findFirst({
where: { id: noteId, userId, trashedAt: null },
select: { id: true, title: true, content: true, notebookId: true, language: true },
})
if (!note) return NextResponse.json({ error: 'Note introuvable' }, { status: 404 })
const textContent = stripHtmlToText(note.content)
if (textContent.length < 80) {
return NextResponse.json({ error: 'Contenu insuffisant pour générer des flashcards (minimum 80 caractères)' }, { status: 400 })
}
try {
await reserveUsageOrThrow(userId, 'ai_flashcard')
} catch (err) {
if (err instanceof QuotaExceededError) {
return NextResponse.json({ error: err.currentQuota === 0 ? 'Fonctionnalité non disponible sur votre abonnement' : 'Quota IA atteint' }, { status: 402 })
}
throw err
}
const cards = await generateFlashcardsFromNote({
title: note.title || 'Sans titre',
textContent,
count,
style,
language: note.language || undefined,
})
if (cards.length === 0) {
return NextResponse.json({ error: 'Génération échouée — aucune carte produite' }, { status: 500 })
}
// Chercher un deck existant pour cette note, ou en créer un
const existing = await prisma.flashcard.findFirst({
where: { noteId: note.id, deck: { userId } },
select: { deckId: true },
})
let deckId: string
if (existing) {
deckId = existing.deckId
// Supprimer les anciennes cartes pour les remplacer
await prisma.flashcard.deleteMany({ where: { noteId: note.id, deckId } })
} else {
const deck = await prisma.flashcardDeck.create({
data: { userId, notebookId: note.notebookId, name: note.title || 'Sans titre' },
})
deckId = deck.id
}
await prisma.flashcard.createMany({
data: cards.map((c) => ({
deckId,
noteId: note.id,
front: c.front,
back: c.back,
type: c.type,
})),
})
await prisma.flashcardDeck.update({ where: { id: deckId }, data: { updatedAt: new Date() } })
return NextResponse.json({ deckId, count: cards.length, cards })
}