mirror of https://github.com/laurent22/joplin.git
Server: Re-enable account when subscription is paid
parent
d8626919e0
commit
4b5318c6d0
|
@ -40,4 +40,26 @@ describe('SubscriptionModel', function() {
|
||||||
expect(sub.user_id).toBe(user.id);
|
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);
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
|
@ -74,19 +74,35 @@ export default class SubscriptionModel extends BaseModel<Subscription> {
|
||||||
|
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
|
||||||
const toSave: Subscription = { id: sub.id };
|
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
toSave.last_payment_time = now;
|
// When a payment is successful, we also activate upload and enable
|
||||||
toSave.last_payment_failed_time = 0;
|
// the user, in case it has been disabled previously due to a failed
|
||||||
await this.save(toSave);
|
// 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 {
|
} else {
|
||||||
// We only update the payment failed time if it's not already set
|
// 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
|
// since the only thing that matter is the first time the payment
|
||||||
// failed.
|
// 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) {
|
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'] });
|
const user = await this.models().user().load(sub.user_id, { fields: ['email', 'id', 'full_name'] });
|
||||||
|
|
||||||
await this.models().email().push({
|
await this.models().email().push({
|
||||||
|
@ -97,7 +113,10 @@ export default class SubscriptionModel extends BaseModel<Subscription> {
|
||||||
sender_id: EmailSender.Support,
|
sender_id: EmailSender.Support,
|
||||||
});
|
});
|
||||||
|
|
||||||
await this.save(toSave);
|
await this.save({
|
||||||
|
id: sub.id,
|
||||||
|
last_payment_failed_time: now,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue