diff --git a/server/models/helpers/SearchHelper.test.ts b/server/models/helpers/SearchHelper.test.ts index cdb878ba09..8627dd37c4 100644 --- a/server/models/helpers/SearchHelper.test.ts +++ b/server/models/helpers/SearchHelper.test.ts @@ -295,6 +295,54 @@ describe("SearchHelper", () => { ); }); + it("should not return documents from other collections when filtering by specific collection without search term", async () => { + const team = await buildTeam(); + const user = await buildUser({ teamId: team.id }); + const collection1 = await buildCollection({ + teamId: team.id, + userId: user.id, + }); + const collection2 = await buildCollection({ + teamId: team.id, + userId: user.id, + }); + const docsInCollection1 = await Promise.all([ + buildDocument({ + teamId: team.id, + userId: user.id, + collectionId: collection1.id, + title: "document 1 in collection 1", + }), + buildDocument({ + teamId: team.id, + userId: user.id, + collectionId: collection1.id, + title: "document 2 in collection 1", + }), + ]); + await Promise.all([ + buildDocument({ + teamId: team.id, + userId: user.id, + collectionId: collection2.id, + title: "document 1 in collection 2", + }), + buildDocument({ + teamId: team.id, + userId: user.id, + collectionId: collection2.id, + title: "document 2 in collection 2", + }), + ]); + const { results } = await SearchHelper.searchForUser(user, { + collectionId: collection1.id, + }); + expect(results.length).toBe(2); + expect(results.map((r) => r.document.id).sort()).toEqual( + docsInCollection1.map((doc) => doc.id).sort() + ); + }); + it("should handle no collections", async () => { const team = await buildTeam(); const user = await buildUser({ teamId: team.id }); diff --git a/server/models/helpers/SearchHelper.ts b/server/models/helpers/SearchHelper.ts index fb4d4d6b40..766e1d0436 100644 --- a/server/models/helpers/SearchHelper.ts +++ b/server/models/helpers/SearchHelper.ts @@ -574,6 +574,9 @@ export default class SearchHelper { ? [options.collectionId] : await model.collectionIds(); + if (options.collectionId) { + where[Op.and].push({ collectionId: options.collectionId }); + } if (collectionIds.length) { where[Op.or].push({ collectionId: collectionIds }); } diff --git a/server/routes/api/documents/documents.test.ts b/server/routes/api/documents/documents.test.ts index bf62ab1d59..b3e1d5ebd3 100644 --- a/server/routes/api/documents/documents.test.ts +++ b/server/routes/api/documents/documents.test.ts @@ -1933,6 +1933,58 @@ describe("#documents.search", () => { expect(body.data[0].document.id).toEqual(document.id); expect(res.status).toEqual(200); }); + + it("should not return documents from other collections when filtering by specific collection without search term", async () => { + const user = await buildUser(); + const collection1 = await buildCollection({ + teamId: user.teamId, + userId: user.id, + }); + const collection2 = await buildCollection({ + teamId: user.teamId, + userId: user.id, + }); + const docsInCollection1 = await Promise.all([ + buildDocument({ + teamId: user.teamId, + userId: user.id, + collectionId: collection1.id, + title: "document 1 in collection 1", + }), + buildDocument({ + teamId: user.teamId, + userId: user.id, + collectionId: collection1.id, + title: "document 2 in collection 1", + }), + ]); + await Promise.all([ + buildDocument({ + teamId: user.teamId, + userId: user.id, + collectionId: collection2.id, + title: "document 1 in collection 2", + }), + buildDocument({ + teamId: user.teamId, + userId: user.id, + collectionId: collection2.id, + title: "document 2 in collection 2", + }), + ]); + const res = await server.post("/api/documents.search", { + body: { + token: user.getJwtToken(), + collectionId: collection1.id, + }, + }); + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.data).toHaveLength(2); + const returnedIds = body.data.map((d: any) => d.document.id).sort(); + const expectedIds = docsInCollection1.map((d) => d.id).sort(); + expect(returnedIds).toEqual(expectedIds); + }); }); describe("#documents.templatize", () => {