import type * as Db from '../../db/Db.js' import type * as FundingTransferEvents from '../../db/tables/fundingTransferEvents.js' import type * as FundingTransfers from '../../db/tables/fundingTransfers.js' import * as WebhookSubscriptions from '../../db/tables/webhookSubscriptions.js' import * as Webhooks from '../Webhooks.js' /** Stages durable webhook obligations for committed funding transfer versions. */ export async function stage( db: Db.Db, options: stage.Options, ): Promise { const dispatchables: Webhooks.ensureQueueEvents.Dispatchable[] = [] const now = new Date().toISOString() for (const { event, record } of options.changes) { const match = /^eip155:(\d+)$/.exec(event.snapshot.destinationChain.id) if (!match?.[1]) continue const chainId = Number(match[1]) if (!Number.isSafeInteger(chainId) || chainId <= 0) continue const subscriptions = await WebhookSubscriptions.listActiveForFundingEvent(db, { chainId, environment: record.environment, eventCreatedAt: event.createdAt, eventType: 'funding:transfer.updated', now, orgId: record.orgId, ...(record.projectId === null ? {} : { projectId: record.projectId }), }) for (const subscription of subscriptions) { if ( typeof subscription.filters['id'] === 'string' && subscription.filters['id'] !== event.transferId ) continue if ( typeof subscription.filters['status'] === 'string' && subscription.filters['status'] !== event.status ) continue dispatchables.push({ envelope: Webhooks.buildKeyedEnvelope({ createdAt: new Date(event.createdAt), data: event.snapshot, key: `${event.transferId}:${event.version}`, subscription, }), subscription, }) } } return Webhooks.ensureQueueEvents(db, dispatchables) } export declare namespace stage { /** One committed transfer record and its immutable public event. */ type Change = { /** Immutable public transfer version. */ event: FundingTransferEvents.Record /** Transfer ownership and environment record. */ record: FundingTransfers.Record } /** Committed transfer changes eligible for customer webhook delivery. */ type Options = { /** Transfer changes committed with their delivery obligations. */ changes: readonly Change[] } }