1+ import { db } from '@sim/db'
2+ import { workflow as workflowTable } from '@sim/db/schema'
13import { createLogger } from '@sim/logger'
24import { toError } from '@sim/utils/errors'
35import { generateId , isValidUuid } from '@sim/utils/id'
6+ import { eq } from 'drizzle-orm'
47import { type NextRequest , NextResponse } from 'next/server'
58import { z } from 'zod'
69import { AuthType , checkHybridAuth , hasExternalApiCredentials } from '@/lib/auth/hybrid'
@@ -48,6 +51,10 @@ import {
4851 workflowHasResponseBlock ,
4952} from '@/lib/workflows/utils'
5053import { executeWorkflowJob , type WorkflowExecutionPayload } from '@/background/workflow-execution'
54+ import {
55+ PublicApiNotAllowedError ,
56+ validatePublicApiAllowed ,
57+ } from '@/ee/access-control/utils/permission-check'
5158import { normalizeName } from '@/executor/constants'
5259import { ExecutionSnapshot } from '@/executor/execution/snapshot'
5360import type {
@@ -312,9 +319,7 @@ async function handleExecutePost(
312319 return NextResponse . json ( { error : auth . error || 'Unauthorized' } , { status : 401 } )
313320 }
314321
315- const { db : dbClient , workflow : workflowTable } = await import ( '@sim/db' )
316- const { eq } = await import ( 'drizzle-orm' )
317- const [ wf ] = await dbClient
322+ const [ wf ] = await db
318323 . select ( {
319324 isPublicApi : workflowTable . isPublicApi ,
320325 isDeployed : workflowTable . isDeployed ,
@@ -325,23 +330,17 @@ async function handleExecutePost(
325330 . where ( eq ( workflowTable . id , workflowId ) )
326331 . limit ( 1 )
327332
328- if ( ! wf ?. isPublicApi || ! wf . isDeployed ) {
329- return NextResponse . json ( { error : auth . error || 'Unauthorized' } , { status : 401 } )
330- }
331-
332- const { isPublicApiDisabled } = await import ( '@/lib/core/config/feature-flags' )
333- if ( isPublicApiDisabled ) {
333+ if ( ! wf ?. isPublicApi || ! wf . isDeployed || ! wf . workspaceId ) {
334334 return NextResponse . json ( { error : auth . error || 'Unauthorized' } , { status : 401 } )
335335 }
336336
337- if ( wf . workspaceId ) {
338- const { getUserPermissionConfig } = await import (
339- '@/ee/access-control/utils/permission-check'
340- )
341- const ownerConfig = await getUserPermissionConfig ( wf . userId , wf . workspaceId )
342- if ( ownerConfig ?. disablePublicApi ) {
337+ try {
338+ await validatePublicApiAllowed ( wf . userId , wf . workspaceId )
339+ } catch ( err ) {
340+ if ( err instanceof PublicApiNotAllowedError ) {
343341 return NextResponse . json ( { error : auth . error || 'Unauthorized' } , { status : 401 } )
344342 }
343+ throw err
345344 }
346345
347346 userId = wf . userId
0 commit comments