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
Sd 875 add promotion code again by admin (#10518)
Kacper Mękarskifd0131aea17c
diff --git a/backend/app/controllers/spree/admin/adjustments_controller.rb b/backend/app/controllers/spree/admin/adjustments_controller.rbindex 38a482f33e..4953e19e82 100644--- a/backend/app/controllers/spree/admin/adjustments_controller.rb+++ b/backend/app/controllers/spree/admin/adjustments_controller.rb@@ -11,6 +11,8 @@ module Spreebefore_action :find_adjustment, only: [:destroy, :edit, :update]+ after_action :delete_promotion_from_order, only: [:destroy], if: -> { @adjustment.destroyed? && @adjustment.promotion? }+def index@adjustments = @order.all_adjustments.eligible.order(created_at: :asc)end@@ -31,6 +33,12 @@ module Spreedef build_resourceparent.adjustments.build(order: parent)end++ def delete_promotion_from_order+ return if @adjustment.source.nil?++ @order.promotions.delete(@adjustment.source.promotion)+ endendendenddiff --git a/backend/spec/controllers/spree/admin/adjustments_controller_controller_spec.rb b/backend/spec/controllers/spree/admin/adjustments_controller_controller_spec.rbindex ba4a0bdeea..a4cb0e20d9 100644--- a/backend/spec/controllers/spree/admin/adjustments_controller_controller_spec.rb+++ b/backend/spec/controllers/spree/admin/adjustments_controller_controller_spec.rb@@ -30,6 +30,70 @@ module Spreeexpect(assigns(:adjustments)).to match_array([adjustment_1])endend++ describe '#destroy' do+ subject(:destroy) do+ delete :destroy, params: { order_id: order.to_param, id: adjustment.id, format: :js }+ end++ shared_examples 'adjustment destroyed' do+ it 'destroys one adjustment' do+ expect { destroy }.to change { Spree::Adjustment.count }.by(-1)+ end++ it 'assigns adjustment object to instance variable' do+ destroy++ expect(assigns(:adjustment)).to eq(adjustment)+ end++ it 'returns 200 status' do+ destroy++ expect(response).to have_http_status(:ok)+ end++ it 'returns success flash response' do+ destroy++ expect(flash[:success]).to eq(Spree.t(:successfully_removed, resource: 'Adjustment'))+ end++ it 'leaves error flash empty' do+ destroy++ expect(flash[:error]).to be_nil+ end+ end++ context 'when adjustment is from coupon code' do+ context 'when is destroyed' do+ let!(:order) { create(:order) }+ let!(:promotion) { create(:promotion, orders: [order]) }+ let!(:source) { Spree::Promotion::Actions::CreateAdjustment.create(promotion_id: promotion.id) }+ let!(:adjustment) { create(:adjustment, order: order, source_type: 'Spree::PromotionAction', source: source) }++ it 'SD-875 removes associated promotion from order' do+ expect(adjustment.order.promotions).to include promotion++ destroy++ expect(adjustment.order.promotions).not_to include promotion+ end++ it_behaves_like 'adjustment destroyed'+ end+ end++ context 'when adjustment is created directly' do+ context 'when is destroyed' do+ let!(:order) { create(:order) }+ let!(:adjustment) { create(:adjustment, order: order, source_type: nil, source: nil) }++ it_behaves_like 'adjustment destroyed'+ end+ end+ endendendenddiff --git a/backend/spec/features/admin/orders/adjustments_promotions_spec.rb b/backend/spec/features/admin/orders/adjustments_promotions_spec.rbindex b7fc922ee9..e068e363f7 100644--- a/backend/spec/features/admin/orders/adjustments_promotions_spec.rb+++ b/backend/spec/features/admin/orders/adjustments_promotions_spec.rb@@ -50,4 +50,26 @@ describe 'Adjustments Promotions', type: :feature doendendend++ context 'admin is able to add promotion code after removing one before' do+ it 'allows to add previously deleted promotion', js: true do+ fill_in 'coupon_code', with: '10_off'+ click_button 'Add Coupon Code'+ expect(page).to have_content('$10 off')+ expect(page).to have_content('-$10.00')++ accept_confirm do+ within_row(1) do+ click_icon(:delete)+ end+ end+ expect(page).not_to have_content('$10 off')+ expect(page).not_to have_content('-$10.00')++ fill_in 'coupon_code', with: '10_off'+ click_button 'Add Coupon Code'+ expect(page).to have_content('$10 off')+ expect(page).to have_content('-$10.00')+ end+ endend