From 96cc11ef321b4580430c05f9344c6eb7dbddcf23 Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Tue, 31 Dec 2024 14:15:02 +0000 Subject: feat: Add support for searching for tagged and listed items --- packages/trpc/lib/__tests__/search.test.ts | 24 +++++++++++++++++++++ packages/trpc/lib/search.ts | 34 ++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) (limited to 'packages/trpc') diff --git a/packages/trpc/lib/__tests__/search.test.ts b/packages/trpc/lib/__tests__/search.test.ts index 31f87dfd..bf32bcb1 100644 --- a/packages/trpc/lib/__tests__/search.test.ts +++ b/packages/trpc/lib/__tests__/search.test.ts @@ -344,6 +344,30 @@ describe("getBookmarkIdsFromMatcher", () => { expect(result).toEqual(["b4"]); }); + it("should handle tagged matcher", async () => { + const matcher: Matcher = { type: "tagged", tagged: true }; + const result = await getBookmarkIdsFromMatcher(mockCtx, matcher); + expect(result.sort()).toEqual(["b1", "b2", "b4", "b5", "b6"]); + }); + + it("should handle tagged matcher with tagged=false", async () => { + const matcher: Matcher = { type: "tagged", tagged: false }; + const result = await getBookmarkIdsFromMatcher(mockCtx, matcher); + expect(result).toEqual(["b3"]); + }); + + it("should handle inlist matcher", async () => { + const matcher: Matcher = { type: "inlist", inList: true }; + const result = await getBookmarkIdsFromMatcher(mockCtx, matcher); + expect(result.sort()).toEqual(["b1", "b2", "b4", "b5", "b6"]); + }); + + it("should handle inlist matcher with inList=false", async () => { + const matcher: Matcher = { type: "inlist", inList: false }; + const result = await getBookmarkIdsFromMatcher(mockCtx, matcher); + expect(result).toEqual(["b3"]); + }); + it("should throw error for unknown matcher type", async () => { const matcher = { type: "unknown" } as unknown as Matcher; await expect(getBookmarkIdsFromMatcher(mockCtx, matcher)).rejects.toThrow( diff --git a/packages/trpc/lib/search.ts b/packages/trpc/lib/search.ts index fcc5abda..e7e6b5f7 100644 --- a/packages/trpc/lib/search.ts +++ b/packages/trpc/lib/search.ts @@ -113,6 +113,23 @@ async function getIds( ), ); } + case "tagged": { + const comp = matcher.tagged ? exists : notExists; + return db + .select({ id: bookmarks.id }) + .from(bookmarks) + .where( + and( + eq(bookmarks.userId, userId), + comp( + db + .select() + .from(tagsOnBookmarks) + .where(and(eq(tagsOnBookmarks.bookmarkId, bookmarks.id))), + ), + ), + ); + } case "listName": { const comp = matcher.inverse ? notExists : exists; return db @@ -140,6 +157,23 @@ async function getIds( ), ); } + case "inlist": { + const comp = matcher.inList ? exists : notExists; + return db + .select({ id: bookmarks.id }) + .from(bookmarks) + .where( + and( + eq(bookmarks.userId, userId), + comp( + db + .select() + .from(bookmarksInLists) + .where(and(eq(bookmarksInLists.bookmarkId, bookmarks.id))), + ), + ), + ); + } case "archived": { return db .select({ id: bookmarks.id }) -- cgit v1.2.3-70-g09d2