Files
Momento/memento-note/app/api/mobile/flashcards/generate/route.ts
Antigravity a84c7e80d6
All checks were successful
CI / Lint, Unit Tests & Build (push) Successful in 6m13s
CI / Deploy production (on server) (push) Successful in 24s
fix(critique): 2 régressions introduites par les fixes précédents
Bug #1 — Memory Echo cassé (runtime crash):
- lib/ai/services/memory-echo.service.ts: lignes adjustedThreshold restaurées
- Cause: sed console.log avait supprimé les lignes de définition
- Effet: findConnections() crashait (ReferenceError)

Bug #2 — Auth mobile totalement cassée (sécurité):
- 14 routes app/api/mobile/*/: getMobileUserId(req) → await getMobileUserId(req)
- Cause: verifyMobileToken devenu async mais callers non mis à jour
- Effet: auth bypassée (Promise truthy au lieu de string)

i18n:
- searchModal.* + insightsView.* propagés dans 13 locales (EN fallback)
2026-07-05 16:53:36 +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 = await 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 })
}