refactor: User permission and limits to run flows
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
import { ValidationError } from 'objection';
|
||||
import type { ModelOptions, QueryContext, StaticHookArguments } from 'objection';
|
||||
import appConfig from '../config/app';
|
||||
import type {
|
||||
ModelOptions,
|
||||
QueryContext,
|
||||
StaticHookArguments,
|
||||
} from 'objection';
|
||||
import ExtendedQueryBuilder from './query-builder';
|
||||
import Base from './base';
|
||||
import Step from './step';
|
||||
import User from './user';
|
||||
import Execution from './execution';
|
||||
import Telemetry from '../helpers/telemetry';
|
||||
import QuotaExceededError from '../errors/quote-exceeded';
|
||||
|
||||
class Flow extends Base {
|
||||
id!: string;
|
||||
@@ -155,36 +157,7 @@ class Flow extends Base {
|
||||
|
||||
async isPaused() {
|
||||
const user = await this.$relatedQuery('user');
|
||||
const currentUsageData = await user.$relatedQuery('currentUsageData');
|
||||
|
||||
return await currentUsageData.checkIfLimitExceeded();
|
||||
}
|
||||
|
||||
async checkIfQuotaExceeded() {
|
||||
if (!appConfig.isCloud) return;
|
||||
|
||||
const user = await this.$relatedQuery('user');
|
||||
const usageData = await user.$relatedQuery('currentUsageData');
|
||||
|
||||
const hasExceeded = await usageData.checkIfLimitExceeded();
|
||||
|
||||
if (hasExceeded) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
async throwIfQuotaExceeded() {
|
||||
if (!appConfig.isCloud) return;
|
||||
|
||||
const hasExceeded = await this.checkIfQuotaExceeded();
|
||||
|
||||
if (hasExceeded) {
|
||||
throw new QuotaExceededError();
|
||||
}
|
||||
|
||||
return this;
|
||||
return await user.isAllowedToRunFlows();
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -2,7 +2,6 @@ import { raw } from 'objection';
|
||||
import Base from './base';
|
||||
import User from './user';
|
||||
import Subscription from './subscription.ee';
|
||||
import { getPlanById } from '../helpers/billing/plans.ee';
|
||||
|
||||
class UsageData extends Base {
|
||||
id!: string;
|
||||
@@ -47,28 +46,6 @@ class UsageData extends Base {
|
||||
},
|
||||
});
|
||||
|
||||
async checkIfLimitExceeded() {
|
||||
const user = await this.$relatedQuery('user');
|
||||
|
||||
if (await user.inTrial()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const subscription = await this.$relatedQuery('subscription');
|
||||
|
||||
if (!subscription) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!subscription.isActive) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const plan = subscription.plan;
|
||||
|
||||
return this.consumedTaskCount >= plan.quota;
|
||||
}
|
||||
|
||||
async increaseConsumedTaskCountByOne() {
|
||||
return await this.$query().patch({
|
||||
consumedTaskCount: raw('consumed_task_count + 1'),
|
||||
|
@@ -165,18 +165,24 @@ class User extends Base {
|
||||
this.trialExpiryDate = DateTime.now().plus({ days: 30 }).toISODate();
|
||||
}
|
||||
|
||||
async hasActiveSubscription() {
|
||||
if (!appConfig.isCloud) {
|
||||
return false;
|
||||
async isAllowedToRunFlows() {
|
||||
if (appConfig.isSelfHosted) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const subscription = await this.$relatedQuery('currentSubscription');
|
||||
if (await this.inTrial()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return subscription?.isActive;
|
||||
if ((await this.hasActiveSubscription()) && (await this.withinLimits())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
async inTrial() {
|
||||
if (!appConfig.isCloud) {
|
||||
if (appConfig.isSelfHosted) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -196,6 +202,24 @@ class User extends Base {
|
||||
return now < expiryDate;
|
||||
}
|
||||
|
||||
async hasActiveSubscription() {
|
||||
if (!appConfig.isCloud) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const subscription = await this.$relatedQuery('currentSubscription');
|
||||
|
||||
return subscription?.isActive;
|
||||
}
|
||||
|
||||
async withinLimits() {
|
||||
const currentSubscription = await this.$relatedQuery('currentSubscription');
|
||||
const plan = currentSubscription.plan;
|
||||
const currentUsageData = await this.$relatedQuery('currentUsageData');
|
||||
|
||||
return currentUsageData.consumedTaskCount >= plan.quota;
|
||||
}
|
||||
|
||||
async $beforeInsert(queryContext: QueryContext) {
|
||||
await super.$beforeInsert(queryContext);
|
||||
await this.generateHash();
|
||||
|
Reference in New Issue
Block a user