fix: subscription middleare

This commit is contained in:
Ahmed Bouhuolia
2024-08-25 12:42:42 +02:00
parent 959ef7a691
commit ee2d8d3065
5 changed files with 77 additions and 14 deletions

View File

@@ -2,6 +2,7 @@ import { Model, mixin } from 'objection';
import SystemModel from '@/system/models/SystemModel';
import moment from 'moment';
import SubscriptionPeriod from '@/services/Subscription/SubscriptionPeriod';
import { SubscriptionPaymentStatus } from '@/interfaces';
export default class PlanSubscription extends mixin(SystemModel) {
public lemonSubscriptionId: number;
@@ -13,6 +14,8 @@ export default class PlanSubscription extends mixin(SystemModel) {
public trialEndsAt: Date;
public paymentStatus: SubscriptionPaymentStatus;
/**
* Table name.
*/
@@ -31,7 +34,16 @@ export default class PlanSubscription extends mixin(SystemModel) {
* Defined virtual attributes.
*/
static get virtualAttributes() {
return ['active', 'inactive', 'ended', 'canceled', 'onTrial', 'status'];
return [
'active',
'inactive',
'ended',
'canceled',
'onTrial',
'status',
'isPaymentFailed',
'isPaymentSucceed',
];
}
/**
@@ -69,6 +81,22 @@ export default class PlanSubscription extends mixin(SystemModel) {
builder.where('trial_ends_at', '<=', endDate);
},
/**
* Filter the failed payment.
* @param builder
*/
failedPayment(builder) {
builder.where('payment_status', SubscriptionPaymentStatus.Failed);
},
/**
* Filter the succeed payment.
* @param builder
*/
succeedPayment(builder) {
builder.where('payment_status', SubscriptionPaymentStatus.Succeed);
},
};
}
@@ -108,6 +136,10 @@ export default class PlanSubscription extends mixin(SystemModel) {
/**
* Check if the subscription is active.
* Crtiria should be:
* - During the trial period and NOT canceled.
* - Not ended.
*
* @return {Boolean}
*/
public active() {
@@ -200,4 +232,20 @@ export default class PlanSubscription extends mixin(SystemModel) {
);
return this.$query().update({ startsAt, endsAt });
}
/**
* Detarmines the subscription payment whether is failed.
* @returns {boolean}
*/
public isPaymentFailed() {
return this.paymentStatus === SubscriptionPaymentStatus.Failed;
}
/**
* Detarmines the subscription payment whether is succeed.
* @returns {boolean}
*/
public isPaymentSucceed() {
return this.paymentStatus === SubscriptionPaymentStatus.Succeed;
}
}