From 2eec4c73bfdada9e80b786cdf51e2a0ff2262e58 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 13 Sep 2024 14:53:20 +1000 Subject: [PATCH] Apply 4 hour completion delay only to one enterprise --- app/jobs/backorder_job.rb | 10 +++++++--- spec/jobs/backorder_job_spec.rb | 23 ++++++++++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/app/jobs/backorder_job.rb b/app/jobs/backorder_job.rb index 6a82d5d86b..401f824d6a 100644 --- a/app/jobs/backorder_job.rb +++ b/app/jobs/backorder_job.rb @@ -5,10 +5,13 @@ class BackorderJob < ApplicationJob FDC_CATALOG_URL = "#{FDC_BASE_URL}/SuppliedProducts".freeze FDC_ORDERS_URL = "#{FDC_BASE_URL}/Orders".freeze - # In the current FDC project, the shop wants to review and adjust orders + # In the current FDC project, one shop wants to review and adjust orders # before finalising. They also run a market stall and need to adjust stock # levels after the market. This should be done within four hours. - SALE_SESSION_DELAY = 4.hours + SALE_SESSION_DELAYS = { + # https://openfoodnetwork.org.uk/handleyfarm/shop + "https://openfoodnetwork.org.uk/api/dfc/enterprises/203468" => 4.hours, + }.freeze queue_as :default @@ -85,7 +88,8 @@ class BackorderJob < ApplicationJob return unless orderer.new?(backorder) - wait_until = order.order_cycle.orders_close_at + SALE_SESSION_DELAY + delay = SALE_SESSION_DELAYS.fetch(backorder.client, 1.minute) + wait_until = order.order_cycle.orders_close_at + delay CompleteBackorderJob.set(wait_until:) .perform_later( user, order.distributor, order.order_cycle, placed_order.semanticId diff --git a/spec/jobs/backorder_job_spec.rb b/spec/jobs/backorder_job_spec.rb index 67098c6761..b36dcfa359 100644 --- a/spec/jobs/backorder_job_spec.rb +++ b/spec/jobs/backorder_job_spec.rb @@ -29,6 +29,7 @@ RSpec.describe BackorderJob do distributors: [order.distributor], variants: [variant], ) + completion_time = order.order_cycle.orders_close_at + 1.minute variant.on_demand = true variant.on_hand = -3 variant.semantic_links << SemanticLink.new( @@ -37,7 +38,7 @@ RSpec.describe BackorderJob do expect { BackorderJob.check_stock(order) - }.to enqueue_job CompleteBackorderJob + }.to enqueue_job(CompleteBackorderJob).at(completion_time) # We ordered a case of 12 cans: -3 + 12 = 9 expect(variant.on_hand).to eq 9 @@ -46,4 +47,24 @@ RSpec.describe BackorderJob do perform_enqueued_jobs(only: CompleteBackorderJob) end end + + describe ".place_order" do + it "schedules backorder completion for specific enterprises" do + order.order_cycle = build( + :simple_order_cycle, + id: 1, + orders_close_at: Date.tomorrow.noon, + ) + completion_time = Date.tomorrow.noon + 4.hours + + orderer = FdcBackorderer.new(user) + backorder = orderer.build_new_order(order) + backorder.client = "https://openfoodnetwork.org.uk/api/dfc/enterprises/203468" + + expect(orderer).to receive(:send_order).and_return(backorder) + expect { + BackorderJob.place_order(user, order, orderer, backorder) + }.to enqueue_job(CompleteBackorderJob).at(completion_time) + end + end end