Class: Spree::WebhookEventSubscriber

Inherits:
Subscriber
  • Object
show all
Defined in:
app/subscribers/spree/webhook_event_subscriber.rb

Overview

Listens to Spree events and queues webhook deliveries for enabled endpoints.

This subscriber listens to all Spree events and for each event, finds all enabled webhook endpoints that are subscribed to that event and queues a delivery job for each one.

The event payload is passed through directly without transformation. Events should already be serialized by the EventSerializer.

Examples:

# Webhooks are automatically delivered when events are published
Spree::Events.publish('order.completed', order: order)

Instance Method Summary collapse

Instance Method Details

#handle(event) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/subscribers/spree/webhook_event_subscriber.rb', line 20

def handle(event)
  return unless Spree::Api::Config.webhooks_enabled
  return if event.store_id.blank?

  # Find all active endpoints for this store subscribed to this event
  endpoints = Spree::WebhookEndpoint.active.where(store_id: event.store_id).select { |endpoint| endpoint.subscribed_to?(event.name) }

  return if endpoints.empty?

  # Queue delivery for each endpoint
  endpoints.each do |endpoint|
    queue_delivery(endpoint, event)
  end
rescue StandardError => e
  Rails.logger.error "[Spree Webhooks] Error processing event: #{e.message}"
  Rails.error.report(e)
end