mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-14 21:27:23 +01:00
handle upgrades and creations better
This commit is contained in:
committed by
Phillip Thelen
parent
e81a052f66
commit
c0c6657536
@@ -13,7 +13,7 @@ import {
|
|||||||
import * as worldState from '../../../../../website/server/libs/worldState';
|
import * as worldState from '../../../../../website/server/libs/worldState';
|
||||||
import { TransactionModel } from '../../../../../website/server/models/transaction';
|
import { TransactionModel } from '../../../../../website/server/models/transaction';
|
||||||
|
|
||||||
describe('payments/index', () => {
|
describe.only('payments/index', () => {
|
||||||
let user;
|
let user;
|
||||||
let group;
|
let group;
|
||||||
let data;
|
let data;
|
||||||
@@ -235,6 +235,48 @@ describe('payments/index', () => {
|
|||||||
expect(recipient.purchased.plan.customerId).to.eql('customer-id');
|
expect(recipient.purchased.plan.customerId).to.eql('customer-id');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('sets plan.perkMonthCount to zero if user is not subscribed', async () => {
|
||||||
|
recipient.purchased.plan = plan;
|
||||||
|
recipient.purchased.plan.perkMonthCount = 1;
|
||||||
|
recipient.purchased.plan.customerId = undefined;
|
||||||
|
data.sub.key = 'basic_earned';
|
||||||
|
data.gift.subscription.key = 'basic_earned';
|
||||||
|
data.gift.subscription.months = 1;
|
||||||
|
|
||||||
|
expect(recipient.purchased.plan.perkMonthCount).to.eql(1);
|
||||||
|
await api.createSubscription(data);
|
||||||
|
|
||||||
|
expect(recipient.purchased.plan.perkMonthCount).to.eql(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('adds to plan.perkMonthCount if user is already subscribed', async () => {
|
||||||
|
recipient.purchased.plan = plan;
|
||||||
|
recipient.purchased.plan.perkMonthCount = 1;
|
||||||
|
data.sub.key = 'basic_earned';
|
||||||
|
data.gift.subscription.key = 'basic_earned';
|
||||||
|
data.gift.subscription.months = 1;
|
||||||
|
|
||||||
|
expect(recipient.purchased.plan.perkMonthCount).to.eql(1);
|
||||||
|
await api.createSubscription(data);
|
||||||
|
|
||||||
|
expect(recipient.purchased.plan.perkMonthCount).to.eql(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('awards perks if plan.perkMonthCount reaches 3', async () => {
|
||||||
|
recipient.purchased.plan = plan;
|
||||||
|
recipient.purchased.plan.perkMonthCount = 2;
|
||||||
|
data.sub.key = 'basic_earned';
|
||||||
|
data.gift.subscription.key = 'basic_earned';
|
||||||
|
data.gift.subscription.months = 1;
|
||||||
|
|
||||||
|
expect(recipient.purchased.plan.perkMonthCount).to.eql(2);
|
||||||
|
await api.createSubscription(data);
|
||||||
|
|
||||||
|
expect(recipient.purchased.plan.perkMonthCount).to.eql(0);
|
||||||
|
expect(recipient.purchased.plan.consecutive.trinkets).to.eql(1);
|
||||||
|
expect(recipient.purchased.plan.consecutive.gemCapExtra).to.eql(5);
|
||||||
|
});
|
||||||
|
|
||||||
it('sets plan.customerId to "Gift" if it does not already exist', async () => {
|
it('sets plan.customerId to "Gift" if it does not already exist', async () => {
|
||||||
expect(recipient.purchased.plan.customerId).to.not.exist;
|
expect(recipient.purchased.plan.customerId).to.not.exist;
|
||||||
|
|
||||||
@@ -438,6 +480,19 @@ describe('payments/index', () => {
|
|||||||
expect(user.purchased.plan.dateCurrentTypeCreated).to.not.eql(initialDate);
|
expect(user.purchased.plan.dateCurrentTypeCreated).to.not.eql(initialDate);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('keeps plan.perkMonthCount when changing subscription type', async () => {
|
||||||
|
await api.createSubscription(data);
|
||||||
|
user.purchased.plan.perkMonthCount = 2;
|
||||||
|
await api.createSubscription(data);
|
||||||
|
expect(user.purchased.plan.perkMonthCount).to.eql(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sets plan.perkMonthCount to zero when creating new subscription', async () => {
|
||||||
|
user.purchased.plan.perkMonthCount = 2;
|
||||||
|
await api.createSubscription(data);
|
||||||
|
expect(user.purchased.plan.perkMonthCount).to.eql(0);
|
||||||
|
});
|
||||||
|
|
||||||
it('awards the Royal Purple Jackalope pet', async () => {
|
it('awards the Royal Purple Jackalope pet', async () => {
|
||||||
await api.createSubscription(data);
|
await api.createSubscription(data);
|
||||||
|
|
||||||
|
|||||||
@@ -114,6 +114,7 @@ async function prepareSubscriptionValues (data) {
|
|||||||
let purchaseType = 'subscribe';
|
let purchaseType = 'subscribe';
|
||||||
let emailType = 'subscription-begins';
|
let emailType = 'subscription-begins';
|
||||||
let recipientIsSubscribed = recipient.isSubscribed();
|
let recipientIsSubscribed = recipient.isSubscribed();
|
||||||
|
let isNewSubscription = !recipientIsSubscribed
|
||||||
|
|
||||||
// If we are buying a group subscription
|
// If we are buying a group subscription
|
||||||
if (data.groupId) {
|
if (data.groupId) {
|
||||||
@@ -154,6 +155,10 @@ async function prepareSubscriptionValues (data) {
|
|||||||
|
|
||||||
const { plan } = recipient.purchased;
|
const { plan } = recipient.purchased;
|
||||||
|
|
||||||
|
if (isNewSubscription) {
|
||||||
|
plan.perkMonthCount = 0;
|
||||||
|
}
|
||||||
|
|
||||||
if (data.gift || !autoRenews) {
|
if (data.gift || !autoRenews) {
|
||||||
if (plan.customerId && !plan.dateTerminated) { // User has active plan
|
if (plan.customerId && !plan.dateTerminated) { // User has active plan
|
||||||
plan.extraMonths += months;
|
plan.extraMonths += months;
|
||||||
@@ -228,6 +233,7 @@ async function prepareSubscriptionValues (data) {
|
|||||||
itemPurchased,
|
itemPurchased,
|
||||||
purchaseType,
|
purchaseType,
|
||||||
emailType,
|
emailType,
|
||||||
|
isNewSubscription
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,13 +249,16 @@ async function createSubscription (data) {
|
|||||||
itemPurchased,
|
itemPurchased,
|
||||||
purchaseType,
|
purchaseType,
|
||||||
emailType,
|
emailType,
|
||||||
|
isNewSubscription
|
||||||
} = await prepareSubscriptionValues(data);
|
} = await prepareSubscriptionValues(data);
|
||||||
|
|
||||||
|
console.log()
|
||||||
// Block sub perks
|
// Block sub perks
|
||||||
if (months > 0) {
|
if (months > 0 && (!data.gift || !isNewSubscription)) {
|
||||||
if (!data.gift && !groupId) {
|
if (!data.gift && !groupId) {
|
||||||
plan.consecutive.offset = months;
|
plan.consecutive.offset = months;
|
||||||
}
|
}
|
||||||
|
console.log("giving benes");
|
||||||
await plan.incrementPerkCounterAndReward(recipient._id, months);
|
await plan.incrementPerkCounterAndReward(recipient._id, months);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,9 @@ import validator from 'validator';
|
|||||||
import baseModel from '../libs/baseModel';
|
import baseModel from '../libs/baseModel';
|
||||||
import { TransactionModel as Transaction } from './transaction';
|
import { TransactionModel as Transaction } from './transaction';
|
||||||
|
|
||||||
|
// multi-month subscriptions are for multiples of 3 months
|
||||||
|
const SUBSCRIPTION_BASIC_BLOCK_LENGTH = 3;
|
||||||
|
|
||||||
export const schema = new mongoose.Schema({
|
export const schema = new mongoose.Schema({
|
||||||
planId: String,
|
planId: String,
|
||||||
subscriptionId: String,
|
subscriptionId: String,
|
||||||
@@ -49,6 +52,8 @@ schema.plugin(baseModel, {
|
|||||||
|
|
||||||
schema.methods.incrementPerkCounterAndReward = async function incrementPerkCounterAndReward
|
schema.methods.incrementPerkCounterAndReward = async function incrementPerkCounterAndReward
|
||||||
(userID, adding) {
|
(userID, adding) {
|
||||||
|
// if perkMonthCount wasn't used before, initialize it.
|
||||||
|
if (!this.perkMonthCount) this.perkMonthCount = this.consecutive.count % SUBSCRIPTION_BASIC_BLOCK_LENGTH;
|
||||||
this.perkMonthCount += adding;
|
this.perkMonthCount += adding;
|
||||||
|
|
||||||
const perks = Math.floor(this.perkMonthCount / 3);
|
const perks = Math.floor(this.perkMonthCount / 3);
|
||||||
|
|||||||
Reference in New Issue
Block a user