docbrown/app/services/webhook/dispatch_event.rb
Anna Buianova 2d04488f4d Dispatch events only to the corresponding user endpoints (#4008)
* Dispatch events only to the corresponding user endpoints

* Specify user for dispatch webhook event test to be sure

* Fix webhooks specs
2019-09-11 15:33:11 -04:00

26 lines
647 B
Ruby

module Webhook
class DispatchEvent
def initialize(event_type, record)
@event_type = event_type
@record = record
end
def self.call(*args)
new(*args).call
end
def call
endpoint_urls = Endpoint.for_events([event_type]).where(user_id: record.user_id).pluck(:target_url)
return if endpoint_urls.empty?
event_json = Event.new(event_type: event_type, payload: PayloadAdapter.new(record).hash).to_json
endpoint_urls.each do |url|
DispatchEventJob.perform_later(endpoint_url: url, payload: event_json)
end
end
private
attr_reader :event_type, :record
end
end