Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add move, cancel, close actions FulfillmentOrder #635

Merged

Conversation

karmakaze
Copy link
Contributor

@karmakaze karmakaze commented Nov 8, 2019

This PR adds the following POST requests:

POST fulfillment_orders/:fulfillment_order_id/move
POST fulfillment_orders/:fulfillment_order_id/cancel
POST fulfillment_orders/:fulfillment_order_id/close

GET requests are in PR #633
Subsequent PRs will add other fulfillment order related write requests to relevant resources.

The approach I took was to add the move, cancel, and close actions on the FulfillmentOrder resource.

Because there's already a legacy Fulfillment resource, I named the fulfillment orders fulfillment as FulfillmentOrderFulfillment.

Since all that's really needed is the fulfillment_order_id, if it's known you can do (rather than querying it first):

  closed_fulfillment_order = FulfillmentOrder.new(id: <the-fo-id>).close

@froot
Copy link

froot commented Nov 13, 2019

A few questions:

test/fulfillment_order_test.rb Outdated Show resolved Hide resolved
test/fulfillment_order_test.rb Outdated Show resolved Hide resolved
test/fulfillment_order_test.rb Outdated Show resolved Hide resolved
@karmakaze karmakaze force-pushed the fulfillment-orders-move-cancel-close branch 2 times, most recently from 1d26023 to 6dae6dd Compare November 13, 2019 23:21
Copy link

@glen-j-nkali glen-j-nkali left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of comments to make sure I am following the logic here. Thank you again for handling this 🙏

lib/shopify_api/resources/fulfillment_order.rb Outdated Show resolved Hide resolved
lib/shopify_api/resources/fulfillment_order.rb Outdated Show resolved Hide resolved
body = { fulfillment_order: { new_location_id: new_location_id } }.to_json
keyed_fos = load_keyed_attributes_from_response(post(:move, {}, body))
if keyed_fos&.fetch('original_fulfillment_order', nil)&.attributes
load(keyed_fos['original_fulfillment_order'].attributes, false, true)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just for my personal comprehension here but what are you trying to achieve here ? Specifically why does this check only apply to the original_fulfillment_order ?

Are you using it to determine wether or not we got here with the error message payload ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was a bit uncertain about this one. The request is made using an instance of a FulfillmentOrder, e.g. fulfillment_order.move(...) where the server responds with multiple keyed FOs. I took the one named original_fulfillment_order to represent the mutation of the fulfillment_order that was the subject of the .move and so load the attributes of original_fulfillment_order into itself.

This is following the pattern where responses that return a single FO and calling load_attributes_from_response will load the calling object's attrributes from the only keyed value.


def cancel
keyed_fos = load_keyed_attributes_from_response(post(:cancel, {}, only_id))
if keyed_fos&.fetch('fulfillment_order', nil)&.attributes

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question as above about why specifically this key ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This key represents the mutation of the subject FO resource issuing the .cancel.

lib/shopify_api/resources/fulfillment_order.rb Outdated Show resolved Hide resolved
@karmakaze karmakaze force-pushed the fulfillment-orders-move-cancel-close branch from e66255e to 634d2b3 Compare November 21, 2019 21:08
@karmakaze karmakaze force-pushed the fulfillment-orders-reads branch 2 times, most recently from 702b858 to c72bff7 Compare November 21, 2019 21:53
@karmakaze karmakaze force-pushed the fulfillment-orders-move-cancel-close branch from 634d2b3 to a47cdba Compare November 26, 2019 18:21
else
[key, FulfillmentOrder.new(fo_attributes)]
end
end.to_h
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: a simpler way to do this would be to use transform_values method:

keyed_fulfillment_orders.transform_values do |fulfillment_order|
    fulfillment_order.nil? nil : FulfillmentOrder.new(fulfillment_order)
end

def move(new_location_id:)
body = { fulfillment_order: { new_location_id: new_location_id } }.to_json
keyed_fos = load_keyed_fulfillment_orders_from_response(post(:move, {}, body))
if keyed_fos&.fetch('original_fulfillment_order', nil)&.attributes
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe consider extracting this into a private method def load_attributes_from_response_for(fulfillment_order_key:) since it's used both in this PR and in https://github.com/Shopify/shopify_api/pull/637/files

fulfillment_order = ShopifyAPI::FulfillmentOrder.find(519788021)
new_location_id = 5

original = fulfillment_order.clone
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was confused reading this at first, would be easier to understand if we rename this to original_fulfillment_order_response an moved_fulfillment_order_response so that it's more clear the intention of declaring these variables.

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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: assert original_fulfillment_order.is_a?(ShopifyApi::FulfillmentOrder)

assert_equal new_location_id, moved_fulfillment_order.assigned_location_id

remaining_fulfillment_order = response_fos['remaining_fulfillment_order']
assert_nil remaining_fulfillment_order
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can be compressed to assert_nil response_fos['remaining_fulfillment_order']


assert_equal 'cancelled', fulfillment_order.status
assert_equal 2, response_fos.count
fulfillment_order = response_fos['fulfillment_order']
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename this to something other than fulfillment_order, maybe response_fulfillment_order.

@karmakaze karmakaze force-pushed the fulfillment-orders-move-cancel-close branch from a47cdba to 1a48c0e Compare December 2, 2019 20:07
Copy link

@froot froot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small necessary change (allowing apps to provide a message when closing a fulfillment order) and some nits. Other than that LGTM!

lib/shopify_api/resources/fulfillment_order.rb Outdated Show resolved Hide resolved
test/assigned_fulfillment_order_test.rb Outdated Show resolved Hide resolved
@karmakaze karmakaze force-pushed the fulfillment-orders-move-cancel-close branch from 1a48c0e to 2bbefa9 Compare December 10, 2019 20:13
@karmakaze karmakaze force-pushed the fulfillment-orders-move-cancel-close branch from 2bbefa9 to 741c27e Compare December 16, 2019 18:21
@karmakaze karmakaze force-pushed the fulfillment-orders-move-cancel-close branch 2 times, most recently from 621057d to 563d01d Compare January 9, 2020 21:06
Copy link
Contributor

@linzhao125 linzhao125 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great after reading all the conversations from previous reviews. It is interesting that the caller fulfillment_order needs to be loaded manually. I think the fact that we want to support both order/#{order_id}/fulfillment_orders and fulfillment_orders/#{fulfillment_order_id} made things a bit tricky. Also we return multiple keyed resources.

@karmakaze karmakaze force-pushed the fulfillment-orders-move-cancel-close branch from 563d01d to eb38d9a Compare January 10, 2020 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants