Files
Momento/memento-note/lib/flashcards/deck-utils.ts
Antigravity 0784c94242
Some checks failed
CI / Lint, Test & Build (push) Failing after 57s
CI / Deploy production (on server) (push) Has been skipped
feat(notes): vues structurées tableau/kanban, flashcards et MCP robuste
Ajoute la base organisable par carnet (schéma, champs partagés, valeurs par note)
avec activation guidée, tableau éditable, kanban et suppression de colonnes.
Corrige le multiselect en vue tableau et enrichit sidebar, grille et i18n FR/EN.
Inclut aussi les améliorations flashcards SM-2, l'audit consentement IA et la
robustesse du serveur MCP (config, validation, rate-limit, métriques).

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-24 23:03:16 +00:00

65 lines
1.6 KiB
TypeScript

import { prisma } from '@/lib/prisma'
import { Prisma } from '@prisma/client'
export async function getOrCreateDeckForNotebook(params: {
userId: string
notebookId: string | null
notebookName?: string | null
manualName?: string
}) {
const { userId, notebookId, notebookName, manualName } = params
if (notebookId) {
const existing = await prisma.flashcardDeck.findFirst({
where: { userId, notebookId },
})
if (existing) return existing
const notebook = await prisma.notebook.findFirst({
where: { id: notebookId, userId },
select: { name: true },
})
if (!notebook) {
throw new Error('Notebook not found')
}
return prisma.flashcardDeck.create({
data: {
userId,
notebookId,
name: notebook.name,
},
}).catch(async (error) => {
if (error instanceof Prisma.PrismaClientKnownRequestError && error.code === 'P2002') {
const raced = await prisma.flashcardDeck.findFirst({
where: { userId, notebookId },
})
if (raced) return raced
}
throw error
})
}
const name = (manualName || notebookName || 'Deck').trim().slice(0, 120)
if (!name) {
throw new Error('Deck name required')
}
return prisma.flashcardDeck.create({
data: {
userId,
name,
},
})
}
export function stripHtmlToText(html: string): string {
return html
.replace(/<script[\s\S]*?<\/script>/gi, ' ')
.replace(/<style[\s\S]*?<\/style>/gi, ' ')
.replace(/<[^>]+>/g, ' ')
.replace(/&nbsp;/g, ' ')
.replace(/\s+/g, ' ')
.trim()
}