refactor: User permission and limits to run flows
This commit is contained in:
@@ -39,6 +39,7 @@ type AppConfig = {
|
|||||||
smtpPassword: string;
|
smtpPassword: string;
|
||||||
fromEmail: string;
|
fromEmail: string;
|
||||||
isCloud: boolean;
|
isCloud: boolean;
|
||||||
|
isSelfHosted: boolean;
|
||||||
paddleVendorId: number;
|
paddleVendorId: number;
|
||||||
paddleVendorAuthCode: string;
|
paddleVendorAuthCode: string;
|
||||||
paddlePublicKey: string;
|
paddlePublicKey: string;
|
||||||
@@ -110,6 +111,7 @@ const appConfig: AppConfig = {
|
|||||||
smtpPassword: process.env.SMTP_PASSWORD,
|
smtpPassword: process.env.SMTP_PASSWORD,
|
||||||
fromEmail: process.env.FROM_EMAIL,
|
fromEmail: process.env.FROM_EMAIL,
|
||||||
isCloud: process.env.AUTOMATISCH_CLOUD === 'true',
|
isCloud: process.env.AUTOMATISCH_CLOUD === 'true',
|
||||||
|
isSelfHosted: process.env.AUTOMATISCH_CLOUD !== 'true',
|
||||||
paddleVendorId: Number(process.env.PADDLE_VENDOR_ID),
|
paddleVendorId: Number(process.env.PADDLE_VENDOR_ID),
|
||||||
paddleVendorAuthCode: process.env.PADDLE_VENDOR_AUTH_CODE,
|
paddleVendorAuthCode: process.env.PADDLE_VENDOR_AUTH_CODE,
|
||||||
paddlePublicKey: process.env.PADDLE_PUBLIC_KEY,
|
paddlePublicKey: process.env.PADDLE_PUBLIC_KEY,
|
||||||
|
@@ -6,17 +6,24 @@ import Flow from '../../models/flow';
|
|||||||
import { processTrigger } from '../../services/trigger';
|
import { processTrigger } from '../../services/trigger';
|
||||||
import actionQueue from '../../queues/action';
|
import actionQueue from '../../queues/action';
|
||||||
import globalVariable from '../../helpers/global-variable';
|
import globalVariable from '../../helpers/global-variable';
|
||||||
import { REMOVE_AFTER_30_DAYS_OR_150_JOBS, REMOVE_AFTER_7_DAYS_OR_50_JOBS } from '../../helpers/remove-job-configuration';
|
import QuotaExceededError from '../../errors/quote-exceeded';
|
||||||
|
import {
|
||||||
|
REMOVE_AFTER_30_DAYS_OR_150_JOBS,
|
||||||
|
REMOVE_AFTER_7_DAYS_OR_50_JOBS,
|
||||||
|
} from '../../helpers/remove-job-configuration';
|
||||||
|
|
||||||
export default async (request: IRequest, response: Response) => {
|
export default async (request: IRequest, response: Response) => {
|
||||||
const flow = await Flow.query()
|
const flow = await Flow.query()
|
||||||
.findById(request.params.flowId)
|
.findById(request.params.flowId)
|
||||||
.throwIfNotFound();
|
.throwIfNotFound();
|
||||||
|
|
||||||
const testRun = !flow.active;
|
const user = await flow.$relatedQuery('user');
|
||||||
|
|
||||||
if (!testRun) {
|
const testRun = !flow.active;
|
||||||
await flow.throwIfQuotaExceeded();
|
const quotaExceeded = !testRun && !(await user.isAllowedToRunFlows());
|
||||||
|
|
||||||
|
if (quotaExceeded) {
|
||||||
|
throw new QuotaExceededError();
|
||||||
}
|
}
|
||||||
|
|
||||||
const triggerStep = await flow.getTriggerStep();
|
const triggerStep = await flow.getTriggerStep();
|
||||||
@@ -58,7 +65,7 @@ export default async (request: IRequest, response: Response) => {
|
|||||||
headers: request.headers,
|
headers: request.headers,
|
||||||
body: request.body,
|
body: request.body,
|
||||||
query: request.query,
|
query: request.query,
|
||||||
}
|
};
|
||||||
|
|
||||||
rawInternalId = JSON.stringify(payload);
|
rawInternalId = JSON.stringify(payload);
|
||||||
}
|
}
|
||||||
@@ -74,7 +81,7 @@ export default async (request: IRequest, response: Response) => {
|
|||||||
flowId: flow.id,
|
flowId: flow.id,
|
||||||
stepId: triggerStep.id,
|
stepId: triggerStep.id,
|
||||||
triggerItem,
|
triggerItem,
|
||||||
testRun
|
testRun,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (testRun) {
|
if (testRun) {
|
||||||
@@ -93,7 +100,7 @@ export default async (request: IRequest, response: Response) => {
|
|||||||
const jobOptions = {
|
const jobOptions = {
|
||||||
removeOnComplete: REMOVE_AFTER_7_DAYS_OR_50_JOBS,
|
removeOnComplete: REMOVE_AFTER_7_DAYS_OR_50_JOBS,
|
||||||
removeOnFail: REMOVE_AFTER_30_DAYS_OR_150_JOBS,
|
removeOnFail: REMOVE_AFTER_30_DAYS_OR_150_JOBS,
|
||||||
}
|
};
|
||||||
|
|
||||||
await actionQueue.add(jobName, jobPayload, jobOptions);
|
await actionQueue.add(jobName, jobPayload, jobOptions);
|
||||||
|
|
||||||
|
@@ -1,13 +1,15 @@
|
|||||||
import { ValidationError } from 'objection';
|
import { ValidationError } from 'objection';
|
||||||
import type { ModelOptions, QueryContext, StaticHookArguments } from 'objection';
|
import type {
|
||||||
import appConfig from '../config/app';
|
ModelOptions,
|
||||||
|
QueryContext,
|
||||||
|
StaticHookArguments,
|
||||||
|
} from 'objection';
|
||||||
import ExtendedQueryBuilder from './query-builder';
|
import ExtendedQueryBuilder from './query-builder';
|
||||||
import Base from './base';
|
import Base from './base';
|
||||||
import Step from './step';
|
import Step from './step';
|
||||||
import User from './user';
|
import User from './user';
|
||||||
import Execution from './execution';
|
import Execution from './execution';
|
||||||
import Telemetry from '../helpers/telemetry';
|
import Telemetry from '../helpers/telemetry';
|
||||||
import QuotaExceededError from '../errors/quote-exceeded';
|
|
||||||
|
|
||||||
class Flow extends Base {
|
class Flow extends Base {
|
||||||
id!: string;
|
id!: string;
|
||||||
@@ -155,36 +157,7 @@ class Flow extends Base {
|
|||||||
|
|
||||||
async isPaused() {
|
async isPaused() {
|
||||||
const user = await this.$relatedQuery('user');
|
const user = await this.$relatedQuery('user');
|
||||||
const currentUsageData = await user.$relatedQuery('currentUsageData');
|
return await user.isAllowedToRunFlows();
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -2,7 +2,6 @@ import { raw } from 'objection';
|
|||||||
import Base from './base';
|
import Base from './base';
|
||||||
import User from './user';
|
import User from './user';
|
||||||
import Subscription from './subscription.ee';
|
import Subscription from './subscription.ee';
|
||||||
import { getPlanById } from '../helpers/billing/plans.ee';
|
|
||||||
|
|
||||||
class UsageData extends Base {
|
class UsageData extends Base {
|
||||||
id!: string;
|
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() {
|
async increaseConsumedTaskCountByOne() {
|
||||||
return await this.$query().patch({
|
return await this.$query().patch({
|
||||||
consumedTaskCount: raw('consumed_task_count + 1'),
|
consumedTaskCount: raw('consumed_task_count + 1'),
|
||||||
|
@@ -165,18 +165,24 @@ class User extends Base {
|
|||||||
this.trialExpiryDate = DateTime.now().plus({ days: 30 }).toISODate();
|
this.trialExpiryDate = DateTime.now().plus({ days: 30 }).toISODate();
|
||||||
}
|
}
|
||||||
|
|
||||||
async hasActiveSubscription() {
|
async isAllowedToRunFlows() {
|
||||||
if (!appConfig.isCloud) {
|
if (appConfig.isSelfHosted) {
|
||||||
return false;
|
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() {
|
async inTrial() {
|
||||||
if (!appConfig.isCloud) {
|
if (appConfig.isSelfHosted) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -196,6 +202,24 @@ class User extends Base {
|
|||||||
return now < expiryDate;
|
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) {
|
async $beforeInsert(queryContext: QueryContext) {
|
||||||
await super.$beforeInsert(queryContext);
|
await super.$beforeInsert(queryContext);
|
||||||
await this.generateHash();
|
await this.generateHash();
|
||||||
|
@@ -17,10 +17,10 @@ export const worker = new Worker(
|
|||||||
const { flowId } = job.data;
|
const { flowId } = job.data;
|
||||||
|
|
||||||
const flow = await Flow.query().findById(flowId).throwIfNotFound();
|
const flow = await Flow.query().findById(flowId).throwIfNotFound();
|
||||||
|
const user = await flow.$relatedQuery('user');
|
||||||
|
const allowedToRunFlows = await user.isAllowedToRunFlows();
|
||||||
|
|
||||||
const quotaExceeded = await flow.checkIfQuotaExceeded();
|
if (!allowedToRunFlows) {
|
||||||
|
|
||||||
if (quotaExceeded) {
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user