Began migration of queries to nested sets
This commit is contained in:
@@ -10,8 +10,8 @@ import {
|
||||
assertCopyPermission
|
||||
} from '/imports/api/sharing/sharingPermissions';
|
||||
import {
|
||||
setLineageOfDocs,
|
||||
getAncestry,
|
||||
fetchDocByRef,
|
||||
getFilter,
|
||||
renewDocIds
|
||||
} from '/imports/api/parenting/parentingFunctions';
|
||||
import { rebuildNestedSets } from '/imports/api/parenting/parentingFunctions';
|
||||
@@ -40,20 +40,20 @@ const copyPropertyToLibrary = new ValidatedMethod({
|
||||
},
|
||||
run({ propId, parentRef, order }) {
|
||||
// get the new ancestry for the properties
|
||||
let { parentDoc, ancestors } = getAncestry({ parentRef });
|
||||
const parentDoc = fetchDocByRef(parentRef);
|
||||
|
||||
// Check permission to edit the destination
|
||||
let rootLibrary;
|
||||
if (parentRef.collection === 'libraries') {
|
||||
rootLibrary = parentDoc;
|
||||
} else if (parentRef.collection === 'libraryNodes') {
|
||||
rootLibrary = Libraries.findOne(parentDoc.ancestors[0].id)
|
||||
rootLibrary = Libraries.findOne(parentDoc.root.id)
|
||||
} else {
|
||||
throw `${parentRef.collection} is not a valid parent collection`
|
||||
}
|
||||
assertEditPermission(rootLibrary, this.userId);
|
||||
|
||||
const insertedRootNode = insertNodeFromProperty(propId, ancestors, order, this);
|
||||
const insertedRootNode = insertNodeFromProperty(propId, order, this);
|
||||
|
||||
// Tree structure changed by inserts, reorder the tree
|
||||
rebuildNestedSets(LibraryNodes, rootLibrary._id);
|
||||
@@ -63,7 +63,7 @@ const copyPropertyToLibrary = new ValidatedMethod({
|
||||
},
|
||||
});
|
||||
|
||||
function insertNodeFromProperty(propId, ancestors, order, method) {
|
||||
function insertNodeFromProperty(propId, order, method) {
|
||||
// Fetch the property and its descendants, provided they have not been
|
||||
// removed
|
||||
let prop = CreatureProperties.findOne({
|
||||
@@ -83,9 +83,9 @@ function insertNodeFromProperty(propId, ancestors, order, method) {
|
||||
// Make sure we can edit this property
|
||||
assertDocEditPermission(prop, method.userId);
|
||||
|
||||
let oldParent = prop.parent;
|
||||
let oldParentId = prop.parentId;
|
||||
const propCursor = CreatureProperties.find({
|
||||
'ancestors.id': propId,
|
||||
...getFilter.descendants(prop),
|
||||
removed: { $ne: true },
|
||||
});
|
||||
|
||||
@@ -105,13 +105,6 @@ function insertNodeFromProperty(propId, ancestors, order, method) {
|
||||
// properties
|
||||
assertSourceLibraryCopyPermission(props, method);
|
||||
|
||||
// re-map all the ancestors
|
||||
setLineageOfDocs({
|
||||
docArray: props,
|
||||
newAncestry: ancestors,
|
||||
oldParent,
|
||||
});
|
||||
|
||||
// Give the docs new IDs without breaking internal references
|
||||
renewDocIds({
|
||||
docArray: props,
|
||||
@@ -119,11 +112,8 @@ function insertNodeFromProperty(propId, ancestors, order, method) {
|
||||
});
|
||||
|
||||
// Order the root node
|
||||
if (order === undefined) {
|
||||
rebuildNestedSets(LibraryNodes, prop.root.id);
|
||||
} else {
|
||||
prop.order = order;
|
||||
}
|
||||
prop.left = Number.MAX_SAFE_INTEGER - 1;
|
||||
prop.right = Number.MAX_SAFE_INTEGER;
|
||||
|
||||
// Clean the props
|
||||
props = cleanProps(props);
|
||||
@@ -135,8 +125,8 @@ function insertNodeFromProperty(propId, ancestors, order, method) {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {[Property]} props The properties to check
|
||||
* @param {String} userId The userId trying to copy these properties to a library
|
||||
* @param props The properties to check
|
||||
* @param userId The userId trying to copy these properties to a library
|
||||
* Checks that every property can be copied out of the library that originated it by this user
|
||||
*/
|
||||
function assertSourceLibraryCopyPermission(props, method) {
|
||||
@@ -155,9 +145,9 @@ function assertSourceLibraryCopyPermission(props, method) {
|
||||
LibraryNodes.find({
|
||||
_id: { $in: libraryNodeIds }
|
||||
}, {
|
||||
fields: { ancestors: 1 }
|
||||
fields: { root: 1 }
|
||||
}).forEach(node => {
|
||||
sourceLibIds.add(node.ancestors?.[0]?.id);
|
||||
sourceLibIds.add(node.root.id);
|
||||
});
|
||||
|
||||
// Assert copy permission on each of those libraries
|
||||
|
||||
Reference in New Issue
Block a user