Creates ObjectEventListener and WebhookTransmitJob

This commit is contained in:
Clarissa Borges 2021-02-13 20:21:45 -03:00 committed by Eric Schultz
parent 0405e59784
commit 3f8f5559ab
5 changed files with 212 additions and 3 deletions

View file

@ -0,0 +1,11 @@
# frozen_string_literal: true
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
class WebhookTransmitJob < ApplicationJob
queue_as :default
def perform(object_event_hook_config, payload)
object_event_hook_config.webhook.transmit(payload)
end
end

View file

@ -0,0 +1,158 @@
# frozen_string_literal: true
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
class ObjectEventListener < ApplicationListener
def self.campaign_gift_created(event)
enqueue_transmissions_to_webhooks(event)
end
def self.campaign_gift_updated(event)
enqueue_transmissions_to_webhooks(event)
end
def self.campaign_gift_deleted(event)
enqueue_transmissions_to_webhooks(event)
end
def self.campaign_gift_option_created(event)
enqueue_transmissions_to_webhooks(event)
end
def self.campaign_gift_option_updated(event)
enqueue_transmissions_to_webhooks(event)
end
def self.campaign_gift_option_deleted(event)
enqueue_transmissions_to_webhooks(event)
end
def self.campaign_gift_purchase_created(event)
enqueue_transmissions_to_webhooks(event)
end
def self.campaign_gift_purchase_updated(event)
enqueue_transmissions_to_webhooks(event)
end
def self.campaign_gift_purchase_deleted(event)
enqueue_transmissions_to_webhooks(event)
end
def self.event_discount_created(event)
enqueue_transmissions_to_webhooks(event)
end
def self.event_discount_updated(event)
enqueue_transmissions_to_webhooks(event)
end
def self.event_discount_deleted(event)
enqueue_transmissions_to_webhooks(event)
end
def self.ticket_created(event)
enqueue_transmissions_to_webhooks(event)
end
def self.ticket_updated(event)
enqueue_transmissions_to_webhooks(event)
end
def self.ticket_deleted(event)
enqueue_transmissions_to_webhooks(event)
end
def self.ticket_level_created(event)
enqueue_transmissions_to_webhooks(event)
end
def self.ticket_level_updated(event)
enqueue_transmissions_to_webhooks(event)
end
def self.ticket_level_deleted(event)
enqueue_transmissions_to_webhooks(event)
end
def self.ticket_purchase_created(event)
enqueue_transmissions_to_webhooks(event)
end
def self.ticket_purchase_updated(event)
enqueue_transmissions_to_webhooks(event)
end
def self.ticket_purchase_deleted(event)
enqueue_transmissions_to_webhooks(event)
end
def self.supporter_address_created(event)
enqueue_transmissions_to_webhooks(event)
end
def self.supporter_address_updated(event)
enqueue_transmissions_to_webhooks(event)
end
def self.supporter_address_deleted(event)
enqueue_transmissions_to_webhooks(event)
end
def self.supporter_note_created(event)
enqueue_transmissions_to_webhooks(event)
end
def self.supporter_note_updated(event)
enqueue_transmissions_to_webhooks(event)
end
def self.supporter_note_deleted(event)
enqueue_transmissions_to_webhooks(event)
end
def self.donation_created(event)
enqueue_transmissions_to_webhooks(event)
end
def self.donation_updated(event)
enqueue_transmissions_to_webhooks(event)
end
def self.donation_deleted(event)
enqueue_transmissions_to_webhooks(event)
end
def self.custom_field_definition_created(event)
enqueue_transmissions_to_webhooks(event)
end
def self.custom_field_definition_deleted(event)
enqueue_transmissions_to_webhooks(event)
end
def self.tag_definition_created(event)
enqueue_transmissions_to_webhooks(event)
end
def self.tag_definition_deleted(event)
enqueue_transmissions_to_webhooks(event)
end
private
def self.enqueue_transmissions_to_webhooks(event)
object_event_hook_configs(event).each do |config|
WebhookTransmitJob.perform_later(config, event)
end
end
def self.object_event_hook_configs(event)
nonprofit(event).object_event_hook_configs.for_type(event["type"])
end
def self.nonprofit(event)
nonprofit_id = event["data"]["object"]["nonprofit"]["id"]
Nonprofit.find_by_id(nonprofit_id)
end
end

View file

@ -65,7 +65,13 @@ class Nonprofit < ApplicationRecord
has_many :profiles, through: :donations
has_many :campaigns, dependent: :destroy
has_many :events, dependent: :destroy
has_many :object_event_hook_configs, dependent: :destroy
has_many :object_event_hook_configs, dependent: :destroy do
def for_type(type)
self.select do |config|
config.object_event_types.include? type
end
end
end
has_many :tickets, through: :events
has_many :roles, as: :host, dependent: :destroy
has_many :users, through: :roles

View file

@ -5,8 +5,10 @@
Wisper.clear if Rails.env.development?
Rails.application.config.houdini.listeners.push(NonprofitMailerListener,
Rails.application.config.houdini.listeners.push(
NonprofitMailerListener,
CreditCardPaymentListener,
SepaPaymentListener,
TicketMailingListener
TicketMailingListener,
ObjectEventListener
)

View file

@ -45,6 +45,38 @@ RSpec.describe Nonprofit, type: :model do
end
end
describe '.for_type' do
let(:nonprofit) { create(:nm_justice) }
let(:type) { 'supporter.created' }
let(:other_type) { 'supporter.deleted' }
let!(:object_event_hook_config) do
nonprofit.object_event_hook_configs.create(
webhook_service: :open_fn,
configuration:
{
webhook_url: 'https://www.openfn.org/inbox/my-inbox-id',
headers: { 'x-api-key': 'my-secret-key' }
},
object_event_types: [type, other_type]
)
end
describe 'there is an ObjectEventHookConfig for the specified type' do
it 'returns a list of ObjectEventHookConfigs' do
result = nonprofit.object_event_hook_configs.for_type(type)
expect(result).to eq([object_event_hook_config])
end
end
describe 'there is not an ObjectEventHookConfig for the specified type' do
let(:some_other_type) { 'supporter.updated' }
it 'returns an empty list' do
result = nonprofit.object_event_hook_configs.for_type(some_other_type)
expect(result).to eq([])
end
end
end
describe 'create' do
describe 'validates on parameters' do
let(:nonprofit) { Nonprofit.new()}