@@ 20,6 20,10 @@ class SyncAssignedOrdersJob < ApplicationJob
begin
shop.with_shopify_session do
+ ShopifyAPI::AssignedFulfillmentOrder.all.each do |fulfillment_order|
+ request_fulfillment(shop, fulfillment_order)
+ end
+
ShopifyAPI::AssignedFulfillmentOrder.all(assignment_status: :fulfillment_requested).each do |fulfillment_order|
sync_fulfillment_order(shop, fulfillment_order)
end
@@ 32,6 36,21 @@ class SyncAssignedOrdersJob < ApplicationJob
end
end
+ def request_fulfillment(shop, fulfillment_order)
+ return unless fulfillment_order.status == "open"
+ return unless fulfillment_order.request_status == "unsubmitted"
+
+ shopify_service = ShopifyAPI::FulfillmentService.all.find { |service|
+ service.location_id == fulfillment_order.assigned_location_id
+ }
+ service = FulfillmentService.find_by!(shopify_id: shopify_service&.id)
+ return unless service.shop_id == shop.id
+
+ ShopifyAPI::FulfillmentRequest.new.tap { |r|
+ r.fulfillment_order_id = fulfillment_order.id
+ }.save
+ end
+
def sync_fulfillment_order(shop, fulfillment_order)
existing = WHIPLASH_HTTP.get("orders/originator/#{fulfillment_order.id}", nil, "X-API-KEY" => shop.whiplash_api_key)
return unless existing.status == 404