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.

Meilisearch: exclude future-dated products from data and count

Damian Legawiecf0b1353965f1
diff --git a/spree/api/spec/controllers/spree/api/v3/store/products/meilisearch_integration_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/products/meilisearch_integration_spec.rb
index b8ac40dd0b..acf828ba61 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/products/meilisearch_integration_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/products/meilisearch_integration_spec.rb
@@ -60,6 +60,14 @@ RSpec.describe 'Meilisearch Integration', type: :controller, if: ENV['MEILISEARC
let!(:draft_product) { create(:product, name: 'Draft Hat', status: 'draft') }
+ let!(:future_product) do
+ create(:product, name: 'Future Coat', status: 'active', store: store, available_on: 2.weeks.from_now, price: 99.99)
+ end
+
+ let!(:discontinued_product) do
+ create(:product, name: 'Old Jacket', status: 'active', store: store, discontinue_on: 2.days.ago, price: 49.99)
+ end
+
before do
request.headers['X-Spree-Api-Key'] = api_key.token
allow(Spree).to receive(:search_provider).and_return('Spree::SearchProvider::Meilisearch')
@@ -199,10 +207,28 @@ RSpec.describe 'Meilisearch Integration', type: :controller, if: ENV['MEILISEARC
names = json_response['data'].map { |p| p['name'] }
expect(names).not_to include('Draft Hat')
end
+
+ it 'excludes products with a future available_on from both data and count' do
+ get :index
+
+ expect(response).to have_http_status(:ok)
+ names = json_response['data'].map { |p| p['name'] }
+ expect(names).not_to include('Future Coat')
+ expect(json_response['meta']['count']).to eq(names.size)
+ end
+
+ it 'excludes products discontinued in the past from both data and count' do
+ get :index
+
+ expect(response).to have_http_status(:ok)
+ names = json_response['data'].map { |p| p['name'] }
+ expect(names).not_to include('Old Jacket')
+ expect(json_response['meta']['count']).to eq(names.size)
+ end
end
describe 'browsing without search query' do
- it 'returns all active products' do
+ it 'returns all active, available, non-discontinued products' do
get :index
expect(response).to have_http_status(:ok)
diff --git a/spree/core/app/models/spree/search_provider/meilisearch.rb b/spree/core/app/models/spree/search_provider/meilisearch.rb
index 61c4fe4ef4..cdc0e84f83 100644
--- a/spree/core/app/models/spree/search_provider/meilisearch.rb
+++ b/spree/core/app/models/spree/search_provider/meilisearch.rb
@@ -233,7 +233,7 @@ module Spree
end
def filterable_attributes
- %w[product_id status in_stock store_ids channel_ids locale currency discontinue_on price category_ids tags option_value_ids]
+ %w[product_id status in_stock store_ids channel_ids locale currency available_on discontinue_on price category_ids tags option_value_ids]
end
def sortable_attributes
@@ -259,13 +259,22 @@ module Spree
# System scoping — always applied. Rarely overridden.
# Mirrors the AR scope: store.products.active(currency) with locale.
def system_filter_conditions
+ now = Time.current
conditions = []
conditions << "store_ids = '#{store.id}'"
conditions << "channel_ids = '#{Spree::Current.channel.id}'" if Spree::Current.channel
conditions << "status = 'active'"
conditions << "locale = '#{locale.to_s.gsub(/[^a-zA-Z_-]/, '')}'"
conditions << "currency = '#{currency.to_s.gsub(/[^A-Z]/, '')}'"
- conditions << "(discontinue_on = 0 OR discontinue_on > #{Time.current.to_i})"
+ # Exclude future-dated products — mirrors +Product.available(Time.current)+.
+ # ISO 8601 strings sort lexicographically in chronological order, so the
+ # string compare is sound. +NOT EXISTS+ catches docs indexed before this
+ # attribute was emitted (legacy indexes), +IS NULL+ catches docs where
+ # the field was emitted as explicit null, and the +<=+ clause filters
+ # the remaining future-dated docs — so the upgrade is non-breaking and
+ # no reindex is required.
+ conditions << "(available_on NOT EXISTS OR available_on IS NULL OR available_on <= '#{now.iso8601}')"
+ conditions << "(discontinue_on = 0 OR discontinue_on > #{now.to_i})"
conditions
end