From 31edcacbe5ef00d7b655566c30af47136a8d29cc Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Mon, 5 Dec 2022 14:09:13 -0500 Subject: [PATCH] Auto-request fulfillment for orders bound to us --- app/jobs/sync_assigned_orders_job.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app/jobs/sync_assigned_orders_job.rb b/app/jobs/sync_assigned_orders_job.rb index 30adc98..caa43e9 100644 --- a/app/jobs/sync_assigned_orders_job.rb +++ b/app/jobs/sync_assigned_orders_job.rb @@ -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 -- 2.38.5