mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 07:37:25 +01:00
fix(subscriptions): gift termination date (#7948)
This commit is contained in:
@@ -78,8 +78,8 @@ describe('payments/index', () => {
|
||||
expect(recipient.purchased.plan.extraMonths).to.eql(3);
|
||||
});
|
||||
|
||||
it('updates date terminated for an existing plan with a terminated date', async () => {
|
||||
let dateTerminated = new Date();
|
||||
it('adds to date terminated for an existing plan with a future terminated date', async () => {
|
||||
let dateTerminated = moment().add(1, 'months').toDate();
|
||||
recipient.purchased.plan = plan;
|
||||
recipient.purchased.plan.dateTerminated = dateTerminated;
|
||||
|
||||
@@ -88,6 +88,15 @@ describe('payments/index', () => {
|
||||
expect(recipient.purchased.plan.dateTerminated).to.eql(moment(dateTerminated).add(3, 'months').toDate());
|
||||
});
|
||||
|
||||
it('replaces date terminated for an account with a past terminated date', async () => {
|
||||
let dateTerminated = moment().subtract(1, 'months').toDate();
|
||||
recipient.purchased.plan.dateTerminated = dateTerminated;
|
||||
|
||||
await api.createSubscription(data);
|
||||
|
||||
expect(moment(recipient.purchased.plan.dateTerminated).format('YYYY-MM-DD')).to.eql(moment().add(3, 'months').format('YYYY-MM-DD'));
|
||||
});
|
||||
|
||||
it('sets a dateTerminated date for a user without an existing subscription', async () => {
|
||||
expect(recipient.purchased.plan.dateTerminated).to.not.exist;
|
||||
|
||||
|
||||
@@ -34,8 +34,12 @@ api.createSubscription = async function createSubscription (data) {
|
||||
if (plan.customerId && !plan.dateTerminated) { // User has active plan
|
||||
plan.extraMonths += months;
|
||||
} else {
|
||||
plan.dateTerminated = moment(plan.dateTerminated).add({months}).toDate();
|
||||
if (!plan.dateUpdated) plan.dateUpdated = new Date();
|
||||
if (moment(plan.dateTerminated).isAfter()) {
|
||||
plan.dateTerminated = moment(plan.dateTerminated).add({months}).toDate();
|
||||
} else {
|
||||
plan.dateTerminated = moment().add({months}).toDate();
|
||||
}
|
||||
}
|
||||
|
||||
if (!plan.customerId) plan.customerId = 'Gift'; // don't override existing customer, but all sub need a customerId
|
||||
|
||||
Reference in New Issue
Block a user