Apply 4 hour completion delay only to one enterprise

This commit is contained in:
Maikel Linke
2024-09-13 14:53:20 +10:00
parent 5ef85aef3e
commit 2eec4c73bf
2 changed files with 29 additions and 4 deletions

View File

@@ -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

View File

@@ -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