mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-10 23:07:47 +00:00
Add ability to activate deactivate a voucher
Plus controller specs
This commit is contained in:
@@ -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?
|
||||
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user