DRY DFC catalog logic for re-use

This commit is contained in:
Maikel Linke
2024-12-18 15:34:42 +11:00
parent 7e3baabd23
commit ef08ae49fe
4 changed files with 40 additions and 6 deletions

View File

@@ -20,12 +20,11 @@ module Admin
catalog_url = params.require(:catalog_url)
broker = FdcOfferBroker.new(spree_current_user, catalog_url)
catalog = DfcCatalog.new(broker.catalog)
# * First step: import all products for given enterprise.
# * Second step: render table and let user decide which ones to import.
imported = broker.catalog.map do |subject|
next unless subject.is_a? DataFoodConsortium::Connector::SuppliedProduct
imported = catalog.products.map do |subject|
adjust_to_wholesale_price(broker, subject)
existing_variant = enterprise.supplied_variants.linked_to(subject.semanticId)

View File

@@ -62,9 +62,7 @@ class StockSyncJob < ApplicationJob
json_catalog = DfcRequest.new(user).call(catalog_id)
graph = DfcIo.import(json_catalog)
graph.select do |subject|
subject.is_a? DataFoodConsortium::Connector::SuppliedProduct
end
DfcCatalog.new(graph).products
end
def linked_variants(enterprises, product_ids)

View File

@@ -0,0 +1,13 @@
# frozen_string_literal: true
class DfcCatalog
def initialize(graph)
@graph = graph
end
def products
@products ||= @graph.select do |subject|
subject.is_a? DataFoodConsortium::Connector::SuppliedProduct
end
end
end

View File

@@ -0,0 +1,24 @@
# frozen_string_literal: true
require_relative "../spec_helper"
RSpec.describe DfcCatalog do
subject(:catalog) { DfcCatalog.new(fdc_catalog_graph) }
let(:fdc_catalog_graph) {
VCR.use_cassette(:fdc_catalog) { broker.catalog }
}
let(:broker) { FdcOfferBroker.new(user, catalog_url) }
let(:user) { build(:testdfc_user) }
let(:catalog_url) {
"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts"
}
describe "#products" do
let(:products) { catalog.products }
it "returns only products" do
expect(products.count).to eq 4
expect(products.map(&:semanticType).uniq).to eq ["dfc-b:SuppliedProduct"]
end
end
end