mirror of
https://github.com/sudoxnym/habitica-self-host.git
synced 2026-07-13 17:52:22 +00:00
feat(plans): consecutive months & extra months (like a time-card)
This commit is contained in:
parent
117a424a5d
commit
57bfee02cf
4 changed files with 39 additions and 17 deletions
11
migrations/20141117_consecutive_months.js
Normal file
11
migrations/20141117_consecutive_months.js
Normal 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}});
|
||||||
|
});
|
||||||
|
|
@ -24,19 +24,19 @@ function revealMysteryItems(user) {
|
||||||
|
|
||||||
exports.createSubscription = function(user, data) {
|
exports.createSubscription = function(user, data) {
|
||||||
if (!user.purchased.plan) user.purchased.plan = {};
|
if (!user.purchased.plan) user.purchased.plan = {};
|
||||||
_(user.purchased.plan)
|
var p = user.purchased.plan;
|
||||||
.merge({ // override with these values
|
_(p).merge({ // override with these values
|
||||||
planId:'basic_earned',
|
planId:'basic_earned',
|
||||||
customerId: data.customerId,
|
customerId: data.customerId,
|
||||||
dateUpdated: new Date(),
|
dateUpdated: new Date(),
|
||||||
gemsBought: 0,
|
gemsBought: 0,
|
||||||
paymentMethod: data.paymentMethod,
|
paymentMethod: data.paymentMethod,
|
||||||
dateTerminated: null
|
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
|
}).defaults({ // allow non-override if a plan was previously used
|
||||||
dateCreated: new Date(),
|
dateCreated: new Date(),
|
||||||
mysteryItems: []
|
mysteryItems: []
|
||||||
});
|
});
|
||||||
revealMysteryItems(user);
|
revealMysteryItems(user);
|
||||||
if(isProduction) utils.txnEmail(user, 'subscription-begins');
|
if(isProduction) utils.txnEmail(user, 'subscription-begins');
|
||||||
user.purchased.txnCount++;
|
user.purchased.txnCount++;
|
||||||
|
|
@ -48,12 +48,16 @@ exports.createSubscription = function(user, data) {
|
||||||
* Sets their subscription to be cancelled later
|
* Sets their subscription to be cancelled later
|
||||||
*/
|
*/
|
||||||
exports.cancelSubscription = function(user, data) {
|
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');
|
if(isProduction) utils.txnEmail(user, 'cancel-subscription');
|
||||||
user.purchased.plan.dateTerminated =
|
p.dateTerminated =
|
||||||
moment( now.format('MM') + '/' + moment(du).format('DD') + '/' + now.format('YYYY') )
|
moment( now.format('MM') + '/' + moment(p.dateUpdated).format('DD') + '/' + now.format('YYYY') )
|
||||||
.add({months:1})
|
.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();
|
.toDate();
|
||||||
|
p.extraMonths = 0; // clear extra time. If they subscribe again, it'll be recalculated from p.dateTerminated
|
||||||
|
|
||||||
utils.ga.event('unsubscribe', 'Stripe').send();
|
utils.ga.event('unsubscribe', 'Stripe').send();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -94,9 +94,13 @@ var UserSchema = new Schema({
|
||||||
planId: String,
|
planId: String,
|
||||||
paymentMethod: String, //enum: ['Paypal','Stripe', '']}
|
paymentMethod: String, //enum: ['Paypal','Stripe', '']}
|
||||||
customerId: String,
|
customerId: String,
|
||||||
|
|
||||||
dateCreated: Date,
|
dateCreated: Date,
|
||||||
dateTerminated: Date,
|
dateTerminated: Date,
|
||||||
dateUpdated: Date,
|
dateUpdated: Date,
|
||||||
|
consecutiveMonths: {type:Number, 'default':0},
|
||||||
|
extraMonths: {type:Number, 'default':0},
|
||||||
|
|
||||||
gemsBought: {type: Number, 'default': 0},
|
gemsBought: {type: Number, 'default': 0},
|
||||||
mysteryItems: {type: Array, 'default': []}
|
mysteryItems: {type: Array, 'default': []}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -228,6 +228,9 @@ script(id='partials/options.settings.subscription.html',type='text/ng-template')
|
||||||
i.glyphicon.glyphicon-time
|
i.glyphicon.glyphicon-time
|
||||||
| #{env.t('subCanceled')} <strong>{{moment(p.dateTerminated).format('MM/DD/YYYY')}}</strong>
|
| #{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.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')
|
h2=env.t('individualSub')
|
||||||
div(ng-include="'partials/options.settings.subscription.perks.html'")
|
div(ng-include="'partials/options.settings.subscription.perks.html'")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue