Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

fix(flows): turning on a flow that was never published now says to publish it first (#16481)

Bartosz Majewski002e1a7fe166
diff --git a/packages/server/api/src/app/flows/flow/flow.service.ts b/packages/server/api/src/app/flows/flow/flow.service.ts
index bee19066f86..1e93741af96 100644
--- a/packages/server/api/src/app/flows/flow/flow.service.ts
+++ b/packages/server/api/src/app/flows/flow/flow.service.ts
@@ -1,4 +1,4 @@
-import { ActivepiecesError, apId, assertNotNullOrUndefined, Cursor, ErrorCode, FlowId, FlowVersionId, isNil, Metadata, PlatformId, ProjectId, SeekPage, tryCatch, UserId } from '@activepieces/core-utils'
+import { ActivepiecesError, apId, Cursor, ErrorCode, FlowId, FlowVersionId, isNil, Metadata, PlatformId, ProjectId, SeekPage, tryCatch, UserId } from '@activepieces/core-utils'
import { apDayjs, apDayjsDuration } from '@activepieces/server-utils'
import { CreateFlowRequest, Flow, FlowCreator, FlowOperationRequest, FlowOperationStatus, FlowOperationType, flowPieceUtil, FlowStatus, FlowTriggerType, FlowVersion, FlowVersionState, PopulatedFlow, requiredActionsUtil, SharedTemplate, TelemetryEventName, TemplateStatus, TemplateType, TriggerSource, UncategorizedFolderId, UserWithMetaInformation } from '@activepieces/shared'
import dayjs from 'dayjs'
@@ -809,7 +809,12 @@ async function applyStatusChange(params: {
}
const publishedFlowVersionId = flowToUpdate.publishedVersionId
- assertNotNullOrUndefined(publishedFlowVersionId, 'publishedFlowVersionId is required')
+ if (isNil(publishedFlowVersionId)) {
+ throw new ActivepiecesError({
+ code: ErrorCode.FLOW_OPERATION_INVALID,
+ params: { message: 'Publish the flow before turning it on' },
+ })
+ }
const publishedFlowVersion = await flowVersionService(log).getFlowVersionOrThrow({
flowId: flowToUpdate.id,
versionId: publishedFlowVersionId,
diff --git a/packages/server/api/test/integration/ce/flows/flow/flow.test.ts b/packages/server/api/test/integration/ce/flows/flow/flow.test.ts
index cc4a2986c81..7a461030f08 100644
--- a/packages/server/api/test/integration/ce/flows/flow/flow.test.ts
+++ b/packages/server/api/test/integration/ce/flows/flow/flow.test.ts
@@ -1,3 +1,4 @@
+import { ErrorCode } from '@activepieces/core-utils'
import { WebhookRenewStrategy } from '@activepieces/pieces-framework'
import {
FlowOperationType,
@@ -191,6 +192,35 @@ describe('Flow API', () => {
expect(Object.keys(responseBody?.version)).toHaveLength(14)
expect(responseBody?.version?.id).toBe(mockFlowVersion.id)
})
+
+ it('Rejects enabling a never published Flow with a 400', async () => {
+ const ctx = await createTestContext(app!)
+
+ const createResponse = await ctx.post('/v1/flows', {
+ displayName: 'never published flow',
+ projectId: ctx.project.id,
+ }, { query: { projectId: ctx.project.id } })
+ expect(createResponse?.statusCode).toBe(StatusCodes.CREATED)
+ const flowId = createResponse?.json().id
+
+ const response = await ctx.post(`/v1/flows/${flowId}`, {
+ type: FlowOperationType.CHANGE_STATUS,
+ request: { status: 'ENABLED' },
+ })
+
+ expect(response?.statusCode).toBe(StatusCodes.BAD_REQUEST)
+ expect(response?.json()).toMatchObject({
+ code: ErrorCode.FLOW_OPERATION_INVALID,
+ params: { message: 'Publish the flow before turning it on' },
+ })
+
+ const disableResponse = await ctx.post(`/v1/flows/${flowId}`, {
+ type: FlowOperationType.CHANGE_STATUS,
+ request: { status: 'DISABLED' },
+ })
+ expect(disableResponse?.statusCode).toBe(StatusCodes.OK)
+ expect(disableResponse?.json().status).toBe(FlowStatus.DISABLED)
+ })
})
describe('Update published version id endpoint', () => {