diff --git a/memento-note/app/actions/notes.ts b/memento-note/app/actions/notes.ts index bc0b45f..763f558 100644 --- a/memento-note/app/actions/notes.ts +++ b/memento-note/app/actions/notes.ts @@ -107,12 +107,22 @@ async function syncLabels(userId: string, noteLabels: string[] = [], notebookId? const nbScope = notebookId ?? null if (noteLabels.length > 0) { - const trimmedNames = [...new Set( - noteLabels.map(name => name?.trim()).filter((n): n is string => Boolean(n)) - )] + // Deduplicate case-insensitively, keep original case + const seen = new Set() + const trimmedNames = noteLabels + .map(name => name?.trim()) + .filter((n): n is string => Boolean(n)) + .filter(n => { + const key = n.toLowerCase() + if (seen.has(key)) return false + seen.add(key) + return true + }) + for (const name of trimmedNames) { + // Case-insensitive find on PostgreSQL const existing = await prisma.label.findFirst({ - where: { userId, name, notebookId: nbScope }, + where: { userId, name: { equals: name, mode: 'insensitive' }, notebookId: nbScope }, }) if (!existing) { await prisma.label.create({ @@ -120,14 +130,16 @@ async function syncLabels(userId: string, noteLabels: string[] = [], notebookId? }) } } + + if (trimmedNames.length === 0) return [] + // Search with original case (case-insensitive on PostgreSQL) + return prisma.label.findMany({ + where: { userId, notebookId: nbScope, name: { in: trimmedNames, mode: 'insensitive' } }, + select: { id: true, name: true }, + }) } - if (noteLabels.length === 0) return [] - const uniqueNames = [...new Set(noteLabels.map(n => n.trim().toLowerCase()).filter(Boolean))] - return prisma.label.findMany({ - where: { userId, notebookId: nbScope, name: { in: uniqueNames } }, - select: { id: true, name: true }, - }) + return [] } catch (error) { console.error('Fatal error in syncLabels:', error) return []