From 1823bc3e0e458843e7884b9024b522b382cb43d1 Mon Sep 17 00:00:00 2001 From: Bakhtiyor Ganijon Date: Mon, 27 Nov 2023 16:42:53 +0900 Subject: [PATCH] Fix post deletion and user history retrieval --- app/api/post/[postid]/route.ts | 37 ++++++++++++++++++++------ components/editor/post-editor-form.tsx | 2 +- components/tags/post-card-v2.tsx | 2 +- lib/prisma/feed.ts | 2 +- lib/prisma/session.ts | 14 +++++++--- 5 files changed, 42 insertions(+), 15 deletions(-) diff --git a/app/api/post/[postid]/route.ts b/app/api/post/[postid]/route.ts index b11622b..d1de058 100644 --- a/app/api/post/[postid]/route.ts +++ b/app/api/post/[postid]/route.ts @@ -92,50 +92,71 @@ export async function DELETE( ) { try { const { postid } = params; + + const post = await postgres.post.findUnique({ + where: { + id: postid, + }, + }); + + if (!post) { + throw new Error('Post not found'); + } - if (!(await verifyCurrentUserHasAccessToPost(postid))) { + const authorId = post.authorId; + + if (!(await verifyCurrentUserHasAccessToPost(post.id))) { return new Response(null, { status: 403 }); } + + // Disconnect all connections of the post await postgres.postTag.deleteMany({ where: { - postId: postid, + postId: post.id, }, }) await postgres.comment.deleteMany({ where: { - postId: postid, + postId: post.id, }, }) await postgres.like.deleteMany({ where: { - postId: postid, + postId: post.id, }, }) await postgres.bookmark.deleteMany({ where: { - postId: postid, + postId: post.id, }, }) - await postgres.readingHistory.deleteMany({ where: { postId: postid, + userId: authorId, + }, + }); + + await postgres.readingHistory.deleteMany({ + where: { + postId: post.id, }, }) + await postgres.draftPost.deleteMany({ where: { - postId: postid, + postId: post.id, }, }) // Delete the post. await postgres.post.delete({ where: { - id: postid, + id: post.id, }, }) diff --git a/components/editor/post-editor-form.tsx b/components/editor/post-editor-form.tsx index 6acdbf8..099a71a 100644 --- a/components/editor/post-editor-form.tsx +++ b/components/editor/post-editor-form.tsx @@ -616,7 +616,7 @@ export function PostEditorForm(props: { post: any, user: any }) { - {props.user?.name.charAt(0)} + {props.user?.name ? props.user?.name.charAt(0) : props.user?.username.charAt(0)}