diff --git a/app/imports/api/creature/creatureProperties/methods/restoreProperty.js b/app/imports/api/creature/creatureProperties/methods/restoreProperty.js index 37713d56..05e0a316 100644 --- a/app/imports/api/creature/creatureProperties/methods/restoreProperty.js +++ b/app/imports/api/creature/creatureProperties/methods/restoreProperty.js @@ -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 } }); } }); diff --git a/app/imports/api/creature/creatureProperties/methods/softRemoveProperty.js b/app/imports/api/creature/creatureProperties/methods/softRemoveProperty.js index 7c0753bc..3d62655a 100644 --- a/app/imports/api/creature/creatureProperties/methods/softRemoveProperty.js +++ b/app/imports/api/creature/creatureProperties/methods/softRemoveProperty.js @@ -23,7 +23,7 @@ const softRemoveProperty = new ValidatedMethod({ assertEditPermission(rootCreature, this.userId); // Do work - softRemove({ _id, collection: CreatureProperties }); + softRemove(CreatureProperties, property); } }); diff --git a/app/imports/api/library/LibraryNodes.js b/app/imports/api/library/LibraryNodes.js index 56373c55..b31333ca 100644 --- a/app/imports/api/library/LibraryNodes.js +++ b/app/imports/api/library/LibraryNodes.js @@ -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); } }); diff --git a/app/imports/api/parenting/parentingFunctions.ts b/app/imports/api/parenting/parentingFunctions.ts index 6dc3d219..96398e53 100644 --- a/app/imports/api/parenting/parentingFunctions.ts +++ b/app/imports/api/parenting/parentingFunctions.ts @@ -5,7 +5,7 @@ import CreatureProperties from '/imports/api/creature/creatureProperties/Creatur import { Mongo } from 'meteor/mongo'; export function getCollectionByName(name: string): Mongo.Collection { - const collection = Mongo.Collection.get(name) + const collection: Mongo.Collection = Mongo.Collection.get(name) if (!collection) { throw new Meteor.Error('bad-collection-reference', `Parent references collection ${name}, which does not exist` diff --git a/app/imports/api/parenting/softRemove.ts b/app/imports/api/parenting/softRemove.ts index 10c49523..3783a3b5 100644 --- a/app/imports/api/parenting/softRemove.ts +++ b/app/imports/api/parenting/softRemove.ts @@ -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 | string, doc?: TreeDoc | string) { +export function softRemove(collectionOrName: Mongo.Collection | string, docOrId?: TreeDoc | string) { const removalDate = new Date(); - if (typeof collection === 'string') { - collection = getCollectionByName(collection); + + let collection: Mongo.Collection; + 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 | 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 | 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 | string, doc: TreeDoc | string, extraUpdates?) { - if (typeof collection === 'string') { - collection = getCollectionByName(collection); +export function restore(collectionOrName: Mongo.Collection | string, docOrId: TreeDoc | string, extraUpdates?) { + + let collection: Mongo.Collection; + 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 | 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 | string, do removedWith: 1, } }, { - selector: { type: 'any' }, multi: true, - }); + }) + 1; }