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.

Report why an invalid price list was rejected, and keep the catalog's timestamp honest

Damian Legawiecaf06d17c6d71
diff --git a/docs/plans/6.0-catalog-agreement-rework.md b/docs/plans/6.0-catalog-agreement-rework.md
index 664d3c921a..b6ff3f041d 100644
--- a/docs/plans/6.0-catalog-agreement-rework.md
+++ b/docs/plans/6.0-catalog-agreement-rework.md
@@ -98,10 +98,11 @@ catalog page this rework establishes.
adopts catalogs for new agreements at whatever pace suits it.
- Admin API: catalogs create/update accept `price_list: {...}` (create or
update the owned list inline); the price-lists endpoints keep working for
- standalone lists; detach = `price_list: null`-style explicit action.
+ standalone lists; `price_list: null` removes the owned list.
- Dashboard: catalog page (agreement editor + products-with-prices view +
- detach warning), New catalog wizard, price-list page banner for owned
- lists linking to their catalog.
+ removal warning), New catalog page. No price-list-page banner for owned
+ lists — they are excluded from that index entirely, since a page offering
+ rules and a schedule describes nothing an owned list has.
## Migration Path
@@ -129,19 +130,24 @@ catalog page this rework establishes.
match). Deliberately no warnings, no bridge and no data task — see the
revised Key Decision above.
4. **API + dashboard** — inline `price_list` payload, agreement editor,
- products-with-prices view, wizard, detach warning. **Partly done
+ products-with-prices view, wizard, removal warning. **Partly done
2026-08-30**: the inline payload (`Spree::Catalogs::Create`/`Update`
- delegating to `SetPriceList` — create, update in place, or detach on an
+ delegating to `SetPriceList` — create, update in place, or remove on an
explicit null; a list a catalog owns is named after the catalog and born
active, since the catalog's own `active` flag already gates the
- agreement) and the agreement editor's Pricing card (normal prices / ±% /
- fixed, with the detach warning stating that a released list starts
- matching by its own rules). The ±% controls landed with
- `6.0-price-list-automatic-pricing.md`. **Still open:** the
- products-with-prices view, and the pricing choice on the New-catalog
- sheet — it still asks only for a name, so "one go" is not yet true at
- creation time. The Terms card arrives with
- `6.0-b2b-quantity-rules.md`.
+ agreement) and the agreement editor, where Pricing is its own card
+ (normal prices / ±% / fixed) between Catalog and Audiences. Fixed mode
+ opens the bulk price spreadsheet inline — it is only half-configured
+ until amounts exist. Catalogs gained a `description` like price lists,
+ shown in the index; the nav puts Catalogs above Price Lists; and the
+ price-lists index lists standalone lists only. Two warnings state
+ consequences before Save: removing the pricing deletes the list, and
+ switching from fixed to ±% discards the entered amounts. The ±% controls
+ landed with `6.0-price-list-automatic-pricing.md`. **Still open:** the
+ products-with-prices view, and the create screen — it is still a
+ name-only sheet, so "one go" is not yet true at creation time; it should
+ become a full page laid out like the price-list create screen. The Terms
+ card arrives with `6.0-b2b-quantity-rules.md`.
## Constraints on Current Work
diff --git a/spree/core/app/models/spree/catalog.rb b/spree/core/app/models/spree/catalog.rb
index 493c572b55..e5291129fc 100644
--- a/spree/core/app/models/spree/catalog.rb
+++ b/spree/core/app/models/spree/catalog.rb
@@ -40,6 +40,7 @@ module Spree
validate :price_list_in_same_store
validate :price_list_must_be_resolvable
validate :price_list_not_owned_elsewhere
+ validate :pending_price_list_is_valid
# The binding is applied inside the save, never on assignment: writing a
# has_one on a persisted record updates the child immediately, so a
@@ -258,6 +259,17 @@ module Spree
errors.add(:price_list, :invalid)
end
+ # An unsaved list is saved inside this record's own save, so its problems
+ # have to surface here — otherwise the save just returns false with no
+ # message and the merchant is told nothing.
+ def pending_price_list_is_valid
+ return if price_list.nil? || price_list.persisted? || price_list.valid?
+
+ price_list.errors.each do |error|
+ errors.add(:price_list, error.message)
+ end
+ end
+
# An id that resolved to nothing in this store is an error, not a silent
# detach — otherwise a typo reads as "remove the pricing".
def price_list_must_be_resolvable
@@ -295,10 +307,20 @@ module Spree
# to bind yet, and skipping that would silently drop it. Any list this
# catalog already owns is released first, or the one-live-list-per-
# catalog rule rejects the replacement before it can take over.
+ #
+ # `save` rather than `save!`: two concurrent creates both see no owner
+ # and both insert, and the unique index rejects the loser. That is a
+ # lost race, not a server error, so it fails the save with a message
+ # the same way a visible conflict does.
if pending && !pending.persisted?
release_owned_price_list
pending.catalog_id = id
- pending.save!
+
+ unless save_pending_list(pending)
+ raise ActiveRecord::RecordNotUnique,
+ "catalog #{id} was given a price list by another request"
+ end
+
finish_price_list_binding
return
end
@@ -332,6 +354,15 @@ module Spree
finish_price_list_binding
end
+ # Saves the new list, treating a lost race for this catalog's single
+ # live-list slot as a failure rather than an exception: the DB unique
+ # index is what decides, since two concurrent creates each see no owner.
+ def save_pending_list(price_list)
+ price_list.save
+ rescue ActiveRecord::RecordNotUnique
+ false
+ end
+
# Compare-and-swap on the release: a list another request has already
# re-homed must not be sent back to standalone matching, where a
# rule-less list prices the whole store.
@@ -357,9 +388,13 @@ module Spree
def finish_price_list_binding
association(:price_list).reset
- # The writes above skip PriceList's own after_commit, so the request's
- # memoized generic-matching set is cleared here instead — a detach must
- # be visible to the rest of this request, not the next one.
+ # The writes above skip PriceList's own callbacks, so the two things
+ # they would have done happen here instead: the `touch: true` on the
+ # child's belongs_to, without which the largest possible change to a
+ # catalog's pricing leaves its cache key untouched…
+ touch if persisted?
+ # …and clearing the request's memoized generic-matching set, so a
+ # removal is visible to the rest of this request, not the next one.
Spree::Current.price_lists = nil
end
end
diff --git a/spree/core/spec/models/spree/catalog_spec.rb b/spree/core/spec/models/spree/catalog_spec.rb
index fdc457b7ff..b959207bf3 100644
--- a/spree/core/spec/models/spree/catalog_spec.rb
+++ b/spree/core/spec/models/spree/catalog_spec.rb
@@ -323,6 +323,26 @@ describe Spree::Catalog, type: :model do
expect(Spree::PriceList.where(id: price_list.id).pick(:catalog_id)).to be_nil
end
+ # The list is saved inside the catalog's own save, so its problems have
+ # to surface as catalog errors — a bare `false` tells the merchant
+ # nothing about what went wrong.
+ it 'reports why an invalid new list was rejected' do
+ catalog = create(:catalog, store: store)
+ catalog.price_list = Spree::PriceList.new(store: store) # no name
+
+ expect(catalog.save).to be false
+ expect(catalog.errors[:price_list]).to be_present
+ end
+
+ # The binding is written straight to the row, which skips the `touch`
+ # on the child's belongs_to — so the largest change to a catalog's
+ # pricing would otherwise leave its cache key untouched.
+ it 'bumps the catalog timestamp when its pricing changes' do
+ catalog = create(:catalog, store: store)
+
+ expect { catalog.update!(price_list: price_list) }.to change { catalog.reload.updated_at }
+ end
+
describe 'the price_list_id writer' do
it 'records an unknown id as invalid rather than raising' do
catalog = create(:catalog, store: store)