diff --git a/lib/shopify_api/resources/fulfillment_order.rb b/lib/shopify_api/resources/fulfillment_order.rb index 0569ca4a2..6dd136d49 100644 --- a/lib/shopify_api/resources/fulfillment_order.rb +++ b/lib/shopify_api/resources/fulfillment_order.rb @@ -12,5 +12,33 @@ def fulfillments(options = {}) fo_fulfillments = get(:fulfillments, options) fo_fulfillments.map { |fof| FulfillmentV2.new(fof.as_json) } end + + def move(new_location_id:) + body = { fulfillment_order: { new_location_id: new_location_id } } + load_values(post(:move, body, only_id)) + end + + def cancel + load_values(post(:cancel, {}, only_id)) + end + + def close + load_attributes_from_response(post(:close, {}, only_id)) + end + + private + + def load_values(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| + if fo_attributes.nil? + [key, nil] + else + [key, FulfillmentOrder.new(fo_attributes)] + end + end.to_h + end end end diff --git a/test/fulfillment_order_test.rb b/test/fulfillment_order_test.rb index d9c16dc3a..f01bd4b1b 100644 --- a/test/fulfillment_order_test.rb +++ b/test/fulfillment_order_test.rb @@ -42,7 +42,6 @@ def setup context "#fulfillments" do should "be able to list fulfillments for a fulfillment order" do fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021) - fake "fulfillment_orders/#{fulfillment_order.id}/fulfillments", method: :get, body: load_fixture('fulfillments') @@ -54,5 +53,80 @@ def setup assert_equal 450789469, fulfillment.order_id end end + + context "#move" do + should "be able to move fulfillment order to a new_location_id" do + fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021) + new_location_id = 5 + + moved = ActiveSupport::JSON.decode(load_fixture('fulfillment_order')) + moved['assigned_location_id'] = new_location_id + fulfillment_order.status = 'closed' + + body = { + original_fulfillment_order: fulfillment_order, + moved_fulfillment_order: moved, + remaining_fulfillment_order: nil, + } + api_version = ShopifyAPI::ApiVersion.find_version('2019-01') + endpoint = "fulfillment_orders/519788021/move" + extension = ".json" + url = "https://this-is-my-test-shop.myshopify.com#{api_version.construct_api_path("#{endpoint}#{extension}")}" + url = url + "?fulfillment_order%5Bnew_location_id%5D=5" + fake endpoint, :method => :post, :url => url, :body => ActiveSupport::JSON.encode(body) + + response_fos = fulfillment_order.move(new_location_id: new_location_id) + assert_equal 3, response_fos.count + original_fulfillment_order = response_fos['original_fulfillment_order'] + refute_nil original_fulfillment_order + assert_equal 'ShopifyAPI::FulfillmentOrder', original_fulfillment_order.class.name + assert_equal 'closed', original_fulfillment_order.status + + moved_fulfillment_order = response_fos['moved_fulfillment_order'] + refute_nil moved_fulfillment_order + assert_equal 'ShopifyAPI::FulfillmentOrder', moved_fulfillment_order.class.name + assert_equal 'open', moved_fulfillment_order.status + assert_equal new_location_id, moved_fulfillment_order.assigned_location_id + + remaining_fulfillment_order = response_fos['remaining_fulfillment_order'] + assert_nil remaining_fulfillment_order + end + end + + context "#cancel" do + should "be able to cancel fulfillment order" do + fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021) + assert_equal 'open', fulfillment_order.status + + cancelled = ActiveSupport::JSON.decode(load_fixture('fulfillment_order')) + cancelled['status'] = 'cancelled' + body = { + fulfillment_order: cancelled, + replacement_fulfillment_order: fulfillment_order, + } + fake "fulfillment_orders/519788021/cancel", :method => :post, :body => ActiveSupport::JSON.encode(body) + + response_fos = fulfillment_order.cancel + assert_equal 2, response_fos.count + fulfillment_order = response_fos['fulfillment_order'] + assert_equal 'cancelled', fulfillment_order.status + replacement_fulfillment_order = response_fos['replacement_fulfillment_order'] + assert_equal 'open', replacement_fulfillment_order.status + end + end + + context "#close" do + should "be able to close 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 end end