feat: add checkIfLimitExceeded in UsageData model
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
import { raw } from 'objection';
|
||||
import Base from './base';
|
||||
import User from './user';
|
||||
import PaymentPlan from './payment-plan.ee';
|
||||
|
||||
class UsageData extends Base {
|
||||
id!: string;
|
||||
userId!: string;
|
||||
consumedTaskCount!: number;
|
||||
nextResetAt!: string;
|
||||
paymentPlan?: PaymentPlan;
|
||||
|
||||
static tableName = 'usage_data';
|
||||
|
||||
@@ -31,8 +33,22 @@ class UsageData extends Base {
|
||||
to: 'users.id',
|
||||
},
|
||||
},
|
||||
paymentPlan: {
|
||||
relation: Base.BelongsToOneRelation,
|
||||
modelClass: PaymentPlan,
|
||||
join: {
|
||||
from: 'usage_data.user_id',
|
||||
to: 'payment_plans.user_id',
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
async checkIfLimitExceeded() {
|
||||
const paymentPlan = await this.$relatedQuery('paymentPlan');
|
||||
|
||||
return this.consumedTaskCount >= paymentPlan.taskCount;
|
||||
}
|
||||
|
||||
async increaseConsumedTaskCountByOne() {
|
||||
return await this.$query().patch({ consumedTaskCount: raw('consumed_task_count + 1') });
|
||||
}
|
||||
|
Reference in New Issue
Block a user