( foundWebhook: any, foundWorkflow: any, body: any, request: NextRequest, options: WebhookProcessorOptions )
| 548 | } |
| 549 | |
| 550 | export async function queueWebhookExecution( |
| 551 | foundWebhook: any, |
| 552 | foundWorkflow: any, |
| 553 | body: any, |
| 554 | request: NextRequest, |
| 555 | options: WebhookProcessorOptions |
| 556 | ): Promise<NextResponse> { |
| 557 | const providerConfig = (foundWebhook.providerConfig as Record<string, unknown>) || {} |
| 558 | const handler = getProviderHandler(foundWebhook.provider) |
| 559 | |
| 560 | try { |
| 561 | if (handler.matchEvent) { |
| 562 | const result = await handler.matchEvent({ |
| 563 | webhook: foundWebhook, |
| 564 | workflow: foundWorkflow, |
| 565 | body, |
| 566 | request, |
| 567 | requestId: options.requestId, |
| 568 | providerConfig, |
| 569 | }) |
| 570 | if (result !== true) { |
| 571 | if (result instanceof NextResponse) { |
| 572 | return result |
| 573 | } |
| 574 | return NextResponse.json({ |
| 575 | message: 'Event type does not match trigger configuration. Ignoring.', |
| 576 | }) |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | const { 'x-sim-idempotency-key': _, ...headers } = Object.fromEntries(request.headers.entries()) |
| 581 | |
| 582 | if (handler.enrichHeaders) { |
| 583 | handler.enrichHeaders( |
| 584 | { webhook: foundWebhook, body, requestId: options.requestId, providerConfig }, |
| 585 | headers |
| 586 | ) |
| 587 | } |
| 588 | |
| 589 | const credentialId = providerConfig.credentialId as string | undefined |
| 590 | const credentialSetId = foundWebhook.credentialSetId as string | undefined |
| 591 | |
| 592 | if (credentialSetId) { |
| 593 | const billingCheck = await verifyCredentialSetBilling(credentialSetId) |
| 594 | if (!billingCheck.valid) { |
| 595 | logger.warn( |
| 596 | `[${options.requestId}] Credential set billing check failed: ${billingCheck.error}` |
| 597 | ) |
| 598 | return NextResponse.json({ error: billingCheck.error }, { status: 403 }) |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | const actorUserId = options.actorUserId |
| 603 | if (!actorUserId) { |
| 604 | logger.error(`[${options.requestId}] No actorUserId provided for webhook ${foundWebhook.id}`) |
| 605 | return NextResponse.json({ error: 'Unable to resolve billing account' }, { status: 500 }) |
| 606 | } |
| 607 |
no test coverage detected