mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-26 01:33:22 +00:00
Move catalog_item builder into the right module
This commit is contained in:
@@ -22,7 +22,7 @@ module DfcProvider
|
||||
end
|
||||
|
||||
def show
|
||||
catalog_item = DfcBuilder.catalog_item(variant)
|
||||
catalog_item = CatalogItemBuilder.catalog_item(variant)
|
||||
offers = catalog_item.offers
|
||||
render json: DfcIo.export(catalog_item, *offers)
|
||||
end
|
||||
|
||||
@@ -1,6 +1,21 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class CatalogItemBuilder < DfcBuilder
|
||||
def self.catalog_item(variant)
|
||||
id = urls.enterprise_catalog_item_url(
|
||||
enterprise_id: variant.supplier_id,
|
||||
id: variant.id,
|
||||
)
|
||||
product = SuppliedProductBuilder.supplied_product(variant)
|
||||
|
||||
DataFoodConsortium::Connector::CatalogItem.new(
|
||||
id, product:,
|
||||
sku: variant.sku,
|
||||
stockLimitation: stock_limitation(variant),
|
||||
offers: [OfferBuilder.build(variant)],
|
||||
)
|
||||
end
|
||||
|
||||
def self.apply_stock(item, variant)
|
||||
limit = item&.stockLimitation
|
||||
|
||||
|
||||
@@ -1,21 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class DfcBuilder
|
||||
def self.catalog_item(variant)
|
||||
id = urls.enterprise_catalog_item_url(
|
||||
enterprise_id: variant.supplier_id,
|
||||
id: variant.id,
|
||||
)
|
||||
product = SuppliedProductBuilder.supplied_product(variant)
|
||||
|
||||
DataFoodConsortium::Connector::CatalogItem.new(
|
||||
id, product:,
|
||||
sku: variant.sku,
|
||||
stockLimitation: stock_limitation(variant),
|
||||
offers: [OfferBuilder.build(variant)],
|
||||
)
|
||||
end
|
||||
|
||||
# The DFC sees "empty" stock as unlimited.
|
||||
# http://static.datafoodconsortium.org/conception/DFC%20-%20Business%20rules.pdf
|
||||
def self.stock_limitation(variant)
|
||||
|
||||
@@ -6,7 +6,7 @@ class EnterpriseBuilder < DfcBuilder
|
||||
# in the DFC standard.
|
||||
|
||||
variants = enterprise.supplied_variants.to_a
|
||||
catalog_items = variants.map(&method(:catalog_item))
|
||||
catalog_items = variants.map(&CatalogItemBuilder.method(:catalog_item))
|
||||
supplied_products = catalog_items.map(&:product)
|
||||
address = AddressBuilder.address(enterprise.address)
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
require_relative "../spec_helper"
|
||||
|
||||
RSpec.describe DfcBuilder do
|
||||
RSpec.describe CatalogItemBuilder do
|
||||
let(:variant) { build(:variant) }
|
||||
|
||||
describe ".catalog_item" do
|
||||
@@ -10,7 +10,7 @@ RSpec.describe DfcBuilder do
|
||||
variant.id = 5
|
||||
variant.supplier_id = 7
|
||||
|
||||
item = DfcBuilder.catalog_item(variant)
|
||||
item = CatalogItemBuilder.catalog_item(variant)
|
||||
|
||||
expect(item.semanticId).to eq(
|
||||
"http://test.host/api/dfc/enterprises/7/catalog_items/5"
|
||||
@@ -21,7 +21,7 @@ RSpec.describe DfcBuilder do
|
||||
variant.id = 5
|
||||
variant.supplier_id = 7
|
||||
|
||||
item = DfcBuilder.catalog_item(variant)
|
||||
item = CatalogItemBuilder.catalog_item(variant)
|
||||
|
||||
expect(item.product.semanticId).to eq(
|
||||
"http://test.host/api/dfc/enterprises/7/supplied_products/5"
|
||||
|
||||
Reference in New Issue
Block a user