Merge pull request #1 from wwahammy/feat/wisper

Feat/wisper
This commit is contained in:
Eric Schultz 2020-02-10 14:27:42 -06:00 committed by GitHub
commit 6a9ed8ff59
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 168 additions and 112 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,13 +0,0 @@
class CampaignCreateJob < ApplicationJob
queue_as :default
def perform(campaign)
if campaign.child_campaign?
CampaignCreationFederatedEmailJob.perform_later(campaign)
else
CampaignCreationEmailFollowupJob.perform_later(campaign)
end
SupporterFundraiserCreateJob.perform_later(campaign)
end
end

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

@ -1,8 +0,0 @@
class PaymentNotificationJob < ApplicationJob
queue_as :default
def perform(donation, locale, user=nil)
PaymentNotificationEmailDonorJob.perform_later donation, locale
PaymentNotificationEmailNonprofitJob.perform_later donation, user
end
end

View file

@ -1,8 +0,0 @@
class TicketCreateJob < ApplicationJob
queue_as :default
def perform(ticket_ids, charge, user=nil)
TicketMailer.followup(ticket_ids, charge_id).deliver_later
TicketMailer.receipt_admin(ticket_ids, user.id).deliver_later
end
end

View file

@ -0,0 +1,5 @@
class ApplicationListener
def name
self.class.name
end
end

View file

@ -0,0 +1,11 @@
class CampaignListener < ApplicationListener
def campaign_create(campaign)
if campaign.child_campaign?
CampaignCreationFederatedEmailJob.perform_later(campaign)
else
CampaignCreationEmailFollowupJob.perform_later(campaign)
end
SupporterFundraiserCreateJob.perform_later(campaign)
end
end

View file

