mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-04-02 06:51:40 +00:00
Move catalog loading to where it's needed
This commit is contained in:
@@ -75,13 +75,7 @@ class BackorderJob < ApplicationJob
|
||||
end
|
||||
|
||||
def self.load_broker(user, urls)
|
||||
FdcOfferBroker.new(load_catalog(user, urls))
|
||||
end
|
||||
|
||||
def self.load_catalog(user, urls)
|
||||
api = DfcRequest.new(user)
|
||||
catalog_json = api.call(urls.catalog_url)
|
||||
DfcIo.import(catalog_json)
|
||||
FdcOfferBroker.new(user, urls)
|
||||
end
|
||||
|
||||
def self.place_order(user, order, orderer, backorder)
|
||||
|
||||
@@ -31,7 +31,7 @@ class CompleteBackorderJob < ApplicationJob
|
||||
# But stock levels could also have been adjusted manually. So we review all
|
||||
# quantities before finalising the order.
|
||||
def adjust_quantities(user, order, urls, variants)
|
||||
broker = FdcOfferBroker.new(BackorderJob.load_catalog(user, urls))
|
||||
broker = FdcOfferBroker.new(user, urls)
|
||||
|
||||
order.lines.each do |line|
|
||||
line.quantity = line.quantity.to_i
|
||||
|
||||
@@ -6,8 +6,19 @@ class FdcOfferBroker
|
||||
Solution = Struct.new(:product, :factor, :offer)
|
||||
RetailSolution = Struct.new(:retail_product_id, :factor)
|
||||
|
||||
def initialize(catalog)
|
||||
@catalog = catalog
|
||||
def self.load_catalog(user, urls)
|
||||
api = DfcRequest.new(user)
|
||||
catalog_json = api.call(urls.catalog_url)
|
||||
DfcIo.import(catalog_json)
|
||||
end
|
||||
|
||||
def initialize(user, urls)
|
||||
@user = user
|
||||
@urls = urls
|
||||
end
|
||||
|
||||
def catalog
|
||||
@catalog ||= self.class.load_catalog(@user, @urls)
|
||||
end
|
||||
|
||||
def best_offer(product_id)
|
||||
@@ -61,7 +72,7 @@ class FdcOfferBroker
|
||||
end
|
||||
|
||||
def catalog_item(id)
|
||||
@catalog_by_id ||= @catalog.index_by(&:semanticId)
|
||||
@catalog_by_id ||= catalog.index_by(&:semanticId)
|
||||
@catalog_by_id[id]
|
||||
end
|
||||
|
||||
@@ -71,7 +82,7 @@ class FdcOfferBroker
|
||||
end
|
||||
|
||||
def production_flows
|
||||
@production_flows ||= @catalog.select do |i|
|
||||
@production_flows ||= catalog.select do |i|
|
||||
i.semanticType == "dfc-b:AsPlannedProductionFlow"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,7 +4,9 @@ require 'spec_helper'
|
||||
|
||||
RSpec.describe CompleteBackorderJob do
|
||||
let(:user) { build(:testdfc_user) }
|
||||
let(:catalog) { BackorderJob.load_catalog(user, urls) }
|
||||
let(:catalog) {
|
||||
VCR.use_cassette(:fdc_catalog) { FdcOfferBroker.load_catalog(user, urls) }
|
||||
}
|
||||
let(:urls) { FdcUrlBuilder.new(product_link) }
|
||||
let(:product_link) {
|
||||
"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts/44519466467635"
|
||||
@@ -19,7 +21,7 @@ RSpec.describe CompleteBackorderJob do
|
||||
let(:orderer) { FdcBackorderer.new(user, urls) }
|
||||
let(:order) {
|
||||
backorder = orderer.find_or_build_order(ofn_order)
|
||||
broker = FdcOfferBroker.new(catalog)
|
||||
broker = FdcOfferBroker.new(user, urls)
|
||||
offer = broker.best_offer(retail_product.semanticId).offer
|
||||
line = orderer.find_or_build_order_line(backorder, offer)
|
||||
line.quantity = 3
|
||||
|
||||
@@ -33,9 +33,9 @@ RSpec.describe FdcBackorderer do
|
||||
expect(backorder.lines).to eq []
|
||||
|
||||
# Add items and place the new order:
|
||||
catalog = BackorderJob.load_catalog(order.distributor.owner, urls)
|
||||
catalog = FdcOfferBroker.load_catalog(order.distributor.owner, urls)
|
||||
product = catalog.find { |i| i.semanticType == "dfc-b:SuppliedProduct" }
|
||||
offer = FdcOfferBroker.new(nil).offer_of(product)
|
||||
offer = FdcOfferBroker.new(nil, nil).offer_of(product)
|
||||
line = subject.find_or_build_order_line(backorder, offer)
|
||||
line.quantity = 3
|
||||
placed_order = subject.send_order(backorder)
|
||||
@@ -71,7 +71,7 @@ RSpec.describe FdcBackorderer do
|
||||
|
||||
describe "#find_or_build_order_line" do
|
||||
it "add quantity to an existing line item", vcr: true do
|
||||
catalog = BackorderJob.load_catalog(order.distributor.owner, urls)
|
||||
catalog = FdcOfferBroker.load_catalog(order.distributor.owner, urls)
|
||||
backorder = subject.find_or_build_order(order)
|
||||
existing_line = backorder.lines[0]
|
||||
|
||||
@@ -83,7 +83,7 @@ RSpec.describe FdcBackorderer do
|
||||
catalog_product = catalog.find do |i|
|
||||
i.semanticId == ordered_product.semanticId
|
||||
end
|
||||
catalog_offer = FdcOfferBroker.new(nil).offer_of(catalog_product)
|
||||
catalog_offer = FdcOfferBroker.new(nil, nil).offer_of(catalog_product)
|
||||
|
||||
# The API response is missing this connection:
|
||||
catalog_offer.offeredItem = catalog_product
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
require 'spec_helper'
|
||||
|
||||
RSpec.describe FdcOfferBroker do
|
||||
subject { FdcOfferBroker.new(catalog) }
|
||||
subject { FdcOfferBroker.new(user, urls) }
|
||||
let(:catalog) {
|
||||
VCR.use_cassette(:fdc_catalog) { BackorderJob.load_catalog(user, urls) }
|
||||
VCR.use_cassette(:fdc_catalog) { subject.catalog }
|
||||
}
|
||||
let(:urls) { FdcUrlBuilder.new(product_link) }
|
||||
let(:product_link) {
|
||||
|
||||
Reference in New Issue
Block a user