From 82a1d6ff0e857d4a1335bf9f8b9ea4b8c55b0d5c Mon Sep 17 00:00:00 2001 From: Phillip Thelen Date: Wed, 30 Nov 2022 15:06:50 +0100 Subject: [PATCH] Improve handling --- website/server/libs/cron.js | 18 +----------------- website/server/libs/payments/subscriptions.js | 11 +++++------ website/server/models/subscriptionPlan.js | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/website/server/libs/cron.js b/website/server/libs/cron.js index cfbdbb0dc5..3555f36836 100644 --- a/website/server/libs/cron.js +++ b/website/server/libs/cron.js @@ -106,32 +106,16 @@ async function grantEndOfTheMonthPerks (user, now) { planMonthsLength = getPlanMonths(plan); } - // every 3 months you get one set of perks - this variable records how many sets you need - let perkAmountNeeded = 0; if (planMonthsLength === 1) { - // User has a single-month recurring subscription and are due for perks - // IF they've been subscribed for a multiple of 3 months. - if (plan.consecutive.count % SUBSCRIPTION_BASIC_BLOCK_LENGTH === 0) { // every 3 months - perkAmountNeeded = 1; - } plan.consecutive.offset = 0; // allow the same logic to be run next month } else { // User has a multi-month recurring subscription // and it renewed in the previous calendar month. - - // e.g., for a 6-month subscription, give two sets of perks - perkAmountNeeded = planMonthsLength / SUBSCRIPTION_BASIC_BLOCK_LENGTH; // don't need to check for perks again for this many months // (subtract 1 because we should have run this when the payment was taken last month) plan.consecutive.offset = planMonthsLength - 1; } - if (perkAmountNeeded > 0) { - // one Hourglass every 3 months - await plan.updateHourglasses(user._id, perkAmountNeeded, 'subscription_perks'); // eslint-disable-line no-await-in-loop - plan.consecutive.gemCapExtra += 5 * perkAmountNeeded; // 5 extra Gems every 3 months - // cap it at 50 (hard 25 limit + extra 25) - if (plan.consecutive.gemCapExtra > 25) plan.consecutive.gemCapExtra = 25; - } + await plan.incrementPerkCounterAndReward(user._id, planMonthsLength); } } } diff --git a/website/server/libs/payments/subscriptions.js b/website/server/libs/payments/subscriptions.js index ca2fd892c9..193b3276c8 100644 --- a/website/server/libs/payments/subscriptions.js +++ b/website/server/libs/payments/subscriptions.js @@ -246,12 +246,11 @@ async function createSubscription (data) { } = await prepareSubscriptionValues(data); // Block sub perks - const perks = Math.floor(months / 3); - if (perks) { - plan.consecutive.offset += months; - plan.consecutive.gemCapExtra += perks * 5; - if (plan.consecutive.gemCapExtra > 25) plan.consecutive.gemCapExtra = 25; - await plan.updateHourglasses(recipient._id, perks, 'subscription_perks'); // one Hourglass every 3 months + if (months > 0) { + if (!data.gift && !groupId) { + plan.consecutive.offset = months; + } + await plan.incrementPerkCounterAndReward(recipient._id, months); } if (recipient !== group) { diff --git a/website/server/models/subscriptionPlan.js b/website/server/models/subscriptionPlan.js index df6d28ecd5..7b93a81b92 100644 --- a/website/server/models/subscriptionPlan.js +++ b/website/server/models/subscriptionPlan.js @@ -15,6 +15,7 @@ export const schema = new mongoose.Schema({ dateUpdated: Date, dateCurrentTypeCreated: Date, extraMonths: { $type: Number, default: 0 }, + perkMonthCount: { $type: Number, default: 0 }, gemsBought: { $type: Number, default: 0 }, mysteryItems: { $type: Array, default: () => [] }, lastReminderDate: Date, // indicates the last time a subscription reminder was sent @@ -46,6 +47,20 @@ schema.plugin(baseModel, { _id: false, }); +schema.methods.incrementPerkCounterAndReward = async function incrementPerkCounterAndReward (userID, adding) { + this.perkMonthCount += adding; + + const perks = Math.floor(this.perkMonthCount / 3); + if (perks > 0) { + this.consecutive.gemCapExtra += 5 * perks; // 5 extra Gems every 3 months + // cap it at 50 (hard 25 limit + extra 25) + if (this.consecutive.gemCapExtra > 25) this.consecutive.gemCapExtra = 25; + this.perkMonthCount -= (perks * 3); + // one Hourglass every 3 months + await this.updateHourglasses(userID, perks, 'subscription_perks'); // eslint-disable-line no-await-in-loop + } +}; + schema.methods.updateHourglasses = async function updateHourglasses (userId, amount, transactionType,