mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
Reset stock for absent products in DFC catalog
This commit is contained in:
@@ -58,6 +58,7 @@ module Admin
|
||||
end
|
||||
|
||||
@count = imported.compact.count
|
||||
@reset_count = reset_absent_variants(catalog).count
|
||||
rescue ActionController::ParameterMissing => e
|
||||
flash[:error] = e.message
|
||||
redirect_to admin_product_import_path
|
||||
@@ -80,5 +81,23 @@ module Admin
|
||||
]
|
||||
end
|
||||
end
|
||||
|
||||
def reset_absent_variants(catalog)
|
||||
present_ids = catalog.products.map(&:semanticId)
|
||||
absent_variants = @enterprise.supplied_variants
|
||||
.includes(:semantic_links).references(:semantic_links)
|
||||
.where.not(semantic_links: { semantic_id: present_ids })
|
||||
|
||||
catalog_url = FdcUrlBuilder.new(present_ids.first).catalog_url
|
||||
absent_variants.select do |variant|
|
||||
# Variants that were in the same catalog before:
|
||||
variant.semantic_links.map(&:semantic_id).any? do |semantic_id|
|
||||
FdcUrlBuilder.new(semantic_id).catalog_url == catalog_url
|
||||
end
|
||||
end.map do |variant|
|
||||
variant.on_demand = false
|
||||
variant.on_hand = 0
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,3 +5,6 @@
|
||||
|
||||
%p= t(".imported_products")
|
||||
= @count
|
||||
|
||||
%p= t(".reset_products")
|
||||
= @reset_count
|
||||
|
||||
@@ -866,6 +866,7 @@ en:
|
||||
import:
|
||||
title: "DFC product catalog import"
|
||||
imported_products: "Imported products:"
|
||||
reset_products: "Stock reset for absent products:"
|
||||
enterprise_fees:
|
||||
index:
|
||||
title: "Enterprise Fees"
|
||||
|
||||
@@ -9,6 +9,7 @@ RSpec.describe "DFC Product Import" do
|
||||
let(:user) { create(:oidc_user, owned_enterprises: [enterprise]) }
|
||||
let(:enterprise) { create(:supplier_enterprise, name: "Saucy preserves") }
|
||||
let(:source_product) { create(:product, name: "Sauce", supplier_id: enterprise.id) }
|
||||
let(:old_product) { create(:product, name: "Best Sauce of 1995", supplier_id: enterprise.id) }
|
||||
|
||||
before do
|
||||
login_as user
|
||||
@@ -52,12 +53,21 @@ RSpec.describe "DFC Product Import" do
|
||||
|
||||
it "imports from a FDC catalog", vcr: true do
|
||||
user.update!(oidc_account: build(:testdfc_account))
|
||||
# One product is existing in OFN
|
||||
|
||||
# One current product is existing in OFN
|
||||
product_id =
|
||||
"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts/44519466467635"
|
||||
linked_variant = source_product.variants.first
|
||||
linked_variant.semantic_links << SemanticLink.new(semantic_id: product_id)
|
||||
|
||||
# One outdated product still exists in OFN
|
||||
old_product_id =
|
||||
"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts/445194664-1995"
|
||||
unlinked_variant = old_product.variants.first
|
||||
unlinked_variant.semantic_links << SemanticLink.new(semantic_id: old_product_id)
|
||||
unlinked_variant.on_demand = true
|
||||
unlinked_variant.on_hand = 3
|
||||
|
||||
visit admin_product_import_path
|
||||
|
||||
url = "https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts"
|
||||
@@ -83,14 +93,18 @@ RSpec.describe "DFC Product Import" do
|
||||
expect {
|
||||
click_button "Import"
|
||||
expect(page).to have_content "Imported products: 3"
|
||||
expect(page).to have_content "Stock reset for absent products: 1"
|
||||
linked_variant.reload
|
||||
}.to change { enterprise.supplied_products.count }.by(2) # 1 updated, 2 new
|
||||
unlinked_variant.reload
|
||||
}.to change { enterprise.supplied_products.count }.by(2) # 1 updated, 2 new, 1 reset
|
||||
.and change { linked_variant.display_name }
|
||||
.and change { linked_variant.unit_value }
|
||||
# 18.85 wholesale variant price divided by 12 cans in the slab.
|
||||
.and change { linked_variant.price }.to(1.57)
|
||||
.and change { linked_variant.on_demand }.to(true)
|
||||
.and change { linked_variant.on_hand }.by(0)
|
||||
.and change { unlinked_variant.on_demand }.to(false)
|
||||
.and change { unlinked_variant.on_hand }.to(0)
|
||||
|
||||
product = Spree::Product.last
|
||||
expect(product.variants[0].semantic_links).to be_present
|
||||
|
||||
Reference in New Issue
Block a user