From 5aabbec42afcabce826fff14a667a2ca3b0b701f Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Fri, 26 Apr 2024 11:59:20 +0100 Subject: fix: Change tag listing API to return unused tags --- packages/trpc/routers/tags.ts | 59 ++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 31 deletions(-) (limited to 'packages/trpc') 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> - >((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 + >( + (acc, curr) => { + if (curr.attachedBy) { + acc[curr.attachedBy]++; + } + return acc; + }, + { ai: 0, human: 0 }, + ), + })); + + return { tags: resp }; }), }); -- cgit v1.2.3-70-g09d2