mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-26 01:33:22 +00:00
Remove old, replaced backorder lookup
This commit is contained in:
@@ -21,7 +21,7 @@ class FdcBackorderer
|
||||
|
||||
# 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)
|
||||
@@ -43,45 +43,6 @@ class FdcBackorderer
|
||||
&.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
|
||||
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -23,8 +23,6 @@ RSpec.describe FdcBackorderer do
|
||||
|
||||
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])
|
||||
@@ -44,13 +42,7 @@ RSpec.describe FdcBackorderer do
|
||||
# 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
|
||||
|
||||
@@ -58,6 +50,8 @@ RSpec.describe FdcBackorderer do
|
||||
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)
|
||||
@@ -82,28 +76,24 @@ RSpec.describe FdcBackorderer do
|
||||
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)
|
||||
expect(backorder.lines.count).to eq 0
|
||||
|
||||
# The API response is missing this connection:
|
||||
catalog_offer.offeredItem = catalog_product
|
||||
# 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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user