-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ShipmentTracking Job and Call when updated
This commit adds a ShipmentTrackingJob that calls the ShipmentTracking Service. This was necessary because the check for the update on the tracking number is called before_save and calling the service at this point would cause a lot of delay for the API to execute. Hence, the service is called asynchronously from this point in the code.
- Loading branch information
1 parent
15c6f65
commit 2b6591c
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusBolt | ||
module ShipmentDecorator | ||
def self.prepended(base) | ||
base.class_eval do | ||
before_save :update_bolt_tracking_info | ||
end | ||
end | ||
|
||
def update_bolt_tracking_info | ||
return unless tracking_was.nil? | ||
return unless tracking_changed? | ||
|
||
payment = order&.payments&.completed&.last | ||
return unless payment&.payment_method.instance_of?(SolidusBolt::PaymentMethod) | ||
|
||
transaction_reference = payment&.response_code | ||
return if transaction_reference.blank? | ||
|
||
SolidusBolt::ShipmentTrackingJob.perform_later(transaction_reference: transaction_reference, shipment: self) | ||
end | ||
|
||
Spree::Shipment.prepend(self) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module SolidusBolt | ||
class ShipmentTrackingJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(transaction_reference:, shipment:) | ||
SolidusBolt::Orders::TrackShipmentService.call(transaction_reference: transaction_reference, shipment: shipment) | ||
end | ||
end | ||
end |
43 changes: 43 additions & 0 deletions
43
spec/decorators/models/solidus_bolt/shipment_decorator_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe SolidusBolt::ShipmentDecorator do | ||
let(:payment_method) { create(:bolt_payment_method) } | ||
let(:payment_source) { create(:bolt_payment_source) } | ||
let(:payment) do | ||
create( | ||
:payment, | ||
state: 'completed', | ||
source_id: payment_source.id, | ||
source_type: SolidusBolt::PaymentSource, | ||
payment_method_id: payment_method.id, | ||
response_code: 'Doctor Strange' | ||
) | ||
end | ||
let(:order) { payment.order } | ||
|
||
describe '#update_bolt_tracking_info' do | ||
before { allow(SolidusBolt::Orders::TrackShipmentService).to receive(:call) } | ||
|
||
context 'when tracking is nil' do | ||
it 'enqueues SolidusBolt::ShipmentTrackingJob' do | ||
create(:shipment, order: order, id: rand(1..10), tracking: nil) | ||
order.shipments.reload | ||
shipment = order.shipments.last | ||
|
||
shipment.tracking = 'MockBolt1678' | ||
expect { shipment.save }.to have_enqueued_job(SolidusBolt::ShipmentTrackingJob) | ||
end | ||
end | ||
|
||
context 'when tracking has a value' do | ||
it 'does not enqueue SolidusBolt::ShipmentTrackingJob' do | ||
create(:shipment, order: order, id: rand(1..10), tracking: 'MockBolt1678') | ||
order.shipments.reload | ||
shipment = order.shipments.last | ||
|
||
shipment.tracking = 'MockBolt1123' | ||
expect { shipment.save }.not_to have_enqueued_job(SolidusBolt::ShipmentTrackingJob) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe SolidusBolt::ShipmentTrackingJob do | ||
subject(:shipment_tracking_job) { | ||
described_class.perform_now(transaction_reference: transaction_reference, shipment: shipment) | ||
} | ||
|
||
let(:shipment) { build(:shipment) } | ||
let(:transaction_reference) { 'Multiverse of Madness' } | ||
|
||
before { allow(SolidusBolt::Orders::TrackShipmentService).to receive(:call) } | ||
|
||
it 'calls the TrackShipmentService' do | ||
shipment_tracking_job | ||
expect(SolidusBolt::Orders::TrackShipmentService).to have_received(:call).with( | ||
transaction_reference: transaction_reference, shipment: shipment | ||
) | ||
end | ||
end |