Skip to content

Commit eb38d9a

Browse files
committed
FulfillmentOrder: move/cancel/close
1 parent 6fc2312 commit eb38d9a

File tree

3 files changed

+133
-2
lines changed

3 files changed

+133
-2
lines changed

lib/shopify_api/resources/fulfillment_order.rb

+43
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,48 @@ def fulfillments(options = {})
1616
fulfillment_hashes = get(:fulfillments, options)
1717
fulfillment_hashes.map { |fulfillment_hash| Fulfillment.new(fulfillment_hash) }
1818
end
19+
20+
def move(new_location_id:)
21+
body = {
22+
fulfillment_order: {
23+
new_location_id: new_location_id
24+
}
25+
}
26+
keyed_fulfillment_orders = keyed_fulfillment_orders_from_response(post(:move, {}, body.to_json))
27+
load_keyed_fulfillment_order(keyed_fulfillment_orders, 'original_fulfillment_order')
28+
keyed_fulfillment_orders
29+
end
30+
31+
def cancel
32+
keyed_fulfillment_orders = keyed_fulfillment_orders_from_response(post(:cancel, {}, only_id))
33+
load_keyed_fulfillment_order(keyed_fulfillment_orders, 'fulfillment_order')
34+
keyed_fulfillment_orders
35+
end
36+
37+
def close(message: nil)
38+
body = {
39+
fulfillment_order: {
40+
message: message
41+
}
42+
}
43+
load_attributes_from_response(post(:close, {}, body.to_json))
44+
end
45+
46+
private
47+
48+
def load_keyed_fulfillment_order(keyed_fulfillment_orders, key)
49+
if keyed_fulfillment_orders[key]&.attributes
50+
load(keyed_fulfillment_orders[key].attributes, false, true)
51+
end
52+
end
53+
54+
def keyed_fulfillment_orders_from_response(response)
55+
return load_attributes_from_response(response) if response.code != '200'
56+
57+
keyed_fulfillment_orders = ActiveSupport::JSON.decode(response.body)
58+
keyed_fulfillment_orders.transform_values do |fulfillment_order_attributes|
59+
FulfillmentOrder.new(fulfillment_order_attributes) if fulfillment_order_attributes
60+
end
61+
end
1962
end
2063
end

test/fulfillment_order_test.rb

+86-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ def setup
4545
context "#fulfillments" do
4646
should "be able to list fulfillments for a fulfillment order" do
4747
fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
48-
4948
fake "fulfillment_orders/#{fulfillment_order.id}/fulfillments", method: :get,
5049
body: load_fixture('fulfillments')
5150

@@ -57,5 +56,91 @@ def setup
5756
assert_equal 450789469, fulfillment.order_id
5857
end
5958
end
59+
60+
context "#move" do
61+
should "move a fulfillment order to a new_location_id" do
62+
fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
63+
new_location_id = 5
64+
65+
fake_original_fulfillment_order = fulfillment_order.clone
66+
fake_original_fulfillment_order.status = 'closed'
67+
fake_moved_fulfillment_order = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
68+
fake_moved_fulfillment_order['assigned_location_id'] = new_location_id
69+
70+
request_body = { fulfillment_order: { new_location_id: 5 } }
71+
body = {
72+
original_fulfillment_order: fake_original_fulfillment_order,
73+
moved_fulfillment_order: fake_moved_fulfillment_order,
74+
remaining_fulfillment_order: nil,
75+
}
76+
fake "fulfillment_orders/519788021/move", :method => :post,
77+
:request_body => ActiveSupport::JSON.encode(request_body),
78+
:body => ActiveSupport::JSON.encode(body)
79+
80+
response_fulfillment_orders = fulfillment_order.move(new_location_id: new_location_id)
81+
82+
assert_equal 'closed', fulfillment_order.status
83+
84+
assert_equal 3, response_fulfillment_orders.count
85+
original_fulfillment_order = response_fulfillment_orders['original_fulfillment_order']
86+
refute_nil original_fulfillment_order
87+
assert original_fulfillment_order.is_a?(ShopifyAPI::FulfillmentOrder)
88+
assert_equal 'closed', original_fulfillment_order.status
89+
90+
moved_fulfillment_order = response_fulfillment_orders['moved_fulfillment_order']
91+
refute_nil moved_fulfillment_order
92+
assert moved_fulfillment_order.is_a?(ShopifyAPI::FulfillmentOrder)
93+
assert_equal 'open', moved_fulfillment_order.status
94+
assert_equal new_location_id, moved_fulfillment_order.assigned_location_id
95+
96+
remaining_fulfillment_order = response_fulfillment_orders['remaining_fulfillment_order']
97+
assert_nil remaining_fulfillment_order
98+
end
99+
end
100+
101+
context "#cancel" do
102+
should "cancel a fulfillment order" do
103+
fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
104+
assert_equal 'open', fulfillment_order.status
105+
106+
cancelled = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
107+
cancelled['status'] = 'cancelled'
108+
body = {
109+
fulfillment_order: cancelled,
110+
replacement_fulfillment_order: fulfillment_order,
111+
}
112+
fake "fulfillment_orders/519788021/cancel", :method => :post, :body => ActiveSupport::JSON.encode(body)
113+
114+
response_fulfillment_orders = fulfillment_order.cancel
115+
116+
assert_equal 'cancelled', fulfillment_order.status
117+
assert_equal 2, response_fulfillment_orders.count
118+
fulfillment_order = response_fulfillment_orders['fulfillment_order']
119+
assert_equal 'cancelled', fulfillment_order.status
120+
replacement_fulfillment_order = response_fulfillment_orders['replacement_fulfillment_order']
121+
assert_equal 'open', replacement_fulfillment_order.status
122+
end
123+
end
124+
125+
context "#close" do
126+
should "be able to close fulfillment order" do
127+
fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
128+
fulfillment_order.status = 'in_progress'
129+
130+
closed = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
131+
closed['status'] = 'incomplete'
132+
request_body = {
133+
fulfillment_order: {
134+
message: "Test close message."
135+
}
136+
}
137+
fake "fulfillment_orders/519788021/close", :method => :post,
138+
:request_body => ActiveSupport::JSON.encode(request_body),
139+
:body => ActiveSupport::JSON.encode(closed)
140+
141+
assert fulfillment_order.close(message: "Test close message.")
142+
assert_equal 'incomplete', fulfillment_order.status
143+
end
144+
end
60145
end
61146
end

test/test_helper.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def assert_request_body(expected)
9292
end
9393

9494
def fake(endpoint, options={})
95+
request_body = options.has_key?(:request_body) ? options.delete(:request_body) : nil
9596
body = options.has_key?(:body) ? options.delete(:body) : load_fixture(endpoint)
9697
format = options.delete(:format) || :json
9798
method = options.delete(:method) || :get
@@ -104,7 +105,9 @@ def fake(endpoint, options={})
104105
"https://this-is-my-test-shop.myshopify.com#{api_version.construct_api_path("#{endpoint}#{extension}")}"
105106
end
106107

107-
WebMock.stub_request(method, url).to_return(
108+
stubbing = WebMock.stub_request(method, url)
109+
stubbing = stubbing.with(body: request_body) if request_body
110+
stubbing.to_return(
108111
body: body, status: status, headers: { content_type: "text/#{format}", content_length: 1 }.merge(options)
109112
)
110113
end

0 commit comments

Comments
 (0)