Fixed soft-remove of props and library props
This commit is contained in:
@@ -23,13 +23,7 @@ const restoreProperty = new ValidatedMethod({
|
||||
assertEditPermission(rootCreature, this.userId);
|
||||
|
||||
// Do work
|
||||
restore({
|
||||
_id,
|
||||
collection: CreatureProperties,
|
||||
extraUpdates: {
|
||||
$set: { dirty: true }
|
||||
},
|
||||
});
|
||||
restore(CreatureProperties, property, { $set: { dirty: true } });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ const softRemoveProperty = new ValidatedMethod({
|
||||
assertEditPermission(rootCreature, this.userId);
|
||||
|
||||
// Do work
|
||||
softRemove({ _id, collection: CreatureProperties });
|
||||
softRemove(CreatureProperties, property);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -284,7 +284,7 @@ const softRemoveLibraryNode = new ValidatedMethod({
|
||||
run({ _id }) {
|
||||
let node = LibraryNodes.findOne(_id);
|
||||
assertNodeEditPermission(node, this.userId);
|
||||
softRemove({ _id, collection: LibraryNodes });
|
||||
softRemove(LibraryNodes, node);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -303,7 +303,7 @@ const restoreLibraryNode = new ValidatedMethod({
|
||||
let node = LibraryNodes.findOne(_id);
|
||||
assertNodeEditPermission(node, this.userId);
|
||||
// Do work
|
||||
restore(LibraryNodes, _id);
|
||||
restore(LibraryNodes, node);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import CreatureProperties from '/imports/api/creature/creatureProperties/Creatur
|
||||
import { Mongo } from 'meteor/mongo';
|
||||
|
||||
export function getCollectionByName(name: string): Mongo.Collection<TreeDoc> {
|
||||
const collection = Mongo.Collection.get(name)
|
||||
const collection: Mongo.Collection<TreeDoc> = Mongo.Collection.get(name)
|
||||
if (!collection) {
|
||||
throw new Meteor.Error('bad-collection-reference',
|
||||
`Parent references collection ${name}, which does not exist`
|
||||
|
||||
@@ -1,19 +1,28 @@
|
||||
import { getCollectionByName, getFilter } from '/imports/api/parenting/parentingFunctions';
|
||||
import { TreeDoc } from '/imports/api/parenting/ChildSchema';
|
||||
|
||||
export async function softRemove(collection: Mongo.Collection<TreeDoc> | string, doc?: TreeDoc | string) {
|
||||
export function softRemove(collectionOrName: Mongo.Collection<TreeDoc> | string, docOrId?: TreeDoc | string) {
|
||||
const removalDate = new Date();
|
||||
if (typeof collection === 'string') {
|
||||
collection = getCollectionByName(collection);
|
||||
|
||||
let collection: Mongo.Collection<TreeDoc>;
|
||||
if (typeof collectionOrName === 'string') {
|
||||
collection = getCollectionByName(collectionOrName);
|
||||
} else {
|
||||
collection = collectionOrName;
|
||||
}
|
||||
if (typeof doc === 'string') {
|
||||
doc = await collection.findOneAsync(doc);
|
||||
|
||||
let doc: TreeDoc | undefined;
|
||||
if (typeof docOrId === 'string') {
|
||||
doc = collection.findOne(docOrId);
|
||||
} else {
|
||||
doc = docOrId
|
||||
}
|
||||
if (!doc) {
|
||||
throw new Meteor.Error('not found', 'The document to remove was not found');
|
||||
}
|
||||
|
||||
// Remove this document
|
||||
const removeDocPromise = collection.updateAsync(
|
||||
collection.update(
|
||||
doc._id,
|
||||
{
|
||||
$set: {
|
||||
@@ -23,13 +32,11 @@ export async function softRemove(collection: Mongo.Collection<TreeDoc> | string,
|
||||
$unset: {
|
||||
removedWith: 1,
|
||||
}
|
||||
}, {
|
||||
selector: { type: 'any' },
|
||||
},
|
||||
}
|
||||
);
|
||||
// Remove all the descendants that have not yet been removed, and set them to be
|
||||
// removed with this document
|
||||
const removeDescendantsPromise = collection.updateAsync({
|
||||
collection.update({
|
||||
...getFilter.descendants(doc),
|
||||
removed: { $ne: true },
|
||||
}, {
|
||||
@@ -39,10 +46,8 @@ export async function softRemove(collection: Mongo.Collection<TreeDoc> | string,
|
||||
removedWith: doc._id,
|
||||
}
|
||||
}, {
|
||||
selector: { type: 'any' },
|
||||
multi: true,
|
||||
});
|
||||
return Promise.all([removeDocPromise, removeDescendantsPromise]);
|
||||
}
|
||||
|
||||
const restoreError = function () {
|
||||
@@ -51,18 +56,26 @@ const restoreError = function () {
|
||||
);
|
||||
};
|
||||
|
||||
export async function restore(collection: Mongo.Collection<TreeDoc> | string, doc: TreeDoc | string, extraUpdates?) {
|
||||
if (typeof collection === 'string') {
|
||||
collection = getCollectionByName(collection);
|
||||
export function restore(collectionOrName: Mongo.Collection<TreeDoc> | string, docOrId: TreeDoc | string, extraUpdates?) {
|
||||
|
||||
let collection: Mongo.Collection<TreeDoc>;
|
||||
if (typeof collectionOrName === 'string') {
|
||||
collection = getCollectionByName(collectionOrName);
|
||||
} else {
|
||||
collection = collectionOrName;
|
||||
}
|
||||
if (typeof doc === 'string') {
|
||||
const foundDoc = await collection.findOneAsync(doc)
|
||||
if (!foundDoc) {
|
||||
throw new Meteor.Error('not found', 'The document to remove was not found');
|
||||
}
|
||||
doc = foundDoc;
|
||||
|
||||
let doc: TreeDoc | undefined;
|
||||
if (typeof docOrId === 'string') {
|
||||
doc = collection.findOne(docOrId);
|
||||
} else {
|
||||
doc = docOrId
|
||||
}
|
||||
const numUpdated: number = await collection.updateAsync({
|
||||
if (!doc) {
|
||||
throw new Meteor.Error('not found', 'The document to remove was not found');
|
||||
}
|
||||
|
||||
const numUpdated: number = collection.update({
|
||||
_id: doc._id,
|
||||
removedWith: { $exists: false }
|
||||
}, {
|
||||
@@ -71,11 +84,11 @@ export async function restore(collection: Mongo.Collection<TreeDoc> | string, do
|
||||
removedAt: 1,
|
||||
},
|
||||
...extraUpdates
|
||||
}, {
|
||||
selector: { type: 'any' },
|
||||
});
|
||||
|
||||
if (numUpdated === 0) restoreError();
|
||||
return collection.updateAsync({
|
||||
|
||||
return collection.update({
|
||||
removedWith: doc._id,
|
||||
}, {
|
||||
$unset: {
|
||||
@@ -84,7 +97,6 @@ export async function restore(collection: Mongo.Collection<TreeDoc> | string, do
|
||||
removedWith: 1,
|
||||
}
|
||||
}, {
|
||||
selector: { type: 'any' },
|
||||
multi: true,
|
||||
});
|
||||
}) + 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user