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.

Added `Spree::Config.coupon_codes_web_limit` and `Spree::Config.coupon_codes_total_limit` configuration options

Damian Legawiec1e70e57f5015
diff --git a/core/app/models/spree/promotion.rb b/core/app/models/spree/promotion.rb
index 79bf3d9bfa..eb8513e2c4 100644
--- a/core/app/models/spree/promotion.rb
+++ b/core/app/models/spree/promotion.rb
@@ -67,7 +67,7 @@ module Spree
validates :number_of_codes, numericality: {
only_integer: true,
greater_than: 0,
- less_than_or_equal_to: ENV.fetch('SPREE_PROMOTION_MAX_NUMBER_OF_CODES', 5000).to_i
+ less_than_or_equal_to: Spree::Config.coupon_codes_total_limit
}, if: -> { multi_codes? }
#
@@ -308,7 +308,7 @@ module Spree
return if number_of_codes <= coupon_codes.count
return unless saved_change_to_number_of_codes?
- if number_of_codes > 500
+ if number_of_codes > Spree::Config.coupon_codes_web_limit
Spree::CouponCodes::BulkGenerateJob.perform_later(id, number_of_codes - coupon_codes.count)
else
Spree::CouponCodes::BulkGenerate.call(promotion: self, quantity: number_of_codes - coupon_codes.count)
diff --git a/core/lib/spree/core/configuration.rb b/core/lib/spree/core/configuration.rb
index e634fe36d1..42803aac9f 100644
--- a/core/lib/spree/core/configuration.rb
+++ b/core/lib/spree/core/configuration.rb
@@ -69,6 +69,9 @@ module Spree
preference :storefront_taxons_path, :string, default: 't'
preference :storefront_pages_path, :string, default: 'pages'
+ # coupon codes
+ preference :coupon_codes_web_limit, :integer, default: 500 # number of coupon codes to be generated in the web process, more than this will be generated in a background job
+ preference :coupon_codes_total_limit, :integer, default: 5000 # the maximum number of coupon codes to be generated
attr_writer :searcher_class
end
diff --git a/core/spec/jobs/spree/coupon_codes/bulk_generate_job_spec.rb b/core/spec/jobs/spree/coupon_codes/bulk_generate_job_spec.rb
new file mode 100644
index 0000000000..2a30b104e1
--- /dev/null
+++ b/core/spec/jobs/spree/coupon_codes/bulk_generate_job_spec.rb
@@ -0,0 +1,18 @@
+require 'spec_helper'
+
+module Spree
+ module CouponCodes
+ describe BulkGenerateJob, type: :job do
+ let!(:promotion) { create(:promotion, multi_codes: true, kind: :coupon_code, number_of_codes: 1000) }
+ let(:quantity) { 1000 }
+
+ describe '#perform' do
+ subject(:perform_job) { described_class.perform_now(promotion.id, quantity) }
+
+ it 'generates coupon codes' do
+ expect { subject }.to change(Spree::CouponCode, :count).by(1000)
+ end
+ end
+ end
+ end
+end
diff --git a/core/spec/jobs/spree/variants/remove_line_item_job_spec.rb b/core/spec/jobs/spree/variants/remove_line_item_job_spec.rb
index 36c917dad0..c321d89b9a 100644
--- a/core/spec/jobs/spree/variants/remove_line_item_job_spec.rb
+++ b/core/spec/jobs/spree/variants/remove_line_item_job_spec.rb
@@ -14,4 +14,3 @@ module Spree
end
end
end
-
diff --git a/core/spec/models/spree/promotion_spec.rb b/core/spec/models/spree/promotion_spec.rb
index 2b75d1ce52..1ba9701902 100644
--- a/core/spec/models/spree/promotion_spec.rb
+++ b/core/spec/models/spree/promotion_spec.rb
@@ -838,5 +838,15 @@ describe Spree::Promotion, type: :model do
expect(promotion.coupon_codes.first.display_code).to start_with('ABC')
end
end
+
+ context 'when number of codes is greater than the web limit', :job do
+ let(:promotion) { create(:promotion, code: nil, multi_codes: true, number_of_codes: 10) }
+
+ it 'generates the codes in a background job' do
+ expect {
+ promotion.update(number_of_codes: 501)
+ }.to enqueue_job(Spree::CouponCodes::BulkGenerateJob).with(promotion.id, 501 - promotion.coupon_codes.count)
+ end
+ end
end
end