diff --git a/test/api/v3/unit/libs/cron.test.js b/test/api/v3/unit/libs/cron.test.js index 71a23e6ae9..0e30cddeb9 100644 --- a/test/api/v3/unit/libs/cron.test.js +++ b/test/api/v3/unit/libs/cron.test.js @@ -62,7 +62,7 @@ describe('cron', () => { describe('end of the month perks', () => { beforeEach(() => { user.purchased.plan.customerId = 'subscribedId'; - user.purchased.plan.dateUpdated = moment('012013', 'MMYYYY'); + user.purchased.plan.dateUpdated = moment().subtract(1, 'months').toDate(); }); it('resets plan.gemsBought on a new month', () => { @@ -72,9 +72,9 @@ describe('cron', () => { }); it('resets plan.dateUpdated on a new month', () => { - let currentMonth = moment().format('MMYYYY'); + let currentMonth = moment().startOf('month'); cron({user, tasksByType, daysMissed, analytics}); - expect(moment(user.purchased.plan.dateUpdated).format('MMYYYY')).to.equal(currentMonth); + expect(moment(user.purchased.plan.dateUpdated).startOf('month').isSame(currentMonth)).to.eql(true); }); it('increments plan.consecutive.count', () => { @@ -83,6 +83,13 @@ describe('cron', () => { expect(user.purchased.plan.consecutive.count).to.equal(1); }); + it('increments plan.consecutive.count by more than 1 if user skipped months between logins', () => { + user.purchased.plan.dateUpdated = moment().subtract(2, 'months').toDate(); + user.purchased.plan.consecutive.count = 0; + cron({user, tasksByType, daysMissed, analytics}); + expect(user.purchased.plan.consecutive.count).to.equal(2); + }); + it('decrements plan.consecutive.offset when offset is greater than 0', () => { user.purchased.plan.consecutive.offset = 2; cron({user, tasksByType, daysMissed, analytics}); @@ -97,6 +104,21 @@ describe('cron', () => { expect(user.purchased.plan.consecutive.offset).to.equal(0); }); + it('increments plan.consecutive.trinkets multiple times if user has been absent with continuous subscription', () => { + user.purchased.plan.dateUpdated = moment().subtract(6, 'months').toDate(); + user.purchased.plan.consecutive.count = 5; + cron({user, tasksByType, daysMissed, analytics}); + expect(user.purchased.plan.consecutive.trinkets).to.equal(2); + }); + + it('does not award unearned plan.consecutive.trinkets if subscription ended during an absence', () => { + user.purchased.plan.dateUpdated = moment().subtract(6, 'months').toDate(); + user.purchased.plan.dateTerminated = moment().subtract(3, 'months').toDate(); + user.purchased.plan.consecutive.count = 5; + cron({user, tasksByType, daysMissed, analytics}); + expect(user.purchased.plan.consecutive.trinkets).to.equal(1); + }); + it('increments plan.consecutive.gemCapExtra when user has reached a month that is a multiple of 3', () => { user.purchased.plan.consecutive.count = 5; user.purchased.plan.consecutive.offset = 1; @@ -105,6 +127,13 @@ describe('cron', () => { expect(user.purchased.plan.consecutive.offset).to.equal(0); }); + it('increments plan.consecutive.gemCapExtra multiple times if user has been absent with continuous subscription', () => { + user.purchased.plan.dateUpdated = moment().subtract(6, 'months').toDate(); + user.purchased.plan.consecutive.count = 5; + cron({user, tasksByType, daysMissed, analytics}); + expect(user.purchased.plan.consecutive.gemCapExtra).to.equal(10); + }); + it('does not increment plan.consecutive.gemCapExtra when user has reached the gemCap limit', () => { user.purchased.plan.consecutive.gemCapExtra = 25; user.purchased.plan.consecutive.count = 5; @@ -118,7 +147,7 @@ describe('cron', () => { expect(user.purchased.plan.customerId).to.exist; }); - it('does reset plan stats until we are after the last day of the cancelled month', () => { + it('does reset plan stats if we are after the last day of the cancelled month', () => { user.purchased.plan.dateTerminated = moment(new Date()).subtract({days: 1}); user.purchased.plan.consecutive.gemCapExtra = 20; user.purchased.plan.consecutive.count = 5; @@ -134,10 +163,14 @@ describe('cron', () => { }); describe('end of the month perks when user is not subscribed', () => { - it('does not reset plan.gemsBought on a new month', () => { + beforeEach(() => { + user.purchased.plan.dateUpdated = moment().subtract(1, 'months').toDate(); + }); + + it('resets plan.gemsBought on a new month', () => { user.purchased.plan.gemsBought = 10; cron({user, tasksByType, daysMissed, analytics}); - expect(user.purchased.plan.gemsBought).to.equal(10); + expect(user.purchased.plan.gemsBought).to.equal(0); }); it('does not reset plan.dateUpdated on a new month', () => { diff --git a/test/api/v3/unit/libs/payments.test.js b/test/api/v3/unit/libs/payments.test.js index cc73c6bf8f..312c22a2d6 100644 --- a/test/api/v3/unit/libs/payments.test.js +++ b/test/api/v3/unit/libs/payments.test.js @@ -80,6 +80,24 @@ describe('payments/index', () => { expect(recipient.purchased.plan.extraMonths).to.eql(3); }); + it('does not set negative extraMonths if plan has past dateTerminated date', async () => { + let dateTerminated = moment().subtract(2, 'months').toDate(); + recipient.purchased.plan.dateTerminated = dateTerminated; + + await api.createSubscription(data); + + expect(recipient.purchased.plan.extraMonths).to.eql(0); + }); + + it('does not reset Gold-to-Gems cap on an existing subscription', async () => { + recipient.purchased.plan = plan; + recipient.purchased.plan.gemsBought = 12; + + await api.createSubscription(data); + + expect(recipient.purchased.plan.gemsBought).to.eql(12); + }); + it('adds to date terminated for an existing plan with a future terminated date', async () => { let dateTerminated = moment().add(1, 'months').toDate(); recipient.purchased.plan = plan; @@ -210,6 +228,25 @@ describe('payments/index', () => { expect(user.purchased.plan.extraMonths).to.within(1.9, 2); }); + it('does not set negative extraMonths if plan has past dateTerminated date', async () => { + user.purchased.plan = plan; + user.purchased.plan.dateTerminated = moment(new Date()).subtract(2, 'months'); + expect(user.purchased.plan.extraMonths).to.eql(0); + + await api.createSubscription(data); + + expect(user.purchased.plan.extraMonths).to.eql(0); + }); + + it('does not reset Gold-to-Gems cap on additional subscription', async () => { + user.purchased.plan = plan; + user.purchased.plan.gemsBought = 10; + + await api.createSubscription(data); + + expect(user.purchased.plan.gemsBought).to.eql(10); + }); + it('sets lastBillingDate if payment method is "Amazon Payments"', async () => { data.paymentMethod = 'Amazon Payments'; @@ -218,7 +255,7 @@ describe('payments/index', () => { expect(user.purchased.plan.lastBillingDate).to.exist; }); - it('increases the user\'s transcation count', async () => { + it('increases the user\'s transaction count', async () => { expect(user.purchased.txnCount).to.eql(0); await api.createSubscription(data); diff --git a/website/server/libs/cron.js b/website/server/libs/cron.js index 761b22c439..3e348e9baf 100644 --- a/website/server/libs/cron.js +++ b/website/server/libs/cron.js @@ -46,24 +46,27 @@ let CLEAR_BUFFS = { function grantEndOfTheMonthPerks (user, now) { let plan = user.purchased.plan; + let subscriptionEndDate = moment(plan.dateTerminated).isBefore() ? moment(plan.dateTerminated).startOf('month') : moment(now).startOf('month'); + let dateUpdatedMoment = moment(plan.dateUpdated).startOf('month'); + let elapsedMonths = moment(subscriptionEndDate).diff(dateUpdatedMoment, 'months'); - if (moment(plan.dateUpdated).format('MMYYYY') !== moment().format('MMYYYY')) { - plan.gemsBought = 0; // reset gem-cap + if (elapsedMonths > 0) { plan.dateUpdated = now; // For every month, inc their "consecutive months" counter. Give perks based on consecutive blocks // If they already got perks for those blocks (eg, 6mo subscription, subscription gifts, etc) - then dec the offset until it hits 0 - // TODO use month diff instead of ++ / --? see https://github.com/HabitRPG/habitrpg/issues/4317 _.defaults(plan.consecutive, {count: 0, offset: 0, trinkets: 0, gemCapExtra: 0}); - plan.consecutive.count++; + for (let i = 0; i < elapsedMonths; i++) { + plan.consecutive.count++; - if (plan.consecutive.offset > 1) { - plan.consecutive.offset--; - } else if (plan.consecutive.count % 3 === 0) { // every 3 months - if (plan.consecutive.offset === 1) plan.consecutive.offset--; - plan.consecutive.trinkets++; - plan.consecutive.gemCapExtra += 5; - if (plan.consecutive.gemCapExtra > 25) plan.consecutive.gemCapExtra = 25; // cap it at 50 (hard 25 limit + extra 25) + if (plan.consecutive.offset > 1) { + plan.consecutive.offset--; + } else if (plan.consecutive.count % 3 === 0) { // every 3 months + if (plan.consecutive.offset === 1) plan.consecutive.offset--; + plan.consecutive.trinkets++; + plan.consecutive.gemCapExtra += 5; + if (plan.consecutive.gemCapExtra > 25) plan.consecutive.gemCapExtra = 25; // cap it at 50 (hard 25 limit + extra 25) + } } } } @@ -121,6 +124,10 @@ export function cron (options = {}) { // "Perfect Day" achievement for perfect days let perfect = true; + // Reset Gold-to-Gems cap if it's the start of the month + if (user.purchased && user.purchased.plan && moment(user.purchased.plan.dateUpdated).startOf('month') !== moment().startOf('month')) { + user.purchased.plan.gemsBought = 0; + } if (user.isSubscribed()) { grantEndOfTheMonthPerks(user, now); if (!CRON_SAFE_MODE) removeTerminatedSubscription(user); diff --git a/website/server/libs/payments.js b/website/server/libs/payments.js index 6b27e88b11..1684a18f24 100644 --- a/website/server/libs/payments.js +++ b/website/server/libs/payments.js @@ -24,17 +24,24 @@ function revealMysteryItems (user) { }); } +function _dateDiff (earlyDate, lateDate) { + if (!earlyDate || !lateDate || moment(lateDate).isBefore(earlyDate)) return 0; + + return moment(lateDate).diff(earlyDate, 'months', true); +} + api.createSubscription = async function createSubscription (data) { let recipient = data.gift ? data.gift.member : data.user; let plan = recipient.purchased.plan; let block = shared.content.subscriptionBlocks[data.gift ? data.gift.subscription.key : data.sub.key]; let months = Number(block.months); + let today = new Date(); if (data.gift) { if (plan.customerId && !plan.dateTerminated) { // User has active plan plan.extraMonths += months; } else { - if (!plan.dateUpdated) plan.dateUpdated = new Date(); + if (!plan.dateUpdated) plan.dateUpdated = today; if (moment(plan.dateTerminated).isAfter()) { plan.dateTerminated = moment(plan.dateTerminated).add({months}).toDate(); } else { @@ -44,20 +51,21 @@ api.createSubscription = async function createSubscription (data) { if (!plan.customerId) plan.customerId = 'Gift'; // don't override existing customer, but all sub need a customerId } else { + if (!plan.dateTerminated) plan.dateTerminated = today; + _(plan).merge({ // override with these values planId: block.key, customerId: data.customerId, - dateUpdated: new Date(), - gemsBought: 0, + dateUpdated: today, paymentMethod: data.paymentMethod, - extraMonths: Number(plan.extraMonths) + - Number(plan.dateTerminated ? moment(plan.dateTerminated).diff(new Date(), 'months', true) : 0), + extraMonths: Number(plan.extraMonths) + _dateDiff(today, plan.dateTerminated), dateTerminated: null, // Specify a lastBillingDate just for Amazon Payments // Resetted every time the subscription restarts - lastBillingDate: data.paymentMethod === 'Amazon Payments' ? new Date() : undefined, + lastBillingDate: data.paymentMethod === 'Amazon Payments' ? today : undefined, }).defaults({ // allow non-override if a plan was previously used - dateCreated: new Date(), + gemsBought: 0, + dateCreated: today, mysteryItems: [], }).value(); } @@ -129,7 +137,7 @@ api.cancelSubscription = async function cancelSubscription (data) { let plan = data.user.purchased.plan; let now = moment(); let remaining = data.nextBill ? moment(data.nextBill).diff(new Date(), 'days') : 30; - let extraDays = Math.ceil(30 * plan.extraMonths); + let extraDays = Math.ceil(30.5 * plan.extraMonths); let nowStr = `${now.format('MM')}/${moment(plan.dateUpdated).format('DD')}/${now.format('YYYY')}`; let nowStrFormat = 'MM/DD/YYYY';