Skip to content

Commit

Permalink
Merge pull request #156 from solidusio-contrib/aldesantis/additional-…
Browse files Browse the repository at this point in the history
…events

Track additional events for subscription updates
  • Loading branch information
aldesantis authored Oct 5, 2020
2 parents fa5876c + 91ab2cc commit 26c3495
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 19 deletions.
24 changes: 24 additions & 0 deletions app/models/solidus_subscriptions/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Subscription < ApplicationRecord
before_validation :set_payment_method
after_create :emit_event_for_creation
before_update :update_actionable_date_if_interval_changed
after_update :emit_events_for_update

# Find all subscriptions that are "actionable"; that is, ones that have an
# actionable_date in the past and are not invalid or canceled.
Expand Down Expand Up @@ -294,5 +295,28 @@ def emit_event_for_transition
subscription: self,
)
end

def emit_events_for_update
if previous_changes.key?('interval_length') || previous_changes.key?('interval_units')
::Spree::Event.fire(
'solidus_subscriptions.subscription_frequency_changed',
subscription: self,
)
end

if previous_changes.key?('shipping_address_id')
::Spree::Event.fire(
'solidus_subscriptions.subscription_shipping_address_changed',
subscription: self,
)
end

if previous_changes.key?('billing_address_id')
::Spree::Event.fire(
'solidus_subscriptions.subscription_billing_address_changed',
subscription: self,
)
end
end
end
end
24 changes: 24 additions & 0 deletions app/subscribers/solidus_subscriptions/event_storage_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ module EventStorageSubscriber
event_action :track_subscription_canceled, event_name: 'solidus_subscriptions.subscription_canceled'
event_action :track_subscription_ended, event_name: 'solidus_subscriptions.subscription_ended'
event_action :track_subscription_repopulated, event_name: 'solidus_subscriptions.subscription_repopulated'
event_action :track_subscription_shipping_address_changed, event_name: 'solidus_subscriptions.subscription_shipping_address_changed'
event_action :track_subscription_billing_address_changed, event_name: 'solidus_subscriptions.subscription_billing_address_changed'
event_action :track_subscription_frequency_changed, event_name: 'solidus_subscriptions.subscription_frequency_changed'

def track_subscription_created(event)
event.payload.fetch(:subscription).events.create!(
Expand Down Expand Up @@ -44,5 +47,26 @@ def track_subscription_repopulated(event)
details: event.payload.fetch(:subscription).as_json,
)
end

def track_subscription_shipping_address_changed(event)
event.payload.fetch(:subscription).events.create!(
event_type: 'subscription_shipping_address_changed',
details: event.payload.fetch(:subscription).as_json,
)
end

def track_subscription_billing_address_changed(event)
event.payload.fetch(:subscription).events.create!(
event_type: 'subscription_billing_address_changed',
details: event.payload.fetch(:subscription).as_json,
)
end

def track_subscription_frequency_changed(event)
event.payload.fetch(:subscription).events.create!(
event_type: 'subscription_frequency_changed',
details: event.payload.fetch(:subscription).as_json,
)
end
end
end
62 changes: 43 additions & 19 deletions spec/models/solidus_subscriptions/subscription_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,56 @@
it { is_expected.to validate_numericality_of(:skip_count).is_greater_than_or_equal_to(0) }
it { is_expected.to validate_numericality_of(:successive_skip_count).is_greater_than_or_equal_to(0) }

it { is_expected.to accept_nested_attributes_for :line_items }
it { is_expected.to accept_nested_attributes_for(:line_items) }

describe '#save' do
context 'when the subscription is new' do
it 'tracks a subscription_created event' do
subscription = build(:subscription)
describe 'creating a subscription' do
it 'tracks the creation' do
stub_const('Spree::Event', class_spy(Spree::Event))

subscription.save!
subscription = create(:subscription)

expect(subscription.events.last).to have_attributes(
event_type: 'subscription_created',
details: a_hash_including('id' => subscription.id),
)
end
expect(Spree::Event).to have_received(:fire).with(
'solidus_subscriptions.subscription_created',
subscription: subscription,
)
end
end

context 'when the subscription is persisted' do
it 'does not track an event' do
subscription = create(:subscription)
describe 'updating a subscription' do
it 'tracks interval changes' do
stub_const('Spree::Event', class_spy(Spree::Event))
subscription = create(:subscription)

subscription.end_date = Time.zone.tomorrow
subscription.update!(interval_length: subscription.interval_length + 1)

expect {
subscription.save!
}.not_to change(subscription.events, :count)
end
expect(Spree::Event).to have_received(:fire).with(
'solidus_subscriptions.subscription_frequency_changed',
subscription: subscription,
)
end

it 'tracks shipping address changes' do
stub_const('Spree::Event', class_spy(Spree::Event))
subscription = create(:subscription)

subscription.update!(shipping_address: create(:address))

expect(Spree::Event).to have_received(:fire).with(
'solidus_subscriptions.subscription_shipping_address_changed',
subscription: subscription,
)
end

it 'tracks billing address changes' do
stub_const('Spree::Event', class_spy(Spree::Event))
subscription = create(:subscription)

subscription.update!(billing_address: create(:address))

expect(Spree::Event).to have_received(:fire).with(
'solidus_subscriptions.subscription_billing_address_changed',
subscription: subscription,
)
end
end

Expand Down

0 comments on commit 26c3495

Please sign in to comment.