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.

Close the remaining review findings on the gift card and media flows

Damian Legawiec64afc5c0ba0a
diff --git a/spree/core/app/workflows/spree/gift_cards/apply.rb b/spree/core/app/workflows/spree/gift_cards/apply.rb
index de7828cc71..1cc837d35c 100644
--- a/spree/core/app/workflows/spree/gift_cards/apply.rb
+++ b/spree/core/app/workflows/spree/gift_cards/apply.rb
@@ -45,6 +45,13 @@ module Spree
failure(order, :gift_card_using_store_credit_error) if order.using_store_credit?
failure(order, :gift_card_mismatched_currency) if gift_card.currency != order.currency
+ # The store controllers check these before they get here, but this
+ # workflow is the shared entry point every caller now uses, so a card
+ # that cannot be spent is refused here rather than only at the edge.
+ failure(order, :gift_card_expired) if gift_card.expired?
+ failure(order, :gift_card_already_redeemed) if gift_card.redeemed?
+ failure(order, :gift_card_canceled) if gift_card.canceled?
+
return if gift_card.customer.blank?
# A card issued to someone is theirs alone.
diff --git a/spree/core/app/workflows/spree/products/nested_attributes.rb b/spree/core/app/workflows/spree/products/nested_attributes.rb
index 90264a5fd3..8357d6cfc7 100644
--- a/spree/core/app/workflows/spree/products/nested_attributes.rb
+++ b/spree/core/app/workflows/spree/products/nested_attributes.rb
@@ -118,18 +118,26 @@ module Spree
# SSRF filtering, a size cap, and skipping a URL this product has
# already fetched.
#
+ # Enqueued after the transaction commits, because the job loads the
+ # product by id: a worker picking it up mid-transaction would not find
+ # a product that is not visible to its connection yet.
+ #
# The asset is always attached to the product so one file is not
# downloaded once per variant; `variant_id` links the resulting asset to
# a variant afterwards.
def enqueue_media_download(product, external_url, attrs)
- Spree::Images::SaveFromUrlJob.perform_later(
+ arguments = [
product.id,
'Spree::Product',
external_url,
attrs[:external_id],
attrs[:position],
attrs[:variant_id]
- )
+ ]
+
+ ActiveRecord.after_all_transactions_commit do
+ Spree::Images::SaveFromUrlJob.perform_later(*arguments)
+ end
end
end
end
diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.yml
index d836da9a17..a8658f5792 100644
--- a/spree/core/config/locales/en.yml
+++ b/spree/core/config/locales/en.yml
@@ -1002,9 +1002,11 @@ en:
general: General
generate_code: Generate coupon code
gift_card: Gift Card
+ gift_card_already_canceled: The Gift Card has already been canceled.
gift_card_already_redeemed: The Gift Card has already been redeemed.
gift_card_applied: The Gift Card was successfully applied to your order!
gift_card_batch: Gift Card Batch
+ gift_card_canceled: The Gift Card has been canceled.
gift_card_customer_not_logged_in: Gift Card cannot be applied with guest user.
gift_card_expired: The Gift Card has expired.
gift_card_mismatched_currency: Gift Card cannot be applied with current currency.
diff --git a/spree/core/spec/workflows/spree/gift_cards/apply_spec.rb b/spree/core/spec/workflows/spree/gift_cards/apply_spec.rb
index c6f8eed5e8..8e9f4f3ccb 100644
--- a/spree/core/spec/workflows/spree/gift_cards/apply_spec.rb
+++ b/spree/core/spec/workflows/spree/gift_cards/apply_spec.rb
@@ -134,6 +134,31 @@ RSpec.describe Spree::GiftCards::Apply do
end
end
+ # The store controllers check these at the edge, but the workflow is the
+ # shared entry point, so it refuses a card that cannot be spent.
+ context 'when the gift card cannot be spent' do
+ it 'refuses an expired card' do
+ gift_card.update!(expires_at: 1.day.ago)
+
+ expect(subject).not_to be_success
+ expect(subject.error.value).to eq(:gift_card_expired)
+ end
+
+ it 'refuses a canceled card' do
+ gift_card.update!(status: 'canceled')
+
+ expect(subject).not_to be_success
+ expect(subject.error.value).to eq(:gift_card_canceled)
+ end
+
+ it 'refuses a redeemed card' do
+ gift_card.update!(status: 'redeemed')
+
+ expect(subject).not_to be_success
+ expect(subject.error.value).to eq(:gift_card_already_redeemed)
+ end
+ end
+
context 'when the gift card has no amount remaining' do
before { gift_card.update!(amount_used: gift_card.amount) }
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 a939b13734..3ce774d990 100644
--- a/spree/core/spec/workflows/spree/products/status_transitions_spec.rb
+++ b/spree/core/spec/workflows/spree/products/status_transitions_spec.rb
@@ -76,6 +76,23 @@ RSpec.describe 'Spree::Products nested attributes' do
)
}.to have_enqueued_job(Spree::Images::SaveFromUrlJob)
end
+
+ # The job loads the product by id, so a worker must not see it before the
+ # transaction that created it has committed.
+ it 'does not enqueue the download until the transaction commits' do
+ enqueued_mid_transaction = nil
+
+ ApplicationRecord.transaction do
+ Spree.product_create_workflow.call(
+ store: store,
+ attributes: { name: 'Deferred Media', media: [{ external_url: 'https://example.com/b.jpg' }] }
+ )
+ enqueued_mid_transaction = enqueued_jobs.count { |job| job['job_class'] == 'Spree::Images::SaveFromUrlJob' }
+ end
+
+ expect(enqueued_mid_transaction).to eq(0)
+ expect(enqueued_jobs.count { |job| job['job_class'] == 'Spree::Images::SaveFromUrlJob' }).to eq(1)
+ end
end
RSpec.describe 'Spree::Products status workflows' do