From 985f14688ea0fcd677a74b88d11e5315f114d45e Mon Sep 17 00:00:00 2001 From: Eric Schultz Date: Mon, 6 Jan 2020 14:03:03 -0600 Subject: [PATCH] Initial event publishing support --- Gemfile | 7 ++++-- Gemfile.lock | 8 ++++++ app/jobs/direct_debit_create_job.rb | 8 ------ app/listeners/credit_card_payment_listener.rb | 12 +++++++++ app/listeners/nonprofit_mailer_listener.rb | 6 +++++ app/listeners/sepa_payment_listener.rb | 12 +++++++++ app/listeners/supporter_mailer_listener.rb | 2 ++ app/listeners/wemove_listener.rb | 13 ++++++++++ config/application.rb | 5 ++-- .../initializers/houdini_event_publisher.rb | 10 ++++++++ lib/event_publisher.rb | 14 +++++++++++ lib/insert/insert_donation.rb | 6 ++--- lib/insert/insert_recurring_donation.rb | 3 --- spec/spec_helper.rb | 3 +++ .../shared_rd_donation_value_context.rb | 25 ++++++------------- 15 files changed, 97 insertions(+), 37 deletions(-) delete mode 100644 app/jobs/direct_debit_create_job.rb create mode 100644 app/listeners/credit_card_payment_listener.rb create mode 100644 app/listeners/nonprofit_mailer_listener.rb create mode 100644 app/listeners/sepa_payment_listener.rb create mode 100644 app/listeners/supporter_mailer_listener.rb create mode 100644 app/listeners/wemove_listener.rb create mode 100644 config/initializers/houdini_event_publisher.rb create mode 100644 lib/event_publisher.rb diff --git a/Gemfile b/Gemfile index 34043b7c..53890254 100755 --- a/Gemfile +++ b/Gemfile @@ -70,6 +70,8 @@ gem 'grape-entity', '~> 0.7.1' gem 'grape-swagger-entity', '~> 0.3.3' gem 'grape-swagger', '~> 0.33.0' gem 'grape', '~> 1.2', '>= 1.2.4' +gem 'wisper', '~> 2.0' +gem 'wisper-activejob', '~> 1.0.0' group :development do gem 'grape_on_rails_routes', '~> 0.3.2' @@ -91,6 +93,8 @@ group :development, :ci, :test do gem 'ruby-prof', '0.15.9' gem 'solargraph', '~> 0.35.1' gem 'standard', '~> 0.1.2' + gem 'rspec-rails', '~> 3.8', '>= 3.8.2' + gem 'rspec', '~> 3.8' end group :ci, :test do @@ -98,13 +102,12 @@ group :ci, :test do gem 'database_cleaner', '~> 1.7' gem 'factory_bot_rails', '~> 5.0', '>= 5.0.2' gem 'factory_bot', '~> 5.0', '>= 5.0.2' - gem 'rspec-rails', '~> 3.8', '>= 3.8.2' - gem 'rspec', '~> 3.8' gem 'simplecov', '~> 0.16.1', require: false gem 'stripe-ruby-mock', '~> 2.4.1', require: 'stripe_mock', git: 'https://github.com/commitchange/stripe-ruby-mock.git', branch: '2.4.1' gem 'test-unit', '~> 3.3' gem 'timecop', '~> 0.9.1' gem 'webmock', '~> 3.6', '>= 3.6.2' + gem 'wisper-rspec', '~> 1.1.0' end group :production do diff --git a/Gemfile.lock b/Gemfile.lock index 273ec37c..20fea479 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -492,6 +492,11 @@ GEM websocket-driver (0.7.1) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.4) + wisper (2.0.1) + wisper-activejob (1.0.0) + activejob (>= 4.0.0) + wisper + wisper-rspec (1.1.0) xml-simple (1.1.5) yard (0.9.20) @@ -575,6 +580,9 @@ DEPENDENCIES traceroute (~> 0.8.0) uglifier (~> 4.1, >= 4.1.20) webmock (~> 3.6, >= 3.6.2) + wisper (~> 2.0) + wisper-activejob (~> 1.0.0) + wisper-rspec (~> 1.1.0) RUBY VERSION ruby 2.5.1p57 diff --git a/app/jobs/direct_debit_create_job.rb b/app/jobs/direct_debit_create_job.rb deleted file mode 100644 index cec8218f..00000000 --- a/app/jobs/direct_debit_create_job.rb +++ /dev/null @@ -1,8 +0,0 @@ -class DirectDebitCreateJob < ApplicationJob - queue_as :default - - def perform(donation_id, locale) - DirectDebitCreateNotifyDonorJob.perform_later donation_id, locale - DirectDebitCreateNotifyNonprofitJob.perform_later donation_id, locale - end -end diff --git a/app/listeners/credit_card_payment_listener.rb b/app/listeners/credit_card_payment_listener.rb new file mode 100644 index 00000000..20eec3ae --- /dev/null +++ b/app/listeners/credit_card_payment_listener.rb @@ -0,0 +1,12 @@ +class CreditCardPaymentListener + def donation_create(donation, locale, user=nil) + if donation.payment_provider == :credit_card + PaymentNotificationEmailDonorJob.perform_later donation, locale + PaymentNotificationEmailNonprofitJob.perform_later donation, user + end + end + + def recurring_donation_create(donation, locale, user=nil) + + end +end diff --git a/app/listeners/nonprofit_mailer_listener.rb b/app/listeners/nonprofit_mailer_listener.rb new file mode 100644 index 00000000..28f77fac --- /dev/null +++ b/app/listeners/nonprofit_mailer_listener.rb @@ -0,0 +1,6 @@ +class NonprofitMailerListener + + def nonprofit_create(nonprofit) + + end +end \ No newline at end of file diff --git a/app/listeners/sepa_payment_listener.rb b/app/listeners/sepa_payment_listener.rb new file mode 100644 index 00000000..292dde36 --- /dev/null +++ b/app/listeners/sepa_payment_listener.rb @@ -0,0 +1,12 @@ +class SepaPaymentListener + def donation_create(donation) + if donation.payment_provider == :sepa + DirectDebitCreateNotifyNonprofitJob.perform_later(donation.id) + DirectDebitCreateNotifyDonorJob.perform_later donation.id, locale + end + end + + def recurring_donation_create(donation, locale, user=nil) + + end +end diff --git a/app/listeners/supporter_mailer_listener.rb b/app/listeners/supporter_mailer_listener.rb new file mode 100644 index 00000000..d7986023 --- /dev/null +++ b/app/listeners/supporter_mailer_listener.rb @@ -0,0 +1,2 @@ +class SupporterMailerListener +end \ No newline at end of file diff --git a/app/listeners/wemove_listener.rb b/app/listeners/wemove_listener.rb new file mode 100644 index 00000000..b520fbe2 --- /dev/null +++ b/app/listeners/wemove_listener.rb @@ -0,0 +1,13 @@ +class WemoveListener + def donation_create(donation) + WeMoveExecuteForDonationsJob.perform_later(donation) + end + + def offsite_donation_create(donation) + WeMoveExecuteForDonationsJob.perform_later(donation) + end + + def recurring_donation_create(donation) + WeMoveExecuteForDonationsJob.perform_later(donation) + end +end diff --git a/config/application.rb b/config/application.rb index 099f6b77..61c6cdf8 100755 --- a/config/application.rb +++ b/config/application.rb @@ -30,10 +30,11 @@ module Commitchange # Custom directories with classes and modules you want to be autoloadable. # config.autoload_paths += %W(#{config.root}/extras) - config.eager_load_paths += Dir["#{config.root}/lib/**/"] + config.eager_load_paths += Dir["#{config.root}/lib/**/", ""] config.paths.add File.join('app', 'api'), glob: File.join('**', '*.rb') - config.eager_load_paths += Dir[Rails.root.join('app', 'api', '*')] + config.paths.add File.join('app', 'listeners'), glob: File.join('**', '*.rb') + config.eager_load_paths += Dir[Rails.root.join('app', 'api', '*'), Rails.root.join('app', 'listeners', '*')] # Only load the plugins named here, in the order given (default is alphabetical). # :all can be used as a placeholder for all plugins not explicitly named. diff --git a/config/initializers/houdini_event_publisher.rb b/config/initializers/houdini_event_publisher.rb new file mode 100644 index 00000000..5aae60c2 --- /dev/null +++ b/config/initializers/houdini_event_publisher.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +HoudiniEventPublisher = EventPublisher.new + +Rails.application.config.to_prepare do + HoudiniEventPublisher.clear if Rails.env.development? + + HoudiniEventPublisher.subscribe_async(NonprofitMailerListener.new) +end diff --git a/lib/event_publisher.rb b/lib/event_publisher.rb new file mode 100644 index 00000000..7cbe6c1b --- /dev/null +++ b/lib/event_publisher.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later +class EventPublisher + include Wisper::Publisher + + def call(event, *args) + broadcast(event, *args) + end + + def subscribe_async(listener, options = {}) + subscribe(listener, options.merge(async: true)) + end +end \ No newline at end of file diff --git a/lib/insert/insert_donation.rb b/lib/insert/insert_donation.rb index 17f86b43..e89f9a32 100644 --- a/lib/insert/insert_donation.rb +++ b/lib/insert/insert_donation.rb @@ -43,8 +43,7 @@ module InsertDonation result['donation'] = insert_donation(data, entities) update_donation_keys(result) result['activity'] = InsertActivities.for_one_time_donations([result['payment'].id]) - PaymentNotificationJob.perform_later result['donation'], entities[:supporter_id].locale - WeMoveExecuteForDonationsJob.perform_later(result['donation']) + HoudiniEventPublisher.call(:donation_create, result['donation'], result['donation'].supporter.locale) result end @@ -105,9 +104,8 @@ module InsertDonation result['donation'] = insert_donation(data, entities) update_donation_keys(result) - DirectDebitCreateJob.perform_later(result['donation'].id, locale_for_supporter(result['donation'].supporter.id)) + HoudiniEventPublisher.call(:donation_create, result['donation'], locale_for_supporter(result['donation'].supporter.id)) - WeMoveExecuteForDonationsJob.perform_later(result['donation']) # do this for making test consistent result['activity'] = {} result diff --git a/lib/insert/insert_recurring_donation.rb b/lib/insert/insert_recurring_donation.rb index 4aee3584..6d4b00d9 100644 --- a/lib/insert/insert_recurring_donation.rb +++ b/lib/insert/insert_recurring_donation.rb @@ -71,7 +71,6 @@ module InsertRecurringDonation end # Send receipts PaymentNotificationJob.perform_later result['donation'], entities[:supporter_id].locale - WeMoveExecuteForDonationsJob.perform_later(result['donation']) result end @@ -96,8 +95,6 @@ module InsertRecurringDonation DonorDirectDebitNotificationJob.perform_later(Donation.find(result['donation']['id']), locale_for_supporter(result['donation']['supporter_id'])); - WeMoveExecuteForDonationsJob.perform_later(result['donation']) - { status: 200, json: result } end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 1dcef0d5..7120c71c 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -18,6 +18,7 @@ require 'support/expect' require 'support/mock_helpers' require 'action_mailer_matchers' require 'active_job' +require 'wisper/rspec/matchers' include ActiveJob::TestHelper RSpec.configure do |config| # rspec-expectations config goes here. You can use an alternate @@ -112,4 +113,6 @@ RSpec.configure do |config| end clear_enqueued_jobs end + + config.include(Wisper::RSpec::BroadcastMatcher) end diff --git a/spec/support/contexts/shared_rd_donation_value_context.rb b/spec/support/contexts/shared_rd_donation_value_context.rb index 7d5937ab..0f99f376 100644 --- a/spec/support/contexts/shared_rd_donation_value_context.rb +++ b/spec/support/contexts/shared_rd_donation_value_context.rb @@ -374,6 +374,9 @@ RSpec.shared_context :shared_rd_donation_value_context do def process_event_donation(data = {}) pay_method = data[:sepa] ? direct_debit_detail : card + + expect(HoudiniEventPublisher).to receive(:call).with(:donation_create,instance_of(Donation), supporter.locale ) + result = yield expected = generate_expected(@donation_id, result['payment'].id, result['charge'].id, pay_method, supporter, nonprofit, @stripe_charge_id, event: event, recurring_donation_expected: data[:recurring_donation], recurring_donation: result['recurring_donation']) @@ -385,18 +388,15 @@ RSpec.shared_context :shared_rd_donation_value_context do if data[:recurring_donation] expect(result['recurring_donation'].attributes).to eq expected[:recurring_donation] end - if (data[:sepa]) - expect(DirectDebitCreateJob).to have_been_enqueued.with(result['donation']['id'], supporter.locale) - else - expect(PaymentNotificationJob).to have_been_enqueued.with(result['donation'], supporter.locale) - end - expect(WeMoveExecuteForDonationsJob).to have_been_enqueued result end def process_campaign_donation(data = {}) pay_method = data[:sepa] ? direct_debit_detail : card + + expect(HoudiniEventPublisher).to receive(:call).with(:donation_create,instance_of(Donation), supporter.locale ) + result = yield expected = generate_expected(@donation_id, result['payment'].id, result['charge'].id, pay_method, supporter, nonprofit, @stripe_charge_id, campaign: campaign, recurring_donation_expected: data[:recurring_donation], recurring_donation: result['recurring_donation']) @@ -408,17 +408,12 @@ RSpec.shared_context :shared_rd_donation_value_context do if data[:recurring_donation] expect(result['recurring_donation'].attributes).to eq expected[:recurring_donation] end - if (data[:sepa]) - - else - expect(PaymentNotificationJob).to have_been_enqueued.with(result['donation'], supporter.locale) - end - expect(WeMoveExecuteForDonationsJob).to have_been_enqueued result end def process_general_donation(data = {}) pay_method = data[:sepa] ? direct_debit_detail : card + expect(HoudiniEventPublisher).to receive(:call).with(:donation_create,instance_of(Donation), supporter.locale ) result = yield expect_payment = nil_or_true(data[:expect_payment]) expect_charge = nil_or_true(data[:expect_charge]) @@ -439,12 +434,6 @@ RSpec.shared_context :shared_rd_donation_value_context do expect(result['recurring_donation'].attributes).to eq expected[:recurring_donation] end - if (data[:sepa]) - - else - expect(PaymentNotificationJob).to have_been_enqueued.with(result['donation'], supporter.locale) - end - expect(WeMoveExecuteForDonationsJob).to have_been_enqueued result end