aboutsummaryrefslogtreecommitdiffstats
path: root/packages/trpc
diff options
context:
space:
mode:
authorMohamedBassem <me@mbassem.com>2024-04-26 12:38:02 +0100
committerMohamedBassem <me@mbassem.com>2024-04-26 12:38:02 +0100
commit7d163f2189c6f8080c0a9185cacab52b1b2cd5c0 (patch)
tree3a9a283b57fbb8dc44e5a555a07632dc9972f241 /packages/trpc
parent0b02f94215d6215c1abef503043e612dd4f4f4df (diff)
downloadkarakeep-7d163f2189c6f8080c0a9185cacab52b1b2cd5c0.tar.zst
feature: Allow users to delete all unused tags in one go
Diffstat (limited to 'packages/trpc')
-rw-r--r--packages/trpc/routers/tags.ts26
1 files changed, 24 insertions, 2 deletions
diff --git a/packages/trpc/routers/tags.ts b/packages/trpc/routers/tags.ts
index b95570ae..dc70b068 100644
--- a/packages/trpc/routers/tags.ts
+++ b/packages/trpc/routers/tags.ts
@@ -1,5 +1,5 @@
import { experimental_trpcMiddleware, TRPCError } from "@trpc/server";
-import { and, eq, inArray } from "drizzle-orm";
+import { and, eq, inArray, notExists } from "drizzle-orm";
import { z } from "zod";
import type { ZAttachedByEnum } from "@hoarder/shared/types/tags";
@@ -115,6 +115,28 @@ export const tagsAppRouter = router({
}
// TODO: Update affected bookmarks in search index
}),
+ deleteUnused: authedProcedure
+ .output(
+ z.object({
+ deletedTags: z.number(),
+ }),
+ )
+ .mutation(async ({ ctx }) => {
+ const res = await ctx.db
+ .delete(bookmarkTags)
+ .where(
+ and(
+ eq(bookmarkTags.userId, ctx.user.id),
+ notExists(
+ ctx.db
+ .select({ id: tagsOnBookmarks.tagId })
+ .from(tagsOnBookmarks)
+ .where(eq(tagsOnBookmarks.tagId, bookmarkTags.id)),
+ ),
+ ),
+ );
+ return { deletedTags: res.changes };
+ }),
update: authedProcedure
.input(
z.object({
@@ -261,7 +283,7 @@ export const tagsAppRouter = router({
tagId: input.intoTagId,
})),
)
- .onConflictDoNothing()
+ .onConflictDoNothing();
}
// Delete the old tags