mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-10 23:07:47 +00:00
Fix DFC engine
This commit is contained in:
@@ -20,8 +20,12 @@ module DfcProvider
|
||||
)
|
||||
product = variant.product
|
||||
|
||||
if variant.new_record?
|
||||
variant.supplier = current_enterprise
|
||||
variant.save!
|
||||
end
|
||||
|
||||
product.save! if product.new_record?
|
||||
variant.save! if variant.new_record?
|
||||
|
||||
supplied_product = SuppliedProductBuilder.supplied_product(variant)
|
||||
render json: DfcIo.export(supplied_product)
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
class DfcBuilder
|
||||
def self.catalog_item(variant)
|
||||
id = urls.enterprise_catalog_item_url(
|
||||
enterprise_id: variant.product.supplier_id,
|
||||
enterprise_id: variant.supplier_id,
|
||||
id: variant.id,
|
||||
)
|
||||
product = SuppliedProductBuilder.supplied_product(variant)
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
class OfferBuilder < DfcBuilder
|
||||
def self.build(variant)
|
||||
id = urls.enterprise_offer_url(
|
||||
enterprise_id: variant.product.supplier_id,
|
||||
enterprise_id: variant.supplier_id,
|
||||
id: variant.id,
|
||||
)
|
||||
|
||||
|
||||
@@ -3,11 +3,11 @@
|
||||
class SuppliedProductBuilder < DfcBuilder
|
||||
def self.supplied_product(variant)
|
||||
id = urls.enterprise_supplied_product_url(
|
||||
enterprise_id: variant.product.supplier_id,
|
||||
enterprise_id: variant.supplier_id,
|
||||
id: variant.id,
|
||||
)
|
||||
product_uri = urls.enterprise_url(
|
||||
variant.product.supplier_id,
|
||||
variant.supplier_id,
|
||||
spree_product_id: variant.product_id
|
||||
)
|
||||
|
||||
@@ -29,14 +29,15 @@ class SuppliedProductBuilder < DfcBuilder
|
||||
if product
|
||||
Spree::Variant.new(
|
||||
product:,
|
||||
supplier:,
|
||||
price: 0,
|
||||
).tap do |variant|
|
||||
apply(supplied_product, variant)
|
||||
end
|
||||
else
|
||||
product = import_product(supplied_product)
|
||||
product.supplier = supplier
|
||||
product.ensure_standard_variant
|
||||
product.variants.first.supplier = supplier
|
||||
product.variants.first
|
||||
end.tap do |variant|
|
||||
link = supplied_product.semanticId
|
||||
|
||||
@@ -15,7 +15,7 @@ RSpec.describe "CatalogItems", type: :request, swagger_doc: "dfc.yaml",
|
||||
let(:product) {
|
||||
create(
|
||||
:base_product,
|
||||
id: 90_000, supplier: enterprise, name: "Apple", description: "Red",
|
||||
id: 90_000, name: "Apple", description: "Red",
|
||||
variants: [variant],
|
||||
primary_taxon: non_local_vegetable
|
||||
)
|
||||
@@ -27,7 +27,7 @@ RSpec.describe "CatalogItems", type: :request, swagger_doc: "dfc.yaml",
|
||||
dfc_id: "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#non-local-vegetable"
|
||||
)
|
||||
}
|
||||
let(:variant) { build(:base_variant, id: 10_001, unit_value: 1, sku: "AR") }
|
||||
let(:variant) { build(:base_variant, id: 10_001, unit_value: 1, sku: "AR", supplier: enterprise) }
|
||||
|
||||
before { login_as user }
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ RSpec.describe "Enterprises", type: :request, swagger_doc: "dfc.yaml", rswag_aut
|
||||
let!(:product) {
|
||||
create(
|
||||
:product_with_image,
|
||||
id: 90_000, supplier: enterprise, name: "Apple", description: "Round",
|
||||
id: 90_000, name: "Apple", description: "Round",
|
||||
variants: [variant],
|
||||
primary_taxon: non_local_vegetable
|
||||
)
|
||||
@@ -39,7 +39,9 @@ RSpec.describe "Enterprises", type: :request, swagger_doc: "dfc.yaml", rswag_aut
|
||||
dfc_id: "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#non-local-vegetable"
|
||||
)
|
||||
}
|
||||
let(:variant) { build(:base_variant, id: 10_001, unit_value: 1, sku: "APP") }
|
||||
let(:variant) {
|
||||
build(:base_variant, id: 10_001, unit_value: 1, sku: "APP", supplier: enterprise)
|
||||
}
|
||||
|
||||
before { login_as user }
|
||||
|
||||
|
||||
@@ -9,11 +9,11 @@ RSpec.describe "Offers", type: :request, swagger_doc: "dfc.yaml", rswag_autodoc:
|
||||
create(
|
||||
:product,
|
||||
id: 90_000,
|
||||
supplier: enterprise, name: "Pesto", description: "Basil Pesto",
|
||||
name: "Pesto", description: "Basil Pesto",
|
||||
variants: [variant],
|
||||
)
|
||||
}
|
||||
let(:variant) { build(:base_variant, id: 10_001, unit_value: 1) }
|
||||
let(:variant) { build(:base_variant, id: 10_001, unit_value: 1, supplier: enterprise) }
|
||||
|
||||
before { login_as user }
|
||||
|
||||
|
||||
@@ -9,11 +9,13 @@ RSpec.describe "SuppliedProducts", type: :request, swagger_doc: "dfc.yaml", rswa
|
||||
create(
|
||||
:product_with_image,
|
||||
id: 90_000,
|
||||
supplier: enterprise, name: "Pesto", description: "Basil Pesto",
|
||||
name: "Pesto", description: "Basil Pesto",
|
||||
variants: [variant]
|
||||
)
|
||||
}
|
||||
let(:variant) { build(:base_variant, id: 10_001, unit_value: 1, primary_taxon: taxon) }
|
||||
let(:variant) {
|
||||
build(:base_variant, id: 10_001, unit_value: 1, primary_taxon: taxon, supplier: enterprise)
|
||||
}
|
||||
let(:taxon) {
|
||||
build(
|
||||
:taxon,
|
||||
|
||||
@@ -8,7 +8,7 @@ RSpec.describe DfcBuilder do
|
||||
describe ".catalog_item" do
|
||||
it "assigns a semantic id" do
|
||||
variant.id = 5
|
||||
variant.product.supplier_id = 7
|
||||
variant.supplier_id = 7
|
||||
|
||||
item = DfcBuilder.catalog_item(variant)
|
||||
|
||||
@@ -19,7 +19,7 @@ RSpec.describe DfcBuilder do
|
||||
|
||||
it "refers to a supplied product" do
|
||||
variant.id = 5
|
||||
variant.product.supplier_id = 7
|
||||
variant.supplier_id = 7
|
||||
|
||||
item = DfcBuilder.catalog_item(variant)
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ require_relative "../spec_helper"
|
||||
RSpec.describe EnterpriseBuilder do
|
||||
subject(:builder) { described_class }
|
||||
let(:enterprise) {
|
||||
build(
|
||||
create(
|
||||
:enterprise,
|
||||
id: 10_000, name: "Fabi's Farm",
|
||||
description: "The place where stuff grows", abn: "123 456 789 0",
|
||||
@@ -13,7 +13,7 @@ RSpec.describe EnterpriseBuilder do
|
||||
)
|
||||
}
|
||||
let(:variant) {
|
||||
create(:product, supplier: enterprise, name: "Apple").variants.first
|
||||
create(:product, supplier_id: enterprise.id, name: "Apple").variants.first
|
||||
}
|
||||
|
||||
describe ".enterprise" do
|
||||
|
||||
@@ -7,13 +7,13 @@ RSpec.describe SuppliedProductBuilder do
|
||||
|
||||
subject(:builder) { described_class }
|
||||
let(:variant) {
|
||||
build(:variant, id: 5, product: spree_product, primary_taxon: taxon)
|
||||
create(:variant, id: 5, product: spree_product, primary_taxon: taxon, supplier:)
|
||||
}
|
||||
let(:spree_product) {
|
||||
create(:product, id: 6, supplier:)
|
||||
create(:product, id: 6)
|
||||
}
|
||||
let(:supplier) {
|
||||
build(:supplier_enterprise, id: 7)
|
||||
create(:supplier_enterprise, id: 7)
|
||||
}
|
||||
let(:taxon) {
|
||||
build(
|
||||
@@ -43,7 +43,7 @@ RSpec.describe SuppliedProductBuilder do
|
||||
variant.product.name = "Apple"
|
||||
product = builder.supplied_product(variant)
|
||||
|
||||
expect(product.name).to eq "Apple"
|
||||
expect(product.name).to match /Apple/
|
||||
end
|
||||
|
||||
it "assigns the variant name if present" do
|
||||
@@ -51,7 +51,7 @@ RSpec.describe SuppliedProductBuilder do
|
||||
variant.display_name = "Granny Smith"
|
||||
product = builder.supplied_product(variant)
|
||||
|
||||
expect(product.name).to eq "Apple - Granny Smith"
|
||||
expect(product.name).to match /Apple - Granny Smith/
|
||||
end
|
||||
|
||||
context "product_type mapping" do
|
||||
@@ -244,7 +244,7 @@ RSpec.describe SuppliedProductBuilder do
|
||||
|
||||
it "doesn't return a product of another enterprise" do
|
||||
variant.save!
|
||||
create(:product, id: 8, supplier: create(:enterprise))
|
||||
create(:product, id: 8, supplier_id: create(:enterprise).id)
|
||||
|
||||
supplied_product.spree_product_uri =
|
||||
"http://test.host/api/dfc/enterprises/7?spree_product_id=8"
|
||||
|
||||
@@ -8,7 +8,7 @@ RSpec.describe "DFC Product Import" do
|
||||
|
||||
let(:user) { create(:oidc_user, owned_enterprises: [enterprise]) }
|
||||
let(:enterprise) { create(:supplier_enterprise) }
|
||||
let(:source_product) { create(:product, supplier: enterprise) }
|
||||
let(:source_product) { create(:product, supplier_id: enterprise.id) }
|
||||
|
||||
before do
|
||||
login_as user
|
||||
|
||||
Reference in New Issue
Block a user