From 0e1ff2b415d49511ab322924ddbe8bf3710a3fc6 Mon Sep 17 00:00:00 2001 From: misterkirill Date: Wed, 16 Jul 2025 19:36:32 +0500 Subject: [PATCH] feat: add lastEdited field update --- src/app/actions/blocks.ts | 164 +++++++++++++++++++++++++++----------- src/app/actions/notes.ts | 11 ++- 2 files changed, 124 insertions(+), 51 deletions(-) diff --git a/src/app/actions/blocks.ts b/src/app/actions/blocks.ts index cc37661..d88d371 100644 --- a/src/app/actions/blocks.ts +++ b/src/app/actions/blocks.ts @@ -3,7 +3,7 @@ import { revalidatePath } from "next/cache"; import { eq } from "drizzle-orm"; import { db } from "@/lib/db"; -import { blocksTable, IBlock } from "@/lib/db/schema"; +import { blocksTable, IBlock, notesTable } from "@/lib/db/schema"; import { requireAuth } from "./auth"; import { assertNoteOwner } from "./notes"; @@ -14,24 +14,31 @@ export async function assertBlockOwner(blockId: string): Promise { where: eq(blocksTable.id, blockId), with: { note: true }, }); - if (!block) { - return false; - } + if (!block) return false; return block.note.authorId === user.id; } export async function createBlock(formData: FormData) { const noteId = formData.get("noteId") as string; + const isAllowed = await assertNoteOwner(noteId); if (!isAllowed) return; const blocks = await getBlocks(noteId); const lastBlock = blocks.pop(); const order = lastBlock === undefined ? 1 : lastBlock.order + 1; - await db - .insert(blocksTable) - .values({ noteId, order }); + + await db.transaction(async (tx) => { + await tx + .insert(blocksTable) + .values({ noteId, order }); + await tx + .update(notesTable) + .set({ lastEdited: new Date() }) + .where(eq(notesTable.id, noteId)); + }); + revalidatePath("/notes/[id]", "page"); } @@ -43,11 +50,8 @@ async function getBlock(blockId: string): Promise { .select() .from(blocksTable) .where(eq(blocksTable.id, blockId)); - if (blocks.length === 0) { - return null; - } else { - return blocks[0]; - } + + return blocks.length === 0 ? null : blocks[0]; } export async function getBlocks(noteId: string): Promise { @@ -62,57 +66,93 @@ export async function getBlocks(noteId: string): Promise { export async function deleteBlock(formData: FormData) { const blockId = formData.get("blockId") as string; + const isAllowed = await assertBlockOwner(blockId); if (!isAllowed) return; - await db - .delete(blocksTable) - .where(eq(blocksTable.id, blockId)); + + const block = await getBlock(blockId); + if (!block) return; + + await db.transaction(async (tx) => { + await tx + .delete(blocksTable) + .where(eq(blocksTable.id, blockId)); + await tx + .update(notesTable) + .set({ lastEdited: new Date() }) + .where(eq(notesTable.id, block.noteId)); + }); + revalidatePath("/notes/[id]", "page"); } export async function changeLock(formData: FormData) { const blockId = formData.get("blockId") as string; const isLocked = formData.get("isLocked") === null ? false : true; + const isAllowed = await assertBlockOwner(blockId); if (!isAllowed) return; - await db - .update(blocksTable) - .set({ isLocked }) - .where(eq(blocksTable.id, blockId)); + + const block = await getBlock(blockId); + if (!block) return; + + await db.transaction(async (tx) => { + await tx + .update(blocksTable) + .set({ isLocked }) + .where(eq(blocksTable.id, blockId)); + await tx + .update(notesTable) + .set({ lastEdited: new Date() }) + .where(eq(notesTable.id, block.noteId)); + }); + revalidatePath("/notes/[id]", "page"); } export async function addLine(formData: FormData) { const blockId = formData.get("blockId") as string; + const isAllowed = await assertBlockOwner(blockId); if (!isAllowed) return; - const blocks = await db - .select() - .from(blocksTable) - .where(eq(blocksTable.id, blockId)); - if (blocks.length === 0) { - return; - } - const block = blocks[0]; - await db - .update(blocksTable) - .set({ lines: [...block.lines, ""] }) - .where(eq(blocksTable.id, blockId)); + + const block = await getBlock(blockId); + if (!block) return; + + await db.transaction(async (tx) => { + await tx + .update(blocksTable) + .set({ lines: [...block.lines, ""] }) + .where(eq(blocksTable.id, blockId)); + await tx + .update(notesTable) + .set({ lastEdited: new Date() }) + .where(eq(notesTable.id, block.noteId)); + }); + revalidatePath("/notes/[id]", "page"); } export async function deleteLine(formData: FormData) { const blockId = formData.get("blockId") as string; - const block = await getBlock(blockId); + const isAllowed = await assertBlockOwner(blockId); if (!isAllowed) return; - if (!block) { - return; - } - await db - .update(blocksTable) - .set({ lines: block.lines.slice(0, -1) }) - .where(eq(blocksTable.id, blockId)); + + const block = await getBlock(blockId); + if (!block) return; + + await db.transaction(async (tx) => { + await tx + .update(blocksTable) + .set({ lines: block.lines.slice(0, -1) }) + .where(eq(blocksTable.id, blockId)); + await tx + .update(notesTable) + .set({ lastEdited: new Date() }) + .where(eq(notesTable.id, block.noteId)); + }); + revalidatePath("/notes/[id]", "page"); } @@ -120,6 +160,7 @@ export async function moveUp(formData: FormData) { const blockId = formData.get("blockId") as string; const isAllowed = await assertBlockOwner(blockId); if (!isAllowed) return; + const block = await getBlock(blockId); if (!block) return; @@ -139,6 +180,10 @@ export async function moveUp(formData: FormData) { .update(blocksTable) .set({ order: block.order }) .where(eq(blocksTable.id, prevBlock.id)); + await tx + .update(notesTable) + .set({ lastEdited: new Date() }) + .where(eq(notesTable.id, block.noteId)); }); revalidatePath("/notes/[id]", "page"); @@ -148,6 +193,7 @@ export async function moveDown(formData: FormData) { const blockId = formData.get("blockId") as string; const isAllowed = await assertBlockOwner(blockId); if (!isAllowed) return; + const block = await getBlock(blockId); if (!block) return; @@ -167,6 +213,10 @@ export async function moveDown(formData: FormData) { .update(blocksTable) .set({ order: block.order }) .where(eq(blocksTable.id, nextBlock.id)); + await tx + .update(notesTable) + .set({ lastEdited: new Date() }) + .where(eq(notesTable.id, block.noteId)); }); revalidatePath("/notes/[id]", "page"); @@ -175,17 +225,37 @@ export async function moveDown(formData: FormData) { export async function setLines(blockId: string, lines: string[]) { const isAllowed = await assertBlockOwner(blockId); if (!isAllowed) return; - await db - .update(blocksTable) - .set({ lines }) - .where(eq(blocksTable.id, blockId)); + + const block = await getBlock(blockId); + if (!block) return; + + await db.transaction(async (tx) => { + await tx + .update(blocksTable) + .set({ lines }) + .where(eq(blocksTable.id, blockId)); + await tx + .update(notesTable) + .set({ lastEdited: new Date() }) + .where(eq(notesTable.id, block.noteId)); + }); } export async function setTag(blockId: string, tag: string) { const isAllowed = await assertBlockOwner(blockId); if (!isAllowed) return; - await db - .update(blocksTable) - .set({ tag }) - .where(eq(blocksTable.id, blockId)); + + const block = await getBlock(blockId); + if (!block) return; + + await db.transaction(async (tx) => { + await tx + .update(blocksTable) + .set({ tag }) + .where(eq(blocksTable.id, blockId)); + await tx + .update(notesTable) + .set({ lastEdited: new Date() }) + .where(eq(notesTable.id, block.noteId)); + }); } diff --git a/src/app/actions/notes.ts b/src/app/actions/notes.ts index 77eed6e..97880a0 100644 --- a/src/app/actions/notes.ts +++ b/src/app/actions/notes.ts @@ -13,9 +13,7 @@ export async function assertNoteOwner(noteId: string): Promise { const note = await db.query.notesTable.findFirst({ where: eq(notesTable.id, noteId), }); - if (!note) { - return false; - } + if (!note) return false; return note.authorId === user.id; } @@ -61,9 +59,14 @@ export async function deleteNote(formData: FormData) { export async function setTitle(noteId: string, title: string) { if (title === "") return; + + const isAllowed = await assertNoteOwner(noteId); + if (!isAllowed) return; + await db .update(notesTable) - .set({ title }) + .set({ title, lastEdited: new Date() }) .where(eq(notesTable.id, noteId)); + revalidatePath("/notes/[id]", "page"); }