diff options
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/base_service.rb | 4 | ||||
-rw-r--r-- | app/services/webhook_service.rb | 22 |
2 files changed, 26 insertions, 0 deletions
diff --git a/app/services/base_service.rb b/app/services/base_service.rb index 99e8c875f..b0c0f9ec4 100644 --- a/app/services/base_service.rb +++ b/app/services/base_service.rb @@ -5,4 +5,8 @@ class BaseService include ActionView::Helpers::SanitizeHelper include RoutingHelper + + def call(*) + raise NotImplementedError + end end diff --git a/app/services/webhook_service.rb b/app/services/webhook_service.rb new file mode 100644 index 000000000..aafa38318 --- /dev/null +++ b/app/services/webhook_service.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +class WebhookService < BaseService + def call(event, object) + @event = Webhooks::EventPresenter.new(event, object) + @body = serialize_event + + webhooks_for_event.each do |webhook_id| + Webhooks::DeliveryWorker.perform_async(webhook_id, @body) + end + end + + private + + def webhooks_for_event + Webhook.enabled.where('? = ANY(events)', @event.type).pluck(:id) + end + + def serialize_event + Oj.dump(ActiveModelSerializers::SerializableResource.new(@event, serializer: REST::Admin::WebhookEventSerializer, scope: nil, scope_name: :current_user).as_json) + end +end |