@@ -7,13 +7,7 @@ import { getSession } from '@/lib/auth'
77import { validateInteger } from '@/lib/core/security/input-validation'
88import { PlatformEvents } from '@/lib/core/telemetry'
99import { generateRequestId } from '@/lib/core/utils/request'
10- import { resolveEnvVarsInObject } from '@/lib/webhooks/env-resolver'
11- import {
12- cleanupExternalWebhook ,
13- createExternalWebhookSubscription ,
14- shouldRecreateExternalWebhookSubscription ,
15- } from '@/lib/webhooks/provider-subscriptions'
16- import { mergeNonUserFields } from '@/lib/webhooks/utils'
10+ import { cleanupExternalWebhook } from '@/lib/webhooks/provider-subscriptions'
1711import { getUserEntityPermissions } from '@/lib/workspaces/permissions/utils'
1812
1913const logger = createLogger ( 'WebhookAPI' )
@@ -89,7 +83,6 @@ export async function GET(request: NextRequest, { params }: { params: Promise<{
8983 }
9084}
9185
92- // Update a webhook
9386export async function PATCH ( request : NextRequest , { params } : { params : Promise < { id : string } > } ) {
9487 const requestId = generateRequestId ( )
9588
@@ -104,7 +97,7 @@ export async function PATCH(request: NextRequest, { params }: { params: Promise<
10497 }
10598
10699 const body = await request . json ( )
107- const { path , provider , providerConfig , isActive, failedCount } = body
100+ const { isActive, failedCount } = body
108101
109102 if ( failedCount !== undefined ) {
110103 const validation = validateInteger ( failedCount , 'failedCount' , { min : 0 } )
@@ -114,28 +107,6 @@ export async function PATCH(request: NextRequest, { params }: { params: Promise<
114107 }
115108 }
116109
117- const originalProviderConfig = providerConfig
118- let resolvedProviderConfig = providerConfig
119- if ( providerConfig ) {
120- const webhookDataForResolve = await db
121- . select ( {
122- workspaceId : workflow . workspaceId ,
123- } )
124- . from ( webhook )
125- . innerJoin ( workflow , eq ( webhook . workflowId , workflow . id ) )
126- . where ( eq ( webhook . id , id ) )
127- . limit ( 1 )
128-
129- if ( webhookDataForResolve . length > 0 ) {
130- resolvedProviderConfig = await resolveEnvVarsInObject (
131- providerConfig ,
132- session . user . id ,
133- webhookDataForResolve [ 0 ] . workspaceId || undefined
134- )
135- }
136- }
137-
138- // Find the webhook and check permissions
139110 const webhooks = await db
140111 . select ( {
141112 webhook : webhook ,
@@ -156,16 +127,12 @@ export async function PATCH(request: NextRequest, { params }: { params: Promise<
156127 }
157128
158129 const webhookData = webhooks [ 0 ]
159-
160- // Check if user has permission to modify this webhook
161130 let canModify = false
162131
163- // Case 1: User owns the workflow
164132 if ( webhookData . workflow . userId === session . user . id ) {
165133 canModify = true
166134 }
167135
168- // Case 2: Workflow belongs to a workspace and user has write or admin permission
169136 if ( ! canModify && webhookData . workflow . workspaceId ) {
170137 const userPermission = await getUserEntityPermissions (
171138 session . user . id ,
@@ -184,70 +151,14 @@ export async function PATCH(request: NextRequest, { params }: { params: Promise<
184151 return NextResponse . json ( { error : 'Access denied' } , { status : 403 } )
185152 }
186153
187- const existingProviderConfig =
188- ( webhookData . webhook . providerConfig as Record < string , unknown > ) || { }
189- const nextProvider = ( provider ?? webhookData . webhook . provider ) as string
190-
191- let nextProviderConfig =
192- providerConfig !== undefined &&
193- resolvedProviderConfig &&
194- typeof resolvedProviderConfig === 'object'
195- ? ( resolvedProviderConfig as Record < string , unknown > )
196- : existingProviderConfig
197-
198- if (
199- providerConfig !== undefined &&
200- shouldRecreateExternalWebhookSubscription ( {
201- previousProvider : webhookData . webhook . provider as string ,
202- nextProvider,
203- previousConfig : existingProviderConfig ,
204- nextConfig : originalProviderConfig as Record < string , unknown > ,
205- } )
206- ) {
207- await cleanupExternalWebhook (
208- { ...webhookData . webhook , providerConfig : existingProviderConfig } ,
209- webhookData . workflow ,
210- requestId
211- )
212-
213- const result = await createExternalWebhookSubscription (
214- request ,
215- {
216- ...webhookData . webhook ,
217- provider : nextProvider ,
218- providerConfig : nextProviderConfig ,
219- } ,
220- webhookData . workflow ,
221- session . user . id ,
222- requestId
223- )
224-
225- nextProviderConfig = result . updatedProviderConfig as Record < string , unknown >
226- }
227-
228154 logger . debug ( `[${ requestId } ] Updating webhook properties` , {
229- hasPathUpdate : path !== undefined ,
230- hasProviderUpdate : provider !== undefined ,
231- hasConfigUpdate : providerConfig !== undefined ,
232155 hasActiveUpdate : isActive !== undefined ,
233156 hasFailedCountUpdate : failedCount !== undefined ,
234157 } )
235158
236- let finalProviderConfig : Record < string , unknown > =
237- ( webhooks [ 0 ] . webhook . providerConfig as Record < string , unknown > ) || { }
238- if ( providerConfig !== undefined && originalProviderConfig ) {
239- const userProvided = originalProviderConfig as Record < string , unknown >
240- finalProviderConfig = { ...userProvided }
241- mergeNonUserFields ( finalProviderConfig , existingProviderConfig , userProvided )
242- mergeNonUserFields ( finalProviderConfig , nextProviderConfig , userProvided )
243- }
244-
245159 const updatedWebhook = await db
246160 . update ( webhook )
247161 . set ( {
248- path : path !== undefined ? path : webhooks [ 0 ] . webhook . path ,
249- provider : provider !== undefined ? provider : webhooks [ 0 ] . webhook . provider ,
250- providerConfig : finalProviderConfig ,
251162 isActive : isActive !== undefined ? isActive : webhooks [ 0 ] . webhook . isActive ,
252163 failedCount : failedCount !== undefined ? failedCount : webhooks [ 0 ] . webhook . failedCount ,
253164 updatedAt : new Date ( ) ,
@@ -330,8 +241,6 @@ export async function DELETE(
330241 }
331242
332243 const foundWebhook = webhookData . webhook
333- const { cleanupExternalWebhook } = await import ( '@/lib/webhooks/provider-subscriptions' )
334-
335244 const providerConfig = foundWebhook . providerConfig as Record < string , unknown > | null
336245 const credentialSetId = providerConfig ?. credentialSetId as string | undefined
337246 const blockId = providerConfig ?. blockId as string | undefined
0 commit comments