Refactor withMembershipScope (#9134)

This commit is contained in:
Tom Moor
2025-05-04 18:37:01 -04:00
committed by GitHub
parent 9c4b4f4989
commit 3f8bdf7ac2
4 changed files with 13 additions and 15 deletions
+3 -7
View File
@@ -634,21 +634,17 @@ class Document extends ArchivableModel<
static withMembershipScope(
userId: string,
scopeOrOptions?: string[] | FindOptions<Document>,
opts?: FindOptions<Document>
options?: FindOptions<Document> & { includeDrafts?: boolean }
) {
const scopes = Array.isArray(scopeOrOptions) ? scopeOrOptions : [];
const options = Array.isArray(scopeOrOptions) ? opts : scopeOrOptions;
return this.scope([
"defaultScope",
options?.includeDrafts ? "withDrafts" : "defaultScope",
"withoutState",
{
method: ["withViews", userId],
},
{
method: ["withMembership", userId, options?.paranoid],
},
...scopes,
]);
}
+4 -2
View File
@@ -182,7 +182,9 @@ export default class SearchHelper {
},
];
return Document.withMembershipScope(user.id, ["withDrafts"]).findAll({
return Document.withMembershipScope(user.id, {
includeDrafts: true,
}).findAll({
where,
subQuery: false,
order: [["updatedAt", "DESC"]],
@@ -262,7 +264,7 @@ export default class SearchHelper {
// Final query to get associated document data
const [documents, count] = await Promise.all([
Document.withMembershipScope(user.id, ["withDrafts"]).findAll({
Document.withMembershipScope(user.id, { includeDrafts: true }).findAll({
where: {
teamId: user.teamId,
id: map(results, "id"),
+3 -3
View File
@@ -535,9 +535,9 @@ router.post(
delete where.updatedAt;
}
const documents = await Document.withMembershipScope(user.id, [
"withDrafts",
]).findAll({
const documents = await Document.withMembershipScope(user.id, {
includeDrafts: true,
}).findAll({
where,
order: [[sort, direction]],
offset: ctx.state.pagination.offset,
@@ -58,9 +58,9 @@ router.post(
const documentIds = memberships
.map((p) => p.documentId)
.filter(Boolean) as string[];
const documents = await Document.withMembershipScope(userId, [
"withDrafts",
]).findAll({
const documents = await Document.withMembershipScope(userId, {
includeDrafts: true,
}).findAll({
where: {
id: documentIds,
},