Skip to content

Commit

Permalink
Resolve transactions with the tracking number!
Browse files Browse the repository at this point in the history
  • Loading branch information
hjhart committed Dec 1, 2016
1 parent 686fb2e commit 28cdf33
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/models/transaction_resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class TransactionResolver
include Virtus.model

attribute :remaining_items, Array[Item]
attribute :fulfillment, Fulfillment

def items
self.remaining_items.select { |item| item.tracking_number == fulfillment.tracking_number }
end
end
1 change: 1 addition & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require_relative '../app/models/item'
require_relative '../app/models/order'
require_relative '../app/models/order_loader'
require_relative '../app/models/transaction_resolver'

# Huh? https://github.com/RubyMoney/money/issues/593
I18n.enforce_available_locales = false
38 changes: 38 additions & 0 deletions spec/models/transaction_resolver_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'environment_helper'

describe TransactionResolver do
context 'simplest case' do
let(:fulfillment) { Fulfillment.new(shipment_date: '11/01/16', total_price: '$12.34') }
let(:items) { [item] }
let(:item) { Item.new(title: 'Furby!', total_price: '$12.34') }

it 'correctly matches the shipments to the items memo' do
result = TransactionResolver.new(fulfillment: fulfillment, remaining_items: items).items
expect(result).to eq([item])
end
end

context 'one fulfillment, three items' do
let(:fulfillment) { Fulfillment.new(shipment_date: '11/05/16', total_price: '$3.00', tracking_number: 'AMZN_US(TBA109359633000)')}
let(:items) { [item_one, item_two, item_three] }
let(:item_one) { Item.new(title: 'Pickles!', total_price: '$1.50', tracking_number: 'AMZN_US(TBA109359633000)') }
let(:item_two) { Item.new(title: 'Pancakes!', total_price: '$1.50', tracking_number: 'AMZN_US(TBA109359633000)') }
let(:item_three) { Item.new(title: 'Dickle!', total_price: '$5.00', tracking_number: 'AMZN_US(BUTTS)') }


it 'correctly matches the shipments to the items memo' do
result = TransactionResolver.new(fulfillment: fulfillment, remaining_items: items).items
expect(result).to eq([item_one, item_two])
end
end

context 'when there is no match' do
let(:fulfillment) { Fulfillment.new(shipment_date: '11/05/16', total_price: '$3.00', tracking_number: 'AMZN_US(TBA109359633000)')}

it 'returns an empty array' do

result = TransactionResolver.new(fulfillment: fulfillment, remaining_items: []).items
expect(result).to be_empty
end
end
end

0 comments on commit 28cdf33

Please sign in to comment.