All stats pass with listeners

This commit is contained in:
Eric Schultz 2020-02-10 14:01:49 -06:00
parent 2a20b9f381
commit e1b2ac4d28
24 changed files with 70 additions and 69 deletions

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

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

@ -1,4 +1,4 @@
class CreditCardPaymentListener
class CreditCardPaymentListener < ApplicationListener
def donation_create(donation, locale, user=nil)
if donation.payment_provider == :credit_card
PaymentNotificationEmailDonorJob.perform_later donation, locale
@ -16,5 +16,18 @@ class CreditCardPaymentListener
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

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

View file

@ -1,12 +1,8 @@
class SepaPaymentListener
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
def recurring_donation_create(donation, locale, user=nil)
end
end

View file

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

View file

@ -1,4 +1,4 @@
class TicketListener
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

View file

@ -1,4 +1,4 @@
class WemoveListener
class WemoveListener < ApplicationListener
def donation_create(donation)
WeMoveExecuteForDonationsJob.perform_later(donation)
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

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

@ -4,7 +4,8 @@
HoudiniEventPublisher = EventPublisher.new
Rails.application.config.to_prepare do
HoudiniEventPublisher.clear if Rails.env.development?
HoudiniEventPublisher.subscribe_async(NonprofitMailerListener.new)
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

View file

@ -4,7 +4,7 @@
class EventPublisher
include Wisper::Publisher
def call(event, *args)
def announce(event, *args)
broadcast(event, *args)
end

View file

@ -43,7 +43,7 @@ module InsertDonation
result['donation'] = insert_donation(data, entities)
update_donation_keys(result)
result['activity'] = InsertActivities.for_one_time_donations([result['payment'].id])
HoudiniEventPublisher.call(:donation_create, result['donation'], result['donation'].supporter.locale)
HoudiniEventPublisher.announce(:donation_create, result['donation'], result['donation'].supporter.locale)
result
end
@ -104,7 +104,7 @@ module InsertDonation
result['donation'] = insert_donation(data, entities)
update_donation_keys(result)
HoudiniEventPublisher.call(:donation_create, result['donation'], locale_for_supporter(result['donation'].supporter.id))
HoudiniEventPublisher.announce(:donation_create, result['donation'], locale_for_supporter(result['donation'].supporter.id))
# do this for making test consistent
result['activity'] = {}

View file

@ -70,7 +70,7 @@ module InsertRecurringDonation
result['activity'] = InsertActivities.for_recurring_donations([result['payment'].id])
end
# Send receipts
HoudiniEventPublisher.call(:recurring_donation_create, result['donation'], entities[:supporter_id].locale)
HoudiniEventPublisher.announce(:recurring_donation_create, result['donation'], entities[:supporter_id].locale)
result
end
@ -93,7 +93,7 @@ module InsertRecurringDonation
InsertDonation.update_donation_keys(result) if result['payment']
HoudiniEventPublisher.call(:recurring_donation_create, result['donation'], entities[:supporter_id].locale)
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
HoudiniEventPublisher.call(:create_refund, 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
HoudiniEventPublisher.call(:ticket_create, result['tickets'], result['charge'])
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
@ -131,6 +132,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,7 +285,7 @@ describe InsertTickets do
success_expectations
expect(QueryRoles).to receive(:is_authorized_for_nonprofit?).with(user.id, nonprofit.id).and_return true
result = nil
expect(HoudiniEventPublisher).to receive(:call).with(:ticket_create, any_args)
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)
expected = generate_expected_tickets(payment_id: result['payment'].id,

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

@ -376,9 +376,9 @@ RSpec.shared_context :shared_rd_donation_value_context do
pay_method = data[:sepa] ? direct_debit_detail : card
unless (data[:recurring_donation])
expect(HoudiniEventPublisher).to receive(:call).with(:donation_create,instance_of(Donation), supporter.locale )
expect(HoudiniEventPublisher).to receive(:announce).with(:donation_create,instance_of(Donation), supporter.locale )
else
expect(HoudiniEventPublisher).to receive(:call).with(:recurring_donation_create,instance_of(Donation), supporter.locale )
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'])
@ -399,9 +399,9 @@ RSpec.shared_context :shared_rd_donation_value_context do
pay_method = data[:sepa] ? direct_debit_detail : card
unless (data[:recurring_donation])
expect(HoudiniEventPublisher).to receive(:call).with(:donation_create,instance_of(Donation), supporter.locale )
expect(HoudiniEventPublisher).to receive(:announce).with(:donation_create,instance_of(Donation), supporter.locale )
else
expect(HoudiniEventPublisher).to receive(:call).with(:recurring_donation_create,instance_of(Donation), supporter.locale )
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'])
@ -420,9 +420,9 @@ RSpec.shared_context :shared_rd_donation_value_context do
def process_general_donation(data = {})
pay_method = data[:sepa] ? direct_debit_detail : card
unless (data[:recurring_donation])
expect(HoudiniEventPublisher).to receive(:call).with(:donation_create,instance_of(Donation), supporter.locale )
expect(HoudiniEventPublisher).to receive(:announce).with(:donation_create,instance_of(Donation), supporter.locale )
else
expect(HoudiniEventPublisher).to receive(:call).with(:recurring_donation_create,instance_of(Donation), supporter.locale )
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])