mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
DRY DFC catalog logic for re-use
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
13
engines/dfc_provider/app/services/dfc_catalog.rb
Normal file
13
engines/dfc_provider/app/services/dfc_catalog.rb
Normal 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
|
||||
24
engines/dfc_provider/spec/services/dfc_catalog_spec.rb
Normal file
24
engines/dfc_provider/spec/services/dfc_catalog_spec.rb
Normal 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
|
||||
Reference in New Issue
Block a user