Skip to content

Commit

Permalink
Remove old, replaced backorder lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
mkllnk committed Dec 17, 2024
1 parent 0f706a9 commit 60b62d4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 280 deletions.
41 changes: 1 addition & 40 deletions app/services/fdc_backorderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def build_new_order(ofn_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
lookup_open_order(ofn_order)
end

def lookup_open_order(ofn_order)
Expand All @@ -43,45 +43,6 @@ def lookup_open_order(ofn_order)
&.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 == order_status.HELD
end

return if open_orders.blank?

# If there are multiple open orders, we don't know which one to choose.
# We want the order we placed for the same distributor in the same order
# cycle before. So here are some assumptions for this to work:
#
# * We see only orders for our distributor. The endpoint URL contains the
# the distributor name and is currently hardcoded.
# * There's only one open order cycle at a time. Otherwise we may select
# an order of an old order cycle.
# * Orders are finalised when the order cycle closes. So _Held_ orders
# always belong to an open order cycle.
# * We see only our own orders. This assumption is wrong. The Shopify
# integration places held orders as well and they are visible to us.
#
# Unfortunately, the endpoint doesn't tell who placed the order.
# TODO: We need to remember the link to the order locally.
# Or the API is updated to include the orderer.
#
# For now, we just guess:
open_orders.last.tap do |order|
# The DFC Connector doesn't recognise status values properly yet.
# So we are overriding the value with something that can be exported.
order.orderStatus = "dfc-v:Held"
end
end

def find_order(semantic_id)
find_subject(import(semantic_id), "dfc-b:Order")
end
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

42 changes: 16 additions & 26 deletions spec/services/fdc_backorderer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

it "creates, finds and updates orders", vcr: true do
# This test case contains a full order life cycle.
# It assumes that there's no open order yet to start with.
# 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 = create(:order_cycle, distributors: [order.distributor])
Expand All @@ -44,20 +42,16 @@
# That process seems to be async.
sleep 10 if VCR.current_cassette.recording?

# Now we can find the open order:
found_backorder = subject.find_or_build_order(order)
expect(found_backorder.semanticId).to eq placed_order.semanticId
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:
# Without a stored semantic link, it can't look it up:
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
expect(found_backorder.lines.count).to eq 1
expect(found_backorder.lines[0].quantity.to_i).to eq 3

# And close the order again:
subject.complete_order(placed_order)
Expand All @@ -82,28 +76,24 @@
it "add quantity to an existing line item", vcr: true do
catalog = FdcOfferBroker.load_catalog(order.distributor.owner, urls.catalog_url)
backorder = subject.find_or_build_order(order)
existing_line = backorder.lines[0]

# The FDC API returns different ids for the same offer.
# In order to test that we can still match it, we are retrieving
# the catalog offer here which is different to the offer on the
# existing order line.
ordered_product = existing_line.offer.offeredItem
catalog_product = catalog.find do |i|
i.semanticId == ordered_product.semanticId
end
catalog_offer = FdcOfferBroker.new(nil, nil).offer_of(catalog_product)

# The API response is missing this connection:
catalog_offer.offeredItem = catalog_product
expect(backorder.lines.count).to eq 0

# Add new item to the new order:
catalog = FdcOfferBroker.load_catalog(order.distributor.owner, urls.catalog_url)
product = catalog.find { |i| i.semanticType == "dfc-b:SuppliedProduct" }
offer = FdcOfferBroker.new(nil, nil).offer_of(product)
line = subject.find_or_build_order_line(backorder, offer)

# Just confirm that we got good test data from the API:
expect(backorder.semanticId).to match %r{^https.*/[0-9]+$}
expect(backorder.lines.count).to eq 1
expect(backorder.lines[0]).to eq line

found_line = subject.find_or_build_order_line(backorder, catalog_offer)
expect {
subject.find_or_build_order_line(backorder, offer)
}.not_to change { backorder.lines.count }

expect(found_line).to eq existing_line
found_line = subject.find_or_build_order_line(backorder, offer)
expect(found_line).to eq line
end
end

Expand Down

0 comments on commit 60b62d4

Please sign in to comment.