Skip to content

Commit

Permalink
Lookup backorder for updates with saved link
Browse files Browse the repository at this point in the history
  • Loading branch information
mkllnk committed Nov 15, 2024
1 parent c3cc681 commit 4945f4d
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 95 deletions.
2 changes: 1 addition & 1 deletion app/jobs/amend_backorder_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def amend_backorder(order)
urls = FdcUrlBuilder.new(reference_link)
orderer = FdcBackorderer.new(user, urls)

backorder = orderer.find_open_order
backorder = orderer.find_open_order(order)

variants = order_cycle.variants_distributed_by(distributor)
adjust_quantities(order_cycle, user, backorder, urls, variants)
Expand Down
1 change: 1 addition & 0 deletions app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def states
has_one :exchange, ->(order) {
outgoing.to_enterprise(order.distributor)
}, through: :order_cycle, source: :exchanges
has_many :semantic_links, through: :exchange

belongs_to :distributor, class_name: 'Enterprise', optional: true
belongs_to :customer, optional: true
Expand Down
34 changes: 32 additions & 2 deletions app/services/fdc_backorderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(user, urls)
end

def find_or_build_order(ofn_order)
find_open_order || build_new_order(ofn_order)
find_open_order(ofn_order) || build_new_order(ofn_order)
end

def build_new_order(ofn_order)
Expand All @@ -19,7 +19,37 @@ def build_new_order(ofn_order)
end
end

def find_open_order
# Try the new method and fall back to old method.
def find_open_order(ofn_order)
lookup_open_order(ofn_order) || find_last_open_order
end

def lookup_open_order(ofn_order)
# There should be only one link at the moment but we may support
# ordering from multiple suppliers one day.
semantic_ids = ofn_order.semantic_links.pluck(:semantic_id)

semantic_ids.lazy
# Make sure we select an order from the right supplier:
.select { |id| id.starts_with?(urls.orders_url) }
# Fetch the order from the remote DFC server, lazily:
.map { |id| find_order(id) }
.compact
# Just in case someone completed the order without updating our database:
.select { |o| o.orderStatus[:path] == "Held" }
.first
# The DFC Connector doesn't recognise status values properly yet.
# So we are overriding the value with something that can be exported.
&.tap { |o| o.orderStatus = "dfc-v:Held" }
end

# DEPRECATED
#
# We now store links to orders we placed. So we don't need to search
# through all orders and pick a random open one.
# But for compatibility with currently open order cycles that don't have
# a stored link yet, we keep this method as well.
def find_last_open_order
graph = import(urls.orders_url)
open_orders = graph&.select do |o|
o.semanticType == "dfc-b:Order" && o.orderStatus[:path] == "Held"
Expand Down

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
let(:order) { build(:order, user:) }

it { is_expected.to have_one :exchange }
it { is_expected.to have_many :semantic_links }

describe "#errors" do
it "provides friendly error messages" do
Expand Down
13 changes: 11 additions & 2 deletions spec/services/fdc_backorderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# After closing the order at the end, the test can be repeated live again.

# Build a new order when no open one is found:
order.order_cycle = build(:order_cycle)
order.order_cycle = create(:order_cycle, distributors: [order.distributor])
backorder = subject.find_or_build_order(order)
expect(backorder.semanticId).to eq urls.orders_url
expect(backorder.lines).to eq []
Expand All @@ -50,10 +50,19 @@
expect(found_backorder.lines.count).to eq 1
expect(found_backorder.lines[0].quantity.to_i).to eq 3

# Without a stored semantic link, it can't look it up directly though:
found_backorder = subject.lookup_open_order(order)
expect(found_backorder).to eq nil

# But with a semantic link, it works:
order.exchange.semantic_links.create!(semantic_id: placed_order.semanticId)
found_backorder = subject.lookup_open_order(order)
expect(found_backorder.semanticId).to eq placed_order.semanticId

# And close the order again:
subject.complete_order(placed_order)
remaining_open_order = subject.find_or_build_order(order)
expect(remaining_open_order.semanticId).not_to eq placed_order.semanticId
expect(remaining_open_order.semanticId).to eq urls.orders_url
end

describe "#find_or_build_order" do
Expand Down

0 comments on commit 4945f4d

Please sign in to comment.