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.

Fix what the workflow conversions dropped

Damian Legawiec5b218ec1ce1c
diff --git a/spree/core/app/models/concerns/spree/purchase/gift_cards.rb b/spree/core/app/models/concerns/spree/purchase/gift_cards.rb
index 0a1f344adb..81e1a5246a 100644
--- a/spree/core/app/models/concerns/spree/purchase/gift_cards.rb
+++ b/spree/core/app/models/concerns/spree/purchase/gift_cards.rb
@@ -34,6 +34,9 @@ module Spree
Spree.gift_card_remove_workflow.call(order: self)
end
+ # @return [Spree::ServiceModule::Result, nil] the caller decides what a
+ # refusal means; Orders::Complete reports it rather than failing the
+ # order, since the card was already drawn down when it was applied.
def redeem_gift_card
return unless gift_card.present?
diff --git a/spree/core/app/models/spree/gift_card.rb b/spree/core/app/models/spree/gift_card.rb
index ac765ab468..c1ef9664c1 100644
--- a/spree/core/app/models/spree/gift_card.rb
+++ b/spree/core/app/models/spree/gift_card.rb
@@ -180,7 +180,9 @@ module Spree
result = workflow.call(gift_card: self)
if result.failure?
- errors.add(:base, result.error.value.to_s)
+ # ResultError#to_s unwraps an ActiveModel::Errors into its full
+ # messages; its `value` would inspect the object into the message.
+ errors.add(:base, result.error.to_s)
raise ActiveRecord::RecordInvalid, self
end
diff --git a/spree/core/app/models/spree/import.rb b/spree/core/app/models/spree/import.rb
index da03100d41..a434c4b72c 100644
--- a/spree/core/app/models/spree/import.rb
+++ b/spree/core/app/models/spree/import.rb
@@ -445,7 +445,9 @@ module Spree
result = workflow.call(import: self)
if result.failure?
- errors.add(:base, result.error.value.to_s)
+ # ResultError#to_s unwraps an ActiveModel::Errors into its full
+ # messages; its `value` would inspect the object into the message.
+ errors.add(:base, result.error.to_s)
raise ActiveRecord::RecordInvalid, self
end
diff --git a/spree/core/app/models/spree/invitation.rb b/spree/core/app/models/spree/invitation.rb
index 0025a7b868..46a09a94d9 100644
--- a/spree/core/app/models/spree/invitation.rb
+++ b/spree/core/app/models/spree/invitation.rb
@@ -88,7 +88,9 @@ module Spree
result = Spree.invitation_accept_workflow.call(invitation: self)
if result.failure?
- errors.add(:base, result.error.value.to_s)
+ # ResultError#to_s unwraps an ActiveModel::Errors into its full
+ # messages; its `value` would inspect the object into the message.
+ errors.add(:base, result.error.to_s)
raise ActiveRecord::RecordInvalid, self
end
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rb
index f36bbe1498..eb59bf1bcd 100644
--- a/spree/core/app/models/spree/product.rb
+++ b/spree/core/app/models/spree/product.rb
@@ -922,7 +922,9 @@ module Spree
result = workflow.call(product: self)
if result.failure?
- errors.add(:base, result.error.value.to_s)
+ # ResultError#to_s unwraps an ActiveModel::Errors into its full
+ # messages; its `value` would inspect the object into the message.
+ errors.add(:base, result.error.to_s)
raise ActiveRecord::RecordInvalid, self
end
diff --git a/spree/core/app/models/spree/promotion_handler/coupon.rb b/spree/core/app/models/spree/promotion_handler/coupon.rb
index 8e35167406..1e7d1cb1a2 100644
--- a/spree/core/app/models/spree/promotion_handler/coupon.rb
+++ b/spree/core/app/models/spree/promotion_handler/coupon.rb
@@ -29,7 +29,18 @@ module Spree
if result.success?
set_success_code(:gift_card_applied)
else
- set_error_code(result.error.value)
+ # A workflow failure carries a symbolic code, but a hook rejection
+ # carries an ActiveModel::Errors — translating that would render
+ # the object's inspect string to the shopper.
+ error = result.error.value
+ if error.is_a?(Symbol)
+ set_error_code(error)
+ else
+ @status_code = :gift_card_not_applied
+ # The rejection message is what the hook author wrote for the
+ # shopper; fall back to the generic string if it says nothing.
+ @error = result.error.to_s.presence || Spree.t(:gift_card_not_applied)
+ end
end
return self
diff --git a/spree/core/app/workflows/spree/imports/complete.rb b/spree/core/app/workflows/spree/imports/complete.rb
index 2ac31a64fd..6434dc3121 100644
--- a/spree/core/app/workflows/spree/imports/complete.rb
+++ b/spree/core/app/workflows/spree/imports/complete.rb
@@ -9,6 +9,7 @@ module Spree
def perform(import:)
super
+ step :ensure_processing
run_hooks :validate
ApplicationRecord.transaction do
@@ -23,6 +24,13 @@ module Spree
private
+ # Only an import that is working through its rows can finish. Without
+ # this a failed or half-mapped import would flip to completed, touch the
+ # store and announce itself as done.
+ def ensure_processing
+ failure(import, :import_not_processing) unless import.status == 'processing'
+ end
+
def mark_completed
failure(import) unless import.update(status: 'completed')
end
diff --git a/spree/core/app/workflows/spree/imports/start_processing.rb b/spree/core/app/workflows/spree/imports/start_processing.rb
index ada25972f1..850e0b1490 100644
--- a/spree/core/app/workflows/spree/imports/start_processing.rb
+++ b/spree/core/app/workflows/spree/imports/start_processing.rb
@@ -9,6 +9,7 @@ module Spree
def perform(import:)
super
+ step :ensure_mapping_completed
run_hooks :validate
ApplicationRecord.transaction do
@@ -21,6 +22,12 @@ module Spree
private
+ # Rows only exist once the mapping is accepted, so processing cannot
+ # start before that.
+ def ensure_mapping_completed
+ failure(import, :import_mapping_not_completed) unless import.status == 'completed_mapping'
+ end
+
def mark_processing
failure(import) unless import.update(status: 'processing')
end
diff --git a/spree/core/app/workflows/spree/orders/complete.rb b/spree/core/app/workflows/spree/orders/complete.rb
index 0c139f8448..3bf6a2afec 100644
--- a/spree/core/app/workflows/spree/orders/complete.rb
+++ b/spree/core/app/workflows/spree/orders/complete.rb
@@ -86,10 +86,22 @@ module Spree
Spree::CouponCodes::CouponCodesHandler.new(order: order).use_all_codes
end
+ # A refused redemption does not fail the order: the card was already
+ # drawn down when it was applied, and the customer's order is placed.
+ # It is reported rather than swallowed, because a card that cannot be
+ # marked spent is a discrepancy someone has to look at.
def redeem_gift_card
return if order.gift_card.nil?
- Spree.gift_card_redeem_workflow.call(gift_card: order.gift_card)
+ result = Spree.gift_card_redeem_workflow.call(gift_card: order.gift_card)
+ return if result.success?
+
+ Rails.error.report(
+ Spree::Core::GatewayError.new("Gift card #{order.gift_card.id} could not be redeemed: #{result.error}"),
+ handled: true,
+ context: { order_id: order.id, gift_card_id: order.gift_card.id },
+ source: 'spree.core'
+ )
end
# Providers that opt into auto_fulfill? (digital delivery today) get
diff --git a/spree/core/app/workflows/spree/price_lists/create.rb b/spree/core/app/workflows/spree/price_lists/create.rb
index 948d54792a..12f3238d81 100644
--- a/spree/core/app/workflows/spree/price_lists/create.rb
+++ b/spree/core/app/workflows/spree/price_lists/create.rb
@@ -32,8 +32,11 @@ module Spree
# Held back from the new record so it saves as a plain row; the
# collections are reconciled once it has an id.
def build_price_list
- @bulk_payloads = attributes.slice(:product_ids, :prices)
- @price_list = store.price_lists.new(attributes.except(:product_ids, :prices))
+ # See Spree::PriceLists::Update on the indifferent access.
+ attrs = attributes.to_h.with_indifferent_access
+
+ @bulk_payloads = attrs.slice(:product_ids, :prices)
+ @price_list = store.price_lists.new(attrs.except(:product_ids, :prices))
end
def save_price_list
diff --git a/spree/core/app/workflows/spree/price_lists/update.rb b/spree/core/app/workflows/spree/price_lists/update.rb
index 74214f0ef8..8a92c25d42 100644
--- a/spree/core/app/workflows/spree/price_lists/update.rb
+++ b/spree/core/app/workflows/spree/price_lists/update.rb
@@ -36,10 +36,14 @@ module Spree
# The bulk payloads are held back rather than assigned, so the record
# saves as a plain row and the collections are reconciled afterwards.
def assign_attributes
- @product_ids = attributes.key?(:product_ids) ? Array(attributes[:product_ids]).compact.uniq : nil
- @prices = attributes.key?(:prices) ? attributes[:prices] : nil
+ # Indifferent access: a host app or importer may pass string keys, and
+ # a missed key would send the bulk payload to the association setter.
+ attrs = attributes.to_h.with_indifferent_access
- price_list.assign_attributes(attributes.except(:product_ids, :prices))
+ @product_ids = attrs.key?(:product_ids) ? Array(attrs[:product_ids]).compact.uniq : nil
+ @prices = attrs.key?(:prices) ? attrs[:prices] : nil
+
+ price_list.assign_attributes(attrs.except(:product_ids, :prices))
end
def save_price_list
diff --git a/spree/core/app/workflows/spree/products/create.rb b/spree/core/app/workflows/spree/products/create.rb
index 92eaa3d6e8..f580355b28 100644
--- a/spree/core/app/workflows/spree/products/create.rb
+++ b/spree/core/app/workflows/spree/products/create.rb
@@ -55,11 +55,16 @@ module Spree
@product.store = store
return if attributes.blank?
+ # Indifferent access because this is a public entry point: a host app
+ # or importer passing a plain string-keyed hash must not have its
+ # nested payloads fall through to the ActiveRecord collection setters.
+ attrs = attributes.to_h.with_indifferent_access
+
# Held back from the record: these are reconciled after the insert,
# against a product that has an id.
- @variants_params = attributes[:variants]
- @media_params = attributes[:media]
- @product.assign_attributes(attributes.except(:variants, :media))
+ @variants_params = attrs[:variants]
+ @media_params = attrs[:media]
+ @product.assign_attributes(attrs.except(:variants, :media))
end
def save_product
diff --git a/spree/core/app/workflows/spree/products/update.rb b/spree/core/app/workflows/spree/products/update.rb
index 9ab21cd4fb..11c63586af 100644
--- a/spree/core/app/workflows/spree/products/update.rb
+++ b/spree/core/app/workflows/spree/products/update.rb
@@ -36,12 +36,17 @@ module Spree
private
def assign_attributes
+ # See Spree::Products::Create — string-keyed hashes reach this from
+ # host apps and importers, and a missed key would send the nested
+ # payload to the ActiveRecord collection setter instead.
+ attrs = attributes.to_h.with_indifferent_access
+
# Held back: variants and media are reconciled after the save, so a
# :validate handler reads `product.changes` describing the edit itself
# rather than a half-applied collection.
- @variants_params = attributes[:variants]
- @media_params = attributes[:media]
- product.assign_attributes(attributes.except(:variants, :media))
+ @variants_params = attrs[:variants]
+ @media_params = attrs[:media]
+ product.assign_attributes(attrs.except(:variants, :media))
end
def save_product
diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.yml
index 667d51fd7b..d836da9a17 100644
--- a/spree/core/config/locales/en.yml
+++ b/spree/core/config/locales/en.yml
@@ -1009,6 +1009,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_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
gift_card_using_store_credit_error: You can't apply the Gift Card after you applied the store credit.
diff --git a/spree/core/spec/models/spree/deprecated_transition_verbs_spec.rb b/spree/core/spec/models/spree/deprecated_transition_verbs_spec.rb
index fa772cb271..61a89c7223 100644
--- a/spree/core/spec/models/spree/deprecated_transition_verbs_spec.rb
+++ b/spree/core/spec/models/spree/deprecated_transition_verbs_spec.rb
@@ -54,6 +54,20 @@ RSpec.describe 'deprecated transition verbs' do
end
end
+ # A workflow failure carries an ActiveModel::Errors; inspecting it into the
+ # message is what leaks "#<ActiveModel::Errors:0x...>" into an API 422.
+ describe 'the message a refusal raises' do
+ it 'reads the errors rather than inspecting them' do
+ product = create(:product, store: store, status: 'draft')
+ allow(product).to receive(:save).and_return(false)
+ product.errors.add(:name, 'is bad')
+
+ expect { product.activate! }.to raise_error(ActiveRecord::RecordInvalid) do |error|
+ expect(error.message).not_to include('ActiveModel::Errors')
+ end
+ end
+ end
+
describe Spree::PriceList do
let(:price_list) { create(:price_list, store: store) }
diff --git a/spree/core/spec/workflows/spree/imports/pipeline_spec.rb b/spree/core/spec/workflows/spree/imports/pipeline_spec.rb
index bfd852b4df..8e89e2b10a 100644
--- a/spree/core/spec/workflows/spree/imports/pipeline_spec.rb
+++ b/spree/core/spec/workflows/spree/imports/pipeline_spec.rb
@@ -107,3 +107,29 @@ RSpec.describe 'Spree::Imports pipeline workflows' do
end
end
end
+
+# The state machine guarded these transitions with `from:`; the workflows must
+# keep those guards or a failed import can flip to completed and announce
+# itself as done.
+RSpec.describe 'Spree::Imports status guards' do
+ let(:store) { Spree::Store.default }
+ let(:import) { create(:product_import, owner: store) }
+
+ it 'refuses to complete an import that is not processing' do
+ import.update!(status: 'failed')
+
+ result = Spree.import_complete_workflow.call(import: import)
+
+ expect(result).not_to be_success
+ expect(result.error.value).to eq(:import_not_processing)
+ expect(import.reload.status).to eq('failed')
+ end
+
+ it 'refuses to start processing before the mapping is accepted' do
+ result = Spree.import_start_processing_workflow.call(import: import)
+
+ expect(result).not_to be_success
+ expect(result.error.value).to eq(:import_mapping_not_completed)
+ expect(import.reload.status).to eq('pending')
+ end
+end
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 0b2b30fc2d..b7e4cf722c 100644
--- a/spree/core/spec/workflows/spree/price_lists/write_spec.rb
+++ b/spree/core/spec/workflows/spree/price_lists/write_spec.rb
@@ -39,6 +39,13 @@ RSpec.describe 'Spree::PriceLists write workflows' do
expect(price_list.reload.name).to eq('Renamed')
end
+ it 'accepts a string-keyed payload' do
+ result = described_class.call(price_list: price_list, attributes: { 'name' => 'StringKeys' })
+
+ expect(result).to be_success
+ expect(price_list.reload.name).to eq('StringKeys')
+ end
+
it 'adds and removes products' do
other = create(:product, store: store)
diff --git a/spree/core/spec/workflows/spree/products/status_transitions_spec.rb b/spree/core/spec/workflows/spree/products/status_transitions_spec.rb
index 1105343524..a939b13734 100644
--- a/spree/core/spec/workflows/spree/products/status_transitions_spec.rb
+++ b/spree/core/spec/workflows/spree/products/status_transitions_spec.rb
@@ -53,6 +53,19 @@ RSpec.describe 'Spree::Products nested attributes' do
Spree.hooks.clear!
end
+ # These are public entry points: a host app or importer passing a plain
+ # string-keyed hash must not have its nested payload fall through to the
+ # ActiveRecord collection setter, which rejects hashes.
+ it 'accepts a string-keyed payload' do
+ result = Spree.product_create_workflow.call(
+ store: store,
+ attributes: { 'name' => 'StringKeys', 'variants' => [{ 'sku' => 'SK-1', 'options' => [] }] }
+ )
+
+ expect(result).to be_success
+ expect(result.value.variants.first.sku).to eq('SK-1')
+ end
+
it 'enqueues a download for media given as an external url' do
product = create(:product, store: store)