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

Fulfillment orders fulfillment cancellation request #637

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions lib/shopify_api/resources/fulfillment_order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,63 @@ def close(message: nil)
load_attributes_from_response(post(:close, {}, body.to_json))
end

def request_fulfillment(fulfillment_order_line_items: nil, message: nil)
body = {
fulfillment_request: {
fulfillment_order_line_items: fulfillment_order_line_items,
message: message
}
}
keyed_fulfillment_orders = keyed_fulfillment_orders_from_response(post(:fulfillment_request, {}, body.to_json))
load_keyed_fulfillment_order(keyed_fulfillment_orders, 'original_fulfillment_order')
keyed_fulfillment_orders
end

def accept_fulfillment_request(message: nil)
body = {
fulfillment_request: {
message: message
}
}
load_attributes_from_response(post('fulfillment_request/accept', {}, body.to_json))
end

def reject_fulfillment_request(message: nil)
body = {
fulfillment_request: {
message: message
}
}
load_attributes_from_response(post('fulfillment_request/reject', {}, body.to_json))
end

def request_cancellation(message: nil)
body = {
cancellation_request: {
message: message
}
}
load_attributes_from_response(post(:cancellation_request, {}, body.to_json))
end

def accept_cancellation_request(message: nil)
body = {
cancellation_request: {
message: message
}
}
load_attributes_from_response(post('cancellation_request/accept', {}, body.to_json))
end

def reject_cancellation_request(message: nil)
body = {
cancellation_request: {
message: message
}
}
load_attributes_from_response(post('cancellation_request/reject', {}, body.to_json))
end

private

def load_keyed_fulfillment_order(keyed_fulfillment_orders, key)
Expand Down
229 changes: 229 additions & 0 deletions test/fulfillment_order_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,234 @@ def setup
assert_equal 'incomplete', fulfillment_order.status
end
end

context "#request_fulfillment" do
should "make a fulfillment request for a fulfillment order including unsubmitted" do
fake_original_fulfillment_order = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
fake_original_fulfillment_order['status'] = 'closed'
fake_submitted_fulfillment_order = fake_original_fulfillment_order.clone
fake_submitted_fulfillment_order['id'] = 2
fake_submitted_fulfillment_order['status'] = 'open'
fake_submitted_fulfillment_order['request_status'] = 'submitted'
fake_unsubmitted_fulfillment_order = fake_original_fulfillment_order.clone
fake_unsubmitted_fulfillment_order['id'] = 3
fake_unsubmitted_fulfillment_order['status'] = 'open'
fake_unsubmitted_fulfillment_order['request_status'] = 'unsubmitted'
body = {
original_fulfillment_order: fake_original_fulfillment_order,
submitted_fulfillment_order: fake_submitted_fulfillment_order,
unsubmitted_fulfillment_order: fake_unsubmitted_fulfillment_order
}
request_body = {
fulfillment_request: {
fulfillment_order_line_items: [
{ id: 1, quantity: 1 }
],
message: 'Fulfill this FO, please.'
}
}
fake "fulfillment_orders/519788021/fulfillment_request", :method => :post,
:request_body => ActiveSupport::JSON.encode(request_body),
: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."
}
response_fulfillment_orders = fulfillment_order.request_fulfillment(params)

assert_equal 'closed', fulfillment_order.status
assert_equal 3, response_fulfillment_orders.size

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

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

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

should "make a fulfillment request for a fulfillment order excluding unsubmitted" do
fake_original_fulfillment_order = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
fake_original_fulfillment_order['status'] = 'closed'
fake_submitted_fulfillment_order = fake_original_fulfillment_order.clone
fake_submitted_fulfillment_order['id'] = 2
fake_submitted_fulfillment_order['status'] = 'open'
fake_submitted_fulfillment_order['request_status'] = 'submitted'
body = {
original_fulfillment_order: fake_original_fulfillment_order,
submitted_fulfillment_order: fake_submitted_fulfillment_order,
unsubmitted_fulfillment_order: nil,
}
request_body = {
fulfillment_request: {
fulfillment_order_line_items: [
{ id: 1, quantity: 1 }
],
message: 'Fulfill this FO, please.'
}
}
fake "fulfillment_orders/519788021/fulfillment_request", :method => :post,
:request_body => ActiveSupport::JSON.encode(request_body),
: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."
}
response_fulfillment_orders = fulfillment_order.request_fulfillment(params)

