mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Complete order 4 hours after order cycle closed
This commit is contained in:
@@ -5,6 +5,11 @@ 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
|
||||
# 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
|
||||
|
||||
queue_as :default
|
||||
|
||||
def self.check_stock(order)
|
||||
@@ -27,7 +32,8 @@ class BackorderJob < ApplicationJob
|
||||
end
|
||||
|
||||
def self.place_backorder(order, linked_variants)
|
||||
orderer = FdcBackorderer.new(order.distributor.owner)
|
||||
user = order.distributor.owner
|
||||
orderer = FdcBackorderer.new(user)
|
||||
backorder = orderer.find_or_build_order(order)
|
||||
catalog = load_catalog(order.distributor.owner)
|
||||
|
||||
@@ -39,7 +45,13 @@ class BackorderJob < ApplicationJob
|
||||
line.quantity = line.quantity.to_i + needed_quantity
|
||||
end
|
||||
|
||||
orderer.send_order(backorder)
|
||||
placed_order = orderer.send_order(backorder)
|
||||
|
||||
if orderer.new?(backorder)
|
||||
wait_until = order.order_cycle.orders_close_at + SALE_SESSION_DELAY
|
||||
CompleteBackorderJob.set(wait_until:)
|
||||
.perform_later(user, placed_order.semanticId)
|
||||
end
|
||||
|
||||
# Once we have transformations and know the quantities in bulk products
|
||||
# we will need to increase on_hand by the ordered quantity.
|
||||
|
||||
11
app/jobs/complete_backorder_job.rb
Normal file
11
app/jobs/complete_backorder_job.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# After an order cycle closed, we need to finalise open draft orders placed
|
||||
# to replenish stock.
|
||||
class CompleteBackorderJob < ApplicationJob
|
||||
def perform(user, order_id)
|
||||
# TODO: review our stock levels and adjust quantities if we got surplus.
|
||||
# This can happen when orders are cancelled and products restocked.
|
||||
FdcBackorderer.new(user).complete_order(order_id)
|
||||
end
|
||||
end
|
||||
@@ -97,7 +97,7 @@ class FdcBackorderer
|
||||
|
||||
api = DfcRequest.new(user)
|
||||
|
||||
method = if backorder.semanticId == FDC_ORDERS_URL
|
||||
method = if new?(backorder)
|
||||
:post # -> create
|
||||
else
|
||||
:put # -> update
|
||||
@@ -115,6 +115,10 @@ class FdcBackorderer
|
||||
send_order(backorder)
|
||||
end
|
||||
|
||||
def new?(order)
|
||||
order.semanticId == FDC_ORDERS_URL
|
||||
end
|
||||
|
||||
def build_sale_session(order)
|
||||
SaleSessionBuilder.build(order.order_cycle).tap do |session|
|
||||
session.semanticId = FDC_SALE_SESSION_URL
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -30,9 +30,15 @@ RSpec.describe BackorderJob do
|
||||
variant.semantic_links << SemanticLink.new(
|
||||
semantic_id: product_link
|
||||
)
|
||||
BackorderJob.check_stock(order)
|
||||
|
||||
expect {
|
||||
BackorderJob.check_stock(order)
|
||||
}.to enqueue_job CompleteBackorderJob
|
||||
|
||||
expect(variant.on_hand).to eq 0
|
||||
|
||||
# Clean up after ourselves:
|
||||
perform_enqueued_jobs(only: CompleteBackorderJob)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user