Skip to content

Commit

Permalink
Add actions fulfillment_request, accept_fulfillment_request, reject_f…
Browse files Browse the repository at this point in the history
…ulfillment_request, cancellation_request, accept_cancellation, reject_cancellation to FulfillmentOrder resource
  • Loading branch information
karmakaze committed Nov 13, 2019
1 parent c10ebe9 commit 29f3a21
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 5 deletions.
45 changes: 40 additions & 5 deletions lib/shopify_api/resources/fulfillment_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,59 @@ def fulfillments(options = {})

def move(new_location_id:)
body = { fulfillment_order: { new_location_id: new_location_id } }
load_values(post(:move, body, only_id))
load_keyed_attributes_from_response(post(:move, body, only_id))
end

def cancel
load_values(post(:cancel, {}, only_id))
load_keyed_attributes_from_response(post(:cancel, {}, only_id))
end

def close
load_attributes_from_response(post(:close, {}, only_id))
end

def fulfillment_request(fulfillment_order_line_items:, message:)
body = {
fulfillment_request: {
fulfillment_order_line_items: fulfillment_order_line_items,
message: message
}
}
load_keyed_attributes_from_response(post(:fulfillment_request, body, only_id))
end

def accept_fulfillment_request(params)
load_attributes_from_response(post('fulfillment_request/accept', params, only_id))
end

def reject_fulfillment_request(params)
load_attributes_from_response(post('fulfillment_request/reject', params, only_id))
end

def cancellation_request(message:)
body = {
cancellation_request: {
message: message
}
}
load_attributes_from_response(post(:cancellation_request, body, only_id))
end

def accept_cancellation_request(params)
load_attributes_from_response(post('cancellation_request/accept', params, only_id))
end

def reject_cancellation_request(params)
load_attributes_from_response(post('cancellation_request/reject', params, only_id))
end

private

def load_values(response)
def load_keyed_attributes_from_response(response)
return load_attributes_from_response(response) if response.code != '200'

keyed_fulfillments = ActiveSupport::JSON.decode(response.body)
keyed_fulfillments.map do |key, fo_attributes|
keyed_fo_attributes = ActiveSupport::JSON.decode(response.body)
keyed_fo_attributes.map do |key, fo_attributes|
if fo_attributes.nil?
[key, nil]
else
Expand Down
158 changes: 158 additions & 0 deletions test/fulfillment_order_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,163 @@ def setup
assert_equal 'closed', fulfillment_order.status
end
end

context "#fulfillment_request" do
should "be able to make a fulfillment request for a fulfillment order" do
original_fulfillment_order = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
submitted_fulfillment_order = original_fulfillment_order.clone
submitted_fulfillment_order['id'] = 2
submitted_fulfillment_order['status'] = 'open'
submitted_fulfillment_order['request_status'] = 'submitted'
unsubmitted_fulfillment_order = original_fulfillment_order.clone
unsubmitted_fulfillment_order['id'] = 3
unsubmitted_fulfillment_order['request_status'] = 'unsubmitted'
original_fulfillment_order['status'] = 'closed'
body = {
original_fulfillment_order: original_fulfillment_order,
submitted_fulfillment_order: submitted_fulfillment_order,
unsubmitted_fulfillment_order: unsubmitted_fulfillment_order
}
fake_query = {
'fulfillment_request[fulfillment_order_line_items][0][id]' => '1',
'fulfillment_request[fulfillment_order_line_items][0][quantity]' => '1',
'fulfillment_request[message]' => 'Fulfill this FO, please.'
}
fake "fulfillment_orders/519788021/fulfillment_request.json#{query_string(fake_query)}", :extension => false,
:method => :post, :body => ActiveSupport::JSON.encode(body)

fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
params = {
fulfillment_order_line_items: [{ id: 1, quantity: 1 }],
message: "Fulfill this FO, please."
}
original_submitted_unsubmitted_fos = fulfillment_order.fulfillment_request(params)

original_fo = original_submitted_unsubmitted_fos['original_fulfillment_order']
assert_equal 519788021, original_fo.id
assert_equal 'closed', original_fo.status

submitted_fo = original_submitted_unsubmitted_fos['submitted_fulfillment_order']
assert_equal 2, submitted_fo.id
assert_equal 'open', submitted_fo.status
assert_equal 'submitted', submitted_fo.request_status

