From d9e9824146fa43104f9d9329dcae627e819ebf6c Mon Sep 17 00:00:00 2001 From: Nareshkumar Rao Date: Tue, 22 Apr 2025 12:48:23 +0200 Subject: [PATCH] cleanup tags on edit --- src/app/blog/write/action.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/app/blog/write/action.ts b/src/app/blog/write/action.ts index 2a9ee67..17cb0b7 100644 --- a/src/app/blog/write/action.ts +++ b/src/app/blog/write/action.ts @@ -39,6 +39,21 @@ export async function savePostServer( const slug = slugify(title, { lower: true, strict: true }); if (existingSlug) { + const post = await prisma.post.findUnique({ + where: { slug: existingSlug }, + include: { tags: true }, + }); + if (!post) { + throw new Error("Post not found"); + } + for (const tag of post.tags) { + const postsWithTag = await prisma.post.count({ + where: { tags: { some: { id: tag.id } } }, + }); + if (postsWithTag == 0) { + await prisma.tag.delete({ where: { id: tag.id } }); + } + } await prisma.post.delete({ where: { slug: existingSlug } }); }