Creates OfflineTransactionRefund event publishing

This commit is contained in:
Clarissa Borges 2021-06-07 17:02:02 -03:00 committed by Eric Schultz
parent 7e02530fda
commit 7972ec8ea7
18 changed files with 402 additions and 24 deletions

View file

@ -731,9 +731,12 @@ Layout/IndentationStyle:
Layout/IndentationWidth:
Width: 1
Metrics/MethodLength:
Max: 25
Metrics/BlockLength:
Exclude:
- '**/*_spec.rb'
- 'spec/**/*.rb'
Style/ClassAndModuleChildren:
EnforcedStyle: compact

View file

@ -151,6 +151,18 @@ class ObjectEventListener < ApplicationListener
enqueue_transmissions_to_webhooks(event)
end
def self.offline_transaction_refund_created(event)
enqueue_transmissions_to_webhooks(event)
end
def self.offline_transaction_refund_updated(event)
enqueue_transmissions_to_webhooks(event)
end
def self.offline_transaction_refund_deleted(event)
enqueue_transmissions_to_webhooks(event)
end
private
def self.enqueue_transmissions_to_webhooks(event)

View file

@ -3,7 +3,7 @@
# 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
# rubocop:disable Metrics/MethodLength, Metrics/BlockLength, Metrics/AbcSize
# rubocop:disable Metrics/BlockLength, Metrics/AbcSize
class OfflineTransactionCharge < ApplicationRecord
include Model::SubtransactionPaymentable
belongs_to :payment

View file

@ -3,5 +3,94 @@
# 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 OfflineTransactionRefund < ApplicationRecord
include Model::SubtransactionPaymentable
belongs_to :payment
delegate :gross_amount, :net_amount, :fee_total, to: :payment
delegate :currency, to: :nonprofit
# rubocop:disable Metrics/BlockLength
concerning :JBuilder do
included do
setup_houid :offtrxrfnd
end
# rubocop:disable Metrics/AbcSize
def to_builder(*expand)
init_builder(*expand) do |json|
json.object 'offline_transaction_refund'
json.gross_amount do
json.cents gross_amount
json.currency currency
end
json.net_amount do
json.cents net_amount
json.currency currency
end
json.fee_total do
json.cents fee_total
json.currency currency
end
json.created payment.date.to_i
json.type 'payment'
json.add_builder_expansion :nonprofit, :supporter, :subtransaction
json.add_builder_expansion :trx, json_attribute: :transaction
end
end
# rubocop:enable Metrics/AbcSize
def to_id
::Jbuilder.new do |json|
json.(self, :id)
json.object 'offline_transaction_refund'
json.type 'payment'
end
end
def publish_created
Houdini.event_publisher.announce(
:offline_transaction_refund_created,
to_event('offline_transaction_refund.created',
:nonprofit,
:trx,
:supporter,
:subtransaction).attributes!
)
Houdini.event_publisher.announce(
:payment_created,
to_event('payment.created',
:nonprofit,
:trx,
:supporter,
:subtransaction).attributes!
)
end
def publish_updated
Houdini.event_publisher.announce(
:offline_transaction_refund_updated,
to_event('offline_transaction_refund.updated', :nonprofit, :trx, :supporter, :subtransaction).attributes!
)
Houdini.event_publisher.announce(
:payment_updated,
to_event('payment.updated', :nonprofit, :trx, :supporter, :subtransaction).attributes!
)
end
def publish_deleted
Houdini.event_publisher.announce(
:offline_transaction_refund_deleted,
to_event('offline_transaction_refund.deleted', :nonprofit, :trx, :supporter, :subtransaction).attributes!
)
Houdini.event_publisher.announce(
:payment_deleted,
to_event('payment.deleted', :nonprofit, :trx, :supporter, :subtransaction).attributes!
)
end
end
# rubocop:enable Metrics/BlockLength
end

View file

@ -135,7 +135,7 @@ class Recurrence < ApplicationRecord # rubocop:disable Metrics/ClassLength
}[time_unit]
end
def recurrence_start_date # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
def recurrence_start_date # rubocop:disable Metrics/AbcSize
paydate = recurring_donation.paydate
paydate = if paydate.nil?
(1..28).cover?(start_date.day) ? start_date.day : 28

View file

