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:
Maikel Linke
2024-10-10 14:08:02 +11:00
parent e429cb7198
commit bda506528f
3 changed files with 73 additions and 1 deletions

View File

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

View 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"
}
}
]
}

View File

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