campaign_gift_purchase.* events can be published
This commit is contained in:
		
							parent
							
								
									a7244f97ec
								
							
						
					
					
						commit
						d0197eb62e
					
				
					 3 changed files with 230 additions and 19 deletions
				
			
		|  | @ -21,10 +21,22 @@ class CampaignGiftPurchase < ApplicationRecord | ||||||
| 	has_one :supporter, through: :trx | 	has_one :supporter, through: :trx | ||||||
| 	has_one :nonprofit, through: :supporter | 	has_one :nonprofit, through: :supporter | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
|  |   # TODO replace with Discard gem | ||||||
|  | 	define_model_callbacks :discard | ||||||
|  | 
 | ||||||
|   validates :amount, presence: true |   validates :amount, presence: true | ||||||
|   validates :campaign, presence: true |   validates :campaign, presence: true | ||||||
|   validates :campaign_gifts, length: { minimum: 1 } |   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) |   def to_builder(*expand) | ||||||
|     init_builder(*expand) do |json| |     init_builder(*expand) do |json| | ||||||
|       json.(self, :id, :deleted) |       json.(self, :id, :deleted) | ||||||
|  | @ -37,11 +49,23 @@ class CampaignGiftPurchase < ApplicationRecord | ||||||
| 
 | 
 | ||||||
|       if expand.include? :campaign_gifts |       if expand.include? :campaign_gifts | ||||||
|         json.campaign_gifts campaign_gifts do |gift| |         json.campaign_gifts campaign_gifts do |gift| | ||||||
|           gift.to_builder |           json.merge! gift.to_builder.attributes! | ||||||
|         end |         end | ||||||
|       else |       else | ||||||
|         json.campaign_gifts campaign_gifts.pluck(:id) |         json.campaign_gifts campaign_gifts.pluck(:id) | ||||||
|       end |       end | ||||||
|     end |     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 | end | ||||||
|  |  | ||||||
|  | @ -4,7 +4,7 @@ | ||||||
| # Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE | # Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE | ||||||
| require 'rails_helper' | require 'rails_helper' | ||||||
| 
 | 
 | ||||||
| RSpec.describe CampaignGiftOption, type: :model do | RSpec.describe CampaignGiftOption, 'type' => :model do | ||||||
|   include_context :shared_donation_charge_context |   include_context :shared_donation_charge_context | ||||||
|   let(:name) {"CUSTOM GIFT OPTION"} |   let(:name) {"CUSTOM GIFT OPTION"} | ||||||
|   let(:amount_one_time) { 400} |   let(:amount_one_time) { 400} | ||||||
|  | @ -57,10 +57,10 @@ RSpec.describe CampaignGiftOption, type: :model do | ||||||
|             'deleted' => false, |             'deleted' => false, | ||||||
|             'description' => description, |             'description' => description, | ||||||
|             'gift_option_amount' => [ |             '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}, |                 'amount' => {'value_in_cents' => amount_recurring, 'currency' => nonprofit.currency}, | ||||||
|                 'recurrence' => { interval: 1, type: 'monthly'} |                 'recurrence' => { 'interval' => 1, 'type' => 'monthly'} | ||||||
|               } |               } | ||||||
|             ], |             ], | ||||||
|             'id'=> kind_of(Numeric), |             'id'=> kind_of(Numeric), | ||||||
|  | @ -98,10 +98,10 @@ RSpec.describe CampaignGiftOption, type: :model do | ||||||
|             'deleted' => false, |             'deleted' => false, | ||||||
|             'description' => description, |             'description' => description, | ||||||
|             'gift_option_amount' => [ |             '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}, |                 'amount' => {'value_in_cents' => amount_recurring, 'currency' => nonprofit.currency}, | ||||||
|                 'recurrence' => { interval: 1, type: 'monthly'} |                 'recurrence' => { 'interval' => 1, 'type' => 'monthly'} | ||||||
|               } |               } | ||||||
|             ], |             ], | ||||||
|             'id'=> kind_of(Numeric), |             'id'=> kind_of(Numeric), | ||||||
|  | @ -161,8 +161,8 @@ RSpec.describe CampaignGiftOption, type: :model do | ||||||
|             'description' => description, |             'description' => description, | ||||||
|             'gift_option_amount' => [ |             'gift_option_amount' => [ | ||||||
|               { |               { | ||||||
|                 'amount' => {value_in_cents:amount_recurring, currency: nonprofit.currency}, |                 'amount' => {'value_in_cents' => amount_recurring, 'currency' => nonprofit.currency}, | ||||||
|                 'recurrence' => { interval: 1, type: 'monthly'} |                 'recurrence' => { 'interval' => 1, 'type' => 'monthly'} | ||||||
|               } |               } | ||||||
|             ], |             ], | ||||||
|             'id'=> kind_of(Numeric), |             'id'=> kind_of(Numeric), | ||||||
|  | @ -205,7 +205,7 @@ RSpec.describe CampaignGiftOption, type: :model do | ||||||
|             'deleted' => false, |             'deleted' => false, | ||||||
|             'description' => description, |             'description' => description, | ||||||
|             'gift_option_amount' => [ |             '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), |             'id'=> kind_of(Numeric), | ||||||
|             'hide_contributions' => false, |             'hide_contributions' => false, | ||||||
|  | @ -252,10 +252,10 @@ RSpec.describe CampaignGiftOption, type: :model do | ||||||
|             'deleted' => true, |             'deleted' => true, | ||||||
|             'description' => description, |             'description' => description, | ||||||
|             'gift_option_amount' => [ |             '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}, |                 'amount' => {'value_in_cents' => amount_recurring, 'currency' => nonprofit.currency}, | ||||||
|                 'recurrence' => { interval: 1, type: 'monthly'} |                 'recurrence' => { 'interval' => 1, 'type' => 'monthly'} | ||||||
|               } |               } | ||||||
|             ], |             ], | ||||||
|             'id'=> kind_of(Numeric), |             'id'=> kind_of(Numeric), | ||||||
|  | @ -299,10 +299,10 @@ RSpec.describe CampaignGiftOption, type: :model do | ||||||
|             'deleted' => true, |             'deleted' => true, | ||||||
|             'description' => description, |             'description' => description, | ||||||
|             'gift_option_amount' => [ |             '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}, |                 'amount' => {'value_in_cents' => amount_recurring, 'currency' => nonprofit.currency}, | ||||||
|                 'recurrence' => { interval: 1, type: 'monthly'} |                 'recurrence' => { 'interval' => 1, 'type' => 'monthly'} | ||||||
|               } |               } | ||||||
|             ], |             ], | ||||||
|             'id'=> kind_of(Numeric), |             'id'=> kind_of(Numeric), | ||||||
|  |  | ||||||
|  | @ -5,5 +5,192 @@ | ||||||
| require 'rails_helper' | require 'rails_helper' | ||||||
| 
 | 
 | ||||||
| RSpec.describe CampaignGiftPurchase, type: :model do | RSpec.describe CampaignGiftPurchase, type: :model do | ||||||
|   pending "add some examples to (or delete) #{__FILE__}" |   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 | end | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 Eric Schultz
						Eric Schultz