From 77e29050c846224bd0acd1688c1c1528bdcb9168 Mon Sep 17 00:00:00 2001 From: Ali BARIN Date: Sun, 9 Apr 2023 16:23:34 +0000 Subject: [PATCH] feat(flow): add virtual paused/published/draft status --- packages/backend/src/graphql/schema.graphql | 7 +++++ packages/backend/src/models/flow.ts | 30 ++++++++++++++++++- packages/backend/src/models/usage-data.ee.ts | 4 +++ packages/types/index.d.ts | 1 + packages/web/src/components/FlowRow/index.tsx | 24 +++++++++++++-- packages/web/src/graphql/queries/get-flow.ts | 1 + packages/web/src/graphql/queries/get-flows.ts | 1 + packages/web/src/locales/en.json | 1 + 8 files changed, 66 insertions(+), 3 deletions(-) diff --git a/packages/backend/src/graphql/schema.graphql b/packages/backend/src/graphql/schema.graphql index 2ffe0fcd..614ca958 100644 --- a/packages/backend/src/graphql/schema.graphql +++ b/packages/backend/src/graphql/schema.graphql @@ -250,6 +250,12 @@ type FlowEdge { node: Flow } +enum FlowStatus { + paused + published + draft +} + type Flow { id: String name: String @@ -257,6 +263,7 @@ type Flow { steps: [Step] createdAt: String updatedAt: String + status: FlowStatus } type Execution { diff --git a/packages/backend/src/models/flow.ts b/packages/backend/src/models/flow.ts index ed57c1f3..f9e1b191 100644 --- a/packages/backend/src/models/flow.ts +++ b/packages/backend/src/models/flow.ts @@ -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): Promise { + 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; diff --git a/packages/backend/src/models/usage-data.ee.ts b/packages/backend/src/models/usage-data.ee.ts index 047d0bf1..6ad13093 100644 --- a/packages/backend/src/models/usage-data.ee.ts +++ b/packages/backend/src/models/usage-data.ee.ts @@ -56,6 +56,10 @@ class UsageData extends Base { const subscription = await this.$relatedQuery('subscription'); + if (!subscription) { + return true; + } + if (!subscription.isActive) { return true; } diff --git a/packages/types/index.d.ts b/packages/types/index.d.ts index a8a7e48f..1dfcd3c0 100644 --- a/packages/types/index.d.ts +++ b/packages/types/index.d.ts @@ -73,6 +73,7 @@ export interface IFlow { name: string; userId: string; active: boolean; + status: 'paused' | 'published' | 'draft'; steps: IStep[]; createdAt: string; updatedAt: string; diff --git a/packages/web/src/components/FlowRow/index.tsx b/packages/web/src/components/FlowRow/index.tsx index 97b3d858..6fdafe91 100644 --- a/packages/web/src/components/FlowRow/index.tsx +++ b/packages/web/src/components/FlowRow/index.tsx @@ -18,6 +18,26 @@ type FlowRowProps = { flow: IFlow; }; +function getFlowStatusTranslationKey(status: IFlow["status"]): string { + if (status === 'published') { + return 'flow.published'; + } else if (status === 'paused') { + return 'flow.paused'; + } + + return 'flow.draft'; +} + +function getFlowStatusColor(status: IFlow["status"]): 'default' | 'primary' | 'secondary' | 'error' | 'info' | 'success' | 'warning' { + if (status === 'published') { + return 'success'; + } else if (status === 'paused') { + return 'error'; + } + + return 'info'; +} + export default function FlowRow(props: FlowRowProps): React.ReactElement { const formatMessage = useFormatMessage(); const contextButtonRef = React.useRef(null); @@ -76,10 +96,10 @@ export default function FlowRow(props: FlowRowProps): React.ReactElement { diff --git a/packages/web/src/graphql/queries/get-flow.ts b/packages/web/src/graphql/queries/get-flow.ts index f3e86446..521dcdab 100644 --- a/packages/web/src/graphql/queries/get-flow.ts +++ b/packages/web/src/graphql/queries/get-flow.ts @@ -6,6 +6,7 @@ export const GET_FLOW = gql` id name active + status steps { id type diff --git a/packages/web/src/graphql/queries/get-flows.ts b/packages/web/src/graphql/queries/get-flows.ts index ab1040f8..d2e5d321 100644 --- a/packages/web/src/graphql/queries/get-flows.ts +++ b/packages/web/src/graphql/queries/get-flows.ts @@ -26,6 +26,7 @@ export const GET_FLOWS = gql` createdAt updatedAt active + status steps { iconUrl } diff --git a/packages/web/src/locales/en.json b/packages/web/src/locales/en.json index b4b7437e..f2217d9a 100644 --- a/packages/web/src/locales/en.json +++ b/packages/web/src/locales/en.json @@ -43,6 +43,7 @@ "flow.active": "ON", "flow.inactive": "OFF", "flow.published": "Published", + "flow.paused": "Paused", "flow.draft": "Draft", "flow.successfullyDeleted": "The flow and associated executions have been deleted.", "flowEditor.publish": "PUBLISH",