diff --git a/test/api/v3/unit/libs/payments.test.js b/test/api/v3/unit/libs/payments.test.js index bed489f490..e655c42080 100644 --- a/test/api/v3/unit/libs/payments.test.js +++ b/test/api/v3/unit/libs/payments.test.js @@ -653,5 +653,32 @@ describe('payments/index', () => { expect(updatedUser.items.pets['Jackalope-RoyalPurple']).to.eql(5); }); + + it('saves previously unused Mystery Items and Hourglasses for an expired subscription', async () => { + let planExpirationDate = new Date(); + planExpirationDate.setDate(planExpirationDate.getDate() - 2); + let mysteryItem = 'item'; + let mysteryItems = [mysteryItem]; + let consecutive = { + trinkets: 3, + }; + + // set expired plan with unused items + plan.mysteryItems = mysteryItems; + plan.consecutive = consecutive; + plan.dateCreated = planExpirationDate; + plan.dateTerminated = planExpirationDate; + plan.customerId = null; + + user.purchased.plan = plan; + + await user.save(); + await api.addSubToGroupUser(user, group); + + let updatedUser = await User.findById(user._id).exec(); + + expect(updatedUser.purchased.plan.mysteryItems[0]).to.eql(mysteryItem); + expect(updatedUser.purchased.plan.consecutive.trinkets).to.equal(consecutive.trinkets); + }); }); }); diff --git a/website/server/libs/payments.js b/website/server/libs/payments.js index 38f681ea07..31c886a27c 100644 --- a/website/server/libs/payments.js +++ b/website/server/libs/payments.js @@ -112,8 +112,8 @@ api.addSubToGroupUser = async function addSubToGroupUser (member, group) { }, }; + let memberPlan = member.purchased.plan; if (member.isSubscribed()) { - let memberPlan = member.purchased.plan; let customerHasCancelledGroupPlan = memberPlan.customerId === this.constants.GROUP_PLAN_CUSTOMER_ID && !member.hasNotCancelled(); let ignorePaymentPlan = paymentMethodsToIgnore.indexOf(memberPlan.paymentMethod) !== -1; let ignoreCustomerId = customerIdsToIgnore.indexOf(memberPlan.customerId) !== -1; @@ -154,6 +154,10 @@ api.addSubToGroupUser = async function addSubToGroupUser (member, group) { }).value(); } + // save unused hourglass and mystery items + plan.consecutive.trinkets = memberPlan.consecutive.trinkets; + plan.mysteryItems = memberPlan.mysteryItems; + member.purchased.plan = plan; member.items.mounts['Jackalope-RoyalPurple'] = true;