chore: Refactor SearchHelper internals

This commit is contained in:
Tom Moor
2023-01-02 20:14:46 -05:00
parent 435969cf4b
commit 6efcf1c1a8
2 changed files with 20 additions and 23 deletions

View File

@@ -194,6 +194,9 @@ describe("#searchForUser", () => {
test("should search only drafts created by user", async () => { test("should search only drafts created by user", async () => {
const user = await buildUser(); const user = await buildUser();
await buildDraftDocument({
title: "test",
});
await buildDraftDocument({ await buildDraftDocument({
teamId: user.teamId, teamId: user.teamId,
userId: user.id, userId: user.id,

View File

@@ -177,24 +177,23 @@ export default class SearchHelper {
): Promise<Document[]> { ): Promise<Document[]> {
const { limit = 15, offset = 0 } = options; const { limit = 15, offset = 0 } = options;
let where: WhereOptions<Document> = { const where: WhereOptions<Document> = {
teamId: user.teamId,
title: { title: {
[Op.iLike]: `%${query}%`, [Op.iLike]: `%${query}%`,
}, },
[Op.and]: [],
}; };
// Ensure we're filtering by the users accessible collections. If // Ensure we're filtering by the users accessible collections. If
// collectionId is passed as an option it is assumed that the authorization // collectionId is passed as an option it is assumed that the authorization
// has already been done in the router // has already been done in the router
if (options.collectionId) { if (options.collectionId) {
where = { where[Op.and].push({
...where,
collectionId: options.collectionId, collectionId: options.collectionId,
}; });
} else { } else {
// @ts-expect-error doesn't like OR null where[Op.and].push({
where = {
...where,
[Op.or]: [ [Op.or]: [
{ {
collectionId: { collectionId: {
@@ -208,32 +207,29 @@ export default class SearchHelper {
createdById: user.id, createdById: user.id,
}, },
], ],
}; });
} }
if (options.dateFilter) { if (options.dateFilter) {
where = { where[Op.and].push({
...where,
updatedAt: { updatedAt: {
[Op.gt]: sequelize.literal( [Op.gt]: sequelize.literal(
`now() - interval '1 ${options.dateFilter}'` `now() - interval '1 ${options.dateFilter}'`
), ),
}, },
}; });
} }
if (!options.includeArchived) { if (!options.includeArchived) {
where = { where[Op.and].push({
...where,
archivedAt: { archivedAt: {
[Op.is]: null, [Op.is]: null,
}, },
}; });
} }
if (options.includeDrafts) { if (options.includeDrafts) {
where = { where[Op.and].push({
...where,
[Op.or]: [ [Op.or]: [
{ {
publishedAt: { publishedAt: {
@@ -244,23 +240,21 @@ export default class SearchHelper {
createdById: user.id, createdById: user.id,
}, },
], ],
}; });
} else { } else {
where = { where[Op.and].push({
...where,
publishedAt: { publishedAt: {
[Op.ne]: null, [Op.ne]: null,
}, },
}; });
} }
if (options.collaboratorIds) { if (options.collaboratorIds) {
where = { where[Op.and].push({
...where,
collaboratorIds: { collaboratorIds: {
[Op.contains]: options.collaboratorIds, [Op.contains]: options.collaboratorIds,
}, },
}; });
} }
return await Document.scope([ return await Document.scope([