From 9f7000009db81f2e5bebcedb9b4e24ce36ccdbc2 Mon Sep 17 00:00:00 2001 From: David Cook Date: Thu, 6 Feb 2025 17:10:01 +1100 Subject: [PATCH] Only import selected products --- .../admin/dfc_product_imports_controller.rb | 5 ++++- app/views/admin/dfc_product_imports/index.html.haml | 7 +++++-- config/locales/en.yml | 2 +- spec/system/admin/dfc_product_import_spec.rb | 12 ++++++++++-- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/controllers/admin/dfc_product_imports_controller.rb b/app/controllers/admin/dfc_product_imports_controller.rb index 860e3fc15c..897d374b49 100644 --- a/app/controllers/admin/dfc_product_imports_controller.rb +++ b/app/controllers/admin/dfc_product_imports_controller.rb @@ -35,13 +35,16 @@ module Admin end def import + ids = params.require(:semanticIds) + # Load DFC catalog JSON graph = DfcIo.import(params.require(:catalog_json)) catalog = DfcCatalog.new(graph) catalog.apply_wholesale_values! # Import all selected products for given enterprise. - imported = catalog.products.map do |subject| + imported = catalog.products.select{ |subject| ids.include?(subject.semanticId) } + .map do |subject| existing_variant = @enterprise.supplied_variants.linked_to(subject.semanticId) if existing_variant diff --git a/app/views/admin/dfc_product_imports/index.html.haml b/app/views/admin/dfc_product_imports/index.html.haml index ad594df8af..e282ec4e81 100644 --- a/app/views/admin/dfc_product_imports/index.html.haml +++ b/app/views/admin/dfc_product_imports/index.html.haml @@ -3,7 +3,7 @@ = render partial: 'spree/admin/shared/product_sub_menu' -%p= t('.catalog_url', catalog_url: @catalog_url) +%p= t('.catalog_url', count: @items.count, catalog_url: @catalog_url) %p= t('.enterprise', enterprise_name: @enterprise.name) %br @@ -16,7 +16,10 @@ %tbody - @items.each do |supplied_product, existing_product| %tr{id: supplied_product.semanticId } - %td= supplied_product.name + %td + %label + = form.check_box 'semanticIds[]', { checked: true }, supplied_product.semanticId, "" + = supplied_product.name %td - if existing_product.present? Update diff --git a/config/locales/en.yml b/config/locales/en.yml index c115fd5642..13e7e76c4f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -848,7 +848,7 @@ en: dfc_product_imports: index: title: "DFC product catalog" - catalog_url: "Products to be imported from: %{catalog_url}" + catalog_url: "%{count} products to be imported from: %{catalog_url}" enterprise: "Import to enterprise: %{enterprise_name}" import: Import import: diff --git a/spec/system/admin/dfc_product_import_spec.rb b/spec/system/admin/dfc_product_import_spec.rb index ffa742caa8..b044e210bc 100644 --- a/spec/system/admin/dfc_product_import_spec.rb +++ b/spec/system/admin/dfc_product_import_spec.rb @@ -58,16 +58,20 @@ RSpec.describe "DFC Product Import" do select enterprise.name, from: "Enterprise" click_button "Preview" + expect(page).to have_content "4 products to be imported" expect(page).to have_content "Saucy preserves" expect(page).not_to have_content "Sauce - 1g" # Does not show other product expect(page).to have_content "Beans - Retail can, 400g (can) Update" # existing product expect(page).to have_content "Beans - Case, 12 x 400g (can) New" + expect(page).to have_content "Chia Seed, Organic - Retail pack, 300g" + + uncheck "Chia Seed, Organic - Case, 8 x 300g" # don't import this one expect { click_button "Import" - expect(page).to have_content "Imported products: 4" + expect(page).to have_content "Imported products: 3" linked_variant.reload - }.to change { enterprise.supplied_products.count } + }.to change { enterprise.supplied_products.count }.by(2) # 1 updated, 2 new .and change { linked_variant.display_name } .and change { linked_variant.unit_value } # 18.85 wholesale variant price divided by 12 cans in the slab. @@ -78,6 +82,10 @@ RSpec.describe "DFC Product Import" do product = Spree::Product.last expect(product.variants[0].semantic_links).to be_present expect(product.image).to be_present + + names = Spree::Product.pluck(:name) + expect(names).to include "Baked British Beans - Case, 12 x 400g (can)" + expect(names).not_to include "Chia Seed, Organic - Case, 8 x 300g" end it "fails gracefully" do