mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-26 01:33:22 +00:00
Fix import of zero-weight products
We don't allow variants to have zero weight or volume. But a DFC import in production showed that some catalogs list products with zero weight. Despite the products having a weight, it's simpler to treat these as items.
This commit is contained in:
@@ -29,11 +29,18 @@ class QuantitativeValueBuilder < DfcBuilder
|
||||
|
||||
def self.apply(quantity, product)
|
||||
measure, unit_name, unit_scale = map_unit(quantity.unit)
|
||||
value = quantity.value.to_f * unit_scale
|
||||
|
||||
if measure.in?(%w(weight volume)) && value <= 0
|
||||
measure = "items"
|
||||
unit_name = "items"
|
||||
value = 1
|
||||
end
|
||||
|
||||
product.variant_unit = measure
|
||||
product.variant_unit_name = unit_name if measure == "items"
|
||||
product.variant_unit_scale = unit_scale
|
||||
product.unit_value = quantity.value.to_f * unit_scale
|
||||
product.unit_value = value
|
||||
end
|
||||
|
||||
# Map DFC units to OFN fields:
|
||||
|
||||
38
engines/dfc_provider/spec/fixtures/files/product.GET.json
vendored
Normal file
38
engines/dfc_provider/spec/fixtures/files/product.GET.json
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
{
|
||||
"@context": "https://www.datafoodconsortium.org",
|
||||
"@graph": [
|
||||
{
|
||||
"@id": "_:b433",
|
||||
"@type": "dfc-b:QuantitativeValue",
|
||||
"dfc-b:hasUnit": "dfc-m:Kilogram",
|
||||
"dfc-b:value": "0"
|
||||
},
|
||||
{
|
||||
"@id": "_:b434",
|
||||
"@type": "dfc-b:Price",
|
||||
"dfc-b:VATrate": "1",
|
||||
"dfc-b:hasUnit": "dfc-m:Euro",
|
||||
"dfc-b:value": "12.06"
|
||||
},
|
||||
{
|
||||
"@id": "https://example-producer.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/5c21b9-95/SuppliedProducts/49055026544964",
|
||||
"@type": "dfc-b:SuppliedProduct",
|
||||
"dfc-b:hasQuantity": "_:b433",
|
||||
"dfc-b:name": "Fillet Steak - 201g x 1 Steak",
|
||||
"dfc-b:referencedBy": "https://example-producer.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/5c21b9-95/SuppliedProducts/49055026544964/CatalogItem"
|
||||
},
|
||||
{
|
||||
"@id": "https://example-producer.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/5c21b9-95/SuppliedProducts/49055026544964/CatalogItem",
|
||||
"@type": "dfc-b:CatalogItem",
|
||||
"dfc-b:offeredThrough": "https://example-producer.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/5c21b9-95/SuppliedProducts/49055026544964/Offer",
|
||||
"dfc-b:stockLimitation": "11"
|
||||
},
|
||||
{
|
||||
"@id": "https://example-producer.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/5c21b9-95/SuppliedProducts/49055026544964/Offer",
|
||||
"@type": "dfc-b:Offer",
|
||||
"dfc-b:hasPrice": {
|
||||
"@id": "_:b434"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -84,6 +84,33 @@ RSpec.describe SuppliedProductBuilder do
|
||||
end
|
||||
end
|
||||
|
||||
describe ".store_product" do
|
||||
let(:subject) { builder.store_product(product, supplier) }
|
||||
let(:product) {
|
||||
DfcIo.import(product_json).find do |subject|
|
||||
subject.is_a? DataFoodConsortium::Connector::SuppliedProduct
|
||||
end
|
||||
}
|
||||
let(:product_json) { ExampleJson.read("product.GET") }
|
||||
|
||||
before do
|
||||
taxon.save!
|
||||
end
|
||||
|
||||
it "stores a new Spree Product and Variant" do
|
||||
expect { subject }.to change {
|
||||
Spree::Product.count
|
||||
}.by(1)
|
||||
|
||||
expect(subject).to be_a(Spree::Variant)
|
||||
expect(subject).to be_valid
|
||||
expect(subject).to be_persisted
|
||||
expect(subject.name).to eq("Fillet Steak - 201g x 1 Steak")
|
||||
expect(subject.variant_unit).to eq("items")
|
||||
expect(subject.unit_value).to eq(1)
|
||||
end
|
||||
end
|
||||
|
||||
describe ".import_product" do
|
||||
let(:supplied_product) do
|
||||
DfcProvider::SuppliedProduct.new(
|
||||
|
||||
Reference in New Issue
Block a user