@@ -2,6 +2,7 @@ import { tasks } from '@trigger.dev/sdk'
22import { and , eq } from 'drizzle-orm'
33import { type NextRequest , NextResponse } from 'next/server'
44import { checkServerSideUsageLimits } from '@/lib/billing'
5+ import { getHighestPrioritySubscription } from '@/lib/billing/core/subscription'
56import { env , isTruthy } from '@/lib/env'
67import { createLogger } from '@/lib/logs/console/logger'
78import {
@@ -11,7 +12,7 @@ import {
1112} from '@/lib/webhooks/utils'
1213import { executeWebhookJob } from '@/background/webhook-execution'
1314import { db } from '@/db'
14- import { subscription , webhook , workflow } from '@/db/schema'
15+ import { webhook , workflow } from '@/db/schema'
1516import { RateLimiter } from '@/services/queue'
1617import type { SubscriptionPlan } from '@/services/queue/types'
1718
@@ -247,21 +248,19 @@ export async function POST(
247248 }
248249
249250 // --- PHASE 3: Rate limiting for webhook execution ---
251+ let isEnterprise = false
250252 try {
251- // Get user subscription for rate limiting
252- const [ subscriptionRecord ] = await db
253- . select ( { plan : subscription . plan } )
254- . from ( subscription )
255- . where ( eq ( subscription . referenceId , foundWorkflow . userId ) )
256- . limit ( 1 )
253+ // Get user subscription for rate limiting (checks both personal and org subscriptions)
254+ const userSubscription = await getHighestPrioritySubscription ( foundWorkflow . userId )
257255
258- const subscriptionPlan = ( subscriptionRecord ?. plan || 'free' ) as SubscriptionPlan
256+ const subscriptionPlan = ( userSubscription ?. plan || 'free' ) as SubscriptionPlan
257+ isEnterprise = subscriptionPlan === 'enterprise'
259258
260259 // Check async rate limits (webhooks are processed asynchronously)
261260 const rateLimiter = new RateLimiter ( )
262- const rateLimitCheck = await rateLimiter . checkRateLimit (
261+ const rateLimitCheck = await rateLimiter . checkRateLimitWithSubscription (
263262 foundWorkflow . userId ,
264- subscriptionPlan ,
263+ userSubscription ,
265264 'webhook' ,
266265 true // isAsync = true for webhook execution
267266 )
@@ -333,7 +332,7 @@ export async function POST(
333332 // Continue processing - better to risk usage limit bypass than fail webhook
334333 }
335334
336- // --- PHASE 5: Queue webhook execution (trigger.dev or direct based on env) ---
335+ // --- PHASE 5: Queue webhook execution (trigger.dev or direct based on plan/ env) ---
337336 try {
338337 const payload = {
339338 webhookId : foundWebhook . id ,
@@ -346,7 +345,9 @@ export async function POST(
346345 blockId : foundWebhook . blockId ,
347346 }
348347
349- const useTrigger = isTruthy ( env . TRIGGER_DEV_ENABLED )
348+ // Enterprise users always execute directly, others check TRIGGER_DEV_ENABLED env
349+ // Note: isEnterprise was already determined during rate limiting phase
350+ const useTrigger = ! isEnterprise && isTruthy ( env . TRIGGER_DEV_ENABLED )
350351
351352 if ( useTrigger ) {
352353 const handle = await tasks . trigger ( 'webhook-execution' , payload )
@@ -358,8 +359,9 @@ export async function POST(
358359 void executeWebhookJob ( payload ) . catch ( ( error ) => {
359360 logger . error ( `[${ requestId } ] Direct webhook execution failed` , error )
360361 } )
362+ const reason = isEnterprise ? 'Enterprise plan' : 'Trigger.dev disabled'
361363 logger . info (
362- `[${ requestId } ] Queued direct webhook execution for ${ foundWebhook . provider } webhook (Trigger.dev disabled )`
364+ `[${ requestId } ] Queued direct webhook execution for ${ foundWebhook . provider } webhook (${ reason } )`
363365 )
364366 }
365367
0 commit comments