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.

Refuse a warehouse filter that names something else

Damian Legawiec9d30685ddda6
diff --git a/spree/core/app/models/spree/variant.rb b/spree/core/app/models/spree/variant.rb
index 1a183e160d..8197b48197 100644
--- a/spree/core/app/models/spree/variant.rb
+++ b/spree/core/app/models/spree/variant.rb
@@ -181,7 +181,14 @@ module Spree
# suffix.
scope :available_at_stock_location, ->(stock_location) {
levels = Spree::StockLevel.table_name
- location_id = Spree::PrefixedId.decode_prefixed_id(stock_location) || stock_location
+ # Decoded against the stock location's own prefix, so another model's ID
+ # cannot resolve to a warehouse whose numeric payload happens to match.
+ # Anything that is neither that nor a raw id selects nothing rather than
+ # reaching the database, where a non-numeric value raises on PostgreSQL.
+ location_id = Spree::StockLocation.decode_own_prefixed_id(stock_location)
+ location_id ||= stock_location if stock_location.to_s.match?(/\A\d+\z/)
+
+ next none if location_id.blank?
joins(:stock_levels).
where(levels => { stock_location_id: location_id }).
diff --git a/spree/core/spec/models/spree/variant_spec.rb b/spree/core/spec/models/spree/variant_spec.rb
index c3f20c12c1..36d3b71bef 100644
--- a/spree/core/spec/models/spree/variant_spec.rb
+++ b/spree/core/spec/models/spree/variant_spec.rb
@@ -282,6 +282,15 @@ describe Spree::Variant, type: :model do
expect(Spree::Variant.available_at_stock_location(source.prefixed_id)).not_to include(stocked)
end
+
+ # The scope is ransackable, so the argument is whatever a client sent.
+ it 'selects nothing for an ID belonging to another model' do
+ expect(Spree::Variant.available_at_stock_location(stocked.product.prefixed_id)).to be_empty
+ end
+
+ it 'selects nothing for a value that is no ID at all' do
+ expect(Spree::Variant.available_at_stock_location('not-an-id')).to be_empty
+ end
end
describe '.eligible' do