Update DFC supplied product

This commit is contained in:
Gaetan Craig-Riou
2024-02-20 16:27:05 +11:00
parent d3e62c4390
commit 6d1249e7f9
3 changed files with 14 additions and 69 deletions

View File

@@ -65,26 +65,25 @@ class SuppliedProductBuilder < DfcBuilder
Spree::Product.new(
name: supplied_product.name,
description: supplied_product.description,
price: 0, # will be in DFC Offer
primary_taxon_id: taxon(supplied_product).id
price: 0 # will be in DFC Offer
).tap do |product|
QuantitativeValueBuilder.apply(supplied_product.quantity, product)
product.ensure_standard_variant
product.variants.first.primary_taxon = taxon(supplied_product)
end
end
def self.apply(supplied_product, variant)
variant.product.assign_attributes(
description: supplied_product.description,
primary_taxon: taxon(supplied_product)
)
variant.product.assign_attributes(description: supplied_product.description)
variant.display_name = supplied_product.name
variant.primary_taxon = taxon(supplied_product)
QuantitativeValueBuilder.apply(supplied_product.quantity, variant.product)
variant.unit_value = variant.product.unit_value
end
def self.product_type(variant)
taxon_dfc_id = variant.product.primary_taxon&.dfc_id
taxon_dfc_id = variant.primary_taxon&.dfc_id
DfcProductTypeFactory.for(taxon_dfc_id)
end

View File

@@ -10,11 +10,10 @@ describe "SuppliedProducts", type: :request, swagger_doc: "dfc.yaml", rswag_auto
:product_with_image,
id: 90_000,
supplier: enterprise, name: "Pesto", description: "Basil Pesto",
variants: [variant],
primary_taxon: taxon
variants: [variant]
)
}
let(:variant) { build(:base_variant, id: 10_001, unit_value: 1) }
let(:variant) { build(:base_variant, id: 10_001, unit_value: 1, primary_taxon: taxon) }
let(:taxon) {
build(
:taxon,
@@ -114,7 +113,7 @@ describe "SuppliedProducts", type: :request, swagger_doc: "dfc.yaml", rswag_auto
product = Spree::Product.find(product_id)
expect(product.name).to eq "Apple"
expect(product.variants).to eq [variant]
expect(product.primary_taxon).to eq(non_local_vegetable)
expect(product.variants.first.primary_taxon).to eq(non_local_vegetable)
# Creates a variant for existing product
supplied_product[:'ofn:spree_product_id'] = product_id
@@ -244,7 +243,7 @@ describe "SuppliedProducts", type: :request, swagger_doc: "dfc.yaml", rswag_auto
}.to change { variant.description }.to("DFC-Pesto updated")
.and change { variant.display_name }.to("Pesto novo")
.and change { variant.unit_value }.to(17)
.and change { variant.product.primary_taxon }.to(drink_taxon)
.and change { variant.primary_taxon }.to(drink_taxon)
end
end
end

View File

@@ -7,12 +7,10 @@ describe SuppliedProductBuilder do
subject(:builder) { described_class }
let(:variant) {
build(:variant, id: 5, product: spree_product)
build(:variant, id: 5, product: spree_product, primary_taxon: taxon)
}
let(:spree_product) {
create(:product, id: 6, supplier:).tap do |p|
p.primary_taxon = taxon
end
create(:product, id: 6, supplier:)
}
let(:supplier) {
build(:supplier_enterprise, id: 7)
@@ -121,7 +119,7 @@ describe SuppliedProductBuilder do
it "assigns the taxon matching the DFC product type" do
product = builder.import_product(supplied_product)
expect(product.primary_taxon).to eq(taxon)
expect(product.variants.first.primary_taxon).to eq(taxon)
end
end
end
@@ -191,58 +189,7 @@ describe SuppliedProductBuilder do
imported_product = imported_variant.product
expect(imported_product.id).to eq(spree_product.id)
expect(imported_product.description).to eq("Better Awesome tomato")
expect(imported_product.primary_taxon).to eq(new_taxon)
end
it "adds a new variant" do
expect(imported_variant.id).to be_nil
expect(imported_variant.product).to eq(spree_product)
expect(imported_variant.display_name).to eq("Tomato")
expect(imported_variant.unit_value).to eq(2000)
end
end
context "with spree_product_uri supplied" do
let(:imported_variant) { builder.import_variant(supplied_product, supplier) }
let(:product_type) { DfcLoader.connector.PRODUCT_TYPES.DRINK.SOFT_DRINK }
let!(:new_taxon) {
create(
:taxon,
name: "Soft Drink",
dfc_id: "https://github.com/datafoodconsortium/taxonomies/releases/latest/download/productTypes.rdf#soft-drink"
)
}
context "when spree_product_uri match the server host" do
let(:supplied_product) do
variant.save! # referenced in spree_product_id
DfcProvider::SuppliedProduct.new(
"https://example.net/tomato",
name: "Tomato",
description: "Better Awesome tomato",
quantity: DataFoodConsortium::Connector::QuantitativeValue.new(
unit: DfcLoader.connector.MEASURES.KILOGRAM,
value: 2,
),
productType: product_type,
spree_product_uri: "http://test.host/api/dfc/enterprises/7?spree_product_id=6"
)
end
it "update an existing Spree::Product" do
imported_product = imported_variant.product
expect(imported_product.id).to eq(spree_product.id)
expect(imported_product.description).to eq("Better Awesome tomato")
expect(imported_product.primary_taxon).to eq(new_taxon)
end
it "adds a new variant" do
expect(imported_variant.id).to be_nil
expect(imported_variant.product).to eq(spree_product)
expect(imported_variant.display_name).to eq("Tomato")
expect(imported_variant.unit_value).to eq(2000)
end
expect(imported_variant.primary_taxon).to eq(new_taxon)
end
context "when spree_product_uri doesn't match the server host" do