From d1b5dcab885f267a5ae4f0de0111cc8febfbddf3 Mon Sep 17 00:00:00 2001 From: Gaetan Craig-Riou Date: Tue, 5 Sep 2023 15:48:59 +0200 Subject: [PATCH] Add ability to activate deactivate a voucher Plus controller specs --- .../admin/enterprises_controller.rb | 15 ++++++ .../enterprises/form/_vouchers.html.haml | 15 +++++- config/locales/en.yml | 1 + .../admin/enterprises_controller_spec.rb | 48 +++++++++++++++++++ 4 files changed, 77 insertions(+), 2 deletions(-) diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index dbdd701fb8..775d00840b 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -63,6 +63,7 @@ module Admin tag_rules_attributes = params[object_name].delete :tag_rules_attributes update_tag_rules(tag_rules_attributes) if tag_rules_attributes.present? update_enterprise_notifications + update_vouchers delete_custom_tab if params[:custom_tab] == 'false' @@ -268,6 +269,20 @@ module Admin @enterprise.update_contact(user_id) end + def update_vouchers + return if params[:enterprise][:voucher_ids].blank? + + params_voucher_ids = params[:enterprise][:voucher_ids].map(&:to_i) + voucher_ids = @enterprise.vouchers.map(&:id) + deleted_voucher_ids = @enterprise.vouchers.only_deleted.map(&:id) + + vouchers_to_destroy = voucher_ids - params_voucher_ids + Voucher.where(id: vouchers_to_destroy).destroy_all if vouchers_to_destroy.present? + + vouchers_to_restore = deleted_voucher_ids.intersection(params_voucher_ids) + Voucher.restore(vouchers_to_restore) if vouchers_to_restore.present? + end + def create_calculator_for(rule, attrs) return unless attrs[:calculator_type].present? && attrs[:calculator_attributes].present? diff --git a/app/views/admin/enterprises/form/_vouchers.html.haml b/app/views/admin/enterprises/form/_vouchers.html.haml index 8df72f951c..5aa9ba9dd4 100644 --- a/app/views/admin/enterprises/form/_vouchers.html.haml +++ b/app/views/admin/enterprises/form/_vouchers.html.haml @@ -3,12 +3,13 @@ = t('.add_new') %br -- if @enterprise.vouchers.present? +- if @enterprise.vouchers.present? || @enterprise.vouchers.only_deleted.present? %table %thead %tr %th= t('.voucher_code') %th= t('.rate') + %th= t('.active') /%th= t('.label') /%th= t('.purpose') /%th= t('.expiry') @@ -20,13 +21,23 @@ %tr %td= voucher.code %td= voucher.display_value + %td= f.check_box :voucher_ids, { :multiple => true }, voucher.id, nil + /%td + /%td + /%td + /%td + /%td + - @enterprise.vouchers.only_deleted.each do |voucher| + %tr + %td= voucher.code + %td= voucher.display_value + %td= f.check_box :voucher_ids, { :multiple => true }, voucher.id, nil /%td /%td /%td /%td /%td /%td - - else %p.text-center = t('.no_voucher_yet') diff --git a/config/locales/en.yml b/config/locales/en.yml index 9020961d32..b4d04d7221 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1236,6 +1236,7 @@ en: use_limit: Use/Limit customers: Customer net_value: Net Value + active: Active? add_new: Add New no_voucher_yet: No Vouchers yet white_label: diff --git a/spec/controllers/admin/enterprises_controller_spec.rb b/spec/controllers/admin/enterprises_controller_spec.rb index 10bd9d3200..c68c04b82d 100644 --- a/spec/controllers/admin/enterprises_controller_spec.rb +++ b/spec/controllers/admin/enterprises_controller_spec.rb @@ -287,6 +287,54 @@ describe Admin::EnterprisesController, type: :controller do end end end + + describe "vouchers" do + let(:enterprise) { create(:distributor_enterprise) } + let!(:voucher_a) { create(:voucher, enterprise: enterprise, code: "voucher 1") } + let!(:voucher_b) { create(:voucher, enterprise: enterprise, code: "voucher 2") } + + before do + controller_login_as_enterprise_user [enterprise] + end + + it "activates checked vouchers" do + voucher_b.destroy + + spree_put :update, + id: enterprise, + enterprise: { + voucher_ids: [voucher_a.id.to_s, voucher_b.id.to_s] + } + + expect(voucher_b.reload.deleted?).to be(false) + end + + it "deactivates unchecked vouchers" do + spree_put :update, + id: enterprise, + enterprise: { + voucher_ids: [voucher_b.id.to_s] + } + + expect(voucher_a.reload.deleted?).to be(true) + end + + context "when activating and deactivating voucher at the same time" do + it "deactivates and activates accordingly" do + voucher_c = create(:voucher, enterprise: enterprise, code: "voucher 3") + voucher_c.destroy + + spree_put :update, + id: enterprise, + enterprise: { + voucher_ids: [voucher_a.id.to_s, voucher_c.id.to_s] + } + + expect(enterprise.vouchers.reload).to include(voucher_c) + expect(enterprise.vouchers.reload.only_deleted).to include(voucher_b) + end + end + end end context "as owner" do