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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Revert "Bulk Promo Codes" (#12010)
Damian Legawiec3142f364fcac
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.rbdeleted file mode 100644index 905fbb5f24..0000000000--- a/api/app/controllers/spree/api/v2/platform/promotion_batches_controller.rb+++ /dev/null@@ -1,49 +0,0 @@-module Spree- module Api- module V2- module Platform- class PromotionBatchesController < ResourceController- def create- 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]- )-- render_serialized_payload { promotion_batch }- end-- def csv_export- 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 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]- )-- render_serialized_payload { promotion_batch }- end-- private-- def model_class- Spree::PromotionBatch- end-- def spree_permitted_attributes- [:template_promotion_id]- end- end- end- end- end-enddiff --git a/api/app/serializers/spree/api/v2/platform/promotion_batch_serializer.rb b/api/app/serializers/spree/api/v2/platform/promotion_batch_serializer.rbdeleted file mode 100644index 93c34885e9..0000000000--- a/api/app/serializers/spree/api/v2/platform/promotion_batch_serializer.rb+++ /dev/null@@ -1,15 +0,0 @@-module Spree- module Api- module V2- module Platform- class PromotionBatchSerializer < BaseSerializer- include ResourceSerializerConcern-- belongs_to :template_promotion, serializer: PromotionSerializer-- has_many :promotions- end- end- end- end-enddiff --git a/api/config/routes.rb b/api/config/routes.rbindex b3ad845b0f..88641f65e2 100644--- a/api/config/routes.rb+++ b/api/config/routes.rb@@ -72,17 +72,6 @@ Spree::Core::Engine.add_routes doresources :promotion_categoriesresources :promotion_rules- # Promotion Batches API- resources :promotion_batches, only: %i[index show create] do- member do- get :csv_export, to: 'promotion_batches#csv_export'- end-- collection do- post :import, to: 'promotion_batches#import'- end- end-# Returns APIresources :customer_returnsresources :reimbursementsdiff --git a/api/lib/spree/api/dependencies.rb b/api/lib/spree/api/dependencies.rbindex df84c35418..ca6ab43655 100644--- a/api/lib/spree/api/dependencies.rb+++ b/api/lib/spree/api/dependencies.rb@@ -114,9 +114,6 @@ module Spreeplatform_shipment_change_state_service: -> { Spree::Dependencies.shipment_change_state_service },platform_shipment_add_item_service: -> { Spree::Dependencies.shipment_add_item_service },platform_shipment_remove_item_service: -> { Spree::Dependencies.shipment_remove_item_service },-- # promotion batches services- platform_promotion_batch_destroy_service: -> { Spree::Dependencies.promotion_batch_destroy_service },}include Spree::DependenciesHelperdiff --git a/api/spec/requests/spree/api/v2/platform/promotion_batches_spec.rb b/api/spec/requests/spree/api/v2/platform/promotion_batches_spec.rbdeleted file mode 100644index b84ccaa176..0000000000--- a/api/spec/requests/spree/api/v2/platform/promotion_batches_spec.rb+++ /dev/null@@ -1,72 +0,0 @@-require 'spec_helper'--describe 'Promotion API v2 spec', type: :request do- include_context 'API v2 tokens'- include_context 'Platform API v2'-- let(:bearer_token) { { 'Authorization' => valid_authorization } }-- describe 'promotion_batches#index' do- before { create(:promotion_batch) }-- before { get '/api/v2/platform/promotion_batches', headers: bearer_token }-- it_behaves_like 'returns 200 HTTP status'-- 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#create' do- before { post '/api/v2/platform/promotion_batches', params: params, headers: bearer_token }-- let(:template_promotion) { create(:promotion) }-- let(:params) do- {- promotion_batch: {- template_promotion_id: template_promotion.id,- amount: 10,- random_characters: 5,- prefix: 'MYPROMO_'- }- }- end-- it_behaves_like 'returns 200 HTTP status'-- 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-- describe 'promotion_batches#import' do- before { post '/api/v2/platform/promotion_batches/import', params: params, headers: bearer_token }-- let(:template_promotion) { create(:promotion) }-- let(:params) do- {- promotion_batch: {- template_promotion_id: template_promotion.id,- codes: ['ABCD', 'EFGH']- }- }- end-- it_behaves_like 'returns 200 HTTP status'-- 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-enddiff --git a/api/spec/serializers/spree/api/v2/platform/promotion_serializer_spec.rb b/api/spec/serializers/spree/api/v2/platform/promotion_serializer_spec.rbindex 84f66bda36..0f2c9bd9f5 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,7 +20,6 @@ describe Spree::Api::V2::Platform::PromotionSerializer dodescription: 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.rbdeleted file mode 100644index d44c3f186d..0000000000--- a/core/app/jobs/spree/promotion_batches/populate_job.rb+++ /dev/null@@ -1,10 +0,0 @@-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-enddiff --git a/core/app/models/spree/promotion.rb b/core/app/models/spree/promotion.rbindex 841352e76a..45c435177a 100644--- a/core/app/models/spree/promotion.rb+++ b/core/app/models/spree/promotion.rb@@ -15,7 +15,6 @@ module Spreeattr_reader :eligibility_errors, :generate_codebelongs_to :promotion_category, optional: true- belongs_to :promotion_batch, optional: truehas_many :promotion_rules, autosave: true, dependent: :destroyalias rules promotion_rules@@ -29,8 +28,6 @@ module Spreehas_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_rulesvalidates_associated :rules@@ -50,7 +47,6 @@ module SpreeON spree_order_promotions.promotion_id = #{table_name}.idSQL}- scope :templates, -> { where(template: true) }self.whitelisted_ransackable_attributes = ['path', 'promotion_category_id', 'code']diff --git a/core/app/models/spree/promotion_batch.rb b/core/app/models/spree/promotion_batch.rbdeleted file mode 100644index a6a8ff057d..0000000000--- a/core/app/models/spree/promotion_batch.rb+++ /dev/null@@ -1,28 +0,0 @@-module Spree- class PromotionBatch < Spree::Base- has_many :promotions- 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-- event :complete do- transition from: :generating, to: :completed- end-- 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-enddiff --git a/core/app/models/spree/promotion_duplicator_core.rb b/core/app/models/spree/promotion_duplicator_core.rbdeleted file mode 100644index 4248694d37..0000000000--- a/core/app/models/spree/promotion_duplicator_core.rb+++ /dev/null@@ -1,45 +0,0 @@-module Spree- class PromotionDuplicatorCore- def initialize(promotion, random_string: generate_random_string(4))- @promotion = promotion- @random_string = random_string- end-- def duplicate- raise NotImplementedError- end-- private-- def copy_rules(new_promotion)- @promotion.promotion_rules.each do |rule|- new_rule = rule.dup- new_promotion.promotion_rules << new_rule-- new_rule.users = rule.users if rule.try(:users)- new_rule.taxons = rule.taxons if rule.try(:taxons)- new_rule.products = rule.products if rule.try(:products)- end- end-- def generate_random_string(number)- charset = Array('A'..'Z') + Array('a'..'z')- Array.new(number) { charset.sample }.join- end-- def copy_actions(new_promotion)- @promotion.promotion_actions.each do |action|- new_action = action.dup- new_action.calculator = action.calculator.dup if action.try(:calculator)-- new_promotion.promotion_actions << new_action-- next unless action.try(:promotion_action_line_items)-- action.promotion_action_line_items.each do |item|- new_action.promotion_action_line_items << item.dup- end- end- end- end-enddiff --git a/core/app/models/spree/promotion_handler/promotion_batch_duplicator.rb b/core/app/models/spree/promotion_handler/promotion_batch_duplicator.rbdeleted file mode 100644index 655632018b..0000000000--- a/core/app/models/spree/promotion_handler/promotion_batch_duplicator.rb+++ /dev/null@@ -1,28 +0,0 @@-module Spree- module PromotionHandler- class PromotionBatchDuplicator < Spree::PromotionDuplicatorCore- def initialize(promotion, promotion_batch_id, code:)- @promotion = promotion- @promotion_batch_id = promotion_batch_id- @code = code- end-- def duplicate- new_promotion = @promotion.dup- new_promotion.usage_limit = 1- new_promotion.promotion_batch_id = @promotion_batch_id- new_promotion.path = "#{@promotion.path}_#{@code}"- new_promotion.code = @code- new_promotion.stores = @promotion.stores-- ActiveRecord::Base.transaction do- new_promotion.save- copy_rules(new_promotion)- copy_actions(new_promotion)- end-- new_promotion- end- end- end-enddiff --git a/core/app/models/spree/promotion_handler/promotion_duplicator.rb b/core/app/models/spree/promotion_handler/promotion_duplicator.rbindex 711221d727..e88c34bb96 100644--- a/core/app/models/spree/promotion_handler/promotion_duplicator.rb+++ b/core/app/models/spree/promotion_handler/promotion_duplicator.rb@@ -1,20 +1,58 @@module Spreemodule PromotionHandler- class PromotionDuplicator < Spree::PromotionDuplicatorCore+ class PromotionDuplicator+ def initialize(promotion, random_string: nil)+ @promotion = promotion+ @random_string = random_string || generate_random_string(4)+ end+def duplicate- new_promotion = @promotion.dup- new_promotion.path = "#{@promotion.path}_#{@random_string}"- new_promotion.name = "New #{@promotion.name}"- new_promotion.code = "#{@promotion.code}_#{@random_string}"- new_promotion.stores = @promotion.stores+ @new_promotion = @promotion.dup+ @new_promotion.path = "#{@promotion.path}_#{@random_string}"+ @new_promotion.name = "New #{@promotion.name}"+ @new_promotion.code = "#{@promotion.code}_#{@random_string}"+ @new_promotion.stores = @promotion.storesActiveRecord::Base.transaction do- new_promotion.save- copy_rules(new_promotion)- copy_actions(new_promotion)+ @new_promotion.save+ copy_rules+ copy_actionsend- new_promotion+ @new_promotion+ end++ private++ def copy_rules+ @promotion.promotion_rules.each do |rule|+ new_rule = rule.dup+ @new_promotion.promotion_rules << new_rule++ new_rule.users = rule.users if rule.try(:users)+ new_rule.taxons = rule.taxons if rule.try(:taxons)+ new_rule.products = rule.products if rule.try(:products)+ end+ end++ def generate_random_string(number)+ charset = Array('A'..'Z') + Array('a'..'z')+ Array.new(number) { charset.sample }.join+ end++ def copy_actions+ @promotion.promotion_actions.each do |action|+ new_action = action.dup+ new_action.calculator = action.calculator.dup if action.try(:calculator)++ @new_promotion.promotion_actions << new_action++ next unless action.try(:promotion_action_line_items)++ action.promotion_action_line_items.each do |item|+ new_action.promotion_action_line_items << item.dup+ end+ endendendenddiff --git a/core/app/services/spree/promotion_batches/create_with_codes.rb b/core/app/services/spree/promotion_batches/create_with_codes.rbdeleted file mode 100644index 22c0c74aaf..0000000000--- a/core/app/services/spree/promotion_batches/create_with_codes.rb+++ /dev/null@@ -1,26 +0,0 @@-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-enddiff --git a/core/app/services/spree/promotion_batches/create_with_random_codes.rb b/core/app/services/spree/promotion_batches/create_with_random_codes.rbdeleted file mode 100644index e8ce0bd803..0000000000--- a/core/app/services/spree/promotion_batches/create_with_random_codes.rb+++ /dev/null@@ -1,22 +0,0 @@-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-enddiff --git a/core/app/services/spree/promotion_batches/export.rb b/core/app/services/spree/promotion_batches/export.rbdeleted file mode 100644index 154b15b5e3..0000000000--- a/core/app/services/spree/promotion_batches/export.rb+++ /dev/null@@ -1,15 +0,0 @@-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-enddiff --git a/core/app/services/spree/promotion_batches/generate_code.rb b/core/app/services/spree/promotion_batches/generate_code.rbdeleted file mode 100644index f34a464768..0000000000--- a/core/app/services/spree/promotion_batches/generate_code.rb+++ /dev/null@@ -1,17 +0,0 @@-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-enddiff --git a/core/app/services/spree/promotion_batches/generate_codes.rb b/core/app/services/spree/promotion_batches/generate_codes.rbdeleted file mode 100644index dad85a03c9..0000000000--- a/core/app/services/spree/promotion_batches/generate_codes.rb+++ /dev/null@@ -1,37 +0,0 @@-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-enddiff --git a/core/app/services/spree/promotion_batches/populate.rb b/core/app/services/spree/promotion_batches/populate.rbdeleted file mode 100644index 68f2dbb0ea..0000000000--- a/core/app/services/spree/promotion_batches/populate.rb+++ /dev/null@@ -1,27 +0,0 @@-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-enddiff --git a/core/config/locales/en.yml b/core/config/locales/en.ymlindex f1ff1ed252..67890a1ac2 100644--- a/core/config/locales/en.yml+++ b/core/config/locales/en.yml@@ -229,9 +229,6 @@ en:spree/promotion:one: Promotionother: Promotions- spree/promotion_batch:- one: Promotion Batch- other: Promotion Batchesspree/promotion_category:one: Promotion Categoryother: Promotion Categories@@ -683,7 +680,6 @@ en:close: Closeclose_all_adjustments: Close All Adjustmentscode: Code- code_upload: Promotion codes are being uploaded. It might take a while.company: Companycompare_at_price: Compare at pricecomplete: complete@@ -946,7 +942,6 @@ en:internationalization: Internationalizationinvalid_credit_card: Invalid credit card.invalid_exchange_variant: Invalid exchange variant.- invalid_file: No file / Wrong file format / Empty fileinvalid_payment_provider: Invalid payment provider.invalid_promotion_action: Invalid promotion action.invalid_promotion_rule: Invalid promotion rule.@@ -1024,7 +1019,6 @@ en:meta_title: Meta Titlemetadata: Metadatamutable: Mutable- mutually_exclusive_inputs: Invalid inputs. Forbidden phrases cannot contain the affix.must_have_one_store: The resource must belong to a store.min: 'Min'minimal_amount: Minimal Amount@@ -1065,7 +1059,6 @@ en:new_payment_method: New Payment Methodnew_product: New Productnew_promotion: New Promotion- new_promotion_batch: New Promotion Batchnew_promotion_category: New Promotion Categorynew_property: New Propertynew_prototype: New Prototype@@ -1110,7 +1103,6 @@ en:no_returns_found: No returns foundno_shipping_method_selected: No shipping method selected.no_state_changes: No state changes yet.- no_template_promotion: Promotion template must be set in order to import promotion codes.no_tracking_present: No tracking details provided.user_not_found: User not foundnone: None@@ -1319,20 +1311,8 @@ en:manual: Manually chooseproducts: Productspromotion: Promotion- promotion_batch: Promotion Batch- promotion_batches: Promotion Batches- promotion_batch_form:- affix: Affix- affix_content: Affix content- batch_size: Batch size- forbidden_phrases: Forbidden phrases- import_codes: Import codes from CSV- populate: Populate- prefix: Prefix- random_part_bytes: Random part bytes- sufix: Sufix- promotion_batch_populated: Promotion Batch is being populated.promotionable: Promotable+ promotion_cloned: Promotion has been clonedpromotion_not_cloned: "Promotion has not been cloned. Reason: %{error}"promotion_action: Promotion Actionpromotion_action_types:@@ -1743,8 +1723,6 @@ en:taxonomy_tree_error: The requested change has not been accepted and the tree has been returned to its previous state, please try again.taxonomy_tree_instruction: ! '* Right click (or double tap on iOS device) a child in the tree to access the menu for adding, deleting or sorting a child.'taxons: Taxons- template_not_found: Template promotion - not found! Assign a template before populating a batch.- template_promotion_already_assigned: Template promotion has already been assigned!test: Testtest_mailer:test_email:diff --git a/core/db/migrate/20231114071427_create_promotion_batches.rb b/core/db/migrate/20231114071427_create_promotion_batches.rbdeleted file mode 100644index 786ae03216..0000000000--- a/core/db/migrate/20231114071427_create_promotion_batches.rb+++ /dev/null@@ -1,11 +0,0 @@-class CreatePromotionBatches < ActiveRecord::Migration[6.1]- def change- create_table :spree_promotion_batches do |t|- t.string :state- t.text :codes-- t.references :template_promotion, foreign_key: { to_table: :spree_promotions }- t.timestamps- end- end-enddiff --git a/core/db/migrate/20231114182646_add_promotion_batch_reference_to_promotions.rb b/core/db/migrate/20231114182646_add_promotion_batch_reference_to_promotions.rbdeleted file mode 100644index 4a9e1a7ccd..0000000000--- a/core/db/migrate/20231114182646_add_promotion_batch_reference_to_promotions.rb+++ /dev/null@@ -1,6 +0,0 @@-class AddPromotionBatchReferenceToPromotions < ActiveRecord::Migration[6.1]- def change- add_reference :spree_promotions, :promotion_batch, foreign_key: { to_table: :spree_promotion_batches}- add_column :spree_promotions, :template, :boolean- end-enddiff --git a/core/lib/spree/core/dependencies.rb b/core/lib/spree/core/dependencies.rbindex 4c3dbdbf2a..5f92821756 100644--- a/core/lib/spree/core/dependencies.rb+++ b/core/lib/spree/core/dependencies.rb@@ -97,10 +97,7 @@ module Spreecredit_card_finder: 'Spree::CreditCards::Find',products_finder: 'Spree::Products::Find',taxon_finder: 'Spree::Taxons::Find',- line_item_by_variant_finder: 'Spree::LineItems::FindByVariant',-- # promotion batches- promotion_batch_destroy_service: 'Spree::PromotionBatches::Destroy'+ line_item_by_variant_finder: 'Spree::LineItems::FindByVariant'}.freezeinclude Spree::DependenciesHelperdiff --git a/core/lib/spree/testing_support/factories/promotion_batch_factory.rb b/core/lib/spree/testing_support/factories/promotion_batch_factory.rbdeleted file mode 100644index c42a094f26..0000000000--- a/core/lib/spree/testing_support/factories/promotion_batch_factory.rb+++ /dev/null@@ -1,7 +0,0 @@-FactoryBot.define do- factory :promotion_batch, class: Spree::PromotionBatch do- association :template_promotion, factory: :promotion-- codes { [SecureRandom.hex(5), SecureRandom.hex(5)] }- end-enddiff --git a/core/spec/models/spree/promotion_batch_spec.rb b/core/spec/models/spree/promotion_batch_spec.rbdeleted file mode 100644index 6f620169df..0000000000--- a/core/spec/models/spree/promotion_batch_spec.rb+++ /dev/null@@ -1,30 +0,0 @@-require 'spec_helper'--describe Spree::PromotionBatch, type: :model do- describe '#save' do- let(:template_promotion) { create(:promotion) }- let(:promotion_batch) { build(:promotion_batch, template_promotion: template_promotion, codes: codes) }-- 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 'passes validation' do- subject- expect(promotion_batch.errors[:codes]).to be_empty- expect(promotion_batch.persisted?).to eq(true)- end- end- end-enddiff --git a/core/spec/models/spree/promotion_handler/promotion_batch_duplicator_spec.rb b/core/spec/models/spree/promotion_handler/promotion_batch_duplicator_spec.rbdeleted file mode 100644index 12aa435e8e..0000000000--- a/core/spec/models/spree/promotion_handler/promotion_batch_duplicator_spec.rb+++ /dev/null@@ -1,128 +0,0 @@-require 'spec_helper'--describe Spree::PromotionHandler::PromotionBatchDuplicator do- subject { described_class.new(promotion, promotion_batch.id, code: 'test') }-- 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) }-- context 'model fields' do- let(:excluded_fields) { ['code', 'path', 'id', 'created_at', 'updated_at', 'deleted_at', 'promotion_batch_id', 'usage_limit'] }-- context "when code is provided" do- subject { described_class.new(promotion, promotion_batch.id, code: provided_code) }-- let(:provided_code) { 'provided_code' }-- it 'assigns a unique code to the duplicated promotion' do- result = new_promotion- expect(result.code).to eq(provided_code)- end-- 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 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 'associates the duplicated 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-enddiff --git a/core/spec/models/spree/promotion_spec.rb b/core/spec/models/spree/promotion_spec.rbindex dd0ff6f438..e41fba918d 100644--- a/core/spec/models/spree/promotion_spec.rb+++ b/core/spec/models/spree/promotion_spec.rb@@ -162,7 +162,7 @@ describe Spree::Promotion, type: :model dolet(:user) { create(:user) }let(:order) { create(:order, user: user) }let(:payload) { { order: order, user: user } }-+before doallow(action1).to receive_messages perform: trueallow(action2).to receive_messages perform: truediff --git a/core/spec/services/spree/promotion_batches/create_with_codes_spec.rb b/core/spec/services/spree/promotion_batches/create_with_codes_spec.rbdeleted file mode 100644index 21b2b93739..0000000000--- a/core/spec/services/spree/promotion_batches/create_with_codes_spec.rb+++ /dev/null@@ -1,31 +0,0 @@-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-enddiff --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.rbdeleted file mode 100644index addb630ae3..0000000000--- a/core/spec/services/spree/promotion_batches/create_with_random_codes_spec.rb+++ /dev/null@@ -1,33 +0,0 @@-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-enddiff --git a/core/spec/services/spree/promotion_batches/export_spec.rb b/core/spec/services/spree/promotion_batches/export_spec.rbdeleted file mode 100644index 7d4711fe08..0000000000--- a/core/spec/services/spree/promotion_batches/export_spec.rb+++ /dev/null@@ -1,24 +0,0 @@-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-enddiff --git a/core/spec/services/spree/promotion_batches/generate_code_spec.rb b/core/spec/services/spree/promotion_batches/generate_code_spec.rbdeleted file mode 100644index 0f74f8eef3..0000000000--- a/core/spec/services/spree/promotion_batches/generate_code_spec.rb+++ /dev/null@@ -1,47 +0,0 @@-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-enddiff --git a/core/spec/services/spree/promotion_batches/generate_codes_spec.rb b/core/spec/services/spree/promotion_batches/generate_codes_spec.rbdeleted file mode 100644index c538a686f7..0000000000--- a/core/spec/services/spree/promotion_batches/generate_codes_spec.rb+++ /dev/null@@ -1,42 +0,0 @@-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-enddiff --git a/core/spec/services/spree/promotion_batches/populate_spec.rb b/core/spec/services/spree/promotion_batches/populate_spec.rbdeleted file mode 100644index ebdf833330..0000000000--- a/core/spec/services/spree/promotion_batches/populate_spec.rb+++ /dev/null@@ -1,38 +0,0 @@-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