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
37
38
# 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?

  # Only load the columns we need for matching and delivery
  endpoints = Spree::WebhookEndpoint
    .enabled
    .where(store_id: event.store_id)
    .select(:id, :subscriptions)

  endpoints.each do |endpoint|
    next unless endpoint.subscribed_to?(event.name)

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