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.

Cleanup Bulk Promo Codes implementation

Rafal Cymeryse362b51cfb9b
diff --git a/api/app/controllers/spree/api/v2/platform/promotion_batches_controller.rb b/api/app/controllers/spree/api/v2/platform/promotion_batches_controller.rb
index dea53e6361..905fbb5f24 100644
--- a/api/app/controllers/spree/api/v2/platform/promotion_batches_controller.rb
+++ b/api/app/controllers/spree/api/v2/platform/promotion_batches_controller.rb
@@ -4,50 +4,33 @@ module Spree
module Platform
class PromotionBatchesController < ResourceController
def create
- super
- rescue ActiveRecord::InvalidForeignKey => e
- render json: { error: e.message }, status: :unprocessable_entity
- end
-
- def destroy
- result = destroy_service.call(promotion_batch: resource)
+ template_promotion = Spree::Promotion.find(params[:promotion_batch][:template_promotion_id])
+ promotion_batch = Spree::PromotionBatches::CreateWithRandomCodes.new.call(
+ template_promotion: template_promotion,
+ amount: params[:promotion_batch][:amount].to_i,
+ random_characters: params[:promotion_batch][:random_characters].to_i,
+ prefix: params[:promotion_batch][:prefix],
+ suffix: params[:promotion_batch][:suffix]
+ )
- if result.success?
- head 204
- else
- render_error_payload(result.error)
- end
+ render_serialized_payload { promotion_batch }
end
def csv_export
- send_data Spree::PromotionBatches::PromotionCodesExporter.new(params).call,
+ send_data Spree::PromotionBatches::Export.new.call(promotion_batch: resource),
filename: "promo_codes_from_batch_id_#{params[:id]}.csv",
disposition: :attachment,
type: 'text/csv'
end
- def csv_import
- file = params[:file]
- Spree::PromotionBatches::PromotionCodesImporter.new(file: file, promotion_batch_id: params[:id]).call
- render json: { message: Spree.t('code_upload') }, status: :ok
- rescue Spree::PromotionBatches::PromotionCodesImporter::Error => e
- render json: { error: e.message }, status: :unprocessable_entity
- end
-
- def populate
- batch_id = params[:id]
- options = {
- batch_size: params[:batch_size].to_i,
- affix: params.dig(:code, :affix)&.to_sym,
- content: params[:affix_content],
- deny_list: params[:forbidden_phrases].split,
- random_part_bytes: params[:random_part_bytes].to_i
- }
+ def import
+ template_promotion = Spree::Promotion.find(params[:promotion_batch][:template_promotion_id])
+ promotion_batch = Spree::PromotionBatches::CreateWithCodes.new.call(
+ template_promotion: template_promotion,
+ codes: params[:promotion_batch][:codes]
+ )
- Spree::Promotions::PopulatePromotionBatch.new(batch_id, options).call
- render json: { message: Spree.t('promotion_batch_populated') }, status: :ok
- rescue Spree::Promotions::PopulatePromotionBatch::TemplateNotFoundError => e
- render json: { error: e.message }, status: :unprocessable_entity
+ render_serialized_payload { promotion_batch }
end
private
@@ -56,17 +39,9 @@ module Spree
Spree::PromotionBatch
end
- def scope_includes
- [:promotion]
- end
-
def spree_permitted_attributes
[:template_promotion_id]
end
-
- def destroy_service
- Spree::Api::Dependencies.platform_promotion_batch_destroy_service.constantize
- end
end
end
end
diff --git a/api/config/routes.rb b/api/config/routes.rb
index 58e92a9bb5..b3ad845b0f 100644
--- a/api/config/routes.rb
+++ b/api/config/routes.rb
@@ -73,11 +73,13 @@ Spree::Core::Engine.add_routes do
resources :promotion_rules
# Promotion Batches API
- resources :promotion_batches do
+ resources :promotion_batches, only: %i[index show create] do
member do
get :csv_export, to: 'promotion_batches#csv_export'
- post :csv_import, to: 'promotion_batches#csv_import'
- post :populate
+ end
+
+ collection do
+ post :import, to: 'promotion_batches#import'
end
end
diff --git a/api/spec/requests/spree/api/v2/platform/promotion_batches_spec.rb b/api/spec/requests/spree/api/v2/platform/promotion_batches_spec.rb
index 617169d8c3..b84ccaa176 100644
--- a/api/spec/requests/spree/api/v2/platform/promotion_batches_spec.rb
+++ b/api/spec/requests/spree/api/v2/platform/promotion_batches_spec.rb
@@ -6,107 +6,67 @@ describe 'Promotion API v2 spec', type: :request do
let(:bearer_token) { { 'Authorization' => valid_authorization } }
- describe 'promotion_batches#create' do
- context 'with valid params' do
- before { post '/api/v2/platform/promotion_batches', params: params, headers: bearer_token }
-
- let(:new_promotion_batch_attributes) do
- {
- template_promotion_id: nil
- }
- end
+ describe 'promotion_batches#index' do
+ before { create(:promotion_batch) }
- let(:params) { { promotion_batch: new_promotion_batch_attributes } }
+ before { get '/api/v2/platform/promotion_batches', headers: bearer_token }
- it 'creates and returns a promotion batch' do
- expect(json_response['data']).to have_relationship(:template_promotion).with_data(nil)
- end
- end
+ it_behaves_like 'returns 200 HTTP status'
- context 'when assigning a non-existing template' do
- before { post '/api/v2/platform/promotion_batches', params: params, headers: bearer_token }
-
- let(:new_promotion_batch_attributes) do
- {
- template_promotion_id: 123
- }
- end
-
- let(:params) { { promotion_batch: new_promotion_batch_attributes } }
-
- it 'does not create and returns an error' do
- expect(json_response.has_key?('error')).to eq(true)
- end
+ it 'returns a list of promotion batches' do
+ expect(json_response['data'].count).to eq(1)
+ expect(json_response['data'].first).to have_type('promotion_batch')
end
end
- describe 'promotion_batches#update' do
- context 'with valid params' do
- before { put "/api/v2/platform/promotion_batches/#{existing_promotion_batch.id}", params: params, headers: bearer_token }
+ describe 'promotion_batches#create' do
+ before { post '/api/v2/platform/promotion_batches', params: params, headers: bearer_token }
- let(:existing_promotion) { create(:promotion) }
- let(:existing_promotion_batch) { create(:promotion_batch, template_promotion_id: nil) }
+ let(:template_promotion) { create(:promotion) }
- let(:update_promotion_attributes) do
- {
- template_promotion_id: existing_promotion.id
+ let(:params) do
+ {
+ promotion_batch: {
+ template_promotion_id: template_promotion.id,
+ amount: 10,
+ random_characters: 5,
+ prefix: 'MYPROMO_'
}
- end
+ }
+ end
- let(:params) { { promotion_batch: update_promotion_attributes } }
+ it_behaves_like 'returns 200 HTTP status'
- it 'updates and returns a promotion batch' do
- expect(json_response['data']).to have_relationship(:template_promotion).with_data({ 'id' => existing_promotion.id.to_s, 'type' => 'promotion' })
- end
+ it 'creates a promotion batch' do
+ expect(Spree::PromotionBatch.count).to eq(1)
+ promotion_batch = Spree::PromotionBatch.first
+ expect(promotion_batch.template_promotion).to eq(template_promotion)
+ expect(promotion_batch.codes.size).to eq(10)
+ expect(promotion_batch.codes.first).to start_with('MYPROMO_')
end
+ end
- context 'when trying to override an assigned template' do
- before do
- existing_promotion
- other_existing_promotion
- existing_promotion_batch
- put "/api/v2/platform/promotion_batches/#{existing_promotion_batch.id}", params: params, headers: bearer_token
- end
-
- let(:existing_promotion) { create(:promotion) }
- let(:other_existing_promotion) { create(:promotion) }
- let(:existing_promotion_batch) { create(:promotion_batch, template_promotion: existing_promotion) }
-
- let(:update_promotion_attributes) do
- {
- template_promotion_id: other_existing_promotion.id
- }
- end
+ describe 'promotion_batches#import' do
+ before { post '/api/v2/platform/promotion_batches/import', params: params, headers: bearer_token }
- let(:params) { { promotion_batch: update_promotion_attributes } }
+ let(:template_promotion) { create(:promotion) }
- it 'does not update and returns an error' do
- expect(json_response.dig(:errors, :template_promotion_id)).to eq(['Template promotion has already been assigned!'])
- end
+ let(:params) do
+ {
+ promotion_batch: {
+ template_promotion_id: template_promotion.id,
+ codes: ['ABCD', 'EFGH']
+ }
+ }
end
- end
- describe 'promotion_batches#populate' do
- context 'with valid params' do
- before { post "/api/v2/platform/promotion_batches/#{existing_promotion_batch.id}/populate", params: params, headers: bearer_token }
-
- let(:existing_promotion_batch) { create(:promotion_batch) }
-
- let(:params) do
- {
- batch_size: 2,
- code: {
- affix: 'prefix'
- },
- affix_content: 'BLACKWEEK_',
- forbidden_phrases: 'forbidden phrases',
- random_part_bytes: 4
- }
- end
+ it_behaves_like 'returns 200 HTTP status'
- it 'is successful' do
- expect(json_response[:message]).to eq('Promotion Batch is being populated.')
- end
+ it 'creates a promotion batch' do
+ expect(Spree::PromotionBatch.count).to eq(1)
+ promotion_batch = Spree::PromotionBatch.first
+ expect(promotion_batch.template_promotion).to eq(template_promotion)
+ expect(promotion_batch.codes).to eq(['ABCD', 'EFGH'])
end
end
end
diff --git a/api/spec/serializers/spree/api/v2/platform/promotion_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/promotion_serializer_spec.rb
index 0f2c9bd9f5..84f66bda36 100644
--- a/api/spec/serializers/spree/api/v2/platform/promotion_serializer_spec.rb
+++ b/api/spec/serializers/spree/api/v2/platform/promotion_serializer_spec.rb
@@ -20,6 +20,7 @@ describe Spree::Api::V2::Platform::PromotionSerializer do
description: resource.description,
expires_at: resource.expires_at,
starts_at: resource.starts_at,
+ template: resource.template,
name: resource.name,
type: resource.type,
usage_limit: resource.usage_limit,
diff --git a/core/app/jobs/spree/promotion_batches/populate_job.rb b/core/app/jobs/spree/promotion_batches/populate_job.rb
new file mode 100644
index 0000000000..d44c3f186d
--- /dev/null
+++ b/core/app/jobs/spree/promotion_batches/populate_job.rb
@@ -0,0 +1,10 @@
+module Spree
+ module PromotionBatches
+ class PopulateJob < Spree::BaseJob
+ def perform(promotion_batch_id)
+ promotion_batch = ::Spree::PromotionBatch.find(promotion_batch_id)
+ Spree::PromotionBatches::Populate.new.call(promotion_batch: promotion_batch)
+ end
+ end
+ end
+end
diff --git a/core/app/jobs/spree/promotions/duplicate_promotion_job.rb b/core/app/jobs/spree/promotions/duplicate_promotion_job.rb
deleted file mode 100644
index fb2fe2a240..0000000000
--- a/core/app/jobs/spree/promotions/duplicate_promotion_job.rb
+++ /dev/null
@@ -1,12 +0,0 @@
-module Spree
- module Promotions
- class DuplicatePromotionJob < Spree::BaseJob
- def perform(template_promotion_id:, batch_id:, options: {}, code: nil)
- promotion = Spree::Promotion.find(template_promotion_id)
- code = code || Spree::PromotionBatches::BatchCodeGenerator.build(batch_id, options)
-
- Spree::PromotionHandler::PromotionBatchDuplicator.new(promotion, batch_id, code: code).duplicate
- end
- end
- end
-end
diff --git a/core/app/jobs/spree/promotions/handle_descendant_promotion_job.rb b/core/app/jobs/spree/promotions/handle_descendant_promotion_job.rb
deleted file mode 100644
index 00d2df4c82..0000000000
--- a/core/app/jobs/spree/promotions/handle_descendant_promotion_job.rb
+++ /dev/null
@@ -1,22 +0,0 @@
-module Spree
- module Promotions
- class HandleDescendantPromotionJob < Spree::BaseJob
- def perform(template_promotion_id:, descendant_promotion_id:, promotion_batch_id:)
- template_promotion = find_promotion(template_promotion_id)
- descendant_promotion = find_promotion(descendant_promotion_id)
-
- handle_promotion(template_promotion, promotion_batch_id, descendant_promotion)
- end
-
- private
-
- def find_promotion(id)
- Spree::Promotion.find(id)
- end
-
- def handle_promotion(template_promotion, promotion_batch_id, descendant_promotion)
- Spree::PromotionHandler::PromotionBatchUpdateHandler.new(template_promotion, promotion_batch_id, descendant_promotion).duplicate
- end
- end
- end
-end
diff --git a/core/app/models/spree/promotion.rb b/core/app/models/spree/promotion.rb
index f578f26d10..841352e76a 100644
--- a/core/app/models/spree/promotion.rb
+++ b/core/app/models/spree/promotion.rb
@@ -29,6 +29,8 @@ module Spree
has_many :store_promotions, class_name: 'Spree::StorePromotion'
has_many :stores, class_name: 'Spree::Store', through: :store_promotions
+ has_many :promotion_batches, foreign_key: 'template_promotion_id'
+
accepts_nested_attributes_for :promotion_actions, :promotion_rules
validates_associated :rules
@@ -40,8 +42,6 @@ module Spree
auto_strip_attributes :code, :path, :name
- after_update :update_descendants
-
scope :coupons, -> { where.not(code: nil) }
scope :advertised, -> { where(advertise: true) }
scope :applied, lambda {
@@ -50,7 +50,7 @@ module Spree
ON spree_order_promotions.promotion_id = #{table_name}.id
SQL
}
- scope :non_batched, -> { where(promotion_batch_id: nil) }
+ scope :templates, -> { where(template: true) }
self.whitelisted_ransackable_attributes = ['path', 'promotion_category_id', 'code']
@@ -240,9 +240,5 @@ module Spree
end
coupon_code
end
-
- def update_descendants
- Spree::PromotionHandler::UpdateDescendantsService.new(self).call
- end
end
end
diff --git a/core/app/models/spree/promotion_batch.rb b/core/app/models/spree/promotion_batch.rb
index 21421be9d5..a6a8ff057d 100644
--- a/core/app/models/spree/promotion_batch.rb
+++ b/core/app/models/spree/promotion_batch.rb
@@ -1,16 +1,28 @@
module Spree
class PromotionBatch < Spree::Base
- validate :template_assignment, on: :update
-
has_many :promotions
- belongs_to :template_promotion, class_name: "Promotion"
+ belongs_to :template_promotion, class_name: 'Promotion'
+
+ validate :validate_codes_present
+
+ serialize :codes, type: Array, coder: YAML
+
+ state_machine initial: :pending do
+ event :start do
+ transition from: :pending, to: :generating
+ end
- private
+ event :complete do
+ transition from: :generating, to: :completed
+ end
- def template_assignment
- if template_promotion_id_changed? && template_promotion_id_was.present?
- errors.add(:template_promotion_id, :template_promotion_already_assigned, message: Spree.t(:template_promotion_already_assigned))
+ event :error do
+ transition from: :generating, to: :error
end
end
+
+ def validate_codes_present
+ errors.add(:codes, Spree.t('no_codes_present')) unless codes.present?
+ end
end
end
diff --git a/core/app/models/spree/promotion_handler/promotion_batch_duplicator.rb b/core/app/models/spree/promotion_handler/promotion_batch_duplicator.rb
index 204dbb0851..655632018b 100644
--- a/core/app/models/spree/promotion_handler/promotion_batch_duplicator.rb
+++ b/core/app/models/spree/promotion_handler/promotion_batch_duplicator.rb
@@ -1,10 +1,9 @@
module Spree
module PromotionHandler
class PromotionBatchDuplicator < Spree::PromotionDuplicatorCore
- def initialize(promotion, promotion_batch_id, random_string: generate_random_string(4), code: nil)
+ def initialize(promotion, promotion_batch_id, code:)
@promotion = promotion
@promotion_batch_id = promotion_batch_id
- @random_string = random_string
@code = code
end
@@ -12,8 +11,8 @@ module Spree
new_promotion = @promotion.dup
new_promotion.usage_limit = 1
new_promotion.promotion_batch_id = @promotion_batch_id
- new_promotion.path = "#{@promotion.path}_#{@random_string}"
- code_assignment(new_promotion)
+ new_promotion.path = "#{@promotion.path}_#{@code}"
+ new_promotion.code = @code
new_promotion.stores = @promotion.stores
ActiveRecord::Base.transaction do
@@ -24,16 +23,6 @@ module Spree
new_promotion
end
-
- private
-
- def code_assignment(new_promotion)
- if @code
- new_promotion.code = @code
- else
- new_promotion.generate_code=(true)
- end
- end
end
end
end
diff --git a/core/app/models/spree/promotion_handler/promotion_batch_update_handler.rb b/core/app/models/spree/promotion_handler/promotion_batch_update_handler.rb
deleted file mode 100644
index 514ff7d508..0000000000
--- a/core/app/models/spree/promotion_handler/promotion_batch_update_handler.rb
+++ /dev/null
@@ -1,29 +0,0 @@
-module Spree
- module PromotionHandler
- class PromotionBatchUpdateHandler < Spree::PromotionDuplicatorCore
- def initialize(promotion, promotion_batch_id, descendant_promotion)
- @promotion = promotion
- @promotion_batch_id = promotion_batch_id
- @descendant_promotion = descendant_promotion
- end
-
- def duplicate
- new_promotion = @promotion.dup
- new_promotion.usage_limit = 1
- new_promotion.promotion_batch_id = @promotion_batch_id
- new_promotion.path = @descendant_promotion.path
- new_promotion.code = @descendant_promotion.code
- new_promotion.stores = @promotion.stores
-
- ActiveRecord::Base.transaction do
- @descendant_promotion.destroy!
- new_promotion.save
- copy_rules(new_promotion)
- copy_actions(new_promotion)
- end
-
- new_promotion
- end
- end
- end
-end
diff --git a/core/app/models/spree/promotion_handler/update_descendants_service.rb b/core/app/models/spree/promotion_handler/update_descendants_service.rb
deleted file mode 100644
index 69dee79fe2..0000000000
--- a/core/app/models/spree/promotion_handler/update_descendants_service.rb
+++ /dev/null
@@ -1,48 +0,0 @@
-module Spree
- module PromotionHandler
- class UpdateDescendantsService
- def initialize(template_promotion)
- @template_promotion = template_promotion
- end
-
- def call
- return unless promotion_batches?
-
- unused_promotions_ids_by_batch.each do |batch|
- batch.each do |batch_id, promotion_ids|
- promotion_ids.each do |promotion_id|
- Spree::Promotions::HandleDescendantPromotionJob.perform_later(
- template_promotion_id: @template_promotion.id,
- descendant_promotion_id: promotion_id,
- promotion_batch_id: batch_id
- )
- end
- end
- end
- end
-
- private
-
- def promotion_batches?
- promotion_batches.any?
- end
-
- def promotion_batches
- @promotion_batches = Spree::PromotionBatch.where(template_promotion_id: @template_promotion.id)
- end
-
- def unused_promotions_ids_by_batch
- @promotion_batches.map do |batch|
- {batch.id => unused_promotions_ids(batch)}
- end
- end
-
- def unused_promotions_ids(batch)
- batch.promotions
- .includes(:promotion_actions)
- .select {|promotion| promotion.credits_count < promotion.usage_limit}
- .pluck(:id)
- end
- end
- end
-end
diff --git a/core/app/presenters/spree/promotion_batch_presenter.rb b/core/app/presenters/spree/promotion_batch_presenter.rb
deleted file mode 100644
index f41bb5ac25..0000000000
--- a/core/app/presenters/spree/promotion_batch_presenter.rb
+++ /dev/null
@@ -1,32 +0,0 @@
-module Spree
- class PromotionBatchPresenter
- def initialize(promotion_batch)
- @promotion_batch = promotion_batch
- end
-
- def call
- {
- template_promotion_name_id: template_promotion_name_id,
- model_name_id: model_name_id
- }
- end
-
- private
-
- attr_reader :promotion_batch
-
- def template_promotion_name_id
- return "" unless promotion_batch.template_promotion
-
- base = promotion_batch.template_promotion.name
- addon = promotion_batch.template_promotion_id.to_s
- base + ' # ' + addon
- end
-
- def model_name_id
- base = promotion_batch.class.name.demodulize
- addon = promotion_batch.id.to_s
- base + ' # ' + addon
- end
- end
-end
diff --git a/core/app/services/spree/promotion_batches/batch_code_generator.rb b/core/app/services/spree/promotion_batches/batch_code_generator.rb
deleted file mode 100644
index c1521c6723..0000000000
--- a/core/app/services/spree/promotion_batches/batch_code_generator.rb
+++ /dev/null
@@ -1,41 +0,0 @@
-module Spree
- module PromotionBatches
- class BatchCodeGenerator
- class << self
- def build(batch_id, options)
- promotion_batch = Spree::PromotionBatch.find(batch_id)
- codes = codes(promotion_batch)
- code_generator = code_generator(options)
-
- loop do
- candidate = generate_code(code_generator)
- break candidate if candidate_valid?(candidate, codes)
- end
- end
-
- private
-
- def codes(promotion_batch)
- promotion_batch.promotions.pluck :code
- end
-
- def code_generator(options)
- Spree::Promotions::CodeGenerator.new(
- content: options[:content],
- affix: options[:affix],
- deny_list: options[:deny_list],
- random_part_bytes: options[:random_part_bytes]
- )
- end
-
- def generate_code(generator)
- generator.build
- end
-
- def candidate_valid?(candidate, codes)
- codes.exclude?(candidate)
- end
- end
- end
- end
-end
diff --git a/core/app/services/spree/promotion_batches/create_with_codes.rb b/core/app/services/spree/promotion_batches/create_with_codes.rb
new file mode 100644
index 0000000000..22c0c74aaf
--- /dev/null
+++ b/core/app/services/spree/promotion_batches/create_with_codes.rb
@@ -0,0 +1,26 @@
+module Spree
+ module PromotionBatches
+ class CreateWithCodes
+ Error = Class.new(StandardError)
+
+ def call(template_promotion:, codes:)
+ validate_codes!(codes)
+
+ promotion_batch = ::Spree::PromotionBatch.create!(
+ template_promotion: template_promotion,
+ codes: codes
+ )
+
+ ::Spree::PromotionBatches::PopulateJob.perform_later(promotion_batch.id)
+
+ promotion_batch
+ end
+
+ private
+
+ def validate_codes!(codes)
+ raise Error, 'Codes cannot be empty' if codes.empty?
+ end
+ end
+ end
+end
diff --git a/core/app/services/spree/promotion_batches/create_with_random_codes.rb b/core/app/services/spree/promotion_batches/create_with_random_codes.rb
new file mode 100644
index 0000000000..e8ce0bd803
--- /dev/null
+++ b/core/app/services/spree/promotion_batches/create_with_random_codes.rb
@@ -0,0 +1,22 @@
+module Spree
+ module PromotionBatches
+ class CreateWithRandomCodes
+ def initialize(create_with_codes: ::Spree::PromotionBatches::CreateWithCodes.new,
+ generate_codes: ::Spree::PromotionBatches::GenerateCodes.new)
+ @create_with_codes = create_with_codes
+ @generate_codes = generate_codes
+ end
+
+ def call(template_promotion:, amount:, random_characters:, prefix:, suffix:)
+ codes = @generate_codes.call(
+ amount: amount,
+ random_characters: random_characters,
+ prefix: prefix,
+ suffix: suffix
+ )
+
+ @create_with_codes.call(template_promotion: template_promotion, codes: codes)
+ end
+ end
+ end
+end
diff --git a/core/app/services/spree/promotion_batches/destroy.rb b/core/app/services/spree/promotion_batches/destroy.rb
deleted file mode 100644
index dbeac387f4..0000000000
--- a/core/app/services/spree/promotion_batches/destroy.rb
+++ /dev/null
@@ -1,15 +0,0 @@
-module Spree
- module PromotionBatches
- class Destroy
- prepend Spree::ServiceModule::Base
-
- def call(promotion_batch:)
- ActiveRecord::Base.transaction do
- promotion_batch.promotions.destroy_all
- promotion_batch.update(template_promotion_id: nil)
- promotion_batch.destroy!
- end
- end
- end
- end
-end
diff --git a/core/app/services/spree/promotion_batches/export.rb b/core/app/services/spree/promotion_batches/export.rb
new file mode 100644
index 0000000000..154b15b5e3
--- /dev/null
+++ b/core/app/services/spree/promotion_batches/export.rb
@@ -0,0 +1,15 @@
+require 'csv'
+
+module Spree
+ module PromotionBatches
+ class Export
+ def call(promotion_batch:)
+ promotions = promotion_batch.promotions
+
+ ::CSV.generate do |csv|
+ promotions.each { |promotion| csv << [promotion.code] }
+ end
+ end
+ end
+ end
+end
diff --git a/core/app/services/spree/promotion_batches/generate_code.rb b/core/app/services/spree/promotion_batches/generate_code.rb
new file mode 100644
index 0000000000..f34a464768
--- /dev/null
+++ b/core/app/services/spree/promotion_batches/generate_code.rb
@@ -0,0 +1,17 @@
+module Spree
+ module PromotionBatches
+ class GenerateCode
+ def initialize(random: SecureRandom)
+ @random = random
+ end
+
+ def call(random_characters:, prefix: nil, suffix: nil)
+ [
+ prefix,
+ @random.hex(random_characters),
+ suffix
+ ].join('')
+ end
+ end
+ end
+end
diff --git a/core/app/services/spree/promotion_batches/generate_codes.rb b/core/app/services/spree/promotion_batches/generate_codes.rb
new file mode 100644
index 0000000000..dad85a03c9
--- /dev/null
+++ b/core/app/services/spree/promotion_batches/generate_codes.rb
@@ -0,0 +1,37 @@
+module Spree
+ module PromotionBatches
+ class GenerateCodes
+ GenerateFailedError = Class.new(StandardError)
+
+ MAX_ATTEMPTS = 3
+
+ def initialize(generate_code: ::Spree::PromotionBatches::GenerateCode.new)
+ @generate_code = generate_code
+ end
+
+ def call(amount:, random_characters:, prefix:, suffix:)
+ result = []
+
+ amount.times do
+ result << generate_next_code(result, random_characters, prefix, suffix)
+ end
+
+ result
+ end
+
+ private
+
+ def generate_next_code(existing_codes, random_characters, prefix, suffix)
+ attempts = 0
+
+ while attempts < MAX_ATTEMPTS
+ code = @generate_code.call(random_characters: random_characters, prefix: prefix, suffix: suffix)
+ return code unless existing_codes.include?(code)
+ attempts += 1
+ end
+
+ raise GenerateFailedError
+ end
+ end
+ end
+end
diff --git a/core/app/services/spree/promotion_batches/populate.rb b/core/app/services/spree/promotion_batches/populate.rb
new file mode 100644
index 0000000000..68f2dbb0ea
--- /dev/null
+++ b/core/app/services/spree/promotion_batches/populate.rb
@@ -0,0 +1,27 @@
+module Spree
+ module PromotionBatches
+ class Populate
+ def initialize(duplicator_class: Spree::PromotionHandler::PromotionBatchDuplicator)
+ @duplicator_class = duplicator_class
+ end
+
+ def call(promotion_batch:)
+ Spree::PromotionBatch.transaction do
+ promotion_batch.start
+ codes = promotion_batch.codes
+ template_promotion = promotion_batch.template_promotion
+
+ codes.each do |code|
+ duplicator = @duplicator_class.new(template_promotion, promotion_batch.id, code: code)
+ duplicator.duplicate
+ end
+
+ promotion_batch.complete
+ end
+ rescue => e
+ promotion_batch.error
+ raise e
+ end
+ end
+ end
+end
diff --git a/core/app/services/spree/promotion_batches/promotion_codes_exporter.rb b/core/app/services/spree/promotion_batches/promotion_codes_exporter.rb
deleted file mode 100644
index dde798a9c7..0000000000
--- a/core/app/services/spree/promotion_batches/promotion_codes_exporter.rb
+++ /dev/null
@@ -1,25 +0,0 @@
-module Spree
- module PromotionBatches
- class PromotionCodesExporter
- def initialize(params)
- @promotion_batch = find_promotion_batch(params[:id])
- end
-
- def call
- Spree::Core::Converters::CSV.to_csv(promo_codes)
- end
-
- private
-
- def find_promotion_batch(id)
- Spree::PromotionBatch.find(id)
- end
-
- def promo_codes
- @promotion_batch.promotions.map do |promotion|
- [promotion.code]
- end
- end
- end
- end
-end
diff --git a/core/app/services/spree/promotion_batches/promotion_codes_importer.rb b/core/app/services/spree/promotion_batches/promotion_codes_importer.rb
deleted file mode 100644
index 513aba690c..0000000000
--- a/core/app/services/spree/promotion_batches/promotion_codes_importer.rb
+++ /dev/null
@@ -1,63 +0,0 @@
-module Spree
- module PromotionBatches
- class PromotionCodesImporter
- Error = Class.new(StandardError)
-
- ALLOWED_FILE_TYPES = %w(text/csv).freeze
-
- def initialize(file:, promotion_batch_id:)
- @content_type = file&.content_type
- @content = file&.read.to_s
- @promotion_batch = find_promotion_batch(promotion_batch_id)
- end
-
- def call
- validate_file!
- validate_promotion_batch!
-
- parsed_rows.each do |parsed_row|
- Spree::Promotions::DuplicatePromotionJob
- .perform_later(template_promotion_id: @promotion_batch.template_promotion_id, batch_id: @promotion_batch.id, code: parsed_row)
- end
- end
-
- private
-
- def find_promotion_batch(id)
- Spree::PromotionBatch.find(id)
- end
-
- def validate_file!
- raise Error, Spree.t('invalid_file') unless file_valid
- end
-
- def file_valid
- file_type_correct? && file_not_empty?
- end
-
- def file_type_correct?
- @content_type.in?(ALLOWED_FILE_TYPES)
- end
-
- def file_not_empty?
- parsed_rows.present?
- end
-
- def validate_promotion_batch!
- raise Error, Spree.t('no_template_promotion') unless batch_validation_condition
- end
-
- def batch_validation_condition
- @promotion_batch.template_promotion_id
- end
-
- def parsed_rows
- CSV.new(rows, headers: false).read.map(&:first)
- end
-
- def rows
- @content.lines.join
- end
- end
- end
-end
diff --git a/core/app/services/spree/promotions/code_generator.rb b/core/app/services/spree/promotions/code_generator.rb
deleted file mode 100644
index 6b464e6649..0000000000
--- a/core/app/services/spree/promotions/code_generator.rb
+++ /dev/null
@@ -1,81 +0,0 @@
-module Spree
- module Promotions
- class CodeGenerator
- MutuallyExclusiveInputsError = Class.new(StandardError)
- RetriesDepleted = Class.new(StandardError)
-
- def initialize(content: nil, affix: nil, deny_list: [], random_part_bytes: 4)
- @content = content
- @affix = affix
- @deny_list = deny_list
- @random_part_bytes = random_part_bytes
- end
-
- def build
- validate_inputs unless deny_list.empty?
- success = false
- result = 100.times do
- candidate = compose
- if valid?(candidate)
- success = true
- break candidate
- end
- end
- success ? result : raise(RetriesDepleted)
- end
-
- private
-
- attr_reader :content, :affix, :deny_list, :random_part_bytes
-
- def validate_inputs
- raise_error if inputs_invalid?
- end
-
- def valid?(subject)
- return true if deny_list.empty?
-
- violation_checks = deny_list.map do |el|
- subject.include?(el)
- end
-
- violation_checks.none?
- end
-
- def compose
- case affix
- when :prefix
- prefix_alorithm
- when :suffix
- suffix_alorithm
- else
- default_algorithm
- end
- end
-
- def prefix_alorithm
- content + random_code
- end
-
- def suffix_alorithm
- random_code + content
- end
-
- def default_algorithm
- random_code
- end
-
- def random_code
- SecureRandom.hex(random_part_bytes)
- end
-
- def raise_error
- raise MutuallyExclusiveInputsError, Spree.t(:mutually_exclusive_inputs)
- end
-
- def inputs_invalid?
- deny_list.include?(content)
- end
- end
- end
-end
diff --git a/core/app/services/spree/promotions/populate_promotion_batch.rb b/core/app/services/spree/promotions/populate_promotion_batch.rb
deleted file mode 100644
index 3ce16c8f6d..0000000000
--- a/core/app/services/spree/promotions/populate_promotion_batch.rb
+++ /dev/null
@@ -1,37 +0,0 @@
-module Spree
- module Promotions
- class PopulatePromotionBatch
- TemplateNotFoundError = Class.new(StandardError)
-
- def initialize(batch_id, options = {})
- @batch_id = batch_id
- @options = options
- end
-
- def call
- validate_template_presence!
-
- size.times do
- DuplicatePromotionJob.perform_later(template_promotion_id: template_promotion_id, batch_id: batch_id, options: options.except(:batch_size))
- end
- end
-
- private
-
- attr_accessor :options
- attr_reader :batch_id
-
- def validate_template_presence!
- raise TemplateNotFoundError, Spree.t('template_not_found') unless template_promotion_id
- end
-
- def size
- options[:batch_size]
- end
-
- def template_promotion_id
- PromotionBatch.find(batch_id).template_promotion_id
- end
- end
- end
-end
diff --git a/core/db/migrate/20231114071427_create_promotion_batches.rb b/core/db/migrate/20231114071427_create_promotion_batches.rb
index dbcd3879d8..786ae03216 100644
--- a/core/db/migrate/20231114071427_create_promotion_batches.rb
+++ b/core/db/migrate/20231114071427_create_promotion_batches.rb
@@ -1,8 +1,10 @@
class CreatePromotionBatches < ActiveRecord::Migration[6.1]
def change
create_table :spree_promotion_batches do |t|
- t.string :status
+ t.string :state
+ t.text :codes
+ t.references :template_promotion, foreign_key: { to_table: :spree_promotions }
t.timestamps
end
end
diff --git a/core/db/migrate/20231114084551_add_template_promotion_id_to_promotion_batches.rb b/core/db/migrate/20231114084551_add_template_promotion_id_to_promotion_batches.rb
deleted file mode 100644
index eee22fbb21..0000000000
--- a/core/db/migrate/20231114084551_add_template_promotion_id_to_promotion_batches.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-class AddTemplatePromotionIdToPromotionBatches < ActiveRecord::Migration[6.1]
- def change
- add_reference(:spree_promotion_batches, :template_promotion, foreign_key: { to_table: :spree_promotions })
- end
-end
diff --git a/core/db/migrate/20231114182646_add_promotion_batch_reference_to_promotions.rb b/core/db/migrate/20231114182646_add_promotion_batch_reference_to_promotions.rb
index f93f6db43c..4a9e1a7ccd 100644
--- a/core/db/migrate/20231114182646_add_promotion_batch_reference_to_promotions.rb
+++ b/core/db/migrate/20231114182646_add_promotion_batch_reference_to_promotions.rb
@@ -1,5 +1,6 @@
class AddPromotionBatchReferenceToPromotions < ActiveRecord::Migration[6.1]
def change
- add_reference(:spree_promotions, :promotion_batch, foreign_key: { to_table: :spree_promotion_batches})
+ add_reference :spree_promotions, :promotion_batch, foreign_key: { to_table: :spree_promotion_batches}
+ add_column :spree_promotions, :template, :boolean
end
end
diff --git a/core/db/migrate/20231211152651_add_unique_index_to_promotions_on_code_and_promotion_batch_id.rb b/core/db/migrate/20231211152651_add_unique_index_to_promotions_on_code_and_promotion_batch_id.rb
deleted file mode 100644
index 7e1bee4147..0000000000
--- a/core/db/migrate/20231211152651_add_unique_index_to_promotions_on_code_and_promotion_batch_id.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-class AddUniqueIndexToPromotionsOnCodeAndPromotionBatchId < ActiveRecord::Migration[6.1]
- def change
- add_index(:spree_promotions, [:code, :promotion_batch_id], unique: true)
- end
-end
diff --git a/core/db/migrate/20231223085020_remove_status_from_pomotion_batches.rb b/core/db/migrate/20231223085020_remove_status_from_pomotion_batches.rb
deleted file mode 100644
index 32d15f4337..0000000000
--- a/core/db/migrate/20231223085020_remove_status_from_pomotion_batches.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-class RemoveStatusFromPomotionBatches < ActiveRecord::Migration[6.1]
- def change
- remove_column(:spree_promotion_batches, :status)
- end
-end
diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rb
index 2d3e8a54b1..540ac5b44b 100644
--- a/core/lib/spree/core.rb
+++ b/core/lib/spree/core.rb
@@ -128,7 +128,6 @@ require 'spree/service_module'
require 'spree/database_type_utilities'
require 'spree/core/importer'
-require 'spree/core/converters'
require 'spree/core/query_filters'
require 'spree/core/product_duplicator'
require 'spree/core/controller_helpers/auth'
diff --git a/core/lib/spree/core/converters.rb b/core/lib/spree/core/converters.rb
deleted file mode 100644
index df98b096f8..0000000000
--- a/core/lib/spree/core/converters.rb
+++ /dev/null
@@ -1,8 +0,0 @@
-module Spree
- module Core
- module Converters
- end
- end
-end
-
-require 'spree/core/converters/csv'
diff --git a/core/lib/spree/core/converters/csv.rb b/core/lib/spree/core/converters/csv.rb
deleted file mode 100644
index 46a461ae9a..0000000000
--- a/core/lib/spree/core/converters/csv.rb
+++ /dev/null
@@ -1,17 +0,0 @@
-require "csv"
-
-module Spree
- module Core
- module Converters
- class CSV
- def self.to_csv(rows)
- ::CSV.generate do |csv|
- rows.each do |row|
- csv << row
- end
- end
- end
- end
- end
- end
-end
diff --git a/core/lib/spree/testing_support/factories/promotion_batch_factory.rb b/core/lib/spree/testing_support/factories/promotion_batch_factory.rb
index 2b69c7cf7b..c42a094f26 100644
--- a/core/lib/spree/testing_support/factories/promotion_batch_factory.rb
+++ b/core/lib/spree/testing_support/factories/promotion_batch_factory.rb
@@ -1,5 +1,7 @@
FactoryBot.define do
factory :promotion_batch, class: Spree::PromotionBatch do
association :template_promotion, factory: :promotion
+
+ codes { [SecureRandom.hex(5), SecureRandom.hex(5)] }
end
end
diff --git a/core/lib/spree/testing_support/factories/promotion_factory.rb b/core/lib/spree/testing_support/factories/promotion_factory.rb
index 16d022d1a3..09b1f4c71b 100644
--- a/core/lib/spree/testing_support/factories/promotion_factory.rb
+++ b/core/lib/spree/testing_support/factories/promotion_factory.rb
@@ -16,14 +16,6 @@ FactoryBot.define do
end
end
- trait :with_unique_code do
- code { SecureRandom.hex(4) }
- end
-
- trait :batched do
- promotion_batch
- end
-
factory :promotion, class: Spree::Promotion do
name { 'Promo' }
diff --git a/core/spec/jobs/spree/promotions/duplicate_promotion_job_spec.rb b/core/spec/jobs/spree/promotions/duplicate_promotion_job_spec.rb
deleted file mode 100644
index e1cccd7f76..0000000000
--- a/core/spec/jobs/spree/promotions/duplicate_promotion_job_spec.rb
+++ /dev/null
@@ -1,69 +0,0 @@
-require 'spec_helper'
-
-module Spree
- describe Promotions::DuplicatePromotionJob do
- describe "#perform" do
- let(:promotion) { build(:promotion) }
- let(:promotion_batch) { build(:promotion_batch) }
- let(:new_code) { 'new_code' }
- let(:duplicator) { instance_double(Spree::PromotionHandler::PromotionDuplicator) }
-
- before do
- allow(Spree::Promotion)
- .to receive(:find)
- .and_return(promotion)
- end
-
- context 'when code is NOT provided' do
- let(:options) { {key: 'value'} }
-
- subject(:execute_job) do
- described_class.new.perform(template_promotion_id: promotion.id, batch_id: promotion_batch.id, options: options)
- end
-
- before do
- allow(Spree::PromotionBatches::BatchCodeGenerator)
- .to receive(:build)
- .with(promotion_batch.id, options)
- .and_return(new_code)
- allow(Spree::PromotionHandler::PromotionBatchDuplicator)
- .to receive(:new)
- .with(promotion, promotion_batch.id, code: new_code)
- .and_return(duplicator)
- allow(duplicator)
- .to receive(:duplicate)
- end
-
- it "sends #duplicate to the duplicator service" do
- expect(Spree::PromotionHandler::PromotionBatchDuplicator)
- .to receive(:new)
- .with(promotion, promotion_batch.id, code: new_code)
- .and_return(duplicator)
- expect(duplicator)
- .to receive(:duplicate)
-
- execute_job
- end
- end
-
- context 'when code IS provided' do
- subject(:execute_job) do
- described_class.new.perform(template_promotion_id: promotion.id, batch_id: promotion_batch.id, code: specified_code)
- end
-
- let(:specified_code) { 'specified_code' }
-
- it "sends #duplicate to the duplicator service" do
- expect(Spree::PromotionHandler::PromotionBatchDuplicator)
- .to receive(:new)
- .with(promotion, promotion_batch.id, code: specified_code)
- .and_return(duplicator)
- expect(duplicator)
- .to receive(:duplicate)
-
- execute_job
- end
- end
- end
- end
-end
diff --git a/core/spec/jobs/spree/promotions/handle_descendant_promotion_job_spec.rb b/core/spec/jobs/spree/promotions/handle_descendant_promotion_job_spec.rb
deleted file mode 100644
index 53d844aebf..0000000000
--- a/core/spec/jobs/spree/promotions/handle_descendant_promotion_job_spec.rb
+++ /dev/null
@@ -1,37 +0,0 @@
-require 'spec_helper'
-
-module Spree
- describe Promotions::HandleDescendantPromotionJob do
- describe "#perform" do
- let(:template_promotion) { build(:promotion) }
- let(:descendant_promotion) { build(:promotion) }
- let(:batch) { instance_double(Spree::PromotionBatch, id: 123) }
- let(:duplicator) { instance_double(Spree::PromotionHandler::PromotionBatchUpdateHandler) }
-
- before do
- allow(Spree::Promotion)
- .to receive(:find)
- .and_return(template_promotion, descendant_promotion)
- end
-
- subject(:execute_job) do
- described_class.new.perform(
- template_promotion_id: template_promotion.id,
- descendant_promotion_id: descendant_promotion.id,
- promotion_batch_id: batch.id
- )
- end
-
- it "sends #duplicate to the duplicator service" do
- expect(Spree::PromotionHandler::PromotionBatchUpdateHandler)
- .to receive(:new)
- .with(template_promotion, batch.id, descendant_promotion)
- .and_return(duplicator)
- expect(duplicator)
- .to receive(:duplicate)
-
- execute_job
- end
- end
- end
-end
diff --git a/core/spec/lib/spree/core/converters/csv_spec.rb b/core/spec/lib/spree/core/converters/csv_spec.rb
deleted file mode 100644
index 312fceeaed..0000000000
--- a/core/spec/lib/spree/core/converters/csv_spec.rb
+++ /dev/null
@@ -1,31 +0,0 @@
-require 'spec_helper'
-
-module Spree
- module Core
- describe Converters::CSV do
- subject(:csv_converter) { described_class }
-
- describe ".to_csv" do
- subject(:convert_to_csv) { csv_converter.to_csv(input) }
-
- let(:input) do
- [
- ["row1a", "row1b", 1, :symbol, "coma,case"],
- ["row2a", "row2b", 2, :symbol, "newline\ncase"],
- ]
- end
-
- let(:expected_csv_representation) do
- [
- "row1a,row1b,1,symbol,\"coma,case\"\n",
- "row2a,row2b,2,symbol,\"newline\ncase\"\n",
- ].join
- end
-
- it "returns a valid csv representation" do
- expect(convert_to_csv).to eql expected_csv_representation
- end
- end
- end
- end
-end
diff --git a/core/spec/models/spree/promotion_batch_spec.rb b/core/spec/models/spree/promotion_batch_spec.rb
index c4c5f068f2..6f620169df 100644
--- a/core/spec/models/spree/promotion_batch_spec.rb
+++ b/core/spec/models/spree/promotion_batch_spec.rb
@@ -1,20 +1,29 @@
require 'spec_helper'
describe Spree::PromotionBatch, type: :model do
- describe 'validations' do
- describe 'template_assignment' do
- let(:promotion) { create(:promotion) }
- let(:other_promotion) { create(:promotion) }
- let(:promotion_batch) { create(:promotion_batch, template_promotion: promotion) }
+ describe '#save' do
+ let(:template_promotion) { create(:promotion) }
+ let(:promotion_batch) { build(:promotion_batch, template_promotion: template_promotion, codes: codes) }
- before do
- promotion
- other_promotion
- promotion_batch
+ subject { promotion_batch.save }
+
+ context 'when there are no codes provided' do
+ let(:codes) { [] }
+
+ it 'fails validation' do
+ subject
+ expect(promotion_batch.errors[:codes]).to_not be_empty
+ expect(promotion_batch.persisted?).to eq(false)
end
+ end
+
+ context 'when codes are provided' do
+ let(:codes) { ['TEST', 'TEST2'] }
- it 'should validate template_assignment on update' do
- expect { promotion_batch.update!(template_promotion: other_promotion) }.to raise_error(ActiveRecord::RecordInvalid)
+ it 'passes validation' do
+ subject
+ expect(promotion_batch.errors[:codes]).to be_empty
+ expect(promotion_batch.persisted?).to eq(true)
end
end
end
diff --git a/core/spec/models/spree/promotion_handler/promotion_batch_duplicator_spec.rb b/core/spec/models/spree/promotion_handler/promotion_batch_duplicator_spec.rb
index 538aa15026..12aa435e8e 100644
--- a/core/spec/models/spree/promotion_handler/promotion_batch_duplicator_spec.rb
+++ b/core/spec/models/spree/promotion_handler/promotion_batch_duplicator_spec.rb
@@ -1,7 +1,7 @@
require 'spec_helper'
describe Spree::PromotionHandler::PromotionBatchDuplicator do
- subject { described_class.new(promotion, promotion_batch.id) }
+ subject { described_class.new(promotion, promotion_batch.id, code: 'test') }
let!(:promo_category) { create(:promotion_category) }
let!(:calculator) { create(:calculator) }
@@ -40,28 +40,14 @@ describe Spree::PromotionHandler::PromotionBatchDuplicator do
it 'assigns a unique code to the duplicated promotion' do
result = new_promotion
- expect(result.code).to match provided_code
- end
- end
-
- context "when code is NOT provided" do
- let(:new_code) { 'secure_random_code' }
-
- before do
- allow(SecureRandom).to receive(:hex).with(4).and_return(new_code)
+ expect(result.code).to eq(provided_code)
end
- it 'assigns a unique code to the duplicated promotion' do
- expect(SecureRandom).to receive(:hex).with(4).and_return(new_code)
- result = new_promotion
- expect(result.code).to match new_code
+ it 'returns a duplicate of a promotion with the path field changed' do
+ expect(new_promotion.path).to eq("#{promotion.path}_#{provided_code}")
end
end
- it 'returns a duplicate of a promotion with the path field changed' do
- expect(new_promotion.path).to match /#{promotion.path}_[a-zA-Z]{4}/
- end
-
it 'returns a duplicate of a promotion with all the fields (except the path, code and usage_limit fields) the same' do
promotion.attributes.each_key do |key|
expect(promotion.send(key)).to eq new_promotion.send(key) unless excluded_fields.include?(key)
diff --git a/core/spec/models/spree/promotion_handler/promotion_batch_update_handler_spec.rb b/core/spec/models/spree/promotion_handler/promotion_batch_update_handler_spec.rb
deleted file mode 100644
index 00d6bc7488..0000000000
--- a/core/spec/models/spree/promotion_handler/promotion_batch_update_handler_spec.rb
+++ /dev/null
@@ -1,127 +0,0 @@
-require 'spec_helper'
-
-describe Spree::PromotionHandler::PromotionBatchUpdateHandler do
- subject { described_class.new(promotion, promotion_batch.id, descendant_promotion) }
-
- let!(:promo_category) { create(:promotion_category) }
- let!(:calculator) { create(:calculator) }
-
- let(:store) { create(:store) }
- let(:store_2) { create(:store) }
- let!(:promotion) do
- create(:promotion_with_item_total_rule,
- description: 'Test description',
- expires_at: Date.current + 30,
- starts_at: Date.current,
- usage_limit: 100,
- match_policy: 'all',
- code: 'test1',
- advertise: true,
- path: 'test1',
- promotion_category: promo_category,
- stores: [store, store_2])
- end
-
- before do
- Spree::Promotion::Actions::CreateItemAdjustments.create!(calculator: calculator, promotion: promotion)
- end
-
- describe '#duplicate' do
- let(:new_promotion) { subject.duplicate }
- let(:promotion_batch) { create(:promotion_batch) }
- let(:descendant_promotion) { create(:promotion, code: descendant_code, path: descendant_path) }
- let(:descendant_code) { 'descendant_code' }
- let(:descendant_path) { 'descendant_path' }
-
- context 'model fields' do
- let(:excluded_fields) { ['code', 'path', 'id', 'created_at', 'updated_at', 'deleted_at', 'promotion_batch_id', 'usage_limit'] }
-
- it 'returns a duplicate of a promotion with all the fields (except the path, code and usage_limit fields) the same' do
- promotion.attributes.each_key do |key|
- expect(promotion.send(key)).to eq new_promotion.send(key) unless excluded_fields.include?(key)
- end
- end
-
- it 'assigns the descendant code and path to the new promotion' do
- expect(new_promotion.code).to match descendant_code
- expect(new_promotion.path).to match descendant_path
- end
-
- it 'destroys the descendant_promotion' do
- expect(descendant_promotion).to receive(:destroy!)
-
- new_promotion
- end
-
- it 'associates the new promotion with a batch' do
- expect(new_promotion.promotion_batch_id).to match promotion_batch.id
- end
- end
-
- context 'model associations - rules' do
- let(:excluded_fields) { ['promotion_id', 'id', 'created_at', 'updated_at', 'deleted_at'] }
-
- it 'copies all promotion rules' do
- expect(promotion.promotion_rules.size).to eq new_promotion.promotion_rules.size
- end
-
- it "promotion rule's fields (except promotion_id) are the same" do
- old_rule = promotion.promotion_rules.first
- new_rule = new_promotion.promotion_rules.first
-
- old_rule.attributes.each_key do |key|
- expect(old_rule.send(key)).to eq new_rule.send(key) unless excluded_fields.include?(key)
- end
- end
-
- it 'assigns a new promotion rule to new promotion' do
- expect(promotion.promotion_rules.first.promotion_id).not_to eq new_promotion.promotion_rules.first.promotion_id
- end
- end
-
- context 'model associations - actions' do
- let(:excluded_fields) { ['promotion_id', 'id', 'created_at', 'updated_at', 'deleted_at'] }
-
- it 'copies all promotion actions' do
- expect(promotion.promotion_actions.size).to eq new_promotion.promotion_actions.size
- end
-
- it 'copies promotion stores' do
- expect(promotion.store_ids).to eq new_promotion.store_ids
- end
-
- it "promotion action's fields (except promotion_id) are the same" do
- old_action = promotion.promotion_actions.first
- new_action = new_promotion.promotion_actions.first
-
- old_action.attributes.each_key do |key|
- expect(old_action.send(key)).to eq new_action.send(key) unless excluded_fields.include?(key)
- end
- end
-
- it 'assigns a new promotion action to new promotion' do
- expect(promotion.promotion_actions.first.promotion_id).not_to eq new_promotion.promotion_actions.first.promotion_id
- end
- end
-
- context "model associations - action's calculator" do
- let(:excluded_fields) { ['calculable_id', 'id', 'created_at', 'updated_at', 'deleted_at'] }
-
- it "copies promotion action's calculator" do
- new_calc = new_promotion.promotion_actions.first.calculator
- old_calc = promotion.promotion_actions.first.calculator
-
- new_calc.attributes.each_key do |key|
- expect(old_calc.send(key)).to eq new_calc.send(key) unless excluded_fields.include?(key)
- end
- end
-
- it 'assigns a new calculator to promotion action' do
- new_calc = new_promotion.promotion_actions.first.calculator
- old_calc = promotion.promotion_actions.first.calculator
-
- expect(old_calc.calculable_id).not_to eq new_calc.calculable_id
- end
- end
- end
-end
diff --git a/core/spec/models/spree/promotion_handler/update_descendants_service_spec.rb b/core/spec/models/spree/promotion_handler/update_descendants_service_spec.rb
deleted file mode 100644
index a95a527b3c..0000000000
--- a/core/spec/models/spree/promotion_handler/update_descendants_service_spec.rb
+++ /dev/null
@@ -1,38 +0,0 @@
-require 'spec_helper'
-
-describe Spree::PromotionHandler::UpdateDescendantsService do
- subject { described_class.new(template_promotion).call }
-
- let(:template_promotion) { create(:promotion) }
- let(:descendant_promotion) { create(:promotion, usage_limit: 1, promotion_batch_id: batch.id) }
- let(:batch) { create(:promotion_batch, template_promotion_id: template_promotion.id ) }
-
- context 'when the promotion is a template for a batch' do
- it 'enqueues a job' do
- expect(Spree::Promotions::HandleDescendantPromotionJob)
- .to receive(:perform_later)
- .with(
- template_promotion_id: template_promotion.id,
- descendant_promotion_id: descendant_promotion.id,
- promotion_batch_id: batch.id
- )
-
- subject
- end
- end
-
- context 'when the promotion is NOT a template for a batch' do
- before do
- allow(Spree::PromotionBatch)
- .to receive(:where)
- .with(template_promotion_id: template_promotion.id )
- .and_return([])
- end
-
- it 'does not enqueue a job' do
- expect(Spree::Promotions::HandleDescendantPromotionJob).not_to receive(:perform_later)
-
- subject
- end
- end
-end
diff --git a/core/spec/models/spree/promotion_spec.rb b/core/spec/models/spree/promotion_spec.rb
index e72785ff8d..dd0ff6f438 100644
--- a/core/spec/models/spree/promotion_spec.rb
+++ b/core/spec/models/spree/promotion_spec.rb
@@ -119,22 +119,6 @@ describe Spree::Promotion, type: :model do
expect(subject).to include(promotion_advertised)
end
end
-
- describe '.non_batched' do
- subject { described_class.non_batched }
-
- let!(:non_batched_promotion) { create :promotion, promotion_batch_id: nil }
- let!(:promotion_batch) { create :promotion_batch }
- let!(:batched_promotion) { create :promotion, promotion_batch_id: promotion_batch.id }
-
- it 'is expected to not include batched promotion' do
- expect(subject).not_to include(batched_promotion)
- end
-
- it 'is expected to include non_batched promotion' do
- expect(subject).to include(non_batched_promotion)
- end
- end
end
describe '#destroy' do
@@ -178,7 +162,7 @@ describe Spree::Promotion, type: :model do
let(:user) { create(:user) }
let(:order) { create(:order, user: user) }
let(:payload) { { order: order, user: user } }
-
+
before do
allow(action1).to receive_messages perform: true
allow(action2).to receive_messages perform: true
@@ -743,20 +727,4 @@ describe Spree::Promotion, type: :model do
end
end
end
-
- describe '#update_descendants' do
- let(:promotion) { create(:promotion) }
- let(:service) { instance_double(Spree::PromotionHandler::UpdateDescendantsService) }
-
- it 'calls the service' do
- expect(Spree::PromotionHandler::UpdateDescendantsService)
- .to receive(:new)
- .with(promotion)
- .and_return(service)
- expect(service)
- .to receive(:call)
-
- promotion.update(description: "anything")
- end
- end
end
diff --git a/core/spec/services/spree/promotion_batches/batch_code_generator_spec.rb b/core/spec/services/spree/promotion_batches/batch_code_generator_spec.rb
deleted file mode 100644
index f522bf6821..0000000000
--- a/core/spec/services/spree/promotion_batches/batch_code_generator_spec.rb
+++ /dev/null
@@ -1,38 +0,0 @@
-require 'spec_helper'
-
-module Spree
- describe PromotionBatches::BatchCodeGenerator do
- subject(:generate_batch_code) { described_class.build(batch_id, options) }
-
- let(:promotion) { build(:promotion, code: existing_code) }
- let(:promotion_batch) { build(:promotion_batch) }
- let(:code_generator) { instance_double(Promotions::CodeGenerator) }
- let(:existing_code) { "existing_code" }
- let(:new_code) { "new_code" }
- let(:options) { {key: 'value'} }
- let(:batch_id) { double }
-
-
- before do
- allow(promotion_batch)
- .to receive(:id)
- .and_return(batch_id)
- allow(Spree::PromotionBatch)
- .to receive(:find)
- .and_return(promotion_batch)
- allow(promotion_batch)
- .to receive(:promotions)
- .and_return([promotion])
- allow(Promotions::CodeGenerator)
- .to receive(:new)
- .and_return(code_generator)
- allow(code_generator)
- .to receive(:build)
- .and_return(existing_code, new_code)
- end
-
- it 'returns new code' do
- expect(subject).to eq(new_code)
- end
- end
-end
diff --git a/core/spec/services/spree/promotion_batches/create_with_codes_spec.rb b/core/spec/services/spree/promotion_batches/create_with_codes_spec.rb
new file mode 100644
index 0000000000..21b2b93739
--- /dev/null
+++ b/core/spec/services/spree/promotion_batches/create_with_codes_spec.rb
@@ -0,0 +1,31 @@
+require 'spec_helper'
+
+module Spree
+ describe PromotionBatches::CreateWithCodes, :job do
+ subject { described_class.new.call(template_promotion: template_promotion, codes: codes) }
+
+ let(:template_promotion) { create(:promotion) }
+
+ context 'when an empty list of codes is provided' do
+ let(:codes) { [] }
+
+ it 'raises an error' do
+ expect { subject }.to raise_error(Spree::PromotionBatches::CreateWithCodes::Error)
+ end
+ end
+
+ context 'when a list of codes is provided' do
+ let(:codes) { ['XXX', 'ZZZ'] }
+
+ it 'creates a promotion batch' do
+ expect(subject.codes).to eq(codes)
+ expect(subject.template_promotion).to eq(template_promotion)
+ expect(subject.state).to eq('pending')
+ end
+
+ it 'enqueues a job to populate promotion batch' do
+ expect { subject }.to have_enqueued_job(Spree::PromotionBatches::PopulateJob)
+ end
+ end
+ end
+end
diff --git a/core/spec/services/spree/promotion_batches/create_with_random_codes_spec.rb b/core/spec/services/spree/promotion_batches/create_with_random_codes_spec.rb
new file mode 100644
index 0000000000..addb630ae3
--- /dev/null
+++ b/core/spec/services/spree/promotion_batches/create_with_random_codes_spec.rb
@@ -0,0 +1,33 @@
+require 'spec_helper'
+
+module Spree
+ describe PromotionBatches::CreateWithRandomCodes, :job do
+ subject do
+ described_class.new(generate_codes: generate_codes_double)
+ .call(template_promotion: template_promotion, amount: amount, random_characters: random_characters, prefix: prefix, suffix: suffix)
+ end
+ let(:generate_codes_double) { instance_double(Spree::PromotionBatches::GenerateCodes) }
+
+ let(:template_promotion) { create(:promotion) }
+ let(:amount) { 3 }
+ let(:random_characters) { 4 }
+ let(:prefix) { 'XXX' }
+ let(:suffix) { 'ZZZ' }
+ let(:generated_codes) { ['XXX_AAA_ZZZ', 'XXX_BBB_ZZZ', 'XXX_CCC_ZZZ'] }
+
+ before do
+ allow(generate_codes_double).to receive(:call).with(amount: amount, random_characters: random_characters, prefix: prefix, suffix: suffix)
+ .and_return(generated_codes)
+ end
+
+ it 'creates a promotion batch' do
+ expect(subject.codes).to eq(generated_codes)
+ expect(subject.template_promotion).to eq(template_promotion)
+ expect(subject.state).to eq('pending')
+ end
+
+ it 'enqueues a job to populate promotion batch' do
+ expect { subject }.to have_enqueued_job(Spree::PromotionBatches::PopulateJob)
+ end
+ end
+end
diff --git a/core/spec/services/spree/promotion_batches/destroy_spec.rb b/core/spec/services/spree/promotion_batches/destroy_spec.rb
deleted file mode 100644
index d261f58a77..0000000000
--- a/core/spec/services/spree/promotion_batches/destroy_spec.rb
+++ /dev/null
@@ -1,23 +0,0 @@
-require 'spec_helper'
-
-module Spree
- describe PromotionBatches::Destroy do
- subject { described_class.call(promotion_batch: promotion_batch) }
-
- let(:promotion_batch) { create(:promotion_batch) }
- let(:promotion) { create(:promotion, promotion_batch_id: promotion_batch.id ) }
- let(:promotion2) { create(:promotion, promotion_batch_id: promotion_batch.id ) }
-
- before do
- promotion_batch
- promotion
- promotion2
- end
-
- it 'destroys the promotion batch with its promotions' do
- subject
- expect(Spree::Promotion.all).not_to include(promotion, promotion2)
- expect(Spree::PromotionBatch.all).not_to include(promotion_batch)
- end
- end
-end
diff --git a/core/spec/services/spree/promotion_batches/export_spec.rb b/core/spec/services/spree/promotion_batches/export_spec.rb
new file mode 100644
index 0000000000..7d4711fe08
--- /dev/null
+++ b/core/spec/services/spree/promotion_batches/export_spec.rb
@@ -0,0 +1,24 @@
+require 'spec_helper'
+
+module Spree
+ describe PromotionBatches::Export do
+ let(:exporter) { described_class.new }
+
+ subject { exporter.call(promotion_batch: promotion_batch )}
+
+ let(:promotion_batch) { create(:promotion_batch, codes: ['CODE1', 'CODE2'], promotions: [promotion1, promotion2]) }
+ let(:promotion1) { create(:promotion, code: 'CODE1') }
+ let(:promotion2) { create(:promotion, code: 'CODE2') }
+
+ let(:expected_codes_csv) do
+ <<~CSV
+ #{promotion1.code}
+ #{promotion2.code}
+ CSV
+ end
+
+ it 'generates correct csv file' do
+ expect(subject).to eq(expected_codes_csv)
+ end
+ end
+end
diff --git a/core/spec/services/spree/promotion_batches/generate_code_spec.rb b/core/spec/services/spree/promotion_batches/generate_code_spec.rb
new file mode 100644
index 0000000000..0f74f8eef3
--- /dev/null
+++ b/core/spec/services/spree/promotion_batches/generate_code_spec.rb
@@ -0,0 +1,47 @@
+require 'spec_helper'
+
+module Spree
+ describe PromotionBatches::GenerateCode do
+ subject { described_class.new(random: random_double).call(random_characters: random_characters, prefix: prefix, suffix: suffix) }
+
+ let(:random_double) { double }
+ let(:random_characters) { 4 }
+ let(:prefix) { nil }
+ let(:suffix) { nil }
+
+ before do
+ allow(random_double).to receive(:hex).with(random_characters).and_return('CODE')
+ end
+
+ context 'when no prefix or suffix is set' do
+ it 'returns randomly generated code' do
+ expect(subject).to eq('CODE')
+ end
+ end
+
+ context 'when a prefix is set' do
+ let(:prefix) { 'MYCAMPAIGN_' }
+
+ it 'returns prefix and randomly generated code' do
+ expect(subject).to eq('MYCAMPAIGN_CODE')
+ end
+ end
+
+ context 'when a suffix is set' do
+ let(:suffix) { '_RETAIL' }
+
+ it 'returns randomly generated code with suffix' do
+ expect(subject).to eq('CODE_RETAIL')
+ end
+ end
+
+ context 'when both a prefix and a suffix is set' do
+ let(:prefix) { 'TESTCAMPAIGN_' }
+ let(:suffix) { '_ONLINE' }
+
+ it 'returns randomly generated code with prefix and suffix' do
+ expect(subject).to eq('TESTCAMPAIGN_CODE_ONLINE')
+ end
+ end
+ end
+end
diff --git a/core/spec/services/spree/promotion_batches/generate_codes_spec.rb b/core/spec/services/spree/promotion_batches/generate_codes_spec.rb
new file mode 100644
index 0000000000..c538a686f7
--- /dev/null
+++ b/core/spec/services/spree/promotion_batches/generate_codes_spec.rb
@@ -0,0 +1,42 @@
+require 'spec_helper'
+
+module Spree
+ describe PromotionBatches::GenerateCodes do
+ subject { described_class.new(generate_code: generate_code_double).call(amount: 3, random_characters: 3, prefix: 'XXX', suffix: 'ZZZ') }
+
+ let(:generate_code_double) { instance_double(Spree::PromotionBatches::GenerateCode) }
+
+ context 'when generator returns unique codes' do
+ before do
+ allow(generate_code_double).to receive(:call).with(random_characters: 3, prefix: 'XXX', suffix: 'ZZZ')
+ .and_return('XXX_AAA_ZZZ', 'XXX_BBB_ZZZ', 'XXX_CCC_ZZZ')
+ end
+
+ it 'returns generated codes' do
+ expect(subject).to eq(['XXX_AAA_ZZZ', 'XXX_BBB_ZZZ', 'XXX_CCC_ZZZ'])
+ end
+ end
+
+ context 'when generator returns duplicate codes' do
+ before do
+ allow(generate_code_double).to receive(:call).with(random_characters: 3, prefix: 'XXX', suffix: 'ZZZ')
+ .and_return('XXX_AAA_ZZZ', 'XXX_BBB_ZZZ', 'XXX_AAA_ZZZ', 'XXX_CCC_ZZZ')
+ end
+
+ it 'skips duplicate codes' do
+ expect(subject).to eq(['XXX_AAA_ZZZ', 'XXX_BBB_ZZZ', 'XXX_CCC_ZZZ'])
+ end
+ end
+
+ context 'when generator returns duplicate codes more than three times in a row' do
+ before do
+ allow(generate_code_double).to receive(:call).with(random_characters: 3, prefix: 'XXX', suffix: 'ZZZ')
+ .and_return('XXX_BBB_ZZZ', 'XXX_AAA_ZZZ', 'XXX_AAA_ZZZ', 'XXX_AAA_ZZZ', 'XXX_AAA_ZZZ', 'XXX_AAA_ZZZ')
+ end
+
+ it 'returns an error' do
+ expect { subject }.to raise_error(Spree::PromotionBatches::GenerateCodes::GenerateFailedError)
+ end
+ end
+ end
+end
diff --git a/core/spec/services/spree/promotion_batches/populate_spec.rb b/core/spec/services/spree/promotion_batches/populate_spec.rb
new file mode 100644
index 0000000000..ebdf833330
--- /dev/null
+++ b/core/spec/services/spree/promotion_batches/populate_spec.rb
@@ -0,0 +1,38 @@
+require 'spec_helper'
+
+module Spree
+ describe PromotionBatches::Populate do
+ subject { described_class.new.call(promotion_batch: promotion_batch) }
+
+ let!(:template_promotion) { create(:promotion) }
+ let(:promotion_batch) { create(:promotion_batch, template_promotion: template_promotion, codes: codes) }
+ let(:codes) { ['ABCD', 'EFGH', 'JKLM'] }
+
+ context 'when promotions can be created' do
+ it 'creates new promotions' do
+ expect { subject }.to change { Spree::Promotion.count }.by(3)
+ end
+
+ it 'changes promotion batch status to completed' do
+ subject
+ expect(promotion_batch.state).to eq('completed')
+ end
+ end
+
+ context 'when an error happens when copying a promotion' do
+ subject { described_class.new(duplicator_class: duplicator_class_double).call(promotion_batch: promotion_batch) }
+ let(:duplicator_class_double) { double }
+ let(:duplicator_double) { instance_double(Spree::PromotionHandler::PromotionBatchDuplicator) }
+
+ before do
+ allow(duplicator_class_double).to receive(:new).and_return(duplicator_double)
+ allow(duplicator_double).to receive(:duplicate).and_raise('Test error')
+ end
+
+ it 'raises an error and updates batch status to error' do
+ expect { subject }.to raise_error('Test error')
+ expect(promotion_batch.state).to eq('error')
+ end
+ end
+ end
+end
diff --git a/core/spec/services/spree/promotion_batches/promotion_codes_exporter_spec.rb b/core/spec/services/spree/promotion_batches/promotion_codes_exporter_spec.rb
deleted file mode 100644
index 28ae7b7cbb..0000000000
--- a/core/spec/services/spree/promotion_batches/promotion_codes_exporter_spec.rb
+++ /dev/null
@@ -1,30 +0,0 @@
-require 'spec_helper'
-
-module Spree
- describe PromotionBatches::PromotionCodesExporter do
- subject(:input) do
- described_class.new(params).call
- end
-
- let(:promotion_batch) { create(:promotion_batch) }
- let(:promotion) { create(:promotion, :with_unique_code, promotion_batch_id: promotion_batch.id ) }
- let(:promotion2) { create(:promotion, :with_unique_code, promotion_batch_id: promotion_batch.id ) }
- let(:params) { { id: promotion_batch.id } }
-
- let(:expected_codes_csv) do
- <<~CSV
- #{promotion.code}
- #{promotion2.code}
- CSV
- end
-
- before do
- promotion
- promotion2
- end
-
- it "generates correct csv file" do
- expect(input).to eq expected_codes_csv
- end
- end
-end
diff --git a/core/spec/services/spree/promotion_batches/promotion_codes_importer_spec.rb b/core/spec/services/spree/promotion_batches/promotion_codes_importer_spec.rb
deleted file mode 100644
index 9494a7f8b3..0000000000
--- a/core/spec/services/spree/promotion_batches/promotion_codes_importer_spec.rb
+++ /dev/null
@@ -1,78 +0,0 @@
-require 'spec_helper'
-
-module Spree
- describe PromotionBatches::PromotionCodesImporter do
- subject(:import_promo_codes) do
- described_class.new(file: file, promotion_batch_id: promotion_batch.id).call
- end
-
- let(:promotion_batch) { build(:promotion_batch) }
- let(:code1) { 'cf14cec8' }
- let(:code2) { '4b2ff1a7' }
- let(:template_promotion_id) { double }
- let(:batch_id) { double }
- let(:content) do
- <<~CSV
- #{code1}
- #{code2}
- CSV
- end
- let(:file) { double }
- let(:content_type) { 'text/csv' }
-
- before do
- allow(file)
- .to receive(:content_type)
- .and_return(content_type)
- allow(file)
- .to receive(:read)
- .and_return(content)
- allow(Spree::PromotionBatch)
- .to receive(:find)
- .and_return(promotion_batch)
- allow(promotion_batch)
- .to receive(:template_promotion_id)
- .and_return(template_promotion_id)
- allow(promotion_batch)
- .to receive(:id)
- .and_return(batch_id)
- end
-
- context "when file is correct" do
- it "enqueues DuplicatePromotionJob jobs", sidekiq: :inline do
- expect(Spree::Promotions::DuplicatePromotionJob)
- .to receive(:perform_later)
- .with(template_promotion_id: template_promotion_id, batch_id: batch_id, code: code1)
- expect(Spree::Promotions::DuplicatePromotionJob)
- .to receive(:perform_later)
- .with(template_promotion_id: template_promotion_id, batch_id: batch_id, code: code2)
-
- import_promo_codes
- end
- end
-
- context "when file has wrong content type" do
- let(:content_type) { 'unallowable_type' }
-
- it "raises an error" do
- expect { import_promo_codes }.to raise_error Spree::PromotionBatches::PromotionCodesImporter::Error
- end
- end
-
- context "when file is empty" do
- let(:content) { "" }
-
- it "raises an error" do
- expect { import_promo_codes }.to raise_error Spree::PromotionBatches::PromotionCodesImporter::Error
- end
- end
-
- context "when there's no file" do
- let(:file) { nil }
-
- it "raises an error" do
- expect { import_promo_codes }.to raise_error Spree::PromotionBatches::PromotionCodesImporter::Error
- end
- end
- end
-end
diff --git a/core/spec/services/spree/promotions/code_generator_spec.rb b/core/spec/services/spree/promotions/code_generator_spec.rb
deleted file mode 100644
index 3224792bad..0000000000
--- a/core/spec/services/spree/promotions/code_generator_spec.rb
+++ /dev/null
@@ -1,85 +0,0 @@
-require 'spec_helper'
-
-module Spree
- describe Promotions::CodeGenerator do
- describe "#build" do
- let(:random_code) { 'secure_random_code' }
- let(:content) { "black_week" }
-
- before do
- allow(SecureRandom).to receive(:hex).with(4).and_return(random_code)
- end
-
- context "prefix" do
- let(:affix) { :prefix }
-
- it "creates a code with a given prefix" do
- code = described_class.new(content: content, affix: affix).build
- expect(code).to eq content.concat(random_code)
- end
- end
-
- context "suffix" do
- let(:affix) { :suffix }
-
- it "creates a code with a given suffix" do
- code = described_class.new(content: content, affix: affix).build
- expect(code).to eq random_code.concat(content)
- end
- end
-
- context "deny-list" do
- let(:forbidden_phrases) { %w(forbidden phrase) }
- let(:deny_list) { forbidden_phrases }
-
- before do
- allow(SecureRandom)
- .to receive(:hex).with(4)
- .and_return("foo_#{forbidden_phrases.first}_bar", "foo_#{forbidden_phrases.last}_bar", random_code)
- end
-
- it "discards code containing forbidden phrases" do
- code = described_class.new(content: content, deny_list: deny_list).build
- expect(code).to eq random_code
- end
- end
-
- context "default" do
- it "creates a code" do
- code = described_class.new.build
- expect(code).to eq random_code
- end
- end
-
- context "forbidden phrases contain the affix" do
- let(:forbidden_phrases) { %w(black_week) }
- let(:deny_list) { forbidden_phrases }
- let(:affix) { :prefix }
-
- it "returns an error" do
- expect {
- described_class.new(content: content, affix: affix, deny_list: deny_list).build
- }.to raise_error Spree::Promotions::CodeGenerator::MutuallyExclusiveInputsError
- end
- end
-
- context "runs out of retries" do
- let(:forbidden_phrases) { (1..100).map(&:to_s) }
- let(:deny_list) { forbidden_phrases }
- let(:code_candidates) { forbidden_phrases }
-
- before do
- allow(SecureRandom)
- .to receive(:hex).with(4)
- .and_return(*code_candidates)
- end
-
- it "returns an error" do
- expect {
- described_class.new(deny_list: deny_list).build
- }.to raise_error Spree::Promotions::CodeGenerator::RetriesDepleted
- end
- end
- end
- end
-end
diff --git a/core/spec/services/spree/promotions/populate_promotion_batch_spec.rb b/core/spec/services/spree/promotions/populate_promotion_batch_spec.rb
deleted file mode 100644
index 8dda6856ef..0000000000
--- a/core/spec/services/spree/promotions/populate_promotion_batch_spec.rb
+++ /dev/null
@@ -1,49 +0,0 @@
-require 'spec_helper'
-
-module Spree
- describe Promotions::PopulatePromotionBatch do
- describe "#call" do
- subject(:populate_promotion_batch) { described_class.new(batch_id, options).call }
-
- let(:batch_id) { double }
- let(:options) { {batch_size: 3} }
- let(:promotion_batch) { build(:promotion_batch) }
- let(:template_promotion_id) { double }
-
- before do
- allow(Spree::PromotionBatch)
- .to receive(:find)
- .and_return(promotion_batch)
- allow(promotion_batch)
- .to receive(:template_promotion_id)
- .and_return(template_promotion_id)
- allow(Spree::Promotions::DuplicatePromotionJob)
- .to receive(:perform_later)
- .with(options)
- end
-
- context "when PromotionBatch has a template assigned" do
- it "enqueues DuplicatePromotionJob jobs", sidekiq: :inline do
- expect(Spree::Promotions::DuplicatePromotionJob)
- .to receive(:perform_later)
- .at_least(options[:batch_size]).times
- .with(template_promotion_id: template_promotion_id, batch_id: batch_id, options: {})
-
- populate_promotion_batch
- end
- end
-
- context "when PromotionBatch hasn't a template assigned" do
- before do
- allow(promotion_batch)
- .to receive(:template_promotion_id)
- .and_return(nil)
- end
-
- it "raises an error" do
- expect { populate_promotion_batch }.to raise_error Spree::Promotions::PopulatePromotionBatch::TemplateNotFoundError
- end
- end
- end
- end
-end