From 6efcf1c1a8d82639ff45772d73475e748ceb0454 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 2 Jan 2023 20:14:46 -0500 Subject: [PATCH] chore: Refactor SearchHelper internals --- server/models/helpers/SearchHelper.test.ts | 3 ++ server/models/helpers/SearchHelper.ts | 40 +++++++++------------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/server/models/helpers/SearchHelper.test.ts b/server/models/helpers/SearchHelper.test.ts index f72cb5e01..8a4ba082b 100644 --- a/server/models/helpers/SearchHelper.test.ts +++ b/server/models/helpers/SearchHelper.test.ts @@ -194,6 +194,9 @@ describe("#searchForUser", () => { test("should search only drafts created by user", async () => { const user = await buildUser(); + await buildDraftDocument({ + title: "test", + }); await buildDraftDocument({ teamId: user.teamId, userId: user.id, diff --git a/server/models/helpers/SearchHelper.ts b/server/models/helpers/SearchHelper.ts index ba19a6021..18c62c312 100644 --- a/server/models/helpers/SearchHelper.ts +++ b/server/models/helpers/SearchHelper.ts @@ -177,24 +177,23 @@ export default class SearchHelper { ): Promise { const { limit = 15, offset = 0 } = options; - let where: WhereOptions = { + const where: WhereOptions = { + teamId: user.teamId, title: { [Op.iLike]: `%${query}%`, }, + [Op.and]: [], }; // Ensure we're filtering by the users accessible collections. If // collectionId is passed as an option it is assumed that the authorization // has already been done in the router if (options.collectionId) { - where = { - ...where, + where[Op.and].push({ collectionId: options.collectionId, - }; + }); } else { - // @ts-expect-error doesn't like OR null - where = { - ...where, + where[Op.and].push({ [Op.or]: [ { collectionId: { @@ -208,32 +207,29 @@ export default class SearchHelper { createdById: user.id, }, ], - }; + }); } if (options.dateFilter) { - where = { - ...where, + where[Op.and].push({ updatedAt: { [Op.gt]: sequelize.literal( `now() - interval '1 ${options.dateFilter}'` ), }, - }; + }); } if (!options.includeArchived) { - where = { - ...where, + where[Op.and].push({ archivedAt: { [Op.is]: null, }, - }; + }); } if (options.includeDrafts) { - where = { - ...where, + where[Op.and].push({ [Op.or]: [ { publishedAt: { @@ -244,23 +240,21 @@ export default class SearchHelper { createdById: user.id, }, ], - }; + }); } else { - where = { - ...where, + where[Op.and].push({ publishedAt: { [Op.ne]: null, }, - }; + }); } if (options.collaboratorIds) { - where = { - ...where, + where[Op.and].push({ collaboratorIds: { [Op.contains]: options.collaboratorIds, }, - }; + }); } return await Document.scope([