recurrence.updated is fired when updated recurring donations

This commit is contained in:
Eric Schultz 2021-06-07 13:56:03 -05:00 committed by Eric Schultz
parent b72512906c
commit e04fd12f78
4 changed files with 62 additions and 1 deletions

View file

@ -2,7 +2,7 @@
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later # 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 # Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
class Recurrence < ApplicationRecord class Recurrence < ApplicationRecord # rubocop:disable Metrics/ClassLength
include Model::Houidable include Model::Houidable
include Model::Jbuilder include Model::Jbuilder
include Model::Eventable include Model::Eventable
@ -113,6 +113,13 @@ class Recurrence < ApplicationRecord
to_event('recurrence.created', :nonprofit, :trx, :supporter).attributes! to_event('recurrence.created', :nonprofit, :trx, :supporter).attributes!
) )
end end
def publish_updated
Houdini.event_publisher.announce(
:recurrence_updated,
to_event('recurrence.updated', :nonprofit, :trx, :supporter).attributes!
)
end
end end
private private

View file

@ -45,6 +45,8 @@ module UpdateRecurringDonations
return ValidationError.new(['Invalid paydate']) unless (1..28).cover?(paydate.to_i) return ValidationError.new(['Invalid paydate']) unless (1..28).cover?(paydate.to_i)
Psql.execute(Qexpr.new.update(:recurring_donations, paydate: paydate).where('id=$id', id: rd['id'])) Psql.execute(Qexpr.new.update(:recurring_donations, paydate: paydate).where('id=$id', id: rd['id']))
recurring_donation = RecurringDonation.find(rd['id'])
recurring_donation.recurrence.publish_updated
rd['paydate'] = paydate rd['paydate'] = paydate
rd rd
end end
@ -71,6 +73,9 @@ module UpdateRecurringDonations
rd.n_failures = 0 rd.n_failures = 0
donation.card = tokenizable donation.card = tokenizable
donation.amount = amount donation.amount = amount
rd.recurrence.amount = amount
rd.recurrence.save!
rd.recurrence.publish_updated
rd.save! rd.save!
donation.save! donation.save!
end end

View file

@ -85,12 +85,59 @@ describe UpdateRecurringDonations do
end end
end end
let(:np_builder_expanded) do
{
'id' => nonprofit.id,
'name' => nonprofit.name,
'object' => 'nonprofit'
}
end
let(:supporter_builder_expanded) do
supporter_to_builder_base.merge({ 'name' => 'Fake Supporter Name' })
end
let(:common_builder_expanded) do
{
'supporter' => supporter_builder_expanded,
'nonprofit' => np_builder_expanded
}
end
it 'changes amount properly' do it 'changes amount properly' do
recurring_donation.n_failures = 2 recurring_donation.n_failures = 2
recurring_donation.save! recurring_donation.save!
recurrence
orig_recurrence = recurring_donation.recurrence
orig_rd = recurring_donation.attributes.with_indifferent_access orig_rd = recurring_donation.attributes.with_indifferent_access
orig_donation = recurring_donation.donation.attributes.with_indifferent_access orig_donation = recurring_donation.donation.attributes.with_indifferent_access
expect(Houdini.event_publisher).to receive(:announce).with(:recurrence_updated, {
'id' => match_houid('objevt'),
'object' => 'object_event',
'type' => 'recurrence.updated',
'data' => {
'object' => common_builder_expanded.merge({
'object' => 'recurrence',
'id' => orig_recurrence.id,
'start_date' => orig_recurrence.start_date,
'recurrences' => [
{
'interval' => 1,
'type' => 'monthly'
}],
'invoice_template' => {
'supporter' => supporter.id,
'amount' => {'cents' => 1000, 'currency' => 'usd'},
'payment_method' => {'type' => 'stripe'},
'trx_assignments' => [
{
'assignment_object' => 'donation',
'designation' => nil,
'amount' => {'cents' => 1000, 'currency' => 'usd'}
}]
}
})
}})
result = nil result = nil
expect { expect {
@ -103,6 +150,7 @@ describe UpdateRecurringDonations do
recurring_donation: orig_rd.merge(amount: 1000, n_failures: 0, start_date: Time.current.to_date) recurring_donation: orig_rd.merge(amount: 1000, n_failures: 0, start_date: Time.current.to_date)
} }
expect(result.attributes).to eq expectations[:recurring_donation] expect(result.attributes).to eq expectations[:recurring_donation]
recurring_donation.reload recurring_donation.reload

View file

@ -36,6 +36,7 @@ RSpec.shared_context :shared_donation_charge_context do
let(:donation_for_rd) { force_create(:donation, recurring: true, nonprofit: nonprofit, supporter: supporter, card: card_with_valid_stripe_id, amount: 500) } let(:donation_for_rd) { force_create(:donation, recurring: true, nonprofit: nonprofit, supporter: supporter, card: card_with_valid_stripe_id, amount: 500) }
let(:recurring_donation) { force_create(:recurring_donation, donation: donation_for_rd, nonprofit: nonprofit, supporter: supporter, start_date: Time.now, interval: 1, time_unit: 'month') } let(:recurring_donation) { force_create(:recurring_donation, donation: donation_for_rd, nonprofit: nonprofit, supporter: supporter, start_date: Time.now, interval: 1, time_unit: 'month') }
let(:recurrence) { force_create(:recurrence, recurring_donation: recurring_donation, supporter: supporter, amount: 500, start_date: Time.now)}
let(:stripe_helper) { StripeMock.create_test_helper } let(:stripe_helper) { StripeMock.create_test_helper }