assert_equal 'closed', fulfillment_order.status
assert_equal 3, response_fulfillment_orders.size

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

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

assert_nil response_fulfillment_orders['unsubmitted_fulfillment_order']
end
end
karmakaze marked this conversation as resolved.
Show resolved Hide resolved

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

message = "LGTM. Accept this FO fulfillment request"
request_body = {
'fulfillment_request' => {
'message' => message
}
}
fake_response = {
fulfillment_order: fulfillment_order.attributes.merge(status: 'in_progress', request_status: 'accepted')
}
fake "fulfillment_orders/519788021/fulfillment_request/accept", :method => :post,
:request_body => ActiveSupport::JSON.encode(request_body),
:body => ActiveSupport::JSON.encode(fake_response)

accepted = fulfillment_order.accept_fulfillment_request(message: message)

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

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

message = "LBTM. Reject this FO fulfillment request"
request_body = {
'fulfillment_request' => {
'message' => message
}
}
fake_response = {
fulfillment_order: fulfillment_order.attributes.merge(status: 'open', request_status: 'rejected')
}
fake "fulfillment_orders/519788021/fulfillment_request/reject", :method => :post,
:request_body => ActiveSupport::JSON.encode(request_body),
:body => ActiveSupport::JSON.encode(fake_response)

rejected = fulfillment_order.reject_fulfillment_request(message: message)

assert_equal true, rejected
assert_equal 'open', fulfillment_order.status
assert_equal 'rejected', fulfillment_order.request_status
end
end

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

message = "Cancelling this please."
request_body = {
'cancellation_request' => {
'message' => message
}
}
cancelling = ActiveSupport::JSON.decode(load_fixture('fulfillment_order'))
cancelling['status'] = 'in_progress'
cancelling['request_status'] = 'cancellation_requested'
fake "fulfillment_orders/519788021/cancellation_request", :method => :post,
:request_body => ActiveSupport::JSON.encode(request_body),
:body => ActiveSupport::JSON.encode({ fulfillment_order: cancelling })

cancelled = fulfillment_order.request_cancellation(message: "Cancelling this please.")

assert_equal true, cancelled
assert_equal 'in_progress', fulfillment_order.status
assert_equal 'cancellation_requested', fulfillment_order.request_status
end
end

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

message = 'Already in-progress. Reject this FO cancellation request'
request_body = {
'cancellation_request' => {
'message' => message
}
}
fake_response = {
fulfillment_order: fulfillment_order.attributes.merge(status: 'cancelled',
request_status: 'cancellation_accepted')
}
fake "fulfillment_orders/519788021/cancellation_request/accept", :method => :post,
:request_body => ActiveSupport::JSON.encode(request_body),
:body => ActiveSupport::JSON.encode(fake_response)

accepted = fulfillment_order.accept_cancellation_request(message: message)

assert_equal true, accepted
assert_equal 'cancelled', fulfillment_order.status
assert_equal 'cancellation_accepted', fulfillment_order.request_status
end
end

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

message = "Already in-progress. Reject this FO cancellation request"
request_body = {
'cancellation_request' => {
'message' => message
}
}
fake_response = {
fulfillment_order: fulfillment_order.attributes.merge(status: 'in_progress',
request_status: 'cancellation_rejected')
}
fake "fulfillment_orders/519788021/cancellation_request/reject", :method => :post,
:request_body => request_body,
:body => ActiveSupport::JSON.encode(fake_response)

rejected = fulfillment_order.reject_cancellation_request(message: message)

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

end
end