From d0197eb62ebaa62ca74b68e807792cd15ab15504 Mon Sep 17 00:00:00 2001 From: Eric Schultz Date: Wed, 10 Feb 2021 14:38:41 -0600 Subject: [PATCH] campaign_gift_purchase.* events can be published --- app/models/campaign_gift_purchase.rb | 26 ++- spec/models/campaign_gift_option_spec.rb | 32 ++-- spec/models/campaign_gift_purchase_spec.rb | 191 ++++++++++++++++++++- 3 files changed, 230 insertions(+), 19 deletions(-) diff --git a/app/models/campaign_gift_purchase.rb b/app/models/campaign_gift_purchase.rb index aff2463b..293971d2 100644 --- a/app/models/campaign_gift_purchase.rb +++ b/app/models/campaign_gift_purchase.rb @@ -21,10 +21,22 @@ class CampaignGiftPurchase < ApplicationRecord has_one :supporter, through: :trx has_one :nonprofit, through: :supporter + + # TODO replace with Discard gem + define_model_callbacks :discard + validates :amount, presence: true validates :campaign, presence: true validates :campaign_gifts, length: { minimum: 1 } + # TODO replace with discard gem + def discard! + run_callbacks(:discard) do + self.deleted = true + save! + end + end + def to_builder(*expand) init_builder(*expand) do |json| json.(self, :id, :deleted) @@ -37,11 +49,23 @@ class CampaignGiftPurchase < ApplicationRecord if expand.include? :campaign_gifts json.campaign_gifts campaign_gifts do |gift| - gift.to_builder + json.merge! gift.to_builder.attributes! end else json.campaign_gifts campaign_gifts.pluck(:id) end end end + + def publish_created + Houdini.event_publisher.announce(:campaign_gift_purchase_created, to_event('campaign_gift_purchase.created', :nonprofit, :supporter, :trx, :campaign, :campaign_gifts).attributes!) + end + + def publish_updated + Houdini.event_publisher.announce(:campaign_gift_purchase_updated, to_event('campaign_gift_purchase.updated', :nonprofit, :supporter, :trx, :campaign, :campaign_gifts).attributes!) + end + + def publish_deleted + Houdini.event_publisher.announce(:campaign_gift_purchase_deleted, to_event('campaign_gift_purchase.deleted', :nonprofit, :supporter, :trx, :campaign, :campaign_gifts).attributes!) + end end diff --git a/spec/models/campaign_gift_option_spec.rb b/spec/models/campaign_gift_option_spec.rb index 2eba3700..3ea85b4f 100644 --- a/spec/models/campaign_gift_option_spec.rb +++ b/spec/models/campaign_gift_option_spec.rb @@ -4,7 +4,7 @@ # Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE require 'rails_helper' -RSpec.describe CampaignGiftOption, type: :model do +RSpec.describe CampaignGiftOption, 'type' => :model do include_context :shared_donation_charge_context let(:name) {"CUSTOM GIFT OPTION"} let(:amount_one_time) { 400} @@ -57,10 +57,10 @@ RSpec.describe CampaignGiftOption, type: :model do 'deleted' => false, 'description' => description, 'gift_option_amount' => [ - {'amount' => {value_in_cents:amount_one_time, currency: nonprofit.currency}}, + {'amount' => {'value_in_cents'=> amount_one_time, 'currency' => nonprofit.currency}}, { - 'amount' => {value_in_cents:amount_recurring, currency: nonprofit.currency}, - 'recurrence' => { interval: 1, type: 'monthly'} + 'amount' => {'value_in_cents' => amount_recurring, 'currency' => nonprofit.currency}, + 'recurrence' => { 'interval' => 1, 'type' => 'monthly'} } ], 'id'=> kind_of(Numeric), @@ -98,10 +98,10 @@ RSpec.describe CampaignGiftOption, type: :model do 'deleted' => false, 'description' => description, 'gift_option_amount' => [ - {'amount' => {value_in_cents:amount_one_time, currency: nonprofit.currency}}, + {'amount' => {'value_in_cents' => amount_one_time, 'currency' => nonprofit.currency}}, { - 'amount' => {value_in_cents:amount_recurring, currency: nonprofit.currency}, - 'recurrence' => { interval: 1, type: 'monthly'} + 'amount' => {'value_in_cents' => amount_recurring, 'currency' => nonprofit.currency}, + 'recurrence' => { 'interval' => 1, 'type' => 'monthly'} } ], 'id'=> kind_of(Numeric), @@ -161,8 +161,8 @@ RSpec.describe CampaignGiftOption, type: :model do 'description' => description, 'gift_option_amount' => [ { - 'amount' => {value_in_cents:amount_recurring, currency: nonprofit.currency}, - 'recurrence' => { interval: 1, type: 'monthly'} + 'amount' => {'value_in_cents' => amount_recurring, 'currency' => nonprofit.currency}, + 'recurrence' => { 'interval' => 1, 'type' => 'monthly'} } ], 'id'=> kind_of(Numeric), @@ -205,7 +205,7 @@ RSpec.describe CampaignGiftOption, type: :model do 'deleted' => false, 'description' => description, 'gift_option_amount' => [ - {'amount' => {value_in_cents:amount_one_time, currency: nonprofit.currency}}, + {'amount' => {'value_in_cents' => amount_one_time, 'currency' => nonprofit.currency}}, ], 'id'=> kind_of(Numeric), 'hide_contributions' => false, @@ -252,10 +252,10 @@ RSpec.describe CampaignGiftOption, type: :model do 'deleted' => true, 'description' => description, 'gift_option_amount' => [ - {'amount' => {value_in_cents:amount_one_time, currency: nonprofit.currency}}, + {'amount' => {'value_in_cents' => amount_one_time, 'currency' => nonprofit.currency}}, { - 'amount' => {value_in_cents:amount_recurring, currency: nonprofit.currency}, - 'recurrence' => { interval: 1, type: 'monthly'} + 'amount' => {'value_in_cents' => amount_recurring, 'currency' => nonprofit.currency}, + 'recurrence' => { 'interval' => 1, 'type' => 'monthly'} } ], 'id'=> kind_of(Numeric), @@ -299,10 +299,10 @@ RSpec.describe CampaignGiftOption, type: :model do 'deleted' => true, 'description' => description, 'gift_option_amount' => [ - {'amount' => {value_in_cents:amount_one_time, currency: nonprofit.currency}}, + {'amount' => {'value_in_cents' => amount_one_time, 'currency' => nonprofit.currency}}, { - 'amount' => {value_in_cents:amount_recurring, currency: nonprofit.currency}, - 'recurrence' => { interval: 1, type: 'monthly'} + 'amount' => {'value_in_cents' => amount_recurring, 'currency' => nonprofit.currency}, + 'recurrence' => { 'interval' => 1, 'type' => 'monthly'} } ], 'id'=> kind_of(Numeric), diff --git a/spec/models/campaign_gift_purchase_spec.rb b/spec/models/campaign_gift_purchase_spec.rb index 09076fb4..7f98467f 100644 --- a/spec/models/campaign_gift_purchase_spec.rb +++ b/spec/models/campaign_gift_purchase_spec.rb @@ -5,5 +5,192 @@ require 'rails_helper' RSpec.describe CampaignGiftPurchase, type: :model do - pending "add some examples to (or delete) #{__FILE__}" -end + include_context :shared_donation_charge_context + # TODO Why are we manually setting everything here? It's not clear what order things should + # go in for a transaction. Therefore, we don't assume the order for now and just make sure the + # the output of to_builder is right + let(:trx) { force_create(:transaction, supporter: supporter, amount: 400)} + + let(:campaign_gift_purchase) {trx.campaign_gift_purchases.create(campaign: campaign, amount: 400, campaign_gifts: [ModernCampaignGift.new(amount: 400, legacy_campaign_gift:lcg)])} + let(:lcg) { CampaignGift.create( + donation: supporter.donations.create(amount: 400, campaign: campaign, nonprofit: nonprofit, supporter:supporter), + campaign_gift_option: campaign_gift_option + )} + let(:campaign_gift_option) { create(:campaign_gift_option, amount_one_time: 400, campaign: campaign)} + let(:campaign_gift) { campaign_gift_purchase.campaign_gifts.first} + + let(:campaign_builder_expanded) do + { + 'id' => kind_of(Numeric), + 'name' => campaign.name, + 'object' => "campaign", + 'nonprofit' => nonprofit.id + } + end + + let(:cgo_builder_expanded) do + { + 'id' => kind_of(Numeric), + 'name' => campaign_gift_option.name, + 'description' => campaign_gift_option.description, + 'hide_contributions' => campaign_gift_option.hide_contributions, + 'order' => campaign_gift_option.order, + 'to_ship' => campaign_gift_option.to_ship, + 'object' => 'campaign_gift_option', + 'deleted' => false, + 'gift_option_amount' => [{ + 'amount' => { + 'value_in_cents' => 400, + 'currency' => 'usd' + }, + }], + 'campaign' => kind_of(Numeric), + 'nonprofit' => kind_of(Numeric) + } + 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(:transaction_builder_expanded) do + { + 'id' => match_houid('trx'), + 'object' => 'transaction', + 'amount' => { + 'value_in_cents' => trx.amount, + 'currency' => 'usd' + }, + 'supporter' => kind_of(Numeric), + 'nonprofit' => kind_of(Numeric) + } + end + + let(:cgp_builder_expanded) do + { + 'id' => match_houid('cgpur'), + 'campaign' => kind_of(Numeric), + 'object' => 'campaign_gift_purchase', + 'campaign_gifts' => [modern_campaign_gift_builder], + 'amount' => { + 'value_in_cents' => trx.amount, + 'currency' => 'usd' + }, + 'supporter' => supporter_builder_expanded, + 'nonprofit' => np_builder_expanded, + 'transaction' => transaction_builder_expanded, + 'deleted' => false + } + end + + let(:modern_campaign_gift_builder) { + { + 'amount' => { + 'value_in_cents' => 400, + 'currency' => 'usd' + }, + 'campaign' => kind_of(Numeric), + 'campaign_gift_option' => kind_of(Numeric), + 'campaign_gift_purchase' => match_houid('cgpur'), + 'deleted' => false, + 'id' => match_houid('cgift'), + 'nonprofit'=> kind_of(Numeric), + 'object' => 'campaign_gift', + 'supporter' => kind_of(Numeric), + 'transaction' => match_houid('trx') + } + } + + + + + it 'announces created properly when called' do + allow(Houdini.event_publisher).to receive(:announce) + expect(Houdini.event_publisher).to receive(:announce).with(:campaign_gift_purchase_created, { + 'id' => match_houid('objevt'), + 'object' => 'object_event', + 'type' => 'campaign_gift_purchase.created', + 'data' => { + 'object' => { + 'id' => match_houid('cgpur'), + 'campaign' => campaign_builder_expanded, + 'object' => 'campaign_gift_purchase', + 'campaign_gifts' => [modern_campaign_gift_builder], + 'amount' => { + 'value_in_cents' => trx.amount, + 'currency' => 'usd' + }, + 'supporter' => supporter_builder_expanded, + 'nonprofit' => np_builder_expanded, + 'transaction' => transaction_builder_expanded, + 'deleted' => false + } + } + }) + + campaign_gift_purchase.publish_created + end + + it 'announces updated properly when called' do + allow(Houdini.event_publisher).to receive(:announce) + expect(Houdini.event_publisher).to receive(:announce).with(:campaign_gift_purchase_updated, { + 'id' => match_houid('objevt'), + 'object' => 'object_event', + 'type' => 'campaign_gift_purchase.updated', + 'data' => { + 'object' => { + 'id' => match_houid('cgpur'), + 'campaign' => campaign_builder_expanded, + 'object' => 'campaign_gift_purchase', + 'campaign_gifts' => [modern_campaign_gift_builder], + 'amount' => { + 'value_in_cents' => trx.amount, + 'currency' => 'usd' + }, + 'supporter' => supporter_builder_expanded, + 'nonprofit' => np_builder_expanded, + 'transaction' => transaction_builder_expanded, + 'deleted' => false + } + } + }) + + campaign_gift_purchase.publish_updated + end + + it 'announces updated deleted properly when called' do + allow(Houdini.event_publisher).to receive(:announce) + expect(Houdini.event_publisher).to receive(:announce).with(:campaign_gift_purchase_deleted, { + 'id' => match_houid('objevt'), + 'object' => 'object_event', + 'type' => 'campaign_gift_purchase.deleted', + 'data' => { + 'object' => { + 'id' => match_houid('cgpur'), + 'campaign' => campaign_builder_expanded, + 'object' => 'campaign_gift_purchase', + 'campaign_gifts' => [modern_campaign_gift_builder], + 'amount' => { + 'value_in_cents' => trx.amount, + 'currency' => 'usd' + }, + 'supporter' => supporter_builder_expanded, + 'nonprofit' => np_builder_expanded, + 'transaction' => transaction_builder_expanded, + 'deleted' => true + } + } + }) + + campaign_gift_purchase.discard! + campaign_gift_purchase.publish_deleted + end +end \ No newline at end of file