@ -15,7 +15,7 @@ class StripeCharge < ApplicationRecord
setup_houid :stripechrg
end
def to_builder(*expand) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
def to_builder(*expand) # rubocop:disable Metrics/AbcSize
init_builder(*expand) do |json|
json.object 'stripe_transaction_charge'
json.gross_amount do
@ -53,7 +53,7 @@ class StripeCharge < ApplicationRecord
end
end
def publish_created # rubocop:disable Metrics/MethodLength
def publish_created
Houdini.event_publisher.announce(
:stripe_transaction_charge_created,
to_event('stripe_transaction_charge.created',

View file

@ -13,7 +13,7 @@ class SubtransactionPayment < ApplicationRecord
has_one :supporter, through: :subtransaction
has_one :nonprofit, through: :subtransaction
delegated_type :paymentable, types: %w[OfflineTransactionCharge StripeCharge]
delegated_type :paymentable, types: %w[OfflineTransactionCharge StripeCharge OfflineTransactionRefund]
delegate :gross_amount, :fee_total, :net_amount, to: :paymentable

View file

@ -14,13 +14,11 @@ class Transaction < ApplicationRecord
has_many :ticket_purchases, through: :transaction_assignments, source: :assignable, source_type: 'TicketPurchase', inverse_of: 'trx'
has_many :campaign_gift_purchases, through: :transaction_assignments, source: :assignable, source_type: 'CampaignGiftPurchase', inverse_of: 'trx'
has_one :subtransaction
has_many :subtransaction_payments, through: :subtransaction
validates :supporter, presence: true
concerning :JBuilder do
include Model::Houidable
include Model::Jbuilder

View file

@ -1049,7 +1049,6 @@ ActiveRecord::Schema.define(version: 2021_06_02_220525) do
add_foreign_key "offline_transaction_refunds", "payments"
add_foreign_key "recurrences", "recurring_donations"
add_foreign_key "recurrences", "supporters"
add_foreign_key "offline_transaction_refunds", "payments"
add_foreign_key "stripe_charges", "payments"
add_foreign_key "stripe_refunds", "payments"
add_foreign_key "subtransaction_payments", "subtransactions"

View file

@ -4,6 +4,6 @@
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
FactoryBot.define do
factory :offline_transaction_charge do
amount { 1 }
payment { '' }
end
end

View file

@ -3,8 +3,7 @@
# 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 :offline_transaction_refund do
payment { "" }
created { "2021-04-12 17:24:33" }
end
factory :offline_transaction_refund do
payment { '' }
end
end

View file

@ -2,8 +2,8 @@
# 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 # rubocop:disable Metrics/BlockLength
factory :recurrence do # rubocop:disable Metrics/BlockLength
FactoryBot.define do
factory :recurrence do
supporter { association :supporter_with_fv_poverty }
recurring_donation do
association(:rd_with_dedication_designation,

View file

@ -0,0 +1,9 @@
# frozen_string_literal: true
# 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 :subtransaction_payment do
paymentable { '' }
end
end

View file

@ -0,0 +1,9 @@
# frozen_string_literal: true
# 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 :subtransaction do
transaction { '' }
end
end

View file

@ -3,7 +3,7 @@
# 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 :transaction do
end
factory :transaction do
supporter { create(:supporter) }
end
end

View file

@ -5,5 +5,265 @@
require 'rails_helper'
RSpec.describe OfflineTransactionRefund, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
let!(:nonprofit) { create(:nm_justice) }
let!(:supporter) { force_create(:supporter, nonprofit: nonprofit) }
let(:offline_transaction_refund) do
build(
:offline_transaction_refund,
payment:
force_create(
:payment,
gross_amount: 500,
net_amount: 400,
fee_total: 100,
date: Time.zone.now,
nonprofit: nonprofit,
supporter: supporter
)
)
end
let(:offline_transaction_charge) do
build(
:offline_transaction_charge,
payment:
force_create(
:payment,
gross_amount: 400,
net_amount: 300,
fee_total: 100,
date: Time.zone.now,
nonprofit: nonprofit,
supporter: supporter
)
)
end
let(:offline_transaction) { build(:offline_transaction) }
let(:transaction) do
trx = supporter.transactions.build(amount: 500)
trx.build_subtransaction(
subtransactable: OfflineTransaction.new(amount: 500),
subtransaction_payments: [
build(:subtransaction_payment, paymentable: offline_transaction_refund),
build(:subtransaction_payment, paymentable: offline_transaction_charge)
]
)
trx.save!
trx
end
let(:event_publisher) { double }
let(:expected_event) do
{
'data' => {
'object' => {
'created' => kind_of(Numeric),
'fee_total' => { 'cents' => 100, 'currency' => nonprofit.currency },
'gross_amount' => { 'cents' => 500, 'currency' => nonprofit.currency },
'id' => match_houid('offtrxrfnd'),
'net_amount' => { 'cents' => 400, 'currency' => nonprofit.currency },
'nonprofit' => {
'id' => nonprofit.id,
'name' => nonprofit.name,
'object' => 'nonprofit'
},
'object' => 'offline_transaction_refund',
'subtransaction' => {
'created' => kind_of(Numeric),
'id' => match_houid('offlinetrx'),
'initial_amount' => { 'cents' => 500, 'currency' => nonprofit.currency },
'net_amount' => { 'cents' => 700, 'currency' => nonprofit.currency },
'nonprofit' => nonprofit.id,
'object' => 'offline_transaction',
'payments' => [
{
'id' => match_houid('offtrxrfnd'),
'object' => 'offline_transaction_refund',
'type' => 'payment'
}, {
'id' => match_houid('offtrxchrg'),
'object' => 'offline_transaction_charge',
'type' => 'payment'
}
],
'supporter' => supporter.id,
'transaction' => match_houid('trx'),
'type' => 'subtransaction'
},
'supporter' => {
'anonymous' => false,
'deleted' => false,
'id' => supporter.id,
'merged_into' => nil,
'name' => supporter.name,
'nonprofit' => kind_of(Numeric),
'object' => 'supporter',
'organization' => nil,
'phone' => nil,
'supporter_addresses' => [kind_of(Numeric)]
},
'transaction' => {
'amount' => { 'cents' => 500, 'currency' => nonprofit.currency },
'created' => kind_of(Numeric),
'id' => match_houid('trx'),
'nonprofit' => kind_of(Numeric),
'object' => 'transaction',
'subtransaction' => {
'id' => match_houid('offlinetrx'),
'object' => 'offline_transaction',
'type' => 'subtransaction'
},
'subtransaction_payments' => [
{
'id' => match_houid('offtrxrfnd'),
'object' => 'offline_transaction_refund',
'type' => 'payment'
}, {
'id' => match_houid('offtrxchrg'),
'object' => 'offline_transaction_charge',
'type' => 'payment'
}
],
'supporter' => supporter.id,
'transaction_assignments' => []
},
'type' => 'payment'
}
},
'id' => match_houid('objevt'),
'object' => 'object_event',
'type' => 'event_type'
}
end
before do
allow(Houdini)
.to receive(:event_publisher)
.and_return(event_publisher)
transaction
end
describe 'offline transaction refund' do
subject { offline_transaction_refund }
it do
is_expected
.to have_attributes(
nonprofit: an_instance_of(Nonprofit),
id: match_houid('offtrxrfnd')
)
end
it { is_expected.to be_persisted }
end
describe '.to_builder' do
subject { JSON.parse(offline_transaction_refund.to_builder.target!) }
it do
is_expected
.to match_json(
{
object: 'offline_transaction_refund',
nonprofit: kind_of(Numeric),
supporter: kind_of(Numeric),
id: match_houid('offtrxrfnd'),
type: 'payment',
fee_total: { cents: 100, currency: nonprofit.currency },
net_amount: { cents: 400, currency: nonprofit.currency },
gross_amount: { cents: 500, currency: nonprofit.currency },
created: kind_of(Numeric),
subtransaction: {
id: match_houid('offlinetrx'),
object: 'offline_transaction',
type: 'subtransaction'
},
transaction: match_houid('trx')
}
)
end
end
describe '.publish_created' do
before do
expected_event['type'] = 'offline_transaction_refund.created'
allow(event_publisher)
.to receive(:announce)
.with(:payment_created, anything)
allow(event_publisher)
.to receive(:announce)
.with(
:offline_transaction_refund_created,
expected_event
)
end
it 'announces offline_transaction_refund.created event' do
offline_transaction_refund.publish_created
expect(event_publisher)
.to have_received(:announce)
.with(
:offline_transaction_refund_created,
expected_event
)
end
end
describe '.publish_updated' do
before do
expected_event['type'] = 'offline_transaction_refund.updated'
allow(event_publisher)
.to receive(:announce)
.with(:payment_updated, anything)
allow(event_publisher)
.to receive(:announce)
.with(
:offline_transaction_refund_updated,
expected_event
)
end
it 'announces offline_transaction_refund.updated event' do
offline_transaction_refund.publish_updated
expect(event_publisher)
.to have_received(:announce)
.with(
:offline_transaction_refund_updated,
expected_event
)
end
end
describe '.publish_deleted' do
before do
expected_event['type'] = 'offline_transaction_refund.deleted'
allow(event_publisher)
.to receive(:announce)
.with(:payment_deleted, anything)
allow(event_publisher)
.to receive(:announce)
.with(
:offline_transaction_refund_deleted,
expected_event
)
end
it 'announces offline_transaction_refund.deleted event' do
offline_transaction_refund.publish_deleted
expect(event_publisher)
.to have_received(:announce)
.with(
:offline_transaction_refund_deleted,
expected_event
)
end
end
end

View file

@ -14,7 +14,7 @@ RSpec.describe Recurrence, type: :model do
describe 'recurrence with donations, designation and dedication' do
subject { create(:recurrence) }
def trx_assignment_match # rubocop:disable Metrics/MethodLength
def trx_assignment_match
{
assignment_object: 'donation',
amount: 500,
@ -113,7 +113,7 @@ RSpec.describe Recurrence, type: :model do
describe 'recurrence_with_paydate_later_in_month' do
subject { create(:recurrence_with_paydate_later_in_month) }
def trx_assignment_match # rubocop:disable Metrics/MethodLength
def trx_assignment_match
{
assignment_object: 'donation',
amount: 500,
@ -208,7 +208,7 @@ RSpec.describe Recurrence, type: :model do
describe 'recurrence_with_paydate_earlier_in_month' do
subject { create(:recurrence_with_paydate_earlier_in_month) }
def trx_assignment_match # rubocop:disable Metrics/MethodLength
def trx_assignment_match
{
assignment_object: 'donation',
amount: 500,