recurrence.created is fired when inserting recurring donations

This commit is contained in:
Eric Schultz 2021-06-07 12:34:08 -05:00 committed by Eric Schultz
parent 157bd20b26
commit b72512906c
6 changed files with 176 additions and 123 deletions

View file

@ -3,7 +3,6 @@
# 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
include Model::Houidable include Model::Houidable
include Model::Jbuilder include Model::Jbuilder
include Model::Eventable include Model::Eventable
@ -44,17 +43,17 @@ class Recurrence < ApplicationRecord
amount: amount || 0, amount: amount || 0,
trx_assignments: trx_assignments, trx_assignments: trx_assignments,
supporter: supporter, supporter: supporter,
payment_method: {type: 'stripe'} payment_method: { type: 'stripe' }
} }
end end
concerning :JBuilder do concerning :JBuilder do # rubocop:disable Metrics/BlockLength
included do included do
setup_houid :recur setup_houid :recur
end end
def to_builder(*expand) def to_builder(*expand) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity
init_builder(*expand) do |json| init_builder(*expand) do |json| # rubocop:disable Metrics/BlockLength
json.start_date start_date.to_i json.start_date start_date.to_i
json.add_builder_expansion :nonprofit, :supporter json.add_builder_expansion :nonprofit, :supporter
@ -65,7 +64,7 @@ class Recurrence < ApplicationRecord
json.(rec, :interval, :type) json.(rec, :interval, :type)
end end
json.invoice_template do json.invoice_template do # rubocop:disable Metrics/BlockLength
json.amount do json.amount do
json.cents amount || 0 json.cents amount || 0
json.currency currency json.currency currency
@ -81,17 +80,21 @@ class Recurrence < ApplicationRecord
json.assignment_object assign[:assignment_object] json.assignment_object assign[:assignment_object]
dedication = assign[:dedication] dedication = assign[:dedication]
if dedication
json.dedication do json.dedication do
json.type dedication['type'] json.type dedication['type']
json.name dedication['name'] json.name dedication['name']
contact = dedication['contact'] contact = dedication['contact']
json.note dedication['note'] json.note dedication['note']
if contact
json.contact do json.contact do
json.email contact['email'] if contact['email'] json.email contact['email'] if contact['email']
json.address contact['address'] if contact['address'] json.address contact['address'] if contact['address']
json.phone contact['phone'] if contact['phone'] json.phone contact['phone'] if contact['phone']
end if contact end
end if dedication end
end
end
json.designation assign[:designation] json.designation assign[:designation]
@ -101,11 +104,9 @@ class Recurrence < ApplicationRecord
end end
end end
end end
end end
end end
def publish_created def publish_created
Houdini.event_publisher.announce( Houdini.event_publisher.announce(
:recurrence_created, :recurrence_created,
@ -120,8 +121,6 @@ class Recurrence < ApplicationRecord
self[:start_date] = Time.current unless self[:start_date] self[:start_date] = Time.current unless self[:start_date]
end end
private
def from_recurring_time_unit_to_recurrence(time_unit) def from_recurring_time_unit_to_recurrence(time_unit)
{ {
'month' => 'monthly', 'month' => 'monthly',
@ -129,4 +128,3 @@ class Recurrence < ApplicationRecord
}[time_unit] }[time_unit]
end end
end end

View file

@ -30,6 +30,7 @@ class RecurringDonation < ApplicationRecord
has_many :charges, through: :donation has_many :charges, through: :donation
has_one :card, through: :donation has_one :card, through: :donation
has_one :supporter, through: :donation has_one :supporter, through: :donation
has_one :recurrence
validates :paydate, numericality: { less_than: 29 }, allow_blank: true validates :paydate, numericality: { less_than: 29 }, allow_blank: true
validates :donation_id, presence: true validates :donation_id, presence: true

View file

@ -92,6 +92,9 @@ module InsertRecurringDonation
# Create the activity record # Create the activity record
result['activity'] = InsertActivities.for_recurring_donations([result['payment'].id]) result['activity'] = InsertActivities.for_recurring_donations([result['payment'].id])
end end
recurrence = result['recurring_donation'].create_recurrence!(supporter: result['recurring_donation'].supporter, start_date:result['recurring_donation'].start_date, amount: result['recurring_donation'].amount )
recurrence.publish_created
# Send receipts # Send receipts
Houdini.event_publisher.announce(:recurring_donation_create, result['donation'], entities[:supporter_id].locale) Houdini.event_publisher.announce(:recurring_donation_create, result['donation'], entities[:supporter_id].locale)
result result

View file

@ -4,13 +4,13 @@
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE # Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
FactoryBot.define do FactoryBot.define do
factory :recurrence do factory :recurrence do
supporter { association :supporter_with_fv_poverty} supporter { association :supporter_with_fv_poverty }
recurring_donation do recurring_donation do
association( :rd_with_dedication_designation, association(:rd_with_dedication_designation,
nonprofit: supporter.nonprofit, nonprofit: supporter.nonprofit,
supporter: supporter, supporter: supporter,
donation: association(:donation_with_dedication_designation, nonprofit: supporter.nonprofit, supporter: supporter) donation: association(:donation_with_dedication_designation, nonprofit: supporter.nonprofit,
) supporter: supporter))
end end
amount { 500 } amount { 500 }
end end

View file

@ -322,6 +322,10 @@ describe InsertRecurringDonation do
donation_builder.merge(common_builder_with_trx, common_builder_expanded) donation_builder.merge(common_builder_with_trx, common_builder_expanded)
end end
let(:recurrence_builder_expanded) do
end
describe 'general donations' do describe 'general donations' do
subject do subject do
@ -357,6 +361,8 @@ describe InsertRecurringDonation do
} }
} }
) )
expect(Houdini.event_publisher).to receive(:announce).with(:recurrence_created, any_args)
subject subject
end end
@ -377,7 +383,7 @@ describe InsertRecurringDonation do
expect(Houdini.event_publisher).to receive(:announce).with(:donation_created, any_args) expect(Houdini.event_publisher).to receive(:announce).with(:donation_created, any_args)
expect(Houdini.event_publisher).to receive(:announce).with(:trx_assignment_created, any_args) expect(Houdini.event_publisher).to receive(:announce).with(:trx_assignment_created, any_args)
expect(Houdini.event_publisher).to receive(:announce).with(:transaction_created, any_args) expect(Houdini.event_publisher).to receive(:announce).with(:transaction_created, any_args)
expect(Houdini.event_publisher).to receive(:announce).with(:recurrence_created, any_args)
subject subject
end end
@ -420,6 +426,7 @@ describe InsertRecurringDonation do
expect(Houdini.event_publisher).to receive(:announce).with(:donation_created, any_args) expect(Houdini.event_publisher).to receive(:announce).with(:donation_created, any_args)
expect(Houdini.event_publisher).to receive(:announce).with(:trx_assignment_created, any_args) expect(Houdini.event_publisher).to receive(:announce).with(:trx_assignment_created, any_args)
expect(Houdini.event_publisher).to receive(:announce).with(:transaction_created, any_args) expect(Houdini.event_publisher).to receive(:announce).with(:transaction_created, any_args)
expect(Houdini.event_publisher).to receive(:announce).with(:recurrence_created, any_args)
subject subject
end end
@ -440,6 +447,7 @@ describe InsertRecurringDonation do
) )
expect(Houdini.event_publisher).to receive(:announce).with(:trx_assignment_created, any_args) expect(Houdini.event_publisher).to receive(:announce).with(:trx_assignment_created, any_args)
expect(Houdini.event_publisher).to receive(:announce).with(:transaction_created, any_args) expect(Houdini.event_publisher).to receive(:announce).with(:transaction_created, any_args)
expect(Houdini.event_publisher).to receive(:announce).with(:recurrence_created, any_args)
subject subject
end end
@ -460,6 +468,45 @@ describe InsertRecurringDonation do
} }
) )
expect(Houdini.event_publisher).to receive(:announce).with(:transaction_created, any_args) expect(Houdini.event_publisher).to receive(:announce).with(:transaction_created, any_args)
expect(Houdini.event_publisher).to receive(:announce).with(:recurrence_created, any_args)
subject
end
it 'has fired recurrence.created' do
expect(Houdini.event_publisher).to receive(:announce).with(:stripe_transaction_charge_created, any_args)
expect(Houdini.event_publisher).to receive(:announce).with(:payment_created, any_args)
expect(Houdini.event_publisher).to receive(:announce).with(:stripe_transaction_created, any_args)
expect(Houdini.event_publisher).to receive(:announce).with(:donation_created, any_args)
expect(Houdini.event_publisher).to receive(:announce).with(:trx_assignment_created, any_args)
expect(Houdini.event_publisher).to receive(:announce).with(:transaction_created, any_args)
expect(Houdini.event_publisher).to receive(:announce).with(:recurrence_created, {
'id' => match_houid('objevt'),
'object' => 'object_event',
'type' => 'recurrence.created',
'data' => {
'object' => common_builder_expanded.merge({
'object' => 'recurrence',
'id' => match_houid('recur'),
'start_date' => Time.current,
'recurrences' => [
{
'interval' => 1,
'type' => 'monthly'
}],
'invoice_template' => {
'supporter' => supporter.id,
'amount' => {'cents' => charge_amount, 'currency' => 'usd'},
'payment_method' => {'type' => 'stripe'},
'trx_assignments' => [
{
'assignment_object' => 'donation',
'dedication' => { 'name' => 'a name', 'type' => 'honor', 'note' => nil},
'designation' => 'designation',
'amount' => {'cents' => charge_amount, 'currency' => 'usd'}
}]
}
})
}})
subject subject
end end
end end

