Skip to content

Commit

Permalink
fixup! Order Tracking Service
Browse files Browse the repository at this point in the history
  • Loading branch information
piyushswain committed Jun 14, 2022
1 parent 4644cca commit 36bc3a4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 76 deletions.
31 changes: 0 additions & 31 deletions app/decorators/models/solidus_bolt/order_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,12 @@ def bolt_user_identity
}
end

def bolt_items
line_items.map do |line_item|
{
reference: line_item.sku,
name: line_item.name,
description: line_item.description,
total_amount: {
amount: line_item.total,
currency: line_item.currency,
currency_symbol: currency_symbol
},
unit_price: {
amount: line_item.price,
currency: line_item.currency,
currency_symbol: currency_symbol
},
tax_amount: {
amount: line_item.additional_tax_total,
currency: line_item.currency,
currency_symbol: currency_symbol
},
quantity: line_item.quantity,
sku: line_item.sku
}
end
end

private

def cents(float)
(float * 100).to_i
end

def currency_symbol
Monetize.from_string(total, currency).symbol
end

Spree::Order.prepend(self)
end
end
40 changes: 28 additions & 12 deletions app/decorators/models/solidus_bolt/shipment_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,37 @@

module SolidusBolt
module ShipmentDecorator
def self.prepended(base)
base.before_save :update_bolt_tracking_info
def bolt_items
line_items.map do |line_item|
{
reference: line_item.sku,
name: line_item.name,
description: line_item.description,
total_amount: {
amount: line_item.total,
currency: line_item.currency,
currency_symbol: currency_symbol
},
unit_price: {
amount: line_item.price,
currency: line_item.currency,
currency_symbol: currency_symbol
},
tax_amount: {
amount: line_item.additional_tax_total,
currency: line_item.currency,
currency_symbol: currency_symbol
},
quantity: line_item.quantity,
sku: line_item.sku
}
end
end

def update_bolt_tracking_info
return if tracking_was.present?
return unless tracking_changed?
private

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)
def currency_symbol
Monetize.from_string(order.total, order.currency).symbol
end

Spree::Shipment.prepend(self)
Expand Down
3 changes: 1 addition & 2 deletions app/services/solidus_bolt/orders/track_shipment_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,12 @@ def track_shipment
end

def build_options
order = shipment.order
{
body: {
transaction_reference: transaction_reference,
tracking_number: shipment.tracking,
carrier: shipment.shipping_method.name,
items: order.bolt_items,
items: shipment.bolt_items,
is_non_bolt_order: false
}.to_json,
headers: {
Expand Down
12 changes: 0 additions & 12 deletions spec/decorators/models/solidus_bolt/order_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,4 @@
expect(order.bolt_user_identity).to eq(result)
end
end

describe '#bolt_items' do
subject(:items) { order.bolt_items }

it 'returns an array' do
expect(items).to be_a(Array)
end

it 'lists all line_items' do
expect(items.count).to eq(order.line_items.count)
end
end
end
32 changes: 13 additions & 19 deletions spec/decorators/models/solidus_bolt/shipment_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,23 @@
end
let(:order) { payment.order }

describe '#update_bolt_tracking_info' do
before { allow(SolidusBolt::Orders::TrackShipmentService).to receive(:call) }
describe '#bolt_items' do
it 'returns an array' do
create(:shipment, order: order, id: rand(1..10), tracking: 'MockBolt1678')
order.shipments.reload
shipment = order.shipments.last
items = shipment.bolt_items

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
expect(items).to be_a(Array)
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
it 'lists all line_items' do
create(:shipment, order: order, id: rand(1..10), tracking: 'MockBolt1678')
order.shipments.reload
shipment = order.shipments.last
items = shipment.bolt_items

shipment.tracking = 'MockBolt1123'
expect { shipment.save }.not_to have_enqueued_job(SolidusBolt::ShipmentTrackingJob)
end
expect(items.count).to eq(order.line_items.count)
end
end
end

0 comments on commit 36bc3a4

Please sign in to comment.