Initial event publishing support

This commit is contained in:
Eric Schultz 2020-01-06 14:03:03 -06:00
parent 238dfec1ac
commit 985f14688e
15 changed files with 97 additions and 37 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,6 @@
class NonprofitMailerListener
def nonprofit_create(nonprofit)
end
end

View file

@ -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

View file

@ -0,0 +1,2 @@
class SupporterMailerListener
end

View file

@ -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

View file

@ -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.

View file

@ -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

14
lib/event_publisher.rb Normal file
View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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