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