View file

@ -5,8 +5,8 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Recurrence, type: :model do RSpec.describe Recurrence, type: :model do
around(:each) do |example| around do |example|
Timecop.freeze(2020,5,4) do Timecop.freeze(2020, 5, 4) do
example.run example.run
end end
end end
@ -14,16 +14,16 @@ RSpec.describe Recurrence, type: :model do
describe 'recurrence with donations, designation and dedication' do describe 'recurrence with donations, designation and dedication' do
subject { create(:recurrence) } subject { create(:recurrence) }
def trx_assignment_match def trx_assignment_match # rubocop:disable Metrics/MethodLength
{ {
assignment_object: 'donation', assignment_object: 'donation',
amount: 500, amount: 500,
dedication: { dedication: {
contact: { contact: {
email: 'email@ema.com', email: 'email@ema.com'
}, },
name: 'our loved one', name: 'our loved one',
note: "we miss them dearly", note: 'we miss them dearly',
type: 'memory' type: 'memory'
}, },
designation: 'designated for soup kitchen' designation: 'designated for soup kitchen'
@ -31,7 +31,7 @@ RSpec.describe Recurrence, type: :model do
end end
def trx_assignment_json def trx_assignment_json
trx_assignment_match.merge({amount: {cents: 500, currency: 'usd'}}) trx_assignment_match.merge({ amount: { cents: 500, currency: 'usd' } })
end end
def invoice_template_match def invoice_template_match
@ -50,22 +50,25 @@ RSpec.describe Recurrence, type: :model do
invoice_template_match.merge(trx_assignments: [trx_assignment_json]).deep_stringify_keys invoice_template_match.merge(trx_assignments: [trx_assignment_json]).deep_stringify_keys
end end
it {is_expected.to have_attributes( it {
is_expected.to have_attributes(
supporter: an_instance_of(Supporter), supporter: an_instance_of(Supporter),
nonprofit: an_instance_of(Nonprofit), nonprofit: an_instance_of(Nonprofit),
start_date: Time.current, start_date: Time.current,
id: match_houid('recur') id: match_houid('recur')
)} )
}
it {is_expected.to have_attributes( recurrences: contain_exactly( it {
is_expected.to have_attributes(recurrences: contain_exactly(
{ {
interval: 1, interval: 1,
type: 'monthly' type: 'monthly'
} }
) ))
)} }
it {is_expected.to be_persisted} it { is_expected.to be_persisted }
it do it do
is_expected.to have_attributes( is_expected.to have_attributes(
@ -74,12 +77,13 @@ RSpec.describe Recurrence, type: :model do
end end
describe '.to_builder' do describe '.to_builder' do
let(:recurrence) { create(:recurrence)} subject { JSON.parse(recurrence.to_builder.target!) }
subject { JSON.parse(recurrence.to_builder.target!)}
let(:invoice_template) { subject['invoice_template']} let(:recurrence) { create(:recurrence) }
let(:invoice_template) { subject['invoice_template'] }
it do is_expected.to match_json({ it do
is_expected.to match_json({
object: 'recurrence', object: 'recurrence',
nonprofit: kind_of(Numeric), nonprofit: kind_of(Numeric),
supporter: kind_of(Numeric), supporter: kind_of(Numeric),
@ -89,12 +93,12 @@ RSpec.describe Recurrence, type: :model do
{ {
interval: 1, interval: 1,
type: 'monthly' type: 'monthly'
}],
invoice_template: { supporter: kind_of(Numeric),
amount: {'cents' => 500, 'currency' => 'usd'},
payment_method: {'type' => 'stripe'},
trx_assignments: [trx_assignment_json]
} }
],
invoice_template: { supporter: kind_of(Numeric),
amount: { 'cents' => 500, 'currency' => 'usd' },
payment_method: { 'type' => 'stripe' },
trx_assignments: [trx_assignment_json] }
}) })
end end
end end