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
@ -18,7 +17,7 @@ class Recurrence < ApplicationRecord
delegate :currency, to: :nonprofit
delegate :designation, :dedication, to: :recurring_donation
validates :recurrences, presence: true
def trx_assignments
@ -44,36 +43,36 @@ 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
json.start_date start_date
json.recurrences recurrences do |rec|
json.(rec, :interval, :type)
end
json.invoice_template do
json.amount do
json.invoice_template do # rubocop:disable Metrics/BlockLength
json.amount do
json.cents amount || 0
json.currency currency
end
json.supporter supporter.id
json.payment_method do
json.payment_method do
json.type 'stripe'
end
@ -81,17 +80,21 @@ class Recurrence < ApplicationRecord
json.assignment_object assign[:assignment_object]
dedication = assign[:dedication]
json.dedication do
json.type dedication['type']
json.name dedication['name']
contact = dedication['contact']
json.note dedication['note']
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
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
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

@ -3,15 +3,15 @@
# 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
FactoryBot.define do
factory :recurrence do
supporter { association :supporter_with_fv_poverty}
recurring_donation do
association( :rd_with_dedication_designation,
nonprofit: supporter.nonprofit,
supporter: supporter,
donation: association(:donation_with_dedication_designation, nonprofit: supporter.nonprofit, supporter: supporter)
)
end
amount { 500 }
end
factory :recurrence do
supporter { association :supporter_with_fv_poverty }
recurring_donation do
association(:rd_with_dedication_designation,
nonprofit: supporter.nonprofit,
supporter: supporter,
donation: association(:donation_with_dedication_designation, nonprofit: supporter.nonprofit,
supporter: supporter))
end
amount { 500 }
end
end

View file

@ -321,6 +321,10 @@ describe InsertRecurringDonation do
let(:donation_builder_expanded) do
donation_builder.merge(common_builder_with_trx, common_builder_expanded)
end
let(:recurrence_builder_expanded) do
end
describe 'general donations' 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,98 +5,102 @@
require 'rails_helper'
RSpec.describe Recurrence, type: :model do
around(:each) do |example|
Timecop.freeze(2020,5,4) do
example.run
end
end
describe 'recurrence with donations, designation and dedication' do
subject { create(:recurrence) }
around do |example|
Timecop.freeze(2020, 5, 4) do
example.run
end
end
def trx_assignment_match
{
assignment_object: 'donation',
amount: 500,
dedication: {
contact: {
email: 'email@ema.com',
},
name: 'our loved one',
note: "we miss them dearly",
type: 'memory'
},
designation: 'designated for soup kitchen'
}
end
describe 'recurrence with donations, designation and dedication' do
subject { create(:recurrence) }
def trx_assignment_json
trx_assignment_match.merge({amount: {cents: 500, currency: 'usd'}})
end
def trx_assignment_match # rubocop:disable Metrics/MethodLength
{
assignment_object: 'donation',
amount: 500,
dedication: {
contact: {
email: 'email@ema.com'
},
name: 'our loved one',
note: 'we miss them dearly',
type: 'memory'
},
designation: 'designated for soup kitchen'
}
end
def invoice_template_match
{
amount: 500,
supporter: an_instance_of(Supporter),
payment_method: {
type: 'stripe'
},
trx_assignments: [trx_assignment_match]
}
end
def trx_assignment_json
trx_assignment_match.merge({ amount: { cents: 500, currency: 'usd' } })
end
def invoice_template_json
invoice_template_match.merge(trx_assignments: [trx_assignment_json]).deep_stringify_keys
end
def invoice_template_match
{
it {is_expected.to have_attributes(
supporter: an_instance_of(Supporter),
nonprofit: an_instance_of(Nonprofit),
start_date: Time.current,
id: match_houid('recur')
)}
amount: 500,
supporter: an_instance_of(Supporter),
payment_method: {
type: 'stripe'
},
trx_assignments: [trx_assignment_match]
}
end
it {is_expected.to have_attributes( recurrences: contain_exactly(
{
interval: 1,
type: 'monthly'
}
)
)}
def invoice_template_json
invoice_template_match.merge(trx_assignments: [trx_assignment_json]).deep_stringify_keys
end
it {is_expected.to be_persisted}
it do
is_expected.to have_attributes(
invoice_template: invoice_template_match
)
end
it {
is_expected.to have_attributes(
supporter: an_instance_of(Supporter),
nonprofit: an_instance_of(Nonprofit),
start_date: Time.current,
id: match_houid('recur')
)
}
describe '.to_builder' do
let(:recurrence) { create(:recurrence)}
subject { JSON.parse(recurrence.to_builder.target!)}
it {
is_expected.to have_attributes(recurrences: contain_exactly(
{
interval: 1,
type: 'monthly'
}
))
}
let(:invoice_template) { subject['invoice_template']}
it { is_expected.to be_persisted }
it do is_expected.to match_json({
object: 'recurrence',
nonprofit: kind_of(Numeric),
supporter: kind_of(Numeric),
id: match_houid('recur'),
start_date: Time.current,
recurrences: [
{
interval: 1,
type: 'monthly'
}],
invoice_template: { supporter: kind_of(Numeric),
amount: {'cents' => 500, 'currency' => 'usd'},
payment_method: {'type' => 'stripe'},
trx_assignments: [trx_assignment_json]
}
})
end
end
end
it do
is_expected.to have_attributes(
invoice_template: invoice_template_match
)
end
describe '.to_builder' do
subject { JSON.parse(recurrence.to_builder.target!) }
let(:recurrence) { create(:recurrence) }
let(:invoice_template) { subject['invoice_template'] }
it do
is_expected.to match_json({
object: 'recurrence',
nonprofit: kind_of(Numeric),
supporter: kind_of(Numeric),
id: match_houid('recur'),
start_date: Time.current,
recurrences: [
{
interval: 1,
type: 'monthly'
}
],
invoice_template: { supporter: kind_of(Numeric),
amount: { 'cents' => 500, 'currency' => 'usd' },
payment_method: { 'type' => 'stripe' },
trx_assignments: [trx_assignment_json] }
})
end
end
end
end