improv: use statusFilter instead of includeArchive,includeDrafts for document search (#6537)
* improv: use statusFilter instead of includeArchive,includeDrafts for document search * improv: update FilterComponent to add support for multiple selected items * feat: update document type search ui * fix test * Restore support for old parameters to avoid breaking change --------- Co-authored-by: Tom Moor <tom.moor@gmail.com>
This commit is contained in:
@@ -4,7 +4,7 @@ import find from "lodash/find";
|
||||
import map from "lodash/map";
|
||||
import queryParser from "pg-tsquery";
|
||||
import { Op, Sequelize, WhereOptions } from "sequelize";
|
||||
import { DateFilter } from "@shared/types";
|
||||
import { DateFilter, StatusFilter } from "@shared/types";
|
||||
import Collection from "@server/models/Collection";
|
||||
import Document from "@server/models/Document";
|
||||
import Share from "@server/models/Share";
|
||||
@@ -36,12 +36,10 @@ type SearchOptions = {
|
||||
share?: Share;
|
||||
/** Limit results to a date range. */
|
||||
dateFilter?: DateFilter;
|
||||
/** Status of the documents to return */
|
||||
statusFilter?: StatusFilter[];
|
||||
/** Limit results to a list of users that collaborated on the document. */
|
||||
collaboratorIds?: string[];
|
||||
/** Include archived documents in the results */
|
||||
includeArchived?: boolean;
|
||||
/** Include draft documents in the results (will only ever return your own) */
|
||||
includeDrafts?: boolean;
|
||||
/** The minimum number of words to be returned in the contextual snippet */
|
||||
snippetMinWords?: number;
|
||||
/** The maximum number of words to be returned in the contextual snippet */
|
||||
@@ -356,36 +354,59 @@ export default class SearchHelper {
|
||||
});
|
||||
}
|
||||
|
||||
if (!options.includeArchived) {
|
||||
where[Op.and].push({
|
||||
archivedAt: {
|
||||
[Op.eq]: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (options.includeDrafts && model instanceof User) {
|
||||
where[Op.and].push({
|
||||
[Op.or]: [
|
||||
const statusQuery = [];
|
||||
if (options.statusFilter?.includes(StatusFilter.Published)) {
|
||||
statusQuery.push({
|
||||
[Op.and]: [
|
||||
{
|
||||
publishedAt: {
|
||||
[Op.ne]: null,
|
||||
},
|
||||
archivedAt: {
|
||||
[Op.eq]: null,
|
||||
},
|
||||
},
|
||||
{
|
||||
createdById: model.id,
|
||||
},
|
||||
{ "$memberships.id$": { [Op.ne]: null } },
|
||||
],
|
||||
});
|
||||
} else {
|
||||
where[Op.and].push({
|
||||
publishedAt: {
|
||||
}
|
||||
|
||||
if (
|
||||
options.statusFilter?.includes(StatusFilter.Draft) &&
|
||||
// Only ever include draft results for the user's own documents
|
||||
model instanceof User
|
||||
) {
|
||||
statusQuery.push({
|
||||
[Op.and]: [
|
||||
{
|
||||
publishedAt: {
|
||||
[Op.eq]: null,
|
||||
},
|
||||
archivedAt: {
|
||||
[Op.eq]: null,
|
||||
},
|
||||
[Op.or]: [
|
||||
{ createdById: model.id },
|
||||
{ "$memberships.id$": { [Op.ne]: null } },
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
if (options.statusFilter?.includes(StatusFilter.Archived)) {
|
||||
statusQuery.push({
|
||||
archivedAt: {
|
||||
[Op.ne]: null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (options.statusFilter?.length) {
|
||||
where[Op.and].push({
|
||||
[Op.or]: statusQuery,
|
||||
});
|
||||
}
|
||||
|
||||
return where;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user