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 a workflow rejection in words rather than a symbol (#14476)

Damian Legawiec56114bc2c152
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/error_handler.rb b/spree/api/app/controllers/concerns/spree/api/v3/error_handler.rb
index 522b2aae26..e0e706426f 100644
--- a/spree/api/app/controllers/concerns/spree/api/v3/error_handler.rb
+++ b/spree/api/app/controllers/concerns/spree/api/v3/error_handler.rb
@@ -133,6 +133,11 @@ module Spree
render_validation_error(error, code: code)
elsif error.is_a?(String)
render_error(code: code, message: error, status: status)
+ elsif error.is_a?(Symbol)
+ # Workflows reject with a symbol naming the reason. Translate it,
+ # falling back to the symbol so a missing key is visible rather
+ # than silently blank.
+ render_error(code: code, message: Spree.t(error, default: error.to_s.humanize), status: status)
else
render_error(code: code, message: error.to_s, status: status)
end
diff --git a/spree/core/app/workflows/spree/price_lists/create.rb b/spree/core/app/workflows/spree/price_lists/create.rb
index 12f3238d81..53ed1616a5 100644
--- a/spree/core/app/workflows/spree/price_lists/create.rb
+++ b/spree/core/app/workflows/spree/price_lists/create.rb
@@ -47,7 +47,10 @@ module Spree
return if @bulk_payloads.empty?
result = Spree.price_list_update_workflow.call(price_list: price_list, attributes: @bulk_payloads)
- failure(price_list, result.error) unless result.success?
+ # `result.error` is already a ResultError; passing it whole would wrap
+ # it in a second one and hide the ActiveModel::Errors that
+ # render_result_error reads to build a 422 with field details.
+ failure(price_list, result.error.value) unless result.success?
end
end
end
diff --git a/spree/core/app/workflows/spree/price_lists/update.rb b/spree/core/app/workflows/spree/price_lists/update.rb
index 8a92c25d42..c191385250 100644
--- a/spree/core/app/workflows/spree/price_lists/update.rb
+++ b/spree/core/app/workflows/spree/price_lists/update.rb
@@ -40,7 +40,7 @@ module Spree
# a missed key would send the bulk payload to the association setter.
attrs = attributes.to_h.with_indifferent_access
- @product_ids = attrs.key?(:product_ids) ? Array(attrs[:product_ids]).compact.uniq : nil
+ @product_ids = attrs.key?(:product_ids) ? decoded_product_ids(attrs[:product_ids]) : nil
@prices = attrs.key?(:prices) ? attrs[:prices] : nil
price_list.assign_attributes(attrs.except(:product_ids, :prices))
@@ -89,6 +89,35 @@ module Spree
touch_variants(variant_ids)
end
+ # Ids arrive prefixed from the console and legacy callers, already
+ # decoded from a controller that ran normalize_params — the same two
+ # shapes price_rows handles for variant_id.
+ #
+ # Resolved against the store's own products rather than decoded and
+ # trusted. A prefix only encodes a number, so `variant_xxx` decodes to
+ # an integer that names a product just as readily; and a blank entry
+ # matches nothing, which would otherwise read as "remove every current
+ # member and add none".
+ def decoded_product_ids(ids)
+ # `nil` is not a list: Array(nil) is [], which would otherwise read as
+ # the empty array that means "clear the list".
+ return nil if ids.nil?
+
+ given = Array(ids)
+ candidates = given.filter_map do |id|
+ next if id.blank?
+
+ Spree::PrefixedId.prefixed_id?(id) ? Spree::PrefixedId.decode_prefixed_id(id) : id
+ end
+
+ # An empty array clears the list; an array of blanks is malformed
+ # input and must not read as the same instruction.
+ return nil if candidates.empty? && given.any?
+ return [] if candidates.empty?
+
+ price_list.store.products.where(id: candidates).ids
+ end
+
def price_rows
Array(@prices).filter_map do |raw|
row = raw.respond_to?(:to_unsafe_h) ? raw.to_unsafe_h.with_indifferent_access : raw.with_indifferent_access
diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.yml
index 883726ed3d..7685cdb4d6 100644
--- a/spree/core/config/locales/en.yml
+++ b/spree/core/config/locales/en.yml
@@ -1013,6 +1013,7 @@ en:
gift_card_expired: The Gift Card has expired.
gift_card_mismatched_currency: Gift Card cannot be applied with current currency.
gift_card_mismatched_customer: This gift card is associated with another user.
+ gift_card_no_amount_remaining: The Gift Card has no amount remaining.
gift_card_not_applied: The Gift Card could not be applied.
gift_card_not_found: The Gift Card was not found.
gift_card_removed: The Gift Card was successfully removed from your order
@@ -1034,6 +1035,7 @@ en:
implement_eligible_for_return: 'Must implement #eligible_for_return? for your EligibilityValidator.'
implement_requires_manual_intervention: 'Must implement #requires_manual_intervention? for your EligibilityValidator.'
import: Import
+ import_has_no_failed_rows: This import has no failed rows to retry.
import_mailer:
greeting: Hi %{user_name},
import_done:
@@ -1041,6 +1043,9 @@ en:
message: Your import has finished. %{completed_count} rows were imported successfully.
subject: Your import %{import_number} has finished processing
view_results: View import report
+ import_mapping_not_completed: The import's column mapping is not complete yet.
+ import_not_mapping: This import is not being mapped.
+ import_not_processing: This import is not processing.
in_stock: In Stock
inactive: Inactive
incl: incl.
@@ -1056,6 +1061,9 @@ en:
invalid_exchange_variant: Invalid exchange variant.
inventory: Inventory
invitation_accepted: Invitation accepted!
+ invitation_already_accepted: This invitation has already been accepted.
+ invitation_expired: This invitation has expired.
+ invitation_invitee_missing: This invitation has no invitee.
invitation_mailer:
greeting: Hi %{user_name},
invitation_accepted:
diff --git a/spree/core/spec/models/spree/price_list_spec.rb b/spree/core/spec/models/spree/price_list_spec.rb
index 7c8b63dc0b..5578949254 100644
--- a/spree/core/spec/models/spree/price_list_spec.rb
+++ b/spree/core/spec/models/spree/price_list_spec.rb
@@ -163,8 +163,10 @@ describe Spree::PriceList, type: :model do
describe 'product membership' do
let(:store) { create(:store, supported_currencies: 'USD,EUR,GBP') }
let(:price_list) { create(:price_list, store: store) }
- let(:product1) { create(:product) }
- let(:product2) { create(:product) }
+ # Same store as the list: membership resolves through the store's own
+ # products, so a product from another store is not a member to be had.
+ let(:product1) { create(:product, store: store) }
+ let(:product2) { create(:product, store: store) }
# Guards the POST/PATCH /price_lists response: membership is reconciled
# through raw upsert_all/delete_all, which bypasses the products
diff --git a/spree/core/spec/workflows/spree/price_lists/write_spec.rb b/spree/core/spec/workflows/spree/price_lists/write_spec.rb
index b7e4cf722c..45d1209c56 100644
--- a/spree/core/spec/workflows/spree/price_lists/write_spec.rb
+++ b/spree/core/spec/workflows/spree/price_lists/write_spec.rb
@@ -46,6 +46,53 @@ RSpec.describe 'Spree::PriceLists write workflows' do
expect(price_list.reload.name).to eq('StringKeys')
end
+ # Prefixed ids arrive from the console and legacy callers; comparing them
+ # against integer primary keys would read as "remove everything".
+ it 'accepts prefixed product ids' do
+ described_class.call(price_list: price_list, attributes: { product_ids: [product.prefixed_id] })
+
+ expect(price_list.product_ids).to eq([product.id])
+ end
+
+ # A prefix only encodes a number, so a variant's id decodes to an integer
+ # that names a product just as readily. Resolving through the store's own
+ # products is what stops one standing in for the other.
+ it 'ignores an id that does not name a product in this store' do
+ Spree.price_list_update_workflow.call(price_list: price_list, attributes: { product_ids: [product.id] })
+ foreign = create(:product, store: create(:store))
+
+ described_class.call(price_list: price_list, attributes: { product_ids: [product.id, foreign.id] })
+
+ expect(price_list.reload.product_ids).to eq([product.id])
+ end
+
+ # Array(nil) is [], so without a guard a nil payload reads as the empty
+ # array that means "clear the list".
+ it 'leaves membership alone when product_ids is nil' do
+ Spree.price_list_update_workflow.call(price_list: price_list, attributes: { product_ids: [product.id] })
+
+ described_class.call(price_list: price_list, attributes: { product_ids: nil })
+
+ expect(price_list.reload.product_ids).to eq([product.id])
+ end
+
+ # Malformed input is not the same instruction as an empty array.
+ it 'leaves membership alone when every id is blank' do
+ Spree.price_list_update_workflow.call(price_list: price_list, attributes: { product_ids: [product.id] })
+
+ described_class.call(price_list: price_list, attributes: { product_ids: ['', ' ', nil] })
+
+ expect(price_list.reload.product_ids).to eq([product.id])
+ end
+
+ it 'clears membership when given an empty array' do
+ Spree.price_list_update_workflow.call(price_list: price_list, attributes: { product_ids: [product.id] })
+
+ described_class.call(price_list: price_list, attributes: { product_ids: [] })
+
+ expect(price_list.reload.product_ids).to be_empty
+ end
+
it 'adds and removes products' do
other = create(:product, store: store)