From 8013fac5b860c80f69eb2345a5f698b91c3fb75e Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Mon, 18 Dec 2023 14:09:53 +1100 Subject: [PATCH] Per review, match on the product type URI instead of name Fox export only --- ...1027041224_add_dfc_name_to_spree_taxons.rb | 2 +- db/schema.rb | 2 +- .../app/services/supplied_product_builder.rb | 17 +++++------ .../spec/requests/catalog_items_spec.rb | 9 +++++- .../spec/requests/enterprises_spec.rb | 9 +++++- .../spec/requests/supplied_products_spec.rb | 17 +++++++++-- .../services/supplied_product_builder_spec.rb | 28 +++++++++++++++---- 7 files changed, 64 insertions(+), 20 deletions(-) diff --git a/db/migrate/20231027041224_add_dfc_name_to_spree_taxons.rb b/db/migrate/20231027041224_add_dfc_name_to_spree_taxons.rb index 224880511e..22ae971b70 100644 --- a/db/migrate/20231027041224_add_dfc_name_to_spree_taxons.rb +++ b/db/migrate/20231027041224_add_dfc_name_to_spree_taxons.rb @@ -1,5 +1,5 @@ class AddDfcNameToSpreeTaxons < ActiveRecord::Migration[7.0] def change - add_column :spree_taxons, :dfc_name, :string + add_column :spree_taxons, :dfc_id, :string end end diff --git a/db/schema.rb b/db/schema.rb index e7bb67f638..5a78096336 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -881,7 +881,7 @@ ActiveRecord::Schema[7.0].define(version: 20231003000823494) do t.string "meta_title", limit: 255 t.string "meta_description", limit: 255 t.string "meta_keywords", limit: 255 - t.string "dfc_name" + t.string "dfc_id" t.index ["parent_id"], name: "index_taxons_on_parent_id" t.index ["permalink"], name: "index_taxons_on_permalink" t.index ["taxonomy_id"], name: "index_taxons_on_taxonomy_id" diff --git a/engines/dfc_provider/app/services/supplied_product_builder.rb b/engines/dfc_provider/app/services/supplied_product_builder.rb index 5596cb9593..5bfa74aad5 100644 --- a/engines/dfc_provider/app/services/supplied_product_builder.rb +++ b/engines/dfc_provider/app/services/supplied_product_builder.rb @@ -61,16 +61,15 @@ class SuppliedProductBuilder < DfcBuilder end def self.product_type(variant) - taxon_name = variant.product.primary_taxon&.dfc_name + taxon_dfc_id = variant.product.primary_taxon&.dfc_id - return nil if taxon_name.nil? + return nil if taxon_dfc_id.nil? populate_product_types if PRODUCT_TYPES.empty? - name = taxon_name.downcase.gsub(" ", "_").to_sym - return nil if PRODUCT_TYPES[name].nil? + return nil if PRODUCT_TYPES[taxon_dfc_id].nil? - call_dfc_product_type(PRODUCT_TYPES[name]) + call_dfc_product_type(PRODUCT_TYPES[taxon_dfc_id]) end def self.populate_product_types @@ -83,10 +82,12 @@ class SuppliedProductBuilder < DfcBuilder def self.record_type(stack, product_type) name = product_type.to_s current_stack = stack.dup.push(name) - PRODUCT_TYPES[name.downcase.to_sym] = current_stack type = call_dfc_product_type(current_stack) + id = type.semanticId + PRODUCT_TYPES[id] = current_stack + # Narrower product types are defined as class method on the current product type object narrowers = type.methods(false).sort @@ -111,8 +112,8 @@ class SuppliedProductBuilder < DfcBuilder def self.taxon(supplied_product) # We use english locale, might need to make this configurable - dfc_name = supplied_product.productType.prefLabels[:en].downcase - taxon = Spree::Taxon.find_by(dfc_name: ) + dfc_id = supplied_product.productType.prefLabels[:en].downcase + taxon = Spree::Taxon.find_by(dfc_id: ) return taxon if taxon.present? diff --git a/engines/dfc_provider/spec/requests/catalog_items_spec.rb b/engines/dfc_provider/spec/requests/catalog_items_spec.rb index 27249778de..f047812e00 100644 --- a/engines/dfc_provider/spec/requests/catalog_items_spec.rb +++ b/engines/dfc_provider/spec/requests/catalog_items_spec.rb @@ -17,7 +17,14 @@ describe "CatalogItems", type: :request, swagger_doc: "dfc.yaml", :base_product, id: 90_000, supplier: enterprise, name: "Apple", description: "Red", variants: [variant], - primary_taxon: build(:taxon, name: "Non local vegetable", dfc_name: "non local vegetable"), + primary_taxon: non_local_vegetable + ) + } + let(:non_local_vegetable) { + build( + :taxon, + name: "Non Local Vegetable", + 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") } diff --git a/engines/dfc_provider/spec/requests/enterprises_spec.rb b/engines/dfc_provider/spec/requests/enterprises_spec.rb index 5a604ff463..ca875360ae 100644 --- a/engines/dfc_provider/spec/requests/enterprises_spec.rb +++ b/engines/dfc_provider/spec/requests/enterprises_spec.rb @@ -29,7 +29,14 @@ describe "Enterprises", type: :request, swagger_doc: "dfc.yaml", rswag_autodoc: :product_with_image, id: 90_000, supplier: enterprise, name: "Apple", description: "Round", variants: [variant], - primary_taxon: build(:taxon, name: "Non local vegetable", dfc_name: "non local vegetable"), + primary_taxon: non_local_vegetable + ) + } + let(:non_local_vegetable) { + build( + :taxon, + name: "Non Local Vegetable", + 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") } diff --git a/engines/dfc_provider/spec/requests/supplied_products_spec.rb b/engines/dfc_provider/spec/requests/supplied_products_spec.rb index d3a63e40ee..f0e3792552 100644 --- a/engines/dfc_provider/spec/requests/supplied_products_spec.rb +++ b/engines/dfc_provider/spec/requests/supplied_products_spec.rb @@ -15,9 +15,20 @@ describe "SuppliedProducts", type: :request, swagger_doc: "dfc.yaml", rswag_auto ) } let(:variant) { build(:base_variant, id: 10_001, unit_value: 1) } - let(:taxon) { build(:taxon, name: "Processed Vegetable", dfc_name: "processed vegetable") } + let(:taxon) { + build( + :taxon, + name: "Processed Vegetable", + dfc_id: "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#processed-vegetable" + ) + } + let!(:non_local_vegetable) { - create(:taxon, name: "Non Local Vegetable", dfc_name: "non local vegetable") + create( + :taxon, + name: "Non Local Vegetable", + dfc_id: "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#non-local-vegetable" + ) } before { login_as user } @@ -173,7 +184,7 @@ describe "SuppliedProducts", type: :request, swagger_doc: "dfc.yaml", rswag_auto put "Update SuppliedProduct" do let!(:drink_taxon) { - create(:taxon, name: "Drink", dfc_name: "drink") + create(:taxon, name: "Drink", dfc_id: "drink") } consumes "application/json" diff --git a/engines/dfc_provider/spec/services/supplied_product_builder_spec.rb b/engines/dfc_provider/spec/services/supplied_product_builder_spec.rb index 1c46bc7f13..977c64d256 100644 --- a/engines/dfc_provider/spec/services/supplied_product_builder_spec.rb +++ b/engines/dfc_provider/spec/services/supplied_product_builder_spec.rb @@ -12,7 +12,13 @@ describe SuppliedProductBuilder do v.product.primary_taxon = taxon end } - let(:taxon) { build(:taxon, name: "Drink", dfc_name: "drink") } + let(:taxon) { + build( + :taxon, + name: "Drink", + dfc_id: "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#drink" + ) + } describe ".supplied_product" do it "assigns a semantic id" do @@ -55,7 +61,13 @@ describe SuppliedProductBuilder do end context "with second level product type" do - let(:taxon) { build(:taxon, name: "Soft Drink", dfc_name: "Soft drink") } + let(:taxon) { + build( + :taxon, + name: "Soft Drink", + dfc_id: "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#soft-drink" + ) + } it "assigns a second level product type" do soft_drink = DfcLoader.connector.PRODUCT_TYPES.DRINK.SOFT_DRINK @@ -65,7 +77,13 @@ describe SuppliedProductBuilder do end context "with leaf level product type" do - let(:taxon) { build(:taxon, name: "Lemonade", dfc_name: "lemonade") } + let(:taxon) { + build( + :taxon, + name: "Lemonade", + dfc_id: "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#lemonade" + ) + } it "assigns a leaf level product type" do lemonade = DfcLoader.connector.PRODUCT_TYPES.DRINK.SOFT_DRINK.LEMONADE @@ -75,7 +93,7 @@ describe SuppliedProductBuilder do end context "with non existing product type" do - let(:taxon) { build(:taxon, name: "other", dfc_name: "other") } + let(:taxon) { build(:taxon, name: "other", dfc_id: "other") } it "returns nil" do expect(product.productType).to be_nil @@ -117,7 +135,7 @@ describe SuppliedProductBuilder do ) end let(:product_type) { DfcLoader.connector.PRODUCT_TYPES.VEGETABLE.NON_LOCAL_VEGETABLE } - let!(:taxon) { create(:taxon, name: "Non local vegetable", dfc_name: "non local vegetable") } + let!(:taxon) { create(:taxon, name: "Non local vegetable", dfc_id: "non local vegetable") } it "creates a new Spree::Product" do product = builder.import_product(supplied_product)