handle upgrades and creations better
This commit is contained in:
@@ -115,6 +115,7 @@ async function prepareSubscriptionValues (data) {
|
||||
let purchaseType = 'subscribe';
|
||||
let emailType = 'subscription-begins';
|
||||
let recipientIsSubscribed = recipient.isSubscribed();
|
||||
let isNewSubscription = !recipientIsSubscribed
|
||||
|
||||
if (data.user && !data.gift && !data.groupId) {
|
||||
const unlockedUser = await User.findOneAndUpdate(
|
||||
@@ -171,6 +172,10 @@ async function prepareSubscriptionValues (data) {
|
||||
|
||||
const { plan } = recipient.purchased;
|
||||
|
||||
if (isNewSubscription) {
|
||||
plan.perkMonthCount = 0;
|
||||
}
|
||||
|
||||
if (data.gift || !autoRenews) {
|
||||
if (plan.customerId && !plan.dateTerminated) { // User has active plan
|
||||
plan.extraMonths += months;
|
||||
@@ -245,6 +250,7 @@ async function prepareSubscriptionValues (data) {
|
||||
itemPurchased,
|
||||
purchaseType,
|
||||
emailType,
|
||||
isNewSubscription
|
||||
};
|
||||
}
|
||||
|
||||
@@ -260,13 +266,16 @@ async function createSubscription (data) {
|
||||
itemPurchased,
|
||||
purchaseType,
|
||||
emailType,
|
||||
isNewSubscription
|
||||
} = await prepareSubscriptionValues(data);
|
||||
|
||||
console.log()
|
||||
// Block sub perks
|
||||
if (months > 0) {
|
||||
if (months > 0 && (!data.gift || !isNewSubscription)) {
|
||||
if (!data.gift && !groupId) {
|
||||
plan.consecutive.offset = months;
|
||||
}
|
||||
console.log("giving benes");
|
||||
await plan.incrementPerkCounterAndReward(recipient._id, months);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,9 @@ import validator from 'validator';
|
||||
import baseModel from '../libs/baseModel';
|
||||
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({
|
||||
planId: String,
|
||||
subscriptionId: String,
|
||||
@@ -49,6 +52,8 @@ schema.plugin(baseModel, {
|
||||
|
||||
schema.methods.incrementPerkCounterAndReward = async function incrementPerkCounterAndReward
|
||||
(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;
|
||||
|
||||
const perks = Math.floor(this.perkMonthCount / 3);
|
||||
|
||||
Reference in New Issue
Block a user