unsubmitted_fo = original_submitted_unsubmitted_fos['unsubmitted_fulfillment_order']
assert_equal 3, unsubmitted_fo.id
assert_equal 'open', unsubmitted_fo.status
assert_equal 'unsubmitted', unsubmitted_fo.request_status
end
end

context "#accept_fulfillment_request" do
should "be able to accept a fulfillment request for a fulfillment order" do
fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)

fake_query = {
'message' => "LGTM. Accept this FO fulfillment request",
'other' => "random"
}
fake_response = { fulfillment_order: fulfillment_order.attributes.merge(status: 'in_progress') }
fake "fulfillment_orders/519788021/fulfillment_request/accept.json#{query_string(fake_query)}",
:extension => false, :method => :post,
:body => ActiveSupport::JSON.encode(fake_response)

params = {
message: 'LGTM. Accept this FO fulfillment request',
other: 'random'
}
accepted = fulfillment_order.accept_fulfillment_request(params)

assert_equal true, accepted
assert_equal 'in_progress', fulfillment_order.status
end
end

context "#reject_fulfillment_request" do
should "be able to reject a fulfillment request for a fulfillment order" do
fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)

fake_query = {
'message' => "LBTM. Reject this FO fulfillment request",
'other' => "text"
}
fake_response = { fulfillment_order: fulfillment_order.attributes.merge(status: 'closed') }
fake "fulfillment_orders/519788021/fulfillment_request/reject.json#{query_string(fake_query)}",
:extension => false, :method => :post,
:body => ActiveSupport::JSON.encode(fake_response)

params = {
message: 'LBTM. Reject this FO fulfillment request',
other: 'text'
}
rejected = fulfillment_order.reject_fulfillment_request(params)

assert_equal true, rejected
assert_equal 'closed', fulfillment_order.status
end
end

context "#cancellation_request" do
should "be able to make a cancellation request for a fulfillment order" do
fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)

closed = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
closed['status'] = 'closed'
fake "fulfillment_orders/519788021/close", :method => :post, :body => ActiveSupport::JSON.encode(closed)

assert_equal 'open', fulfillment_order.status
assert fulfillment_order.close
assert_equal 'closed', fulfillment_order.status
end
end

context "#accept_cancellation_request" do
should "be able to accept a cancellation request for a fulfillment order" do
fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)

fake_query = {
'message' => "Already in-progress. Reject this FO cancellation request",
'other' => "blah"
}
fake_response = { fulfillment_order: fulfillment_order.attributes.merge(status: 'closed') }
fake "fulfillment_orders/519788021/cancellation_request/accept.json#{query_string(fake_query)}",
:extension => false, :method => :post,
:body => ActiveSupport::JSON.encode(fake_response)

params = {
message: 'Already in-progress. Reject this FO cancellation request',
other: 'blah'
}
accepted = fulfillment_order.accept_cancellation_request(params)

assert_equal true, accepted
assert_equal 'closed', fulfillment_order.status
end
end

context "#reject_cancellation_request" do
should "be able to reject a cancellation request for a fulfillment order" do
fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)

fake_query = {
'message' => "Already in-progress. Reject this FO cancellation request",
'other' => "blah"
}
fake_response = { fulfillment_order: fulfillment_order.attributes.merge(status: 'in_progress') }
fake "fulfillment_orders/519788021/cancellation_request/reject.json#{query_string(fake_query)}",
:extension => false, :method => :post,
:body => ActiveSupport::JSON.encode(fake_response)

params = {
message: 'Already in-progress. Reject this FO cancellation request',
other: 'blah'
}
rejected = fulfillment_order.reject_cancellation_request(params)

assert_equal true, rejected
assert_equal 'in_progress', fulfillment_order.status
end
end

end
end
5 changes: 5 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ def fake(endpoint, options={})
)
end

def query_string(hash)
return '' if hash.nil? || hash.empty?
'?' + hash.map { |k, v| URI::encode(k.to_s) + '=' + URI::encode(v.to_s) }.join('&')
end

def ar_version_before?(version_string)
Gem::Version.new(ActiveResource::VERSION::STRING) < Gem::Version.new(version_string)
end
Expand Down

0 comments on commit 29f3a21

Please sign in to comment.