feat: add subscription update and cancel logics
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
import { raw } from 'objection';
|
||||
import Base from './base';
|
||||
import User from './user';
|
||||
import PaymentPlan from './payment-plan.ee';
|
||||
import Subscription from './subscription.ee';
|
||||
import { getPlanById } from '../helpers/billing/plans.ee';
|
||||
|
||||
class UsageData extends Base {
|
||||
id!: string;
|
||||
userId!: string;
|
||||
subscriptionId?: string;
|
||||
consumedTaskCount!: number;
|
||||
nextResetAt!: string;
|
||||
paymentPlan?: PaymentPlan;
|
||||
subscription?: Subscription;
|
||||
|
||||
static tableName = 'usage_data';
|
||||
|
||||
@@ -19,6 +21,7 @@ class UsageData extends Base {
|
||||
properties: {
|
||||
id: { type: 'string', format: 'uuid' },
|
||||
userId: { type: 'string', format: 'uuid' },
|
||||
subscriptionId: { type: 'string', format: 'uuid' },
|
||||
consumedTaskCount: { type: 'integer' },
|
||||
nextResetAt: { type: 'string' },
|
||||
},
|
||||
@@ -33,20 +36,22 @@ class UsageData extends Base {
|
||||
to: 'users.id',
|
||||
},
|
||||
},
|
||||
paymentPlan: {
|
||||
subscription: {
|
||||
relation: Base.BelongsToOneRelation,
|
||||
modelClass: PaymentPlan,
|
||||
modelClass: Subscription,
|
||||
join: {
|
||||
from: 'usage_data.user_id',
|
||||
to: 'payment_plans.user_id',
|
||||
from: 'usage_data.subscription_id',
|
||||
to: 'subscriptions.id',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
async checkIfLimitExceeded() {
|
||||
const paymentPlan = await this.$relatedQuery('paymentPlan');
|
||||
const subscription = await this.$relatedQuery('subscription');
|
||||
|
||||
return this.consumedTaskCount >= paymentPlan.taskCount;
|
||||
const plan = subscription.plan;
|
||||
|
||||
return this.consumedTaskCount >= plan.quota;
|
||||
}
|
||||
|
||||
async increaseConsumedTaskCountByOne() {
|
||||
|
Reference in New Issue
Block a user