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
Allow listing automatic collection products; block only manual curation
Łukasz Adamczyk140163a1a43a
diff --git a/spree/api/app/controllers/spree/api/v3/admin/collections/products_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/collections/products_controller.rbindex 47811b0572..397122985b 100644--- a/spree/api/app/controllers/spree/api/v3/admin/collections/products_controller.rb+++ b/spree/api/app/controllers/spree/api/v3/admin/collections/products_controller.rb@@ -14,6 +14,7 @@ module Spree# Skip the base single-resource load — membership actions resolve# the product against the collection's scope themselves (set_product).skip_before_action :set_resource+ before_action :ensure_manual_collection, only: [:create, :destroy, :reposition]before_action :set_product, only: [:destroy, :reposition]# POST /api/v3/admin/collections/:collection_id/products@@ -68,12 +69,11 @@ module Spreeend# Loads the parent collection (runs before the base set_resource).- # Scoped to +manual+ so automatic (rule-based) collections — whose- # membership is materialized by RegenerateProducts and would drop any- # manual add on the next regeneration — can't have their membership- # listed or mutated through this endpoint (mirrors the categories API).+ # Any collection resolves — listing (index) an automatic collection's+ # rule-materialized membership is valid. Manual-only curation is enforced+ # separately by +ensure_manual_collection+ on the write actions.def set_parent- @parent_collection = Spree::Collection.manual.+ @parent_collection = Spree::Collection.accessible_by(current_ability, :update).for_store(current_store).find_by_prefix_id!(params[:collection_id])@@ -82,6 +82,21 @@ module Spreeprivate+ # Manual curation (add/remove/reposition) is invalid on automatic+ # collections: their membership is materialized from rules by+ # RegenerateProducts, so a manual change would be wiped on the next+ # regeneration. Listing (index) the materialized membership is fine.+ def ensure_manual_collection+ return if @parent_collection.manual?++ render_error(+ code: ERROR_CODES[:validation_error],+ message: Spree.t('api.errors.automatic_collection_curation',+ default: "Products of an automatic collection are managed by its rules and can't be curated manually"),+ status: :unprocessable_content+ )+ end+def set_product@product = scope.find_by_prefix_id!(params[:id])enddiff --git a/spree/api/config/locales/en.yml b/spree/api/config/locales/en.ymlindex 8cede02974..8d95809de2 100644--- a/spree/api/config/locales/en.yml+++ b/spree/api/config/locales/en.yml@@ -10,6 +10,7 @@ en:invalid_taxonomy_id: Invalid taxonomy id.must_specify_api_key: You must specify an API key.errors:+ automatic_collection_curation: Products of an automatic collection are managed by its rules and can't be curated manuallyguest_checkout_not_allowed: You must be signed in to complete checkout.storefront_login_required: Authentication required to access this store.negative_quantity: quantity is negativediff --git a/spree/api/spec/controllers/spree/api/v3/admin/collections/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/collections/products_controller_spec.rbindex 35c2d9a187..42442c3529 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/collections/products_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/collections/products_controller_spec.rb@@ -45,11 +45,15 @@ RSpec.describe Spree::Api::V3::Admin::Collections::ProductsController, type: :coexpect(json_response['data']).to eq([])end- it '404s for an automatic collection (curation is manual-only)' do+ it 'lists the rule-materialized products of an automatic collection' doautomatic = create(:automatic_collection, store: store)+ member = create(:product, stores: [store])+ Spree::ProductCollection.create!(collection: automatic, product: member, position: 1)+get :index, params: { collection_id: automatic.prefixed_id }, as: :json- expect(response).to have_http_status(:not_found)+ expect(response).to have_http_status(:ok)+ expect(json_response['data'].map { |p| p['id'] }).to include(member.prefixed_id)endend@@ -70,11 +74,14 @@ RSpec.describe Spree::Api::V3::Admin::Collections::ProductsController, type: :coexpect(response).to have_http_status(:not_found)end- it '404s for an automatic collection (membership is rule-managed, not curated)' do+ it 'rejects adding to an automatic collection (rule-managed, not curated)' doautomatic = create(:automatic_collection, store: store)post :create, params: { collection_id: automatic.prefixed_id, product_id: product.prefixed_id }, as: :json- expect(response).to have_http_status(:not_found)+ expect(response).to have_http_status(:unprocessable_content)+ expect(json_response['error']['code']).to eq('validation_error')+ expect(json_response['error']['message']).to eq(Spree.t('api.errors.automatic_collection_curation'))+ expect(member_ids).not_to include(product.id)endend@@ -96,6 +103,15 @@ RSpec.describe Spree::Api::V3::Admin::Collections::ProductsController, type: :coexpect(response).to have_http_status(:not_found)end++ it 'rejects removing from an automatic collection before looking up the product' do+ automatic = create(:automatic_collection, store: store)+ delete :destroy, params: { collection_id: automatic.prefixed_id, id: product.prefixed_id }, as: :json++ expect(response).to have_http_status(:unprocessable_content)+ expect(json_response['error']['code']).to eq('validation_error')+ expect(json_response['error']['message']).to eq(Spree.t('api.errors.automatic_collection_curation'))+ endenddescribe 'PATCH #reposition' do