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.

Sweep unbuyable lines when the customer returns, not on every write

Damian Legawiec3da89b61050b
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/cart_resolvable.rb b/spree/api/app/controllers/concerns/spree/api/v3/cart_resolvable.rb
index 6b6b398617..6577494ce4 100644
--- a/spree/api/app/controllers/concerns/spree/api/v3/cart_resolvable.rb
+++ b/spree/api/app/controllers/concerns/spree/api/v3/cart_resolvable.rb
@@ -40,8 +40,16 @@ module Spree
end
# Render the cart as JSON using the cart serializer.
- def render_cart(status: :ok)
- @cart = @cart.remove_out_of_stock_items!
+ #
+ # +sweep+ drops lines the customer can no longer buy — a discontinued
+ # product, or stock that ran out while the cart sat idle. It belongs to
+ # reads of a cart the customer is returning to, not to the write that
+ # just happened: an add that a stock check already approved must not be
+ # second-guessed by a sweep in the same request, and an external
+ # inventory provider makes that contradiction routine, since the
+ # provider answers the write while the sweep reads Spree's own rows.
+ def render_cart(status: :ok, sweep: false)
+ @cart = @cart.remove_out_of_stock_items! if sweep
render json: Spree.api.cart_serializer.new(@cart, params: serializer_params).to_h, status: status
end
diff --git a/spree/api/app/controllers/spree/api/v3/store/carts_controller.rb b/spree/api/app/controllers/spree/api/v3/store/carts_controller.rb
index 8fbeb26169..99474b0059 100644
--- a/spree/api/app/controllers/spree/api/v3/store/carts_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/carts_controller.rb
@@ -25,7 +25,9 @@ module Spree
end
end
- render_cart
+ # The customer is returning to a cart that may have sat for days,
+ # so this is where anything they can no longer buy is dropped.
+ render_cart(sweep: true)
end
# POST /api/v3/store/carts
@@ -98,7 +100,8 @@ module Spree
result = Spree.cart_associate_service.call(guest_cart: @cart, customer: current_user, guest_only: true)
if result.success?
- render_cart
+ # Signing in hands back a cart built earlier, possibly long ago.
+ render_cart(sweep: true)
else
render_service_error(result.error.to_s)
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/stock_levels_bulk_upsert_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/stock_levels_bulk_upsert_spec.rb
deleted file mode 100644
index e3c79e1365..0000000000
--- a/spree/api/spec/controllers/spree/api/v3/admin/stock_levels_bulk_upsert_spec.rb
+++ /dev/null
@@ -1,134 +0,0 @@
-require 'spec_helper'
-
-RSpec.describe Spree::Api::V3::Admin::StockLevelsController, type: :controller do
- render_views
-
- include_context 'API v3 Admin authenticated'
-
- let!(:stock_location) { create(:stock_location, store: store) }
- let!(:variant) { create(:variant) }
-
- before do
- request.headers.merge!(headers)
- variant.stock_levels.destroy_all
- create(:stock_level, variant: variant, stock_location: stock_location,
- count_on_hand: 5, adjust_count_on_hand: false)
- variant.stock_levels.reload
- end
-
- def stock_level
- variant.stock_levels.find_by(stock_location: stock_location)
- end
-
- describe 'POST #bulk_upsert' do
- it 'sets the level a feed reports' do
- post :bulk_upsert, params: {
- stock_levels: [{ variant_id: variant.prefixed_id, stock_location_id: stock_location.prefixed_id,
- count_on_hand: 12 }]
- }, as: :json
-
- expect(response).to have_http_status(:ok)
- expect(json_response['stock_level_count']).to eq(1)
- expect(stock_level.count_on_hand).to eq(12)
- end
-
- # A merchant has to be able to see why a number moved, so a feed writes
- # history rather than overwriting the figure.
- it 'records the change as a movement rather than a silent overwrite' do
- expect do
- post :bulk_upsert, params: {
- stock_levels: [{ variant_id: variant.prefixed_id, stock_location_id: stock_location.prefixed_id,
- count_on_hand: 12 }]
- }, as: :json
- end.to change(Spree::StockMovement, :count).by(1)
-
- expect(Spree::StockMovement.last.quantity).to eq(7)
- end
-
- it 'accepts a relative adjustment as well as an absolute level' do
- post :bulk_upsert, params: {
- stock_levels: [{ variant_id: variant.prefixed_id, stock_location_id: stock_location.prefixed_id,
- adjustment: -2 }]
- }, as: :json
-
- expect(stock_level.count_on_hand).to eq(3)
- end
-
- it 'addresses records by the keys the external system holds' do
- variant.set_external_id('erp', 'MAT-100')
- stock_location.set_external_id('erp', 'WH-1')
-
- post :bulk_upsert, params: {
- stock_levels: [{ variant: { external_id: { erp: 'MAT-100' } },
- stock_location: { external_id: { erp: 'WH-1' } },
- count_on_hand: 9 }]
- }, as: :json
-
- expect(response).to have_http_status(:ok)
- expect(stock_level.count_on_hand).to eq(9)
- end
-
- it 'writes many pairs in one call' do
- second = create(:variant)
- second.stock_levels.destroy_all
- create(:stock_level, variant: second, stock_location: stock_location, count_on_hand: 0,
- adjust_count_on_hand: false)
-
- post :bulk_upsert, params: {
- stock_levels: [
- { variant_id: variant.prefixed_id, stock_location_id: stock_location.prefixed_id, count_on_hand: 1 },
- { variant_id: second.prefixed_id, stock_location_id: stock_location.prefixed_id, count_on_hand: 2 }
- ]
- }, as: :json
-
- expect(json_response['stock_level_count']).to eq(2)
- expect(stock_level.count_on_hand).to eq(1)
- expect(second.stock_levels.find_by(stock_location: stock_location).count_on_hand).to eq(2)
- end
-
- it 'sets the backorder flag when the feed states it' do
- post :bulk_upsert, params: {
- stock_levels: [{ variant_id: variant.prefixed_id, stock_location_id: stock_location.prefixed_id,
- count_on_hand: 5, backorderable: false }]
- }, as: :json
-
- expect(stock_level.backorderable).to be(false)
- end
-
- it 'rejects a row that names neither a level nor an adjustment' do
- post :bulk_upsert, params: {
- stock_levels: [{ variant_id: variant.prefixed_id, stock_location_id: stock_location.prefixed_id }]
- }, as: :json
-
- expect(response).to have_http_status(:unprocessable_content)
- expect(json_response['error']['details']['rows'].first['missing']).to include('count_on_hand')
- end
-
- it 'refuses to write another store stock' do
- other_store = create(:store)
- other_location = create(:stock_location, store: other_store)
-
- post :bulk_upsert, params: {
- stock_levels: [{ variant_id: variant.prefixed_id, stock_location_id: other_location.prefixed_id,
- count_on_hand: 99 }]
- }, as: :json
-
- expect(response).to have_http_status(:unprocessable_content)
- expect(stock_level.count_on_hand).to eq(5)
- end
-
- it 'asks for the parameter rather than silently doing nothing' do
- post :bulk_upsert, params: {}, as: :json
-
- expect(response).to have_http_status(:unprocessable_content)
- expect(json_response['error']['code']).to eq('missing_stock_levels')
- end
-
- it 'accepts an empty batch as a no-op' do
- post :bulk_upsert, params: { stock_levels: [] }, as: :json
-
- expect(response).to have_http_status(:ok)
- expect(json_response['stock_level_count']).to eq(0)
- end
- end
-end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/store_data_sources_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/store_data_sources_spec.rb
deleted file mode 100644
index 83235f70fb..0000000000
--- a/spree/api/spec/controllers/spree/api/v3/admin/store_data_sources_spec.rb
+++ /dev/null
@@ -1,67 +0,0 @@
-require 'spec_helper'
-
-RSpec.describe Spree::Api::V3::Admin::StoreController, 'data sources', type: :controller do
- render_views
-
- include_context 'API v3 Admin authenticated'
-
- before { request.headers.merge!(headers) }
-
- describe 'GET #data_sources' do
- it 'lists Spree own engines as available' do
- get :data_sources, as: :json
-
- expect(response).to have_http_status(:ok)
- pricing = json_response['data']['pricing_providers']
- expect(pricing.map { |row| row['key'] }).to include('internal')
- expect(pricing.find { |row| row['key'] == 'internal' }['available']).to be(true)
- end
-
- it 'offers the failure policies a merchant can choose between' do
- get :data_sources, as: :json
-
- expect(json_response['data']['failure_policies']).to contain_exactly('fallback', 'strict')
- end
-
- # A connector gem's provider is listed but not selectable until its
- # integration is connected, so the dashboard can say why rather than
- # leaving the merchant wondering where their ERP went.
- it 'marks a provider whose integration is not connected as unavailable' do
- needs_credentials = Class.new(Spree::PricingProvider::Base) do
- def self.key = 'acme_erp'
- def self.provider_name = 'Acme ERP'
- def self.integration_class = 'SpreeAcmeErp::Integration'
- end
- Spree.pricing_providers << needs_credentials
-
- get :data_sources, as: :json
-
- row = json_response['data']['pricing_providers'].find { |item| item['key'] == 'acme_erp' }
- expect(row['available']).to be(false)
- expect(row['integration_class']).to eq('SpreeAcmeErp::Integration')
- ensure
- Spree.pricing_providers.delete(needs_credentials)
- end
- end
-
- describe 'PATCH #update' do
- it 'saves the chosen providers and policies' do
- patch :update, params: {
- preferred_pricing_provider: 'internal',
- preferred_inventory_provider: 'internal',
- preferred_pricing_provider_failure_policy: 'fallback',
- preferred_inventory_provider_failure_policy: 'strict'
- }, as: :json
-
- expect(response).to have_http_status(:ok)
- expect(json_response['preferred_pricing_provider_failure_policy']).to eq('fallback')
- expect(store.reload.preferred_inventory_provider_failure_policy).to eq('strict')
- end
-
- it 'refuses a policy that is neither falling back nor strict' do
- patch :update, params: { preferred_pricing_provider_failure_policy: 'ignore' }, as: :json
-
- expect(response).to have_http_status(:unprocessable_content)
- end
- end
-end
diff --git a/spree/core/spec/workflows/spree/carts/add_item_pricing_provider_spec.rb b/spree/core/spec/workflows/spree/carts/add_item_pricing_provider_spec.rb
deleted file mode 100644
index 4284e48451..0000000000
--- a/spree/core/spec/workflows/spree/carts/add_item_pricing_provider_spec.rb
+++ /dev/null
@@ -1,32 +0,0 @@
-require 'spec_helper'
-RSpec.describe Spree::Carts::AddItem, 'with an external pricing provider' do
- let(:store) { @default_store }
- let(:cart) { create(:cart, store: store) }
- let(:variant) { create(:variant, price: 20) }
- let(:klass) do
- Class.new(Spree::PricingProvider::Base) do
- class << self; attr_accessor :calls; end
- self.calls = 0
- def self.key = 'contract'
- def price_for(ctx)
- self.class.calls += 1
- Spree::Price.new(variant: ctx.variant, currency: ctx.currency, amount: 5)
- end
- end
- end
- before { Spree.pricing_providers << klass; stub_store_preferences(store, pricing_provider: 'contract') }
- after { Spree.pricing_providers.delete(klass) }
-
- # The second add increments an existing line; the quantity-change callback
- # used to ask the provider again from inside the transaction.
- it 'asks the provider once per add, even when incrementing an existing line' do
- Spree::Carts::AddItem.call(cart: cart, variant: variant, quantity: 1)
- klass.calls = 0
-
- Spree::Carts::AddItem.call(cart: cart, variant: variant, quantity: 1)
-
- expect(klass.calls).to eq(1)
- expect(cart.line_items.reload.first.price).to eq(5)
- expect(cart.line_items.first.quantity).to eq(2)
- end
-end