mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 15:48:04 +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);
|
expect(recipient.purchased.plan.extraMonths).to.eql(3);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('updates date terminated for an existing plan with a terminated date', async () => {
|
it('adds to date terminated for an existing plan with a future terminated date', async () => {
|
||||||
let dateTerminated = new Date();
|
let dateTerminated = moment().add(1, 'months').toDate();
|
||||||
recipient.purchased.plan = plan;
|
recipient.purchased.plan = plan;
|
||||||
recipient.purchased.plan.dateTerminated = dateTerminated;
|
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());
|
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 () => {
|
it('sets a dateTerminated date for a user without an existing subscription', async () => {
|
||||||
expect(recipient.purchased.plan.dateTerminated).to.not.exist;
|
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
|
if (plan.customerId && !plan.dateTerminated) { // User has active plan
|
||||||
plan.extraMonths += months;
|
plan.extraMonths += months;
|
||||||
} else {
|
} else {
|
||||||
plan.dateTerminated = moment(plan.dateTerminated).add({months}).toDate();
|
|
||||||
if (!plan.dateUpdated) plan.dateUpdated = new Date();
|
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
|
if (!plan.customerId) plan.customerId = 'Gift'; // don't override existing customer, but all sub need a customerId
|
||||||
|
|||||||
Reference in New Issue
Block a user