fulfillment_order.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. module ShopifyAPI
  2. class FulfillmentOrder < Base
  3. def self.find(scope, *args)
  4. if scope == :all
  5. order_id = args.first&.dig(:params, :order_id)
  6. raise ShopifyAPI::ValidationException, "'order_id' is required" if order_id.blank?
  7. order = ::ShopifyAPI::Order.new(id: order_id)
  8. order.fulfillment_orders(args.first[:params].except(:order_id))
  9. else
  10. super(scope, *args)
  11. end
  12. end
  13. def fulfillments(options = {})
  14. fulfillment_hashes = get(:fulfillments, options)
  15. fulfillment_hashes.map { |fulfillment_hash| Fulfillment.new(fulfillment_hash) }
  16. end
  17. def move(new_location_id:)
  18. body = {
  19. fulfillment_order: {
  20. new_location_id: new_location_id
  21. }
  22. }
  23. keyed_fulfillment_orders = keyed_fulfillment_orders_from_response(post(:move, {}, body.to_json))
  24. load_keyed_fulfillment_order(keyed_fulfillment_orders, 'original_fulfillment_order')
  25. keyed_fulfillment_orders
  26. end
  27. def cancel
  28. keyed_fulfillment_orders = keyed_fulfillment_orders_from_response(post(:cancel, {}, only_id))
  29. load_keyed_fulfillment_order(keyed_fulfillment_orders, 'fulfillment_order')
  30. keyed_fulfillment_orders
  31. end
  32. def close(message: nil)
  33. body = {
  34. fulfillment_order: {
  35. message: message
  36. }
  37. }
  38. load_attributes_from_response(post(:close, {}, body.to_json))
  39. end
  40. private
  41. def load_keyed_fulfillment_order(keyed_fulfillment_orders, key)
  42. if keyed_fulfillment_orders[key]&.attributes
  43. load(keyed_fulfillment_orders[key].attributes, false, true)
  44. end
  45. end
  46. def keyed_fulfillment_orders_from_response(response)
  47. return load_attributes_from_response(response) if response.code != '200'
  48. keyed_fulfillment_orders = ActiveSupport::JSON.decode(response.body)
  49. keyed_fulfillment_orders.transform_values do |fulfillment_order_attributes|
  50. FulfillmentOrder.new(fulfillment_order_attributes) if fulfillment_order_attributes
  51. end
  52. end
  53. end
  54. end