~singpolyma/whiplash-mboa

31edcacbe5ef00d7b655566c30af47136a8d29cc — Stephen Paul Weber 11 months ago da3be64
Auto-request fulfillment for orders bound to us
1 files changed, 19 insertions(+), 0 deletions(-)

M app/jobs/sync_assigned_orders_job.rb
M app/jobs/sync_assigned_orders_job.rb => app/jobs/sync_assigned_orders_job.rb +19 -0
@@ 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