Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion server/models/Collective.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,8 @@ class Collective extends ModelWithPublicId<
// Cancel the platform subscription if it exists
const currentSubscription = await PlatformSubscription.getCurrentSubscription(this.id, { transaction });
if (currentSubscription) {
// Inclusive: false in case they start a new subscription on the same day. This also means we won't bill for the last day.
// The subscription is terminated at the start of the day (we don't bill for the deactivation day),
// so that a new one can be created if money management is reactivated the same day.
await currentSubscription.terminate({ inclusive: false, transaction });
}
};
Expand Down
8 changes: 6 additions & 2 deletions server/models/PlatformSubscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ class PlatformSubscription extends Model<
if (subStart.isSameOrAfter(dayStart)) {
await this.destroy({ transaction });
} else {
await this.update({ period: [this.start, { value: date, inclusive }] }, { transaction });
// Periods are aligned to day boundaries (see `createSubscription`): terminate at the start of
// the day so that a subscription created later the same day doesn't overlap this one.
await this.update({ period: [this.start, { value: dayStart.toDate(), inclusive }] }, { transaction });
}
}

Expand Down Expand Up @@ -578,7 +580,9 @@ class PlatformSubscription extends Model<
notify?: boolean;
},
): Promise<PlatformSubscription> {
const currentSubscription = await PlatformSubscription.getCurrentSubscription(collective.id);
const currentSubscription = await PlatformSubscription.getCurrentSubscription(collective.id, {
transaction: opts?.transaction,
});
const newSubscriptionStart = moment.utc(when).startOf('day');
const previousPlan = currentSubscription?.plan;

Expand Down
104 changes: 104 additions & 0 deletions test/server/models/PlatformSubscriptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,79 @@ describe('server/models/PlatformSubscriptions', () => {
).to.be.rejectedWith(Error);
});

it('can create a subscription the same day the previous one was terminated', async () => {
const admin = await fakeUser();
const collective = await fakeCollective({ admin });
const subscription = await PlatformSubscription.createSubscription(
collective,
new Date(Date.UTC(2016, 0, 1)),
{
title: 'A plan',
},
admin,
);

// Terminate mid-day, like `deactivateMoneyManagement` does
await subscription.terminate({ date: new Date(Date.UTC(2016, 0, 5, 13, 59, 46)), inclusive: false });

// The termination is aligned to the start of the day
await subscription.reload();
expect(subscription.end.inclusive).to.be.false;
expect(moment.utc(subscription.end.value).toISOString()).to.equal('2016-01-05T00:00:00.000Z');

const newSubscription = await PlatformSubscription.createSubscription(
collective,
new Date(Date.UTC(2016, 0, 5, 15, 0, 0)),
{
title: 'A plan',
},
admin,
);

expect(newSubscription.start.inclusive).to.be.true;
expect(moment.utc(newSubscription.start.value).toISOString()).to.equal('2016-01-05T00:00:00.000Z');

expect(newSubscription.end.inclusive).to.be.true;
expect(newSubscription.end.value).to.equal(Infinity);
});

it('supports repeatedly terminating and re-creating subscriptions the same day', async () => {
const admin = await fakeUser();
const collective = await fakeCollective({ admin });
const plan = { title: 'A plan' };
const firstSubscription = await PlatformSubscription.createSubscription(
collective,
new Date(Date.UTC(2016, 0, 1)),
plan,
admin,
);

// First deactivation: the subscription started on a previous day, its period is truncated
await firstSubscription.terminate({ date: new Date(Date.UTC(2016, 0, 5, 10, 0, 0)), inclusive: false });

// Second and third cycles: the subscription started the same day, it is destroyed on termination
for (const hour of [11, 13]) {
const subscription = await PlatformSubscription.createSubscription(
collective,
new Date(Date.UTC(2016, 0, 5, hour, 0, 0)),
plan,
admin,
);
await subscription.terminate({ date: new Date(Date.UTC(2016, 0, 5, hour + 1, 0, 0)), inclusive: false });
await subscription.reload({ paranoid: false });
expect(subscription.deletedAt).to.not.be.null;
}

const lastSubscription = await PlatformSubscription.createSubscription(
collective,
new Date(Date.UTC(2016, 0, 5, 15, 0, 0)),
plan,
admin,
);
expect(moment.utc(lastSubscription.start.value).toISOString()).to.equal('2016-01-05T00:00:00.000Z');
expect(lastSubscription.end.value).to.equal(Infinity);
});

it('emits PLATFORM_SUBSCRIPTION_UPDATED activity', async () => {
const admin = await fakeUser();
const collective = await fakeCollective({ admin });
Expand Down Expand Up @@ -353,6 +426,37 @@ describe('server/models/PlatformSubscriptions', () => {
expect(newSubscription.end.value).to.equal(Infinity);
});

it('can replace a subscription the same day the previous one was terminated', async () => {
const admin = await fakeUser();
const collective = await fakeCollective({ admin });
const subscription = await PlatformSubscription.createSubscription(
collective,
new Date(Date.UTC(2016, 0, 1)),
{
title: 'A plan',
},
admin,
);

// Terminate mid-day, like `deactivateMoneyManagement` does
await subscription.terminate({ date: new Date(Date.UTC(2016, 0, 5, 13, 59, 46)), inclusive: false });

const newSubscription = await PlatformSubscription.replaceCurrentSubscription(
collective,
new Date(Date.UTC(2016, 0, 5, 15, 0, 0)),
{
title: 'A new plan',
},
admin,
);

expect(newSubscription.start.inclusive).to.be.true;
expect(moment.utc(newSubscription.start.value).toISOString()).to.equal('2016-01-05T00:00:00.000Z');

expect(newSubscription.end.inclusive).to.be.true;
expect(newSubscription.end.value).to.equal(Infinity);
});

it('emits PLATFORM_SUBSCRIPTION_UPDATED activity with legacy and new plan details', async () => {
const admin = await fakeUser();
const collective = await fakeCollective({ admin });
Expand Down
Loading