@@ -2,6 +2,7 @@ import { db } from '@sim/db'
22import { webhook } from '@sim/db/schema'
33import { eq } from 'drizzle-orm'
44import { type NextRequest , NextResponse } from 'next/server'
5+ import { env } from '@/lib/env'
56import { createLogger } from '@/lib/logs/console/logger'
67import { generateRequestId } from '@/lib/utils'
78
@@ -13,7 +14,6 @@ export async function GET(request: NextRequest) {
1314 const requestId = generateRequestId ( )
1415
1516 try {
16- // Get the webhook ID and provider from the query parameters
1717 const { searchParams } = new URL ( request . url )
1818 const webhookId = searchParams . get ( 'id' )
1919
@@ -24,7 +24,6 @@ export async function GET(request: NextRequest) {
2424
2525 logger . debug ( `[${ requestId } ] Testing webhook with ID: ${ webhookId } ` )
2626
27- // Find the webhook in the database
2827 const webhooks = await db . select ( ) . from ( webhook ) . where ( eq ( webhook . id , webhookId ) ) . limit ( 1 )
2928
3029 if ( webhooks . length === 0 ) {
@@ -36,8 +35,14 @@ export async function GET(request: NextRequest) {
3635 const provider = foundWebhook . provider || 'generic'
3736 const providerConfig = ( foundWebhook . providerConfig as Record < string , any > ) || { }
3837
39- // Construct the webhook URL
40- const baseUrl = new URL ( request . url ) . origin
38+ if ( ! env . NEXT_PUBLIC_APP_URL ) {
39+ logger . error ( `[${ requestId } ] NEXT_PUBLIC_APP_URL not configured, cannot test webhook` )
40+ return NextResponse . json (
41+ { success : false , error : 'NEXT_PUBLIC_APP_URL must be configured' } ,
42+ { status : 500 }
43+ )
44+ }
45+ const baseUrl = env . NEXT_PUBLIC_APP_URL
4146 const webhookUrl = `${ baseUrl } /api/webhooks/trigger/${ foundWebhook . path } `
4247
4348 logger . info ( `[${ requestId } ] Testing webhook for provider: ${ provider } ` , {
@@ -46,7 +51,6 @@ export async function GET(request: NextRequest) {
4651 isActive : foundWebhook . isActive ,
4752 } )
4853
49- // Provider-specific test logic
5054 switch ( provider ) {
5155 case 'whatsapp' : {
5256 const verificationToken = providerConfig . verificationToken
@@ -59,30 +63,25 @@ export async function GET(request: NextRequest) {
5963 )
6064 }
6165
62- // Generate a test challenge
6366 const challenge = `test_${ Date . now ( ) } `
6467
65- // Construct the WhatsApp verification URL
6668 const whatsappUrl = `${ webhookUrl } ?hub.mode=subscribe&hub.verify_token=${ verificationToken } &hub.challenge=${ challenge } `
6769
6870 logger . debug ( `[${ requestId } ] Testing WhatsApp webhook verification` , {
6971 webhookId,
7072 challenge,
7173 } )
7274
73- // Make a request to the webhook endpoint
7475 const response = await fetch ( whatsappUrl , {
7576 headers : {
7677 'User-Agent' : 'facebookplatform/1.0' ,
7778 } ,
7879 } )
7980
80- // Get the response details
8181 const status = response . status
8282 const contentType = response . headers . get ( 'content-type' )
8383 const responseText = await response . text ( )
8484
85- // Check if the test was successful
8685 const success = status === 200 && responseText === challenge
8786
8887 if ( success ) {
@@ -139,7 +138,6 @@ export async function GET(request: NextRequest) {
139138 )
140139 }
141140
142- // Test the webhook endpoint with a simple message to check if it's reachable
143141 const testMessage = {
144142 update_id : 12345 ,
145143 message : {
@@ -165,7 +163,6 @@ export async function GET(request: NextRequest) {
165163 url : webhookUrl ,
166164 } )
167165
168- // Make a test request to the webhook endpoint
169166 const response = await fetch ( webhookUrl , {
170167 method : 'POST' ,
171168 headers : {
@@ -175,16 +172,12 @@ export async function GET(request: NextRequest) {
175172 body : JSON . stringify ( testMessage ) ,
176173 } )
177174
178- // Get the response details
179175 const status = response . status
180176 let responseText = ''
181177 try {
182178 responseText = await response . text ( )
183- } catch ( _e ) {
184- // Ignore if we can't get response text
185- }
179+ } catch ( _e ) { }
186180
187- // Consider success if we get a 2xx response
188181 const success = status >= 200 && status < 300
189182
190183 if ( success ) {
@@ -196,7 +189,6 @@ export async function GET(request: NextRequest) {
196189 } )
197190 }
198191
199- // Get webhook info from Telegram API
200192 let webhookInfo = null
201193 try {
202194 const webhookInfoUrl = `https://api.telegram.org/bot${ botToken } /getWebhookInfo`
@@ -215,7 +207,6 @@ export async function GET(request: NextRequest) {
215207 logger . warn ( `[${ requestId } ] Failed to get Telegram webhook info` , e )
216208 }
217209
218- // Format the curl command for testing
219210 const curlCommand = [
220211 `curl -X POST "${ webhookUrl } "` ,
221212 `-H "Content-Type: application/json"` ,
@@ -288,16 +279,13 @@ export async function GET(request: NextRequest) {
288279 }
289280
290281 case 'generic' : {
291- // Get the general webhook configuration
292282 const token = providerConfig . token
293283 const secretHeaderName = providerConfig . secretHeaderName
294284 const requireAuth = providerConfig . requireAuth
295285 const allowedIps = providerConfig . allowedIps
296286
297- // Generate sample curl command for testing
298287 let curlCommand = `curl -X POST "${ webhookUrl } " -H "Content-Type: application/json"`
299288
300- // Add auth headers to the curl command if required
301289 if ( requireAuth && token ) {
302290 if ( secretHeaderName ) {
303291 curlCommand += ` -H "${ secretHeaderName } : ${ token } "`
@@ -306,7 +294,6 @@ export async function GET(request: NextRequest) {
306294 }
307295 }
308296
309- // Add a sample payload
310297 curlCommand += ` -d '{"event":"test_event","timestamp":"${ new Date ( ) . toISOString ( ) } "}'`
311298
312299 logger . info ( `[${ requestId } ] General webhook test successful: ${ webhookId } ` )
@@ -391,7 +378,6 @@ export async function GET(request: NextRequest) {
391378 } )
392379 }
393380
394- // Add the Airtable test case
395381 case 'airtable' : {
396382 const baseId = providerConfig . baseId
397383 const tableId = providerConfig . tableId
@@ -408,7 +394,6 @@ export async function GET(request: NextRequest) {
408394 )
409395 }
410396
411- // Define a sample payload structure
412397 const samplePayload = {
413398 webhook : {
414399 id : 'whiYOUR_WEBHOOK_ID' ,
@@ -418,16 +403,15 @@ export async function GET(request: NextRequest) {
418403 } ,
419404 payloadFormat : 'v0' ,
420405 actionMetadata : {
421- source : 'tableOrViewChange' , // Example source
406+ source : 'tableOrViewChange' ,
422407 sourceMetadata : { } ,
423408 } ,
424409 payloads : [
425410 {
426411 timestamp : new Date ( ) . toISOString ( ) ,
427- baseTransactionNumber : Date . now ( ) , // Example transaction number
412+ baseTransactionNumber : Date . now ( ) ,
428413 changedTablesById : {
429414 [ tableId ] : {
430- // Example changes - structure may vary based on actual event
431415 changedRecordsById : {
432416 recSAMPLEID1 : {
433417 current : { cellValuesByFieldId : { fldSAMPLEID : 'New Value' } } ,
@@ -442,7 +426,6 @@ export async function GET(request: NextRequest) {
442426 ] ,
443427 }
444428
445- // Generate sample curl command
446429 let curlCommand = `curl -X POST "${ webhookUrl } " -H "Content-Type: application/json"`
447430 curlCommand += ` -d '${ JSON . stringify ( samplePayload , null , 2 ) } '`
448431
@@ -519,7 +502,6 @@ export async function GET(request: NextRequest) {
519502 }
520503
521504 default : {
522- // Generic webhook test
523505 logger . info ( `[${ requestId } ] Generic webhook test successful: ${ webhookId } ` )
524506 return NextResponse . json ( {
525507 success : true ,
0 commit comments