Add ability to activate deactivate a voucher

Plus controller specs
This commit is contained in:
Gaetan Craig-Riou
2023-09-05 15:48:59 +02:00
parent 79f4caaee2
commit d1b5dcab88
4 changed files with 77 additions and 2 deletions

View File

@@ -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?

View File

@@ -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')

View File

@@ -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:

View File

@@ -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