From 47cea0af6bdd4686a33e49c33811e59ac14e0c2d Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Fri, 15 Dec 2023 13:29:18 +1100 Subject: [PATCH] Add matching DFC product type to taxon when importing product --- .../app/services/supplied_product_builder.rb | 16 +++++-- .../services/supplied_product_builder_spec.rb | 44 +++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/engines/dfc_provider/app/services/supplied_product_builder.rb b/engines/dfc_provider/app/services/supplied_product_builder.rb index 3814e98212..970bef97b7 100644 --- a/engines/dfc_provider/app/services/supplied_product_builder.rb +++ b/engines/dfc_provider/app/services/supplied_product_builder.rb @@ -38,13 +38,12 @@ class SuppliedProductBuilder < DfcBuilder end end - # TODO fix the taxon here def self.import_product(supplied_product) Spree::Product.new( name: supplied_product.name, description: supplied_product.description, price: 0, # will be in DFC Offer - primary_taxon: Spree::Taxon.first, # dummy value until we have a mapping + primary_taxon: taxon(supplied_product) ).tap do |product| QuantitativeValueBuilder.apply(supplied_product.quantity, product) end @@ -109,5 +108,16 @@ class SuppliedProductBuilder < DfcBuilder type end - private_class_method :product_type, :populate_product_types, :record_type + 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: ) + + return taxon if taxon.present? + + nil + end + + private_class_method :product_type, :populate_product_types, :record_type, :call_dfc_product_type, + :taxon end 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 2e213a0525..1c46bc7f13 100644 --- a/engines/dfc_provider/spec/services/supplied_product_builder_spec.rb +++ b/engines/dfc_provider/spec/services/supplied_product_builder_spec.rb @@ -102,4 +102,48 @@ describe SuppliedProductBuilder do expect(product.image).to eq variant.product.image.url(:product) end end + + describe ".import_product" do + let(:supplied_product) do + DataFoodConsortium::Connector::SuppliedProduct.new( + "https://example.net/tomato", + name: "Tomato", + description: "Awesome tomato", + quantity: DataFoodConsortium::Connector::QuantitativeValue.new( + unit: DfcLoader.connector.MEASURES.KILOGRAM, + value: 2, + ), + productType: product_type, + ) + 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") } + + it "creates a new Spree::Product" do + product = builder.import_product(supplied_product) + + expect(product).to be_a(Spree::Product) + expect(product.name).to eq("Tomato") + expect(product.description).to eq("Awesome tomato") + expect(product.variant_unit).to eq("weight") + end + + describe "taxon" do + it "assigns the taxon matching the DFC product type" do + product = builder.import_product(supplied_product) + + expect(product.primary_taxon).to eq(taxon) + end + + describe "when no matching taxon" do + let(:product_type) { DfcLoader.connector.PRODUCT_TYPES.DRINK } + + it "set the taxon to nil" do + product = builder.import_product(supplied_product) + + expect(product.primary_taxon).to be_nil + end + end + end + end end