Skip to content

armgit / spree

Public Imported from GitHub
0

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.

Fix: Narrowed storefront product filters to the buyer's catalogs

Damian Legawiecbb6961629eee
diff --git a/spree/api/app/controllers/spree/api/v3/store/products/filters_controller.rb b/spree/api/app/controllers/spree/api/v3/store/products/filters_controller.rb
index 82639f0317..9501436e48 100644
--- a/spree/api/app/controllers/spree/api/v3/store/products/filters_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/products/filters_controller.rb
@@ -5,6 +5,7 @@ module Spree
module Products
class FiltersController < Store::BaseController
include Spree::Api::V3::Store::SearchProviderSupport
+ include Spree::Api::V3::Store::StorefrontProducts
def index
json = Rails.cache.fetch(filters_cache_key, expires_in: 15.minutes) do
@@ -30,8 +31,8 @@ module Spree
def filters_cache_key
products_table = Spree::Product.table_name
- stats = current_store.products.available(Time.current, current_currency, include_preorderable: true)
- .pick(Arel.sql("MAX(#{products_table}.updated_at)"), Arel.sql("COUNT(DISTINCT #{products_table}.id)"))
+ stats = storefront_products.
+ pick(Arel.sql("MAX(#{products_table}.updated_at)"), Arel.sql("COUNT(DISTINCT #{products_table}.id)"))
max_updated = stats&.first&.to_i
product_count = stats&.last || 0
@@ -41,6 +42,7 @@ module Spree
current_currency,
current_locale,
(hide_prices? ? 'prices_hidden' : nil),
+ buyer_catalog_fragment,
category&.cache_key_with_version,
collection&.cache_key_with_version,
search_query,
@@ -53,11 +55,22 @@ module Spree
parts.compact.join('/')
end
+ # The buyer's catalogs partition the cache: a facet payload built
+ # for one assortment must not be served to a buyer who sees
+ # another, or it would describe products their listing never
+ # returns.
+ def buyer_catalog_fragment
+ catalogs = Spree::Catalog.for_buyer(store: current_store, customer: current_user, channel: current_channel)
+ return if catalogs.empty?
+
+ "catalogs-#{catalogs.map(&:id).sort.join('-')}"
+ end
+
def filters_scope
# Mirror ProductsController#scope so facet counts describe the
# same product set the listing returns (coming-soon pre-orders
- # included).
- scope = current_store.products.available(Time.current, current_currency, include_preorderable: true)
+ # included) — narrowed to the buyer's catalogs like the listing.
+ scope = storefront_products
scope = scope.in_category(category) if category.present?
scope = scope.in_collection(collection) if collection.present?
scope
diff --git a/spree/api/spec/controllers/spree/api/v3/store/products/filters_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/products/filters_controller_spec.rb
index 57a43e6433..ef0f4f96d7 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/products/filters_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/products/filters_controller_spec.rb
@@ -112,6 +112,28 @@ RSpec.describe Spree::Api::V3::Store::Products::FiltersController, type: :contro
expect(small_option['count']).to eq(1)
end
+ # Facets describe the listing the buyer gets, so a buyer narrowed to a
+ # catalog must not learn about products outside it.
+ context 'for a buyer narrowed to a company catalog' do
+ let(:company) { create(:company, store: store) }
+ let(:catalog) { create(:catalog, store: store) }
+
+ before do
+ create(:catalog_assignment, catalog: catalog, assignable: company)
+ catalog.add_products([product2.id])
+ create(:company_membership, company: company, customer: user)
+ request.headers['Authorization'] = "Bearer #{jwt_token}"
+ end
+
+ it 'describes only the products in the catalog' do
+ get :index
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['total_count']).to eq(1)
+ expect(json_response['filters'].find { |f| f['name'] == 'color' }).to be_nil
+ end
+ end
+
it 'returns all sort options' do
get :index