Add matching DFC product type to taxon when importing product

This commit is contained in:
Gaetan Craig-Riou
2023-12-15 13:29:18 +11:00
parent e917b26e91
commit 47cea0af6b
2 changed files with 57 additions and 3 deletions

View File

@@ -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

View File

@@ -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