'use server' import { revalidatePath } from 'next/cache' import prisma from '@/lib/prisma' import { auth } from '@/auth' import { parseNote } from '@/lib/utils' import { createNoteHistorySnapshot, parseNoteHistoryEntry, } from '@/lib/note-history' export async function getNoteHistory(noteId: string, limit = 30) { const session = await auth() if (!session?.user?.id) return [] const clampedLimit = Math.min(Math.max(limit, 1), 100) const note = await prisma.note.findFirst({ where: { id: noteId, userId: session.user.id }, select: { id: true, historyEnabled: true }, }) if (!note || !note.historyEnabled) return [] const entries = await prisma.noteHistory.findMany({ where: { noteId: note.id, userId: session.user.id }, orderBy: { createdAt: 'desc' }, take: clampedLimit, }) return entries.map(parseNoteHistoryEntry) } export async function restoreNoteVersion(noteId: string, historyEntryId: string) { const session = await auth() if (!session?.user?.id) throw new Error('Unauthorized') const [note, historyEntry] = await Promise.all([ prisma.note.findFirst({ where: { id: noteId, userId: session.user.id }, select: { id: true, notebookId: true, historyEnabled: true }, }), prisma.noteHistory.findFirst({ where: { id: historyEntryId, noteId, userId: session.user.id, }, }), ]) if (!note || !note.historyEnabled) throw new Error('History is disabled for this note') if (!historyEntry) throw new Error('History entry not found') const userId = session.user.id const restored = await prisma.note.update({ where: { id: note.id, userId }, data: { title: historyEntry.title, content: historyEntry.content, color: historyEntry.color, isPinned: historyEntry.isPinned, isArchived: historyEntry.isArchived, type: historyEntry.type, checkItems: historyEntry.checkItems, labels: historyEntry.labels, images: historyEntry.images, links: historyEntry.links, isMarkdown: historyEntry.isMarkdown, size: historyEntry.size, notebookId: historyEntry.notebookId, contentUpdatedAt: new Date(), }, }) revalidatePath('/home') return parseNote(restored) } export async function commitNoteHistory(noteId: string) { const session = await auth() if (!session?.user?.id) throw new Error('Unauthorized') const note = await prisma.note.findFirst({ where: { id: noteId, userId: session.user.id }, select: { id: true, historyEnabled: true }, }) if (!note) throw new Error('Note not found') if (!note.historyEnabled) throw new Error('History is disabled for this note') await createNoteHistorySnapshot({ noteId: note.id, userId: session.user.id, reason: 'manual-commit', }) } export async function deleteNoteHistoryEntry(noteId: string, historyEntryId: string) { const session = await auth() if (!session?.user?.id) throw new Error('Unauthorized') const entry = await prisma.noteHistory.findFirst({ where: { id: historyEntryId, noteId, userId: session.user.id }, }) if (!entry) throw new Error('History entry not found') await prisma.noteHistory.delete({ where: { id: historyEntryId }, }) } export async function enableNoteHistory(noteId: string) { const session = await auth() if (!session?.user?.id) throw new Error('Unauthorized') const note = await prisma.note.findFirst({ where: { id: noteId, userId: session.user.id }, select: { id: true }, }) if (!note) throw new Error('Note not found') await prisma.note.update({ where: { id: noteId }, data: { historyEnabled: true }, }) }