feat(plans): consecutive months & extra months (like a time-card)

This commit is contained in:
Tyler Renelle 2014-11-26 12:39:30 -07:00
parent 117a424a5d
commit 57bfee02cf
4 changed files with 39 additions and 17 deletions

View file

@ -0,0 +1,11 @@
// require moment, lodash
db.users.find(
{'purchased.plan.customerId':{$ne:null}},
{'purchased.plan':1}
).forEach(function(user){
var p = user.purchased.plan;
var latestMonth = p.dateTerminated || p.dateCreated;
// TODO is rounding up what we want?
var consecutiveMonths = Math.ceil(moment(p.dateCreated).diff(latestMonth, 'months', true));
db.users.update({_id: user._id}, {$set: {'purchased.plan.consecutiveMonths': consecutiveMonths}});
});

View file

@ -24,19 +24,19 @@ function revealMysteryItems(user) {
exports.createSubscription = function(user, data) {
if (!user.purchased.plan) user.purchased.plan = {};
_(user.purchased.plan)
.merge({ // override with these values
planId:'basic_earned',
customerId: data.customerId,
dateUpdated: new Date(),
gemsBought: 0,
paymentMethod: data.paymentMethod,
dateTerminated: null
})
.defaults({ // allow non-override if a plan was previously used
dateCreated: new Date(),
mysteryItems: []
});
var p = user.purchased.plan;
_(p).merge({ // override with these values
planId:'basic_earned',
customerId: data.customerId,
dateUpdated: new Date(),
gemsBought: 0,
paymentMethod: data.paymentMethod,
extraMonths: p.extraMonths + (p.dateTerminated ? moment(p.dateTerminated).diff(new Date(),'months',true) : 0),
dateTerminated: null
}).defaults({ // allow non-override if a plan was previously used
dateCreated: new Date(),
mysteryItems: []
});
revealMysteryItems(user);
if(isProduction) utils.txnEmail(user, 'subscription-begins');
user.purchased.txnCount++;
@ -48,12 +48,16 @@ exports.createSubscription = function(user, data) {
* Sets their subscription to be cancelled later
*/
exports.cancelSubscription = function(user, data) {
var du = user.purchased.plan.dateUpdated, now = moment();
var p = user.purchased.plan,
now = moment();
if(isProduction) utils.txnEmail(user, 'cancel-subscription');
user.purchased.plan.dateTerminated =
moment( now.format('MM') + '/' + moment(du).format('DD') + '/' + now.format('YYYY') )
.add({months:1})
p.dateTerminated =
moment( now.format('MM') + '/' + moment(p.dateUpdated).format('DD') + '/' + now.format('YYYY') )
.add({months:1})// end their subscription 1mo from their last payment
.add({months:p.extraMonths})// plus any extra time (carry-over, gifted subscription, etc) they have
.toDate();
p.extraMonths = 0; // clear extra time. If they subscribe again, it'll be recalculated from p.dateTerminated
utils.ga.event('unsubscribe', 'Stripe').send();
}

View file

@ -94,9 +94,13 @@ var UserSchema = new Schema({
planId: String,
paymentMethod: String, //enum: ['Paypal','Stripe', '']}
customerId: String,
dateCreated: Date,
dateTerminated: Date,
dateUpdated: Date,
consecutiveMonths: {type:Number, 'default':0},
extraMonths: {type:Number, 'default':0},
gemsBought: {type: Number, 'default': 0},
mysteryItems: {type: Array, 'default': []}
}

View file

@ -228,6 +228,9 @@ script(id='partials/options.settings.subscription.html',type='text/ng-template')
i.glyphicon.glyphicon-time
| #{env.t('subCanceled')} <strong>{{moment(p.dateTerminated).format('MM/DD/YYYY')}}</strong>
p.alert.alert-success(ng-if='!p.dateTerminated')=env.t('subscribed')
p.alert.alert-success(ng-if='p.extraMonths')
span.glyphicon.glyphicon-credit-card
| You have {{p.extraMonths | number:2}} months of subscription credit.
h2=env.t('individualSub')
div(ng-include="'partials/options.settings.subscription.perks.html'")