Skip to content

Commit

Permalink
Refactor deleteComment and deletePost functions to improve status upd…
Browse files Browse the repository at this point in the history
…ate logic; add checks to prevent redundant deletions and enhance error handling
  • Loading branch information
WillCorrigan committed Dec 22, 2024
1 parent a5a62cf commit 20d5ccb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
24 changes: 17 additions & 7 deletions packages/frontpage/lib/data/db/comment.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -369,27 +377,29 @@ 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({
id: schema.Comment.id,
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,
);
});
Expand Down
31 changes: 26 additions & 5 deletions packages/frontpage/lib/data/db/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 20d5ccb

Please sign in to comment.