From b0e31d018b7a77e3c5d0f8ed43ae2e016c17497f Mon Sep 17 00:00:00 2001 From: Keith Kim Date: Wed, 6 Nov 2019 16:49:40 -0500 Subject: [PATCH] Add fulfillment order related GET requests: - orders/:order_id/fulfillment_orders - assigned_fulfillment_orders - fulfillment_orders/:fulfillment_order_id - fulfillment_orders/:fulfillment_order_id/fulfillments Read FulfillmentOrder for an Order or with just an order_id --- .../resources/assigned_fulfillment_order.rb | 12 +++ .../resources/fulfillment_order.rb | 17 ++++ lib/shopify_api/resources/order.rb | 5 ++ test/assigned_fulfillment_order_test.rb | 40 ++++++++++ .../fixtures/assigned_fulfillment_orders.json | 78 +++++++++++++++++++ test/fixtures/fulfillment_order.json | 38 +++++++++ test/fixtures/fulfillment_orders.json | 78 +++++++++++++++++++ test/fixtures/fulfillments.json | 53 +++++++++++++ test/fulfillment_order_test.rb | 48 ++++++++++++ test/order_test.rb | 15 ++++ 10 files changed, 384 insertions(+) create mode 100644 lib/shopify_api/resources/assigned_fulfillment_order.rb create mode 100644 lib/shopify_api/resources/fulfillment_order.rb create mode 100644 test/assigned_fulfillment_order_test.rb create mode 100644 test/fixtures/assigned_fulfillment_orders.json create mode 100644 test/fixtures/fulfillment_order.json create mode 100644 test/fixtures/fulfillment_orders.json create mode 100644 test/fixtures/fulfillments.json create mode 100644 test/fulfillment_order_test.rb diff --git a/lib/shopify_api/resources/assigned_fulfillment_order.rb b/lib/shopify_api/resources/assigned_fulfillment_order.rb new file mode 100644 index 000000000..68d38826a --- /dev/null +++ b/lib/shopify_api/resources/assigned_fulfillment_order.rb @@ -0,0 +1,12 @@ +module ShopifyAPI + class AssignedFulfillmentOrder < Base + CANCELLATION_REQUESTED = 'cancellation_requested' + FULFILLMENT_REQUESTED = 'fulfillment_requested' + FULFILLMENT_ACCEPTED = 'fulfillment_accepted' + + def self.all(options = {}) + assigned_fulfillment_orders = super(options) + assigned_fulfillment_orders.map { |afo| FulfillmentOrder.new(afo.as_json) } + end + end +end diff --git a/lib/shopify_api/resources/fulfillment_order.rb b/lib/shopify_api/resources/fulfillment_order.rb new file mode 100644 index 000000000..3dd02730c --- /dev/null +++ b/lib/shopify_api/resources/fulfillment_order.rb @@ -0,0 +1,17 @@ +module ShopifyAPI + class FulfillmentOrder < Base + def self.all(options = {}) + order_id = options.dig(:params, :order_id) + if order_id + order = ::ShopifyAPI::Order.new(id: order_id) + order.fulfillment_orders + else + super(options) + end + end + + def fulfillments(options = {}) + get(:fulfillments, options) + end + end +end diff --git a/lib/shopify_api/resources/order.rb b/lib/shopify_api/resources/order.rb index 2d980e06b..22b2e42b8 100644 --- a/lib/shopify_api/resources/order.rb +++ b/lib/shopify_api/resources/order.rb @@ -30,6 +30,11 @@ def capture(amount = "", currency: nil) Transaction.create(capture_transaction) end + def fulfillment_orders(options = {}) + order_fulfillment_orders = get(:fulfillment_orders, options) + order_fulfillment_orders.map { |ofo| FulfillmentOrder.new(ofo.as_json) } + end + class ClientDetails < Base end end diff --git a/test/assigned_fulfillment_order_test.rb b/test/assigned_fulfillment_order_test.rb new file mode 100644 index 000000000..03a414a61 --- /dev/null +++ b/test/assigned_fulfillment_order_test.rb @@ -0,0 +1,40 @@ +require 'test_helper' + +class AssignedFulFillmentOrderTest < Test::Unit::TestCase + context "AssignedFulfillmentOrder" do + context "#all" do + should "be able to list assigned fulfillment orders by assigned_status" do + fo_fixture = load_fixture('assigned_fulfillment_orders') + fake 'assigned_fulfillment_orders.json?assigned_status=cancellation_requested', method: :get, + body: fo_fixture, extension: false + + assigned_fulfillment_orders = ShopifyAPI::AssignedFulfillmentOrder.all( + params: { assigned_status: 'cancellation_requested' } + ) + + assert_equal 2, assigned_fulfillment_orders.count + assigned_fulfillment_orders.each do |assigned_fulfillment_order| + assert_equal 'open', assigned_fulfillment_order.status + assert_equal 'unsubmitted', assigned_fulfillment_order.request_status + end + end + + should "be able to list assigned fulfillment orders by location_ids" do + fo_fixture = load_fixture('assigned_fulfillment_orders') + assigned_location_id = 905684977 + fake "assigned_fulfillment_orders.json?location_ids%5B%5D=#{assigned_location_id}", method: :get, + body: fo_fixture, extension: false + + assigned_fulfillment_orders = ShopifyAPI::AssignedFulfillmentOrder.all( + params: { location_ids: [assigned_location_id] } + ) + + assert_equal 2, assigned_fulfillment_orders.count + assigned_fulfillment_orders.each do |assigned_fulfillment_order| + assert_equal 'ShopifyAPI::FulfillmentOrder', fulfillment_order.class.name + assert_equal assigned_location_id, assigned_fulfillment_order.assigned_location_id + end + end + end + end +end diff --git a/test/fixtures/assigned_fulfillment_orders.json b/test/fixtures/assigned_fulfillment_orders.json new file mode 100644 index 000000000..ef44616b3 --- /dev/null +++ b/test/fixtures/assigned_fulfillment_orders.json @@ -0,0 +1,78 @@ +[ + { + "id": 519788021, + "order_id": 450789469, + "fulfillment_service_handle": "shipwire", + "status": "open", + "requires_shipping": true, + "is_not_deleted": true, + "deleted_at": null, + "created_at": "2016-07-12T11:23:42-04:00", + "updated_at": "2016-07-12T11:23:42-04:00", + "happened_at": "2016-07-12T11:23:42-04:00", + "assigned_location_id": 905684977, + "request_status": "unsubmitted", + "delivery_category": null, + "fulfillment_order_line_items": [ + { + "id": 519788021, + "shop_id": 690933842, + "fulfillment_order_id": 519788021, + "line_item_id": 466157049, + "variant_id": 43729076, + "inventory_item_id": 808950810, + "inventory_commitment_id": 273471403, + "quantity": 1, + "sku": "IPOD2008GREEN", + "vendor": null, + "variant_title": "green", + "product_title": "IPod Nano - 8gb", + "pre_tax_price": 398.00, + "currency": "USD", + "properties": null, + "is_not_deleted": true, + "deleted_at": null, + "created_at": "2016-07-12T11:23:54-04:00", + "updated_at": "2016-07-12T11:23:54-04:00" + } + ] + }, + { + "id": 519788022, + "order_id": 450789469, + "fulfillment_service_handle": "manual", + "status": "open", + "requires_shipping": true, + "is_not_deleted": true, + "deleted_at": null, + "created_at": "2016-07-12T11:23:42-04:00", + "updated_at": "2016-07-12T11:23:42-04:00", + "happened_at": "2016-07-12T11:23:42-04:00", + "assigned_location_id": 905684977, + "request_status": "unsubmitted", + "delivery_category": null, + "fulfillment_order_line_items": [ + { + "id": 519788021, + "shop_id": 690933842, + "fulfillment_order_id": 519788021, + "line_item_id": 466157049, + "variant_id": 43729076, + "inventory_item_id": 808950810, + "inventory_commitment_id": 273471403, + "quantity": 1, + "sku": "IPOD2008RED", + "vendor": null, + "variant_title": "red", + "product_title": "IPod Nano - 8gb", + "pre_tax_price": 398.00, + "currency": "USD", + "properties": null, + "is_not_deleted": true, + "deleted_at": null, + "created_at": "2016-07-12T11:23:54-04:00", + "updated_at": "2016-07-12T11:23:54-04:00" + } + ] + } +] diff --git a/test/fixtures/fulfillment_order.json b/test/fixtures/fulfillment_order.json new file mode 100644 index 000000000..54ef71589 --- /dev/null +++ b/test/fixtures/fulfillment_order.json @@ -0,0 +1,38 @@ +{ + "id": 519788021, + "order_id": 450789469, + "fulfillment_service_handle": "shipwire", + "status": "open", + "requires_shipping": true, + "is_not_deleted": true, + "deleted_at": null, + "created_at": "2016-07-12T11:23:42-04:00", + "updated_at": "2016-07-12T11:23:42-04:00", + "happened_at": "2016-07-12T11:23:42-04:00", + "assigned_location_id": 905684977, + "request_status": "unsubmitted", + "delivery_category": null, + "fulfillment_order_line_items": [ + { + "id": 519788021, + "shop_id": 690933842, + "fulfillment_order_id": 519788021, + "line_item_id": 466157049, + "variant_id": 43729076, + "inventory_item_id": 808950810, + "inventory_commitment_id": 273471403, + "quantity": 1, + "sku": "IPOD2008GREEN", + "vendor": null, + "variant_title": "green", + "product_title": "IPod Nano - 8gb", + "pre_tax_price": 398.00, + "currency": "USD", + "properties": null, + "is_not_deleted": true, + "deleted_at": null, + "created_at": "2016-07-12T11:23:54-04:00", + "updated_at": "2016-07-12T11:23:54-04:00" + } + ] +} diff --git a/test/fixtures/fulfillment_orders.json b/test/fixtures/fulfillment_orders.json new file mode 100644 index 000000000..ef44616b3 --- /dev/null +++ b/test/fixtures/fulfillment_orders.json @@ -0,0 +1,78 @@ +[ + { + "id": 519788021, + "order_id": 450789469, + "fulfillment_service_handle": "shipwire", + "status": "open", + "requires_shipping": true, + "is_not_deleted": true, + "deleted_at": null, + "created_at": "2016-07-12T11:23:42-04:00", + "updated_at": "2016-07-12T11:23:42-04:00", + "happened_at": "2016-07-12T11:23:42-04:00", + "assigned_location_id": 905684977, + "request_status": "unsubmitted", + "delivery_category": null, + "fulfillment_order_line_items": [ + { + "id": 519788021, + "shop_id": 690933842, + "fulfillment_order_id": 519788021, + "line_item_id": 466157049, + "variant_id": 43729076, + "inventory_item_id": 808950810, + "inventory_commitment_id": 273471403, + "quantity": 1, + "sku": "IPOD2008GREEN", + "vendor": null, + "variant_title": "green", + "product_title": "IPod Nano - 8gb", + "pre_tax_price": 398.00, + "currency": "USD", + "properties": null, + "is_not_deleted": true, + "deleted_at": null, + "created_at": "2016-07-12T11:23:54-04:00", + "updated_at": "2016-07-12T11:23:54-04:00" + } + ] + }, + { + "id": 519788022, + "order_id": 450789469, + "fulfillment_service_handle": "manual", + "status": "open", + "requires_shipping": true, + "is_not_deleted": true, + "deleted_at": null, + "created_at": "2016-07-12T11:23:42-04:00", + "updated_at": "2016-07-12T11:23:42-04:00", + "happened_at": "2016-07-12T11:23:42-04:00", + "assigned_location_id": 905684977, + "request_status": "unsubmitted", + "delivery_category": null, + "fulfillment_order_line_items": [ + { + "id": 519788021, + "shop_id": 690933842, + "fulfillment_order_id": 519788021, + "line_item_id": 466157049, + "variant_id": 43729076, + "inventory_item_id": 808950810, + "inventory_commitment_id": 273471403, + "quantity": 1, + "sku": "IPOD2008RED", + "vendor": null, + "variant_title": "red", + "product_title": "IPod Nano - 8gb", + "pre_tax_price": 398.00, + "currency": "USD", + "properties": null, + "is_not_deleted": true, + "deleted_at": null, + "created_at": "2016-07-12T11:23:54-04:00", + "updated_at": "2016-07-12T11:23:54-04:00" + } + ] + } +] diff --git a/test/fixtures/fulfillments.json b/test/fixtures/fulfillments.json new file mode 100644 index 000000000..d9c66c6e6 --- /dev/null +++ b/test/fixtures/fulfillments.json @@ -0,0 +1,53 @@ +[ + { + "created_at": "2014-03-07T16:14:08-05:00", + "id": 255858046, + "order_id": 450789469, + "service": "manual", + "status": "failure", + "tracking_company": null, + "updated_at": "2014-03-07T16:14:08-05:00", + "tracking_number": "1Z2345", + "tracking_numbers": [ + "1Z2345" + ], + "tracking_url": "http://wwwapps.ups.com/etracking/tracking.cgi?InquiryNumber1=1Z2345&TypeOfInquiryNumber=T&AcceptUPSLicenseAgreement=yes&submit=Track", + "tracking_urls": [ + "http://wwwapps.ups.com/etracking/tracking.cgi?InquiryNumber1=1Z2345&TypeOfInquiryNumber=T&AcceptUPSLicenseAgreement=yes&submit=Track" + ], + "receipt": { + "testcase": true, + "authorization": "123456" + }, + "line_items": [ + { + "fulfillment_service": "manual", + "fulfillment_status": null, + "grams": 200, + "id": 466157049, + "price": "199.00", + "product_id": 632910392, + "quantity": 1, + "requires_shipping": true, + "sku": "IPOD2008GREEN", + "taxable": true, + "title": "IPod Nano - 8gb", + "variant_id": 39072856, + "variant_title": "green", + "vendor": null, + "name": "IPod Nano - 8gb - green", + "variant_inventory_management": "shopify", + "properties": [ + { + "name": "Custom Engraving", + "value": "Happy Birthday" + } + ], + "product_exists": true, + "tax_lines": [ + + ] + } + ] + } +] diff --git a/test/fulfillment_order_test.rb b/test/fulfillment_order_test.rb new file mode 100644 index 000000000..18fc8978f --- /dev/null +++ b/test/fulfillment_order_test.rb @@ -0,0 +1,48 @@ +require 'test_helper' + +class FulFillmentOrderTest < Test::Unit::TestCase + def setup + super + fake "fulfillment_orders/519788021", method: :get, + body: load_fixture('fulfillment_order') + end + + context "FulfillmentOrder" do + context "#find" do + should "be able to find fulfillment order" do + fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021) + assert_equal 519788021, fulfillment_order.id + assert_equal 450789469, fulfillment_order.order_id + end + end + + context "#all" do + should "be able to list fulfillment orders for an order" do + fake 'orders/450789469/fulfillment_orders', method: :get, body: load_fixture('fulfillment_orders') + + fulfillment_orders = ShopifyAPI::FulfillmentOrder.all( + params: { order_id: 450789469 } + ) + + assert_equal 2, fulfillment_orders.count + assert_equal [519788021, 519788022], fulfillment_orders.map(&:id).sort + assert_equal [450789469], fulfillment_orders.map(&:order_id).uniq + end + end + + 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') + + fulfillments = fulfillment_order.fulfillments + + assert_equal 1, fulfillments.count + fulfillment = fulfillments.first + assert_equal 450789469, fulfillment['order_id'] + end + end + end +end diff --git a/test/order_test.rb b/test/order_test.rb index 8c70bb162..54e446ff0 100644 --- a/test/order_test.rb +++ b/test/order_test.rb @@ -72,4 +72,19 @@ class OrderTest < Test::Unit::TestCase }, }.to_json) end + + test "fulfillment_orders should get fulfillment orders for an order" do + fake 'orders/450789469', body: load_fixture('order') + order = ShopifyAPI::Order.find(450789469) + + fake 'orders/450789469/fulfillment_orders', method: :get, body: load_fixture('fulfillment_orders') + fulfillment_orders = order.fulfillment_orders + + assert_equal 2, fulfillment_orders.count + fulfillment_orders.each do |fulfillment_order| + assert_equal 'ShopifyAPI::FulfillmentOrder', fulfillment_order.class.name + assert_equal 450789469, fulfillment_order.order_id + end + assert_equal [519788021, 519788022], fulfillment_orders.map(&:id).sort + end end