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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Address review: empty payload is not a removal, and zero is not a discount
Damian Legawiec7212171024ea
diff --git a/packages/admin-sdk/src/params.ts b/packages/admin-sdk/src/params.tsindex 92e508a8dd..b182ce671f 100644--- a/packages/admin-sdk/src/params.ts+++ b/packages/admin-sdk/src/params.ts@@ -2575,10 +2575,15 @@ export interface CatalogParams {price_list_id?: string | null/*** The price list this catalog prices through, written inline: an object- * creates the owned list or updates the one already there, and an explicit- * `null` detaches it. Detaching is deliberate — a released list starts- * matching by its own rules again, and a rule-less list applies to every- * shopper. Omit the key to leave the list alone.+ * creates the owned list or updates the one already there. An explicit+ * `null` **deletes** that list — a list a catalog owns carries no rules of+ * its own, so releasing it into general matching would price every+ * shopper; it is a soft delete, so the prices stay recoverable. Omit the+ * key, or send `{}`, to leave the pricing alone.+ *+ * Do not send this together with `price_list_id`: a catalog may only own+ * its own list, and the pair is refused rather than letting one catalog+ * edit another's pricing.*/price_list?: CatalogPriceListParams | nullmetadata?: Record<string, unknown>diff --git a/packages/dashboard/src/locales/fr.json b/packages/dashboard/src/locales/fr.jsonindex 7e2911f858..aafdf23202 100644--- a/packages/dashboard/src/locales/fr.json+++ b/packages/dashboard/src/locales/fr.json@@ -1435,7 +1435,7 @@},"price_list": {"adjust_compare_at": {- "help": "Déduire le prix barré du prix barré de base avec le même pourcentage.",+ "help": "Calculer le prix barré à partir du prix barré de base en appliquant le même pourcentage.","label": "Ajuster aussi les prix barrés"},"adjustment_direction": {diff --git a/packages/dashboard/src/schemas/price-list.ts b/packages/dashboard/src/schemas/price-list.tsindex 2808e944ae..7a0dad2c3d 100644--- a/packages/dashboard/src/schemas/price-list.ts+++ b/packages/dashboard/src/schemas/price-list.ts@@ -166,7 +166,10 @@ export function adjustmentFormValues(percentage: string | null | undefined): {adjustment_magnitude: string} {const parsed = percentage == null || percentage === '' ? null : Number(percentage)- if (parsed === null || !Number.isFinite(parsed)) {+ // Zero is no adjustment, and the form's own rules require a magnitude+ // above zero for automatic mode — loading it as automatic would leave the+ // page unsaveable until the user noticed the error.+ if (parsed === null || !Number.isFinite(parsed) || parsed === 0) {return { pricing_mode: 'fixed', adjustment_direction: 'decrease', adjustment_magnitude: '' }}diff --git a/spree/core/app/models/spree/catalog.rb b/spree/core/app/models/spree/catalog.rbindex e5291129fc..5e822c5cd1 100644--- a/spree/core/app/models/spree/catalog.rb+++ b/spree/core/app/models/spree/catalog.rb@@ -183,6 +183,10 @@ module Spree# lands in the save rather than on assignment.def price_list=(list)@pending_price_list = list+ # This writer states the whole selection, so an id an earlier+ # `price_list_id=` recorded is spent — left behind, it would reject a+ # detach as an unresolvable id.+ remove_instance_variable(:@pending_price_list_id) if defined?(@pending_price_list_id)end# Reads back an assigned-but-unsaved binding, so callers see what theydiff --git a/spree/core/app/models/spree/price_list.rb b/spree/core/app/models/spree/price_list.rbindex 04419f78b1..bd162b6d8e 100644--- a/spree/core/app/models/spree/price_list.rb+++ b/spree/core/app/models/spree/price_list.rb@@ -45,10 +45,11 @@ module Spreevalidates :catalog_id, uniqueness: { scope: spree_base_uniqueness_scope,conditions: -> { where(deleted_at: nil) } }, allow_nil: true# Greater than -100: at exactly -100 every derived price is zero, and- # below it the arithmetic goes negative. The upper end is deliberately- # open — a markup has no natural ceiling.+ # below it the arithmetic goes negative. Capped at what the+ # `decimal(6,3)` column can hold, so an accepted value cannot fail on the+ # way to the database.validates :price_adjustment_percentage,- numericality: { greater_than: -100 }, allow_nil: true+ numericality: { greater_than: -100, less_than: 1000 }, allow_nil: truevalidate :starts_at_before_ends_atvalidate :catalog_in_same_storevalidate :percentage_requires_catalog@@ -146,7 +147,10 @@ module Spree# only what it holds explicit rows for.# @return [Boolean]def automatic_pricing?- price_adjustment_percentage.present?+ # Zero is no adjustment at all: deriving base × 1.0 would stamp every+ # price with this list's id while changing nothing, and the dashboard+ # has no "0%" to show. It reads as a fixed list.+ price_adjustment_percentage.present? && !price_adjustment_percentage.zero?end# What a base price is multiplied by to derive this list's price:diff --git a/spree/core/app/workflows/spree/catalogs/set_price_list.rb b/spree/core/app/workflows/spree/catalogs/set_price_list.rbindex 6529f2c9d9..4ccb41bb6f 100644--- a/spree/core/app/workflows/spree/catalogs/set_price_list.rb+++ b/spree/core/app/workflows/spree/catalogs/set_price_list.rb@@ -22,8 +22,12 @@ module Spreeprivate+ # Only an explicit nil removes the list. `{}` is a caller saying+ # "change nothing about the pricing", and destroying a list over that+ # would make an empty payload the most destructive one.def write_price_list- return detach if attributes.blank?+ return detach if attributes.nil?+ return if attributes.empty?existing = catalog.price_listreturn update_existing(existing) if existingdiff --git a/spree/core/spec/models/spree/catalog_spec.rb b/spree/core/spec/models/spree/catalog_spec.rbindex b959207bf3..7da10f208c 100644--- a/spree/core/spec/models/spree/catalog_spec.rb+++ b/spree/core/spec/models/spree/catalog_spec.rb@@ -352,6 +352,20 @@ describe Spree::Catalog, type: :model doexpect(catalog.errors[:price_list]).to be_presentend+ # The association writer states the whole selection, so an id recorded+ # by an earlier `price_list_id=` is spent — otherwise it rejects the+ # detach as an unresolvable id.+ it 'lets a later detach override an id assigned first' do+ catalog = create(:catalog, store: store)++ catalog.price_list_id = price_list.id+ catalog.price_list = nil++ expect(catalog).to be_valid+ expect(catalog.save).to be true+ expect(catalog.reload.price_list).to be_nil+ end+it 'does not reach another store list' docatalog = create(:catalog, store: store)foreign = create(:price_list, store: create(:store))diff --git a/spree/core/spec/models/spree/price_list_spec.rb b/spree/core/spec/models/spree/price_list_spec.rbindex 246dfe65ac..9373b33d76 100644--- a/spree/core/spec/models/spree/price_list_spec.rb+++ b/spree/core/spec/models/spree/price_list_spec.rb@@ -20,6 +20,22 @@ describe Spree::PriceList, type: :model doexpect(build(:price_list, price_adjustment_percentage: -15)).to be_automatic_pricingend+ # Zero would derive base × 1.0 — every price stamped with this list's id+ # and not one of them changed.+ it 'treats a zero percentage as no adjustment at all' do+ expect(build(:price_list, price_adjustment_percentage: 0)).not_to be_automatic_pricing+ end++ # The column is decimal(6,3); anything larger would be accepted here and+ # fail on the way to the database.+ it 'refuses a markup the column cannot hold' do+ catalog = create(:catalog, store: @default_store)+ owned = ->(pct) { build(:price_list, store: @default_store, catalog: catalog, price_adjustment_percentage: pct) }++ expect(owned.call(1000)).not_to be_valid+ expect(owned.call(999.999)).to be_valid+ end+it 'turns the percentage into the factor a base price is multiplied by' doexpect(build(:price_list, price_adjustment_percentage: -15).adjustment_factor).to eq(0.85)expect(build(:price_list, price_adjustment_percentage: 10).adjustment_factor).to eq(1.1)diff --git a/spree/core/spec/workflows/spree/catalogs/set_price_list_spec.rb b/spree/core/spec/workflows/spree/catalogs/set_price_list_spec.rbindex f12c828267..aaddd49e48 100644--- a/spree/core/spec/workflows/spree/catalogs/set_price_list_spec.rb+++ b/spree/core/spec/workflows/spree/catalogs/set_price_list_spec.rb@@ -69,6 +69,19 @@ describe Spree::Catalogs::SetPriceList doexpect(Spree::PricingProvider::Internal::Resolution.new(context).resolve.amount).to eq(20)end+ # Only an explicit nil removes the list. An empty payload says "change+ # nothing", and destroying over that would make the emptiest request the+ # most destructive one.+ it 'leaves the list alone on an empty payload' do+ call(price_adjustment_percentage: '-15')+ list = catalog.reload.price_list++ expect(described_class.call(catalog: catalog, attributes: {})).to be_success++ expect(catalog.reload.price_list).to eq(list)+ expect(Spree::PriceList.where(id: list.id)).to be_present+ end+it 'is a no-op when detaching a catalog that owns nothing' doexpect(described_class.call(catalog: catalog, attributes: nil)).to be_successexpect(catalog.reload.price_list).to be_nil