Server: Re-enable account when subscription is paid

pull/5291/head
Laurent Cozic 2021-08-10 18:21:15 +01:00
parent d8626919e0
commit 4b5318c6d0
2 changed files with 49 additions and 8 deletions

View File

@ -40,4 +40,26 @@ describe('SubscriptionModel', function() {
expect(sub.user_id).toBe(user.id);
});
test('should enable and allow the user to upload if a payment is successful', async function() {
let { user } = await models().subscription().saveUserAndSubscription(
'toto@example.com',
'Toto',
AccountType.Pro,
'STRIPE_USER_ID',
'STRIPE_SUB_ID'
);
await models().user().save({
id: user.id,
enabled: 0,
can_upload: 0,
});
await models().subscription().handlePayment('STRIPE_SUB_ID', true);
user = await models().user().load(user.id);
expect(user.can_upload).toBe(1);
expect(user.enabled).toBe(1);
});
});

View File

@ -74,19 +74,35 @@ export default class SubscriptionModel extends BaseModel<Subscription> {
const now = Date.now();
const toSave: Subscription = { id: sub.id };
if (success) {
toSave.last_payment_time = now;
toSave.last_payment_failed_time = 0;
await this.save(toSave);
// When a payment is successful, we also activate upload and enable
// the user, in case it has been disabled previously due to a failed
// payment.
const user = await this.models().user().load(sub.user_id);
await this.withTransaction(async () => {
if (!user.enabled || !user.can_upload) {
await this.models().user().save({
id: sub.user_id,
enabled: 1,
can_upload: 1,
});
}
await this.save({
id: sub.id,
last_payment_time: now,
last_payment_failed_time: 0,
});
});
} else {
// We only update the payment failed time if it's not already set
// since the only thing that matter is the first time the payment
// failed.
//
// We don't update the user can_upload and enabled properties here
// because it's done after a few days from CronService.
if (!sub.last_payment_failed_time) {
toSave.last_payment_failed_time = now;
const user = await this.models().user().load(sub.user_id, { fields: ['email', 'id', 'full_name'] });
await this.models().email().push({
@ -97,7 +113,10 @@ export default class SubscriptionModel extends BaseModel<Subscription> {
sender_id: EmailSender.Support,
});
await this.save(toSave);
await this.save({
id: sub.id,
last_payment_failed_time: now,
});
}
}
}