aboutsummaryrefslogtreecommitdiffstats
path: root/packages/trpc
diff options
context:
space:
mode:
authorMohamedBassem <me@mbassem.com>2024-04-26 11:59:20 +0100
committerMohamedBassem <me@mbassem.com>2024-04-26 11:59:20 +0100
commit5aabbec42afcabce826fff14a667a2ca3b0b701f (patch)
treed4191e153c977a244f0223baa15d5640e1df06a6 /packages/trpc
parent4efcac7e32b1471078142ece8c7d8db3ebffa443 (diff)
downloadkarakeep-5aabbec42afcabce826fff14a667a2ca3b0b701f.tar.zst
fix: Change tag listing API to return unused tags
Diffstat (limited to 'packages/trpc')
-rw-r--r--packages/trpc/routers/tags.ts59
1 files changed, 28 insertions, 31 deletions
diff --git a/packages/trpc/routers/tags.ts b/packages/trpc/routers/tags.ts
index 2e6efa8e..4806079a 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, count, eq, inArray } from "drizzle-orm";
+import { and, eq, inArray } from "drizzle-orm";
import { z } from "zod";
import type { ZAttachedByEnum } from "@hoarder/shared/types/tags";
@@ -306,36 +306,33 @@ export const tagsAppRouter = router({
}),
)
.query(async ({ ctx }) => {
- const res = await ctx.db
- .select({
- id: tagsOnBookmarks.tagId,
- name: bookmarkTags.name,
- attachedBy: tagsOnBookmarks.attachedBy,
- count: count(),
- })
- .from(tagsOnBookmarks)
- .groupBy(tagsOnBookmarks.tagId, tagsOnBookmarks.attachedBy)
- .innerJoin(bookmarkTags, eq(bookmarkTags.id, tagsOnBookmarks.tagId))
- .where(eq(bookmarkTags.userId, ctx.user.id));
-
- const tags = res.reduce<
- Record<string, z.infer<typeof zGetTagResponseSchema>>
- >((acc, row) => {
- if (!(row.id in acc)) {
- acc[row.id] = {
- id: row.id,
- name: row.name,
- count: 0,
- countAttachedBy: {
- ai: 0,
- human: 0,
+ const tags = await ctx.db.query.bookmarkTags.findMany({
+ where: eq(bookmarkTags.userId, ctx.user.id),
+ with: {
+ tagsOnBookmarks: {
+ columns: {
+ attachedBy: true,
},
- };
- }
- acc[row.id].count += row.count;
- acc[row.id].countAttachedBy[row.attachedBy]! += row.count;
- return acc;
- }, {});
- return { tags: Object.values(tags) };
+ },
+ },
+ });
+
+ const resp = tags.map(({ tagsOnBookmarks, ...rest }) => ({
+ ...rest,
+ count: tagsOnBookmarks.length,
+ countAttachedBy: tagsOnBookmarks.reduce<
+ Record<ZAttachedByEnum, number>
+ >(
+ (acc, curr) => {
+ if (curr.attachedBy) {
+ acc[curr.attachedBy]++;
+ }
+ return acc;
+ },
+ { ai: 0, human: 0 },
+ ),
+ }));
+
+ return { tags: resp };
}),
});