Skip to content

Commit

Permalink
Add ShipmentTracking Job and Call when updated
Browse files Browse the repository at this point in the history
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
piyushswain committed Jun 10, 2022
1 parent 15c6f65 commit 2b6591c
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
26 changes: 26 additions & 0 deletions app/decorators/models/solidus_bolt/shipment_decorator.rb
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
11 changes: 11 additions & 0 deletions app/jobs/solidus_bolt/shipment_tracking_job.rb
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 spec/decorators/models/solidus_bolt/shipment_decorator_spec.rb
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
19 changes: 19 additions & 0 deletions spec/jobs/solidus_bolt/shipment_tracking_job_spec.rb
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

0 comments on commit 2b6591c

Please sign in to comment.