From 5f5fe801f6af3e0017986b83932a8ab61e05a49b Mon Sep 17 00:00:00 2001 From: Stefan Zermatten Date: Mon, 21 Jun 2021 16:06:29 +0200 Subject: [PATCH] Added method to transfer character ownership --- .../methods/transferCreatureOwnership.js | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 app/imports/api/creature/creatures/methods/transferCreatureOwnership.js diff --git a/app/imports/api/creature/creatures/methods/transferCreatureOwnership.js b/app/imports/api/creature/creatures/methods/transferCreatureOwnership.js new file mode 100644 index 00000000..c7012d03 --- /dev/null +++ b/app/imports/api/creature/creatures/methods/transferCreatureOwnership.js @@ -0,0 +1,55 @@ +import SimpleSchema from 'simpl-schema'; +import { ValidatedMethod } from 'meteor/mdg:validated-method'; +import { RateLimiterMixin } from 'ddp-rate-limiter-mixin'; +import Creatures from '/imports/api/creature/creatures/Creatures.js'; +import { assertOwnership } from '/imports/api/creature/creatures/creaturePermissions.js'; +import { getUserTier } from '/imports/api/users/patreon/tiers.js'; + +const transferCreatureOwnership = new ValidatedMethod({ + + name: 'creatures.methods.transferOwnership', + + validate: new SimpleSchema({ + creatureId: { + type: String, + regEx: SimpleSchema.RegEx.Id, + }, + userId: { + type: String, + regEx: SimpleSchema.RegEx.Id, + }, + }).validator(), + + mixins: [RateLimiterMixin], + rateLimit: { + numRequests: 5, + timeInterval: 5000, + }, + + run({creatureId, userId}) { + assertOwnership(creatureId, this.userId); + + let tier = getUserTier(userId); + let currentCharacterCount = Creatures.find({ + owner: userId, + }, { + fields: {_id: 1}, + }).count(); + + if ( + tier.characterSlots !== -1 && + currentCharacterCount >= tier.characterSlots + ){ + throw new Meteor.Error('Creatures.methods.transferOwnership.denied', + 'The new owner is already at their character limit') + } + + Creatures.update(creatureId, { + $set: {owner: userId}, + }); + + return creatureId; + }, +}); + +export default transferCreatureOwnership;