Move catalog loading to where it's needed

This commit is contained in:
Maikel Linke
2024-09-17 11:56:55 +10:00
parent 070b93c531
commit 7f62b49da5
6 changed files with 27 additions and 20 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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) {