11import { createLogger } from '@sim/logger'
22import { getErrorMessage } from '@sim/utils/errors'
3- import {
4- assertWorkflowMutable ,
5- getActiveWorkflowRecord ,
6- WorkflowLockedError ,
7- } from '@sim/workflow-authz'
3+ import { assertWorkflowMutable , WorkflowLockedError } from '@sim/workflow-authz'
84import { type NextRequest , NextResponse } from 'next/server'
95import {
106 v1DeployWorkflowBodySchema ,
@@ -16,12 +12,10 @@ import { generateRequestId } from '@/lib/core/utils/request'
1612import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
1713import { captureServerEvent } from '@/lib/posthog/server'
1814import { performFullDeploy , performFullUndeploy } from '@/lib/workflows/orchestration'
15+ import { statusForOrchestrationError } from '@/lib/workflows/orchestration/types'
1916import { createApiResponse , getUserLimits } from '@/app/api/v1/logs/meta'
20- import {
21- checkRateLimit ,
22- createRateLimitResponse ,
23- validateWorkspaceAccess ,
24- } from '@/app/api/v1/middleware'
17+ import { checkRateLimit , createRateLimitResponse } from '@/app/api/v1/middleware'
18+ import { resolveV1DeploymentWorkflow } from '@/app/api/v1/workflows/utils'
2519
2620const logger = createLogger ( 'V1WorkflowDeployAPI' )
2721
@@ -55,16 +49,9 @@ export const POST = withRouteHandler(
5549 return validationErrorResponse ( body . error )
5650 }
5751
58- const workflowData = await getActiveWorkflowRecord ( id )
59- if ( ! workflowData ?. workspaceId ) {
60- return NextResponse . json ( { error : 'Workflow not found' } , { status : 404 } )
61- }
62- const workspaceId = workflowData . workspaceId
63-
64- const accessError = await validateWorkspaceAccess ( rateLimit , userId , workspaceId , 'admin' )
65- if ( accessError ) {
66- return NextResponse . json ( { error : 'Workflow not found' } , { status : 404 } )
67- }
52+ const target = await resolveV1DeploymentWorkflow ( rateLimit , userId , id )
53+ if ( ! target . ok ) return target . response
54+ const { workflow, workspaceId } = target
6855
6956 await assertWorkflowMutable ( id )
7057
@@ -73,17 +60,18 @@ export const POST = withRouteHandler(
7360 const result = await performFullDeploy ( {
7461 workflowId : id ,
7562 userId,
76- workflowName : workflowData . name || undefined ,
63+ workflowName : workflow . name || undefined ,
7764 versionName : body . data . name ,
7865 versionDescription : body . data . description ?? undefined ,
7966 requestId,
8067 request,
8168 } )
8269
8370 if ( ! result . success ) {
84- const status =
85- result . errorCode === 'validation' ? 400 : result . errorCode === 'not_found' ? 404 : 500
86- return NextResponse . json ( { error : result . error || 'Failed to deploy workflow' } , { status } )
71+ return NextResponse . json (
72+ { error : result . error || 'Failed to deploy workflow' } ,
73+ { status : statusForOrchestrationError ( result . errorCode ) }
74+ )
8775 }
8876
8977 captureServerEvent (
@@ -142,18 +130,11 @@ export const DELETE = withRouteHandler(
142130
143131 const { id } = parsed . data . params
144132
145- const workflowData = await getActiveWorkflowRecord ( id )
146- if ( ! workflowData ?. workspaceId ) {
147- return NextResponse . json ( { error : 'Workflow not found' } , { status : 404 } )
148- }
149- const workspaceId = workflowData . workspaceId
150-
151- const accessError = await validateWorkspaceAccess ( rateLimit , userId , workspaceId , 'admin' )
152- if ( accessError ) {
153- return NextResponse . json ( { error : 'Workflow not found' } , { status : 404 } )
154- }
133+ const target = await resolveV1DeploymentWorkflow ( rateLimit , userId , id )
134+ if ( ! target . ok ) return target . response
135+ const { workflow, workspaceId } = target
155136
156- if ( ! workflowData . isDeployed ) {
137+ if ( ! workflow . isDeployed ) {
157138 return NextResponse . json ( { error : 'Workflow is not deployed' } , { status : 400 } )
158139 }
159140
0 commit comments