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

View file

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

View file

@ -92,6 +92,9 @@ module InsertRecurringDonation
# Create the activity record
result['activity'] = InsertActivities.for_recurring_donations([result['payment'].id])
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
Houdini.event_publisher.announce(:recurring_donation_create, result['donation'], entities[:supporter_id].locale)
result

View file

@ -4,13 +4,13 @@
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
FactoryBot.define do
factory :recurrence do
supporter { association :supporter_with_fv_poverty}
supporter { association :supporter_with_fv_poverty }
recurring_donation do
association( :rd_with_dedication_designation,
association(:rd_with_dedication_designation,
nonprofit: supporter.nonprofit,
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
amount { 500 }
end

View file

@ -322,6 +322,10 @@ describe InsertRecurringDonation do
donation_builder.merge(common_builder_with_trx, common_builder_expanded)
end
let(:recurrence_builder_expanded) do
end
describe 'general donations' do
subject do
@ -357,6 +361,8 @@ describe InsertRecurringDonation do
}
}
)
expect(Houdini.event_publisher).to receive(:announce).with(:recurrence_created, any_args)
subject
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(: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, any_args)
subject
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(: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, any_args)
subject
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(:transaction_created, any_args)
expect(Houdini.event_publisher).to receive(:announce).with(:recurrence_created, any_args)
subject
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(: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
end
end

View file

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