From c60718feea239fa441bf52855b41570d4f8f1fce Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 6 Mar 2025 09:29:35 +1100 Subject: [PATCH] List absent products in import preview --- .../admin/dfc_product_imports_controller.rb | 29 +++++++++++-------- .../_absent_variant.html.haml | 8 +++++ .../dfc_product_imports/import.html.haml | 2 +- .../admin/dfc_product_imports/index.html.haml | 2 ++ config/locales/en.yml | 10 +++++++ spec/system/admin/dfc_product_import_spec.rb | 2 ++ 6 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 app/views/admin/dfc_product_imports/_absent_variant.html.haml diff --git a/app/controllers/admin/dfc_product_imports_controller.rb b/app/controllers/admin/dfc_product_imports_controller.rb index cfd7ba339a..d5d2699808 100644 --- a/app/controllers/admin/dfc_product_imports_controller.rb +++ b/app/controllers/admin/dfc_product_imports_controller.rb @@ -21,6 +21,7 @@ module Admin # Render table and let user decide which ones to import. @items = list_products(catalog) + @absent_items = absent_variants(catalog) rescue URI::InvalidURIError flash[:error] = t ".invalid_url" redirect_to admin_product_import_path @@ -89,21 +90,25 @@ module Admin # We don't delete the variant because it may come back at a later time and # we don't want to lose the connection to previous orders. 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| + absent_variants(catalog).map do |variant| variant.on_demand = false variant.on_hand = 0 end end + + def absent_variants(catalog) + present_ids = catalog.products.map(&:semanticId) + catalog_url = FdcUrlBuilder.new(present_ids.first).catalog_url + + @enterprise.supplied_variants + .includes(:semantic_links).references(:semantic_links) + .where.not(semantic_links: { semantic_id: present_ids }) + .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 + end end end diff --git a/app/views/admin/dfc_product_imports/_absent_variant.html.haml b/app/views/admin/dfc_product_imports/_absent_variant.html.haml new file mode 100644 index 0000000000..e0bd9bad91 --- /dev/null +++ b/app/views/admin/dfc_product_imports/_absent_variant.html.haml @@ -0,0 +1,8 @@ +%tr + %td + %label + ❌ + = absent_variant.product_and_full_name + %td + = t(".reset") + = link_to(absent_variant.product_id, edit_admin_product_path(absent_variant.product_id)) diff --git a/app/views/admin/dfc_product_imports/import.html.haml b/app/views/admin/dfc_product_imports/import.html.haml index e84a56f3cc..63ca01a33b 100644 --- a/app/views/admin/dfc_product_imports/import.html.haml +++ b/app/views/admin/dfc_product_imports/import.html.haml @@ -5,4 +5,4 @@ %p= t(".imported_products", count: @count) -%p= t(".reset_products", count: @reset_count) +%p= t(".reset_products", count: @reset_count) if @reset_count.positive? diff --git a/app/views/admin/dfc_product_imports/index.html.haml b/app/views/admin/dfc_product_imports/index.html.haml index 720705d373..1b170e01da 100644 --- a/app/views/admin/dfc_product_imports/index.html.haml +++ b/app/views/admin/dfc_product_imports/index.html.haml @@ -6,6 +6,7 @@ %p= t('.catalog_url', count: @items.count, catalog_url: @catalog_url) %p= t('.enterprise', enterprise_name: @enterprise.name) + %p= t('.absent_products', count: @absent_items.count) %br = form_with url: main_app.import_admin_dfc_product_imports_path, html: { "data-controller": "checked" } do |form| @@ -32,6 +33,7 @@ = link_to(existing_product.id, edit_admin_product_path(existing_product)) - else = t(".new") + = render partial: "absent_variant", collection: @absent_items %span{ "data-controller": "checked-feedback", "data-checked-feedback-translation-value": "admin.dfc_product_imports.index.selected" } = t(".selected", count: @items.count) diff --git a/config/locales/en.yml b/config/locales/en.yml index 2f1a41b4e7..1added29b5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -850,9 +850,19 @@ en: connection_invalid_html: | Connecting with your OIDC account failed. Please refresh your OIDC connection at: %{oidc_settings_link} + absent_variant: + reset: "Reset stock" index: title: "DFC product catalog" catalog_url: "%{count} products to be imported from: %{catalog_url}" + absent_products: + zero: "" + one: | + One product is no longer in the catalog. + It will be marked as unavailable by resetting stock to zero. + other: | + %{count} products are no longer in the catalog. + They will be marked as unavailable by resetting stock to zero. enterprise: "Import to enterprise: %{enterprise_name}" select_all: "Select/deselect all" update: Update diff --git a/spec/system/admin/dfc_product_import_spec.rb b/spec/system/admin/dfc_product_import_spec.rb index f5f0ae9c97..83c784b570 100644 --- a/spec/system/admin/dfc_product_import_spec.rb +++ b/spec/system/admin/dfc_product_import_spec.rb @@ -76,11 +76,13 @@ RSpec.describe "DFC Product Import" do click_button "Preview" expect(page).to have_content "4 products to be imported" + expect(page).to have_content "One product is no longer" 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" + expect(page).to have_content "Best Sauce of 1995 - 1g Reset stock" # I can select all uncheck "Chia Seed, Organic - Case, 8 x 300g"