From 57bfee02cf34b9b44f662f90328d2615cc8f4ebe Mon Sep 17 00:00:00 2001 From: Tyler Renelle Date: Wed, 26 Nov 2014 12:39:30 -0700 Subject: [PATCH] feat(plans): consecutive months & extra months (like a time-card) --- migrations/20141117_consecutive_months.js | 11 +++++++ src/controllers/payments/index.js | 38 +++++++++++++---------- src/models/user.js | 4 +++ views/options/settings.jade | 3 ++ 4 files changed, 39 insertions(+), 17 deletions(-) create mode 100644 migrations/20141117_consecutive_months.js diff --git a/migrations/20141117_consecutive_months.js b/migrations/20141117_consecutive_months.js new file mode 100644 index 0000000000..6893cdcb4c --- /dev/null +++ b/migrations/20141117_consecutive_months.js @@ -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}}); +}); diff --git a/src/controllers/payments/index.js b/src/controllers/payments/index.js index ba6359d4f9..eed12edd4d 100644 --- a/src/controllers/payments/index.js +++ b/src/controllers/payments/index.js @@ -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(); } diff --git a/src/models/user.js b/src/models/user.js index 4aef97d59c..e973fa9a12 100644 --- a/src/models/user.js +++ b/src/models/user.js @@ -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': []} } diff --git a/views/options/settings.jade b/views/options/settings.jade index 99c94f4dc2..3cd1d09223 100644 --- a/views/options/settings.jade +++ b/views/options/settings.jade @@ -228,6 +228,9 @@ script(id='partials/options.settings.subscription.html',type='text/ng-template') i.glyphicon.glyphicon-time | #{env.t('subCanceled')} {{moment(p.dateTerminated).format('MM/DD/YYYY')}} 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'")