Fixed soft-remove of props and library props

This commit is contained in:
ThaumRystra
2024-05-04 18:57:56 +02:00
parent c5f6ce81bd
commit c11013eddb
5 changed files with 44 additions and 38 deletions

View File

@@ -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 } });
}
});

View File

@@ -23,7 +23,7 @@ const softRemoveProperty = new ValidatedMethod({
assertEditPermission(rootCreature, this.userId);
// Do work
softRemove({ _id, collection: CreatureProperties });
softRemove(CreatureProperties, property);
}
});

View File

@@ -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);
}
});

View File

@@ -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`

View File

@@ -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;
}