@ -0,0 +1,33 @@
class CreditCardPaymentListener < ApplicationListener
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)
if donation.payment_provider == :credit_card
PaymentNotificationEmailDonorJob.perform_later donation, locale
PaymentNotificationEmailNonprofitJob.perform_later donation, user
end
end
def refund_create(refund)
RefundNotificationJob.perform_later refund
end
def recurring_donation_payment_succeeded(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_payment_failed(donation, locale)
FailedRecurringDonationPaymentDonorEmailJob.perform_later(donation)
if (donation.recurring_donation.n_failures >= 3)
FailedRecurringDonationPaymentNonprofitEmailJob.perform_later(donation)
end
end
end

View file

@ -0,0 +1,5 @@
class NonprofitMailerListener < ApplicationListener
def nonprofit_create(nonprofit)
end
end

View file

@ -0,0 +1,8 @@
class SepaPaymentListener < ApplicationListener
def donation_create(donation)
if donation.payment_provider == :sepa
DirectDebitCreateNotifyNonprofitJob.perform_later(donation.id)
DirectDebitCreateNotifyDonorJob.perform_later donation.id, locale
end
end
end

View file

@ -0,0 +1,6 @@
class TicketListener < ApplicationListener
def ticket_create(tickets, charge, user=nil)
TicketMailer.followup(tickets.map{|i| i.id}, charge && charge.id).deliver_later
TicketMailer.receipt_admin(tickets.map{|i| i.id}, user && user.id).deliver_later
end
end

View file

@ -0,0 +1,13 @@
class WemoveListener < ApplicationListener
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

@ -99,7 +99,7 @@ class Campaign < ApplicationRecord
after_create do
user = profile.user
Role.create(name: :campaign_editor, user_id: user.id, host: self)
CampaignCreateJob.perform_later(self)
HoudiniEventPublisher.announce(:campaign_create, self)
self
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

@ -18,7 +18,7 @@ development:
encoding: unicode
database: commitchange_development
pool: 5
username: admin
username: houdini_user
password: password
host: <%= ENV['DATABASE_HOST'] || 'localhost' %>
@ -27,7 +27,7 @@ test:
encoding: unicode
database: commitchange_test
pool: 5
username: admin
username: houdini_user
password: password
host: <%= ENV['DATABASE_HOST'] || 'localhost' %>
ci:
@ -35,7 +35,7 @@ ci:
encoding: unicode
database: commitchange_ci
pool: 5
username: admin
username: houdini_user
password: password
host: <%= ENV['DATABASE_HOST'] || 'localhost' %>

View file

@ -0,0 +1,11 @@
# 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
Wisper.clear if Rails.env.development?
[NonprofitMailerListener, CreditCardPaymentListener, SepaPaymentListener, TicketListener].each do |listener|
HoudiniEventPublisher.subscribe_async(listener)
end
end

View file

@ -13,12 +13,6 @@ module CreateCampaign
if !params[:campaign][:parent_campaign_id]
campaign = nonprofit.campaigns.create params[:campaign]
# do notifications
user = campaign.profile.user
Role.create(name: :campaign_editor, user_id: user.id, host: self)
CampaignCreateJob.perform_later(self)
SupporterFundraiserCreateJob.perform_later(self) unless QueryRoles.is_nonprofit_user?(user.id, nonprofit_id)
return { errors: campaign.errors.messages }.as_json unless campaign.errors.empty?
return campaign.as_json

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 announce(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.announce(: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.announce(: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

@ -70,8 +70,7 @@ module InsertRecurringDonation
result['activity'] = InsertActivities.for_recurring_donations([result['payment'].id])
end
# Send receipts
PaymentNotificationJob.perform_later result['donation'], entities[:supporter_id].locale
WeMoveExecuteForDonationsJob.perform_later(result['donation'])
HoudiniEventPublisher.announce(:recurring_donation_create, result['donation'], entities[:supporter_id].locale)
result
end
@ -94,9 +93,7 @@ module InsertRecurringDonation
InsertDonation.update_donation_keys(result) if result['payment']
DonorDirectDebitNotificationJob.perform_later(Donation.find(result['donation']['id']), locale_for_supporter(result['donation']['supporter_id']));
WeMoveExecuteForDonationsJob.perform_later(result['donation'])
HoudiniEventPublisher.announce(:recurring_donation_create, result['donation'], entities[:supporter_id].locale)
{ status: 200, json: result }
end

View file

@ -66,7 +66,7 @@ module InsertRefunds
# Update original payment to increment its refund_total for any future refund attempts
Qx.update(:payments).set("refund_total=refund_total + #{h['amount'].to_i}").ts.where(id: original_payment['id']).execute
# Send the refund receipts in a delayed job
RefundNotificationJob.perform_later Refund.find(refund_row['id'])
HoudiniEventPublisher.announce(:create_refund, Refund.find(refund_row['id']))
{ 'payment' => payment_row, 'refund' => refund_row }
end
end

View file

@ -100,7 +100,7 @@ module InsertTickets
ticket_ids = result['tickets'].map(&:id)
charge_id = result['charge'] ? result['charge'].id : nil
TicketCreateJob.perform_later(ticket_ids, charge_id && Charge.find(result['charge']&.id))
HoudiniEventPublisher.announce(:ticket_create, result['tickets'], result['charge'])
result
end

View file

@ -75,21 +75,16 @@ module PayRecurringDonation
'old_donation' => true
))
if result['charge']['status'] != 'failed'
result['recurring_donation'] = Psql.execute(
Qexpr.new.update(:recurring_donations, n_failures: 0)
.where('id=$id', id: rd_id).returning('*')
).first
PaymentNotificationJob.perform_later donation, donation&.supporter&.locale || 'en'
rd.update_attributes(n_failures: 0)
result['recurring_donation'] = rd
HoudiniEventPublisher.announce(:recurring_donation_payment_succeeded, donation, donation&.supporter&.locale || 'en')
InsertActivities.for_recurring_donations([result['payment']['id']])
else
result['recurring_donation'] = Psql.execute(
Qexpr.new.update(:recurring_donations, n_failures: rd['n_failures'] + 1)
.where('id=$id', id: rd_id).returning('*')
).first
FailedRecurringDonationPaymentDonorEmailJob.perform_later Donation.find(rd['donation_id'])
if rd['n_failures'] >= 3
FailedRecurringDonationPaymentNonprofitEmailJob.perform_later Donation.find(rd['donation_id'])
end
rd.n_failures += 1
rd.save!
result['recurring_donation'] = rd
HoudiniEventPublisher.announce(:recurring_donation_payment_failed, donation)
InsertSupporterNotes.create([{ content: "This supporter had a payment failure for their recurring donation with ID #{rd_id}", supporter_id: donation['supporter_id'], user_id: 540 }])
end
result

View file

@ -98,6 +98,7 @@ describe InsertDonation do
end
it 'process campaign donation' do
expect(HoudiniEventPublisher).to receive(:announce).with(:campaign_create, any_args)
process_campaign_donation { InsertDonation.with_stripe(amount: charge_amount, nonprofit_id: nonprofit.id, supporter_id: supporter.id, token: source_token.token, campaign_id: campaign.id, date: (Time.now + 1.day).to_s, dedication: 'dedication', designation: 'designation') }
end
@ -108,18 +109,6 @@ describe InsertDonation do
end
describe '#with_sepa' do
# let!(:nonprofit) { Nonprofit.create(name: 'new', city: 'NY', state_code: 'NY') }
# let(:supporter) { Supporter.create(nonprofit: nonprofit) }
# let(:direct_debit) { DirectDebitDetail.create(supporter_id: supporter.id, account_holder_name: 'name', iban: 'de1234561234561234', bic: 'yxz') }
# let(:data) do
# {
# 'amount' => 2000,
# 'supporter_id' => supporter.id,
# 'nonprofit_id' => nonprofit.id,
# 'recurring' => false,
# 'direct_debit_detail_id' => direct_debit.id
# }
# end
include_context :shared_rd_donation_value_context
describe 'saves donation' do
@ -131,6 +120,7 @@ describe InsertDonation do
end
it 'process campaign donation' do
expect(HoudiniEventPublisher).to receive(:announce).with(:campaign_create, any_args)
process_campaign_donation(sepa: true) { InsertDonation.with_sepa(amount: charge_amount, nonprofit_id: nonprofit.id, supporter_id: supporter.id, direct_debit_detail_id: direct_debit_detail.id, campaign_id: campaign.id, date: (Time.now + 1.day).to_s, dedication: 'dedication', designation: 'designation') }
end

View file

@ -131,6 +131,7 @@ describe InsertRecurringDonation do
end
it 'process campaign donation' do
expect(HoudiniEventPublisher).to receive(:announce).with(:campaign_create, any_args)
process_campaign_donation(recurring_donation: { paydate: nil, interval: 2, time_unit: 'month', start_date: Time.current.beginning_of_day }) { InsertRecurringDonation.with_stripe(amount: charge_amount, nonprofit_id: nonprofit.id, supporter_id: supporter.id, token: source_token.token, campaign_id: campaign.id, date: (Time.now + 1.day).to_s, dedication: 'dedication', designation: 'designation', recurring_donation: { interval: 2 }) }
end

View file

@ -285,9 +285,9 @@ describe InsertTickets do
success_expectations
expect(QueryRoles).to receive(:is_authorized_for_nonprofit?).with(user.id, nonprofit.id).and_return true
result = nil
expect {
expect(HoudiniEventPublisher).to receive(:announce).with(:ticket_create, any_args)
result = InsertTickets.create(tickets: [{ quantity: 1, ticket_level_id: ticket_level.id }], nonprofit_id: nonprofit.id, supporter_id: supporter.id, token: source_token.token, event_id: event.id, kind: 'offsite', offsite_payment: { kind: 'check', check_number: 'fake_checknumber' }, current_user: user)
}.to have_enqueued_job(TicketCreateJob)
expected = generate_expected_tickets(payment_id: result['payment'].id,
nonprofit: nonprofit,
supporter: supporter,

View file

@ -16,15 +16,14 @@ RSpec.describe Campaign, type: :model do
end
it 'parent campaign sends out a create job' do
expect(HoudiniEventPublisher).to receive(:announce).with(:campaign_create, any_args).exactly(:once)
parent_campaign
expect(CampaignCreateJob).to have_been_enqueued.exactly(:once)
end
it 'child campaign sends out federated create job' do
expect(HoudiniEventPublisher).to receive(:announce).with(:campaign_create, any_args).exactly(:twice)
parent_campaign
expect(CampaignCreateJob).to have_been_enqueued.exactly(:once)
child_campaign
expect(CampaignCreateJob).to have_been_enqueued.exactly(:twice)
end
end
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,12 @@ RSpec.shared_context :shared_rd_donation_value_context do
def process_event_donation(data = {})
pay_method = data[:sepa] ? direct_debit_detail : card
unless (data[:recurring_donation])
expect(HoudiniEventPublisher).to receive(:announce).with(:donation_create,instance_of(Donation), supporter.locale )
else
expect(HoudiniEventPublisher).to receive(:announce).with(:recurring_donation_create,instance_of(Donation), supporter.locale )
end
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,40 +391,38 @@ 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
unless (data[:recurring_donation])
expect(HoudiniEventPublisher).to receive(:announce).with(:donation_create,instance_of(Donation), supporter.locale )
else
expect(HoudiniEventPublisher).to receive(:announce).with(:recurring_donation_create,instance_of(Donation), supporter.locale )
end
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'])
expect(result.count).to eq expected.count
expect(result['donation'].attributes).to eq expected[:donation]
expect(result['charge'].attributes).to eq expected[:charge]
# expect(result[:json]['activity']).to eq expected[:activity]
expect(result['payment'].attributes).to eq expected[:payment]
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
unless (data[:recurring_donation])
expect(HoudiniEventPublisher).to receive(:announce).with(:donation_create,instance_of(Donation), supporter.locale )
else
expect(HoudiniEventPublisher).to receive(:announce).with(:recurring_donation_create,instance_of(Donation), supporter.locale )
end
result = yield
expect_payment = nil_or_true(data[:expect_payment])
expect_charge = nil_or_true(data[:expect_charge])
@ -439,12 +443,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