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 () => {
const user = await buildUser();
await buildDraftDocument({
title: "test",
});
await buildDraftDocument({
teamId: user.teamId,
userId: user.id,

View File

@@ -177,24 +177,23 @@ export default class SearchHelper {
): Promise<Document[]> {
const { limit = 15, offset = 0 } = options;
let where: WhereOptions<Document> = {
const where: WhereOptions<Document> = {
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([