Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Added `Taxons::AddProducts` and `Taxons::RemoveProducts` services
Damian Legawiec109bc72e6b9c
diff --git a/core/app/services/spree/taxons/add_products.rb b/core/app/services/spree/taxons/add_products.rbnew file mode 100644index 0000000000..1c91c2af56--- /dev/null+++ b/core/app/services/spree/taxons/add_products.rb@@ -0,0 +1,39 @@+module Spree+ module Taxons+ class AddProducts+ prepend Spree::ServiceModule::Base++ # Creates classifications for the given taxons and products in bulk.+ #+ # @param taxons [Array<Spree::Taxon>]+ # @param products [Array<Spree::Product>]+ # @return [Spree::ServiceModule::Base::Result]+ def call(taxons:, products:)+ return if taxons.blank? || products.blank?++ # build the params for the insert_all+ classifications_params = taxons.pluck(:id).flat_map do |taxon_id|+ position = Spree::Classification.where(taxon_id: taxon_id).count++ products.pluck(:id).map do |product_id|+ {+ taxon_id: taxon_id,+ product_id: product_id,+ position: (position += 1),+ created_at: Time.current,+ updated_at: Time.current+ }+ end+ end+ # doing a quick insert_all here to avoid the overhead of instantiating+ Spree::Classification.insert_all(classifications_params)++ # clearing cache+ Spree::Product.where(id: products.pluck(:id)).touch_all+ Spree::Taxon.where(id: taxons.pluck(:id)).touch_all++ success(true)+ end+ end+ end+enddiff --git a/core/app/services/spree/taxons/remove_products.rb b/core/app/services/spree/taxons/remove_products.rbnew file mode 100644index 0000000000..d80e6acee9--- /dev/null+++ b/core/app/services/spree/taxons/remove_products.rb@@ -0,0 +1,50 @@+module Spree+ module Taxons+ class RemoveProducts+ prepend Spree::ServiceModule::Base++ # Removes the given products from the given taxons.+ #+ # @param taxons [Array<Spree::Taxon>]+ # @param products [Array<Spree::Product>]+ # @return [Spree::ServiceModule::Base::Result]+ def call(taxons:, products:)+ return if taxons.blank? || products.blank?++ ApplicationRecord.transaction do+ taxons.pluck(:id).each do |taxon_id|+ Spree::Classification.where(taxon_id: taxon_id, product_id: products.pluck(:id)).delete_all+ end++ classifications_params = taxons.pluck(:id).flat_map do |taxon_id|+ position = 0+ existing_product_ids = Spree::Classification.where(taxon_id: taxon_id).pluck(:product_id)++ existing_product_ids.map do |product_id|+ {+ taxon_id: taxon_id,+ product_id: product_id,+ position: (position += 1),+ created_at: Time.current,+ updated_at: Time.current+ }+ end+ end++ if classifications_params.any?+ Spree::Classification.upsert_all(+ classifications_params,+ unique_by: :index_spree_products_taxons_on_product_id_and_taxon_id+ )+ end+ end++ # clear cache+ Spree::Product.where(id: products.pluck(:id)).touch_all+ Spree::Taxon.where(id: taxons.pluck(:id)).touch_all++ success(true)+ end+ end+ end+enddiff --git a/core/spec/services/spree/taxons/add_products_spec.rb b/core/spec/services/spree/taxons/add_products_spec.rbnew file mode 100644index 0000000000..bf9b267953--- /dev/null+++ b/core/spec/services/spree/taxons/add_products_spec.rb@@ -0,0 +1,37 @@+require 'spec_helper'++module Spree+ describe Taxons::AddProducts do+ let(:service) { described_class }+ let(:taxons) { create_list(:taxon, 2) }+ let(:products) { create_list(:product, 3) }++ describe '#call' do+ subject { service.call(taxons: taxons, products: products) }++ it 'creates classifications for each taxon-product pair' do+ expect { subject }.to change { Spree::Classification.count }.by(taxons.size * products.size)+ end++ it 'sets the correct position for each classification' do+ subject+ taxons.each do |taxon|+ expect(taxon.classifications.pluck(:position)).to eq((1..products.size).to_a)+ end+ end++ it 'touches all products' do+ expect { subject }.to change { Spree::Product.where(id: products.pluck(:id)).pluck(:updated_at) }+ end++ it 'touches all taxons' do+ expect { subject }.to change { Spree::Taxon.where(id: taxons.pluck(:id)).pluck(:updated_at) }+ end++ it 'returns a successful result' do+ expect(subject.success?).to be true+ expect(subject.value).to eq true+ end+ end+ end+enddiff --git a/core/spec/services/spree/taxons/remove_products_spec.rb b/core/spec/services/spree/taxons/remove_products_spec.rbnew file mode 100644index 0000000000..a51f393a74--- /dev/null+++ b/core/spec/services/spree/taxons/remove_products_spec.rb@@ -0,0 +1,45 @@+require 'spec_helper'++module Spree+ describe Taxons::RemoveProducts do+ let(:service) { described_class }+ let(:taxons) { create_list(:taxon, 2) }+ let(:products) { create_list(:product, 3) }++ describe '#call' do+ subject { service.call(taxons: taxons, products: products) }++ before do+ # Add products to taxons initially+ Spree::Taxons::AddProducts.call(taxons: taxons, products: products)+ end++ it 'removes classifications for each taxon-product pair' do+ expect { subject }.to change { Spree::Classification.count }.by(-(taxons.size * products.size))+ end++ it 'resets the position for remaining classifications' do+ other_product = create(:product)+ Spree::Taxons::AddProducts.call(taxons: taxons, products: [other_product])++ subject+ taxons.each do |taxon|+ expect(taxon.classifications.pluck(:position)).to eq([1])+ end+ end++ it 'touches all products' do+ expect { subject }.to change { Spree::Product.where(id: products.pluck(:id)).pluck(:updated_at) }+ end++ it 'touches all taxons' do+ expect { subject }.to change { Spree::Taxon.where(id: taxons.pluck(:id)).pluck(:updated_at) }+ end++ it 'returns a successful result' do+ expect(subject.success?).to be true+ expect(subject.value).to eq true+ end+ end+ end+end