perf: Requesting less db columns when calculating collection permissions (#3498)

perf: Not looping collection documentStructure for unpublish permission calculation
This commit is contained in:
Tom Moor
2022-05-15 06:46:24 -07:00
committed by GitHub
parent 36a3ae4b01
commit 8815a58ff5
5 changed files with 43 additions and 30 deletions

View File

@@ -1,6 +1,5 @@
import invariant from "invariant";
import { Document, Revision, User, Team } from "@server/models";
import { NavigationNode } from "~/types";
import { allow, _cannot as cannot } from "./cancan";
allow(User, "createDocument", Team, (user, team) => {
@@ -327,18 +326,5 @@ allow(User, "unpublish", Document, (user, document) => {
if (cannot(user, "update", document.collection)) {
return false;
}
const documentID = document.id;
const hasChild = (documents: NavigationNode[]): boolean =>
documents.some((doc) => {
if (doc.id === documentID) {
return doc.children.length > 0;
}
return hasChild(doc.children);
});
return (
!hasChild(document.collection.documentStructure || []) &&
user.teamId === document.teamId
);
return user.teamId === document.teamId;
});