feat(flow): add virtual paused/published/draft status

This commit is contained in:
Ali BARIN
2023-04-09 16:23:34 +00:00
parent df55f746ca
commit 77e29050c8
8 changed files with 66 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
import { ValidationError } from 'objection';
import type { ModelOptions, QueryContext } from 'objection';
import type { ModelOptions, QueryContext, StaticHookArguments } from 'objection';
import appConfig from '../config/app';
import ExtendedQueryBuilder from './query-builder';
import Base from './base';
@@ -14,6 +14,7 @@ class Flow extends Base {
name!: string;
userId!: string;
active: boolean;
status: 'paused' | 'published' | 'draft';
steps: Step[];
published_at: string;
remoteWebhookId: string;
@@ -65,6 +66,26 @@ class Flow extends Base {
},
});
static async afterFind(args: StaticHookArguments<any>): Promise<any> {
const { result } = args;
const referenceFlow = result[0];
if (referenceFlow) {
const shouldBePaused = await referenceFlow.isPaused();
for (const flow of result) {
if (!flow.active) {
flow.status = 'draft';
} else if (flow.active && shouldBePaused) {
flow.status = 'paused';
} else {
flow.status = 'published';
}
}
}
}
async lastInternalId() {
const lastExecution = await this.$relatedQuery('executions')
.orderBy('created_at', 'desc')
@@ -132,6 +153,13 @@ 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;