From 20d5ccb5d26f1ae18f1c1503b438e44714f205f2 Mon Sep 17 00:00:00 2001 From: WillCorrigan Date: Sun, 22 Dec 2024 15:31:51 +0000 Subject: [PATCH] Refactor deleteComment and deletePost functions to improve status update logic; add checks to prevent redundant deletions and enhance error handling --- packages/frontpage/lib/data/db/comment.ts | 24 +++++++++++++----- packages/frontpage/lib/data/db/post.ts | 31 +++++++++++++++++++---- 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/packages/frontpage/lib/data/db/comment.ts b/packages/frontpage/lib/data/db/comment.ts index 7a91258..962266c 100644 --- a/packages/frontpage/lib/data/db/comment.ts +++ b/packages/frontpage/lib/data/db/comment.ts @@ -1,7 +1,15 @@ import "server-only"; import { cache } from "react"; import { db } from "@/lib/db"; -import { eq, sql, desc, and, InferSelectModel, isNotNull } from "drizzle-orm"; +import { + eq, + sql, + desc, + and, + InferSelectModel, + isNotNull, + ne, +} from "drizzle-orm"; import * as schema from "@/lib/schema"; import { getUser, isAdmin } from "../user"; import { DID } from "../atproto/did"; @@ -369,13 +377,14 @@ export type DeleteCommentInput = { export async function deleteComment({ rkey, authorDid }: DeleteCommentInput) { await db.transaction(async (tx) => { - const [deletedComment] = await tx + const [updatedComment] = await tx .update(schema.Comment) .set({ status: "deleted" }) .where( and( eq(schema.Comment.rkey, rkey), eq(schema.Comment.authorDid, authorDid), + ne(schema.Comment.status, "deleted"), ), ) .returning({ @@ -383,13 +392,14 @@ export async function deleteComment({ rkey, authorDid }: DeleteCommentInput) { postId: schema.Comment.postId, }); - if (!deletedComment) { - throw new Error("Failed to delete comment"); - } + invariant( + updatedComment, + "Failed to update comment status to deleted or comment not found", + ); await deleteCommentAggregateTrigger( - deletedComment.postId, - deletedComment.id, + updatedComment.postId, + updatedComment.id, tx, ); }); diff --git a/packages/frontpage/lib/data/db/post.ts b/packages/frontpage/lib/data/db/post.ts index 4822933..c20a9b2 100644 --- a/packages/frontpage/lib/data/db/post.ts +++ b/packages/frontpage/lib/data/db/post.ts @@ -2,11 +2,21 @@ import "server-only"; import { cache } from "react"; import { db } from "@/lib/db"; -import { eq, sql, desc, and, isNull, or, InferSelectModel } from "drizzle-orm"; +import { + eq, + sql, + desc, + and, + isNull, + or, + InferSelectModel, + ne, +} from "drizzle-orm"; import * as schema from "@/lib/schema"; import { getUser, isAdmin } from "../user"; import { DID } from "../atproto/did"; import { newPostAggregateTrigger } from "./triggers"; +import { invariant } from "@/lib/utils"; const buildUserHasVotedQuery = cache(async () => { const user = await getUser(); @@ -226,14 +236,25 @@ export async function deletePost({ authorDid, rkey }: DeletePostInput) { console.log("Deleting post", rkey); await db.transaction(async (tx) => { console.log("Updating post status to deleted", rkey); - await tx + const [updatedPost] = await tx .update(schema.Post) .set({ status: "deleted" }) .where( - and(eq(schema.Post.rkey, rkey), eq(schema.Post.authorDid, authorDid)), - ); + and( + eq(schema.Post.rkey, rkey), + eq(schema.Post.authorDid, authorDid), + ne(schema.Post.status, "deleted"), + ), + ) + .returning({ id: schema.Post.id }); + + invariant( + updatedPost, + "Failed to update post status to deleted or post not found", + ); + + console.log("Done deleting post transaction"); }); - console.log("Done deleting post transaction"); } type ModeratePostInput = {