From 755107ec9f60034119cbabdf8c2b077aedd92fee Mon Sep 17 00:00:00 2001 From: Vishal Jain Date: Sun, 16 Oct 2022 13:39:32 +0530 Subject: [PATCH 1/2] Fix snail issue for all enterprise fees --- .../admin/enterprise_fees_controller.rb | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/app/controllers/admin/enterprise_fees_controller.rb b/app/controllers/admin/enterprise_fees_controller.rb index f14612a2cdb..8bd4793c5ba 100644 --- a/app/controllers/admin/enterprise_fees_controller.rb +++ b/app/controllers/admin/enterprise_fees_controller.rb @@ -6,6 +6,7 @@ module Admin class EnterpriseFeesController < Admin::ResourceController before_action :load_enterprise_fee_set, only: :index before_action :load_data + before_action :check_enterprise_fee_input, only: [:bulk_update] def index @include_calculators = params[:include_calculators].present? @@ -35,13 +36,6 @@ def for_order_cycle end def bulk_update - @flat_percent_value = enterprise_fee_bulk_params.dig('collection_attributes', '0', 'calculator_attributes', 'preferred_flat_percent') - - unless @flat_percent_value.nil? || Float(@flat_percent_value, exception: false) - flash[:error] = I18n.t(:calculator_preferred_value_error) - return redirect_to redirect_path - end - @enterprise_fee_set = Sets::EnterpriseFeeSet.new(enterprise_fee_bulk_params) if @enterprise_fee_set.save @@ -105,5 +99,25 @@ def enterprise_fee_bulk_params ] ) end + + def check_enterprise_fee_input + enterprise_fee_bulk_params['collection_attributes'].each do |_, fee_row| + enterprise_fees = fee_row['calculator_attributes']&.slice( + :preferred_flat_percent, :preferred_amount, + :preferred_first_item, :preferred_additional_item, + :preferred_minimal_amount, :preferred_normal_amount, + :preferred_discount_amount, :preferred_per_unit + ) + + next unless enterprise_fees + + enterprise_fees.each do |_, enterprise_amount| + unless enterprise_amount.nil? || Float(enterprise_amount, exception: false) + flash[:error] = I18n.t(:calculator_preferred_value_error) + return redirect_to redirect_path + end + end + end + end end end From de28027623091f1ad111604a9e79d6338237e4ee Mon Sep 17 00:00:00 2001 From: Vishal Jain Date: Thu, 20 Oct 2022 11:36:33 +0530 Subject: [PATCH 2/2] add system spec --- spec/system/admin/enterprise_fees_spec.rb | 38 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/spec/system/admin/enterprise_fees_spec.rb b/spec/system/admin/enterprise_fees_spec.rb index dbd9a4e2ad2..2c4fd4a5ac4 100644 --- a/spec/system/admin/enterprise_fees_spec.rb +++ b/spec/system/admin/enterprise_fees_spec.rb @@ -58,6 +58,34 @@ expect(page).to have_selector "#sets_enterprise_fee_set_collection_attributes_0_calculator_attributes_preferred_flat_percent[value='12.34']" end + it "creating an enterprise fee with invalid amount shows error flash message" do + # Given an enterprise + e = create(:supplier_enterprise, name: 'Feedme') + + # When I go to the enterprise fees page + login_as_admin_and_visit admin_enterprise_fees_path + + # And I fill in the fields for a new enterprise fee and click update + select 'Feedme', from: 'sets_enterprise_fee_set_collection_attributes_0_enterprise_id' + select 'Admin', from: 'sets_enterprise_fee_set_collection_attributes_0_fee_type' + fill_in 'sets_enterprise_fee_set_collection_attributes_0_name', with: 'Hello!' + select 'GST', from: 'sets_enterprise_fee_set_collection_attributes_0_tax_category_id' + select 'Flat Percent', from: 'sets_enterprise_fee_set_collection_attributes_0_calculator_type' + click_button 'Update' + + # Then I should see my fee and fields for the calculator + expect(page).to have_content "Your enterprise fees have been updated." + expect(page).to have_selector "input[value='Hello!']" + + # When I fill in the calculator fields and click update + fill_in 'sets_enterprise_fee_set_collection_attributes_0_calculator_attributes_preferred_flat_percent', + with: "\'20.0'" + click_button 'Update' + + # Then I should see the flash error message + expect(flash_message).to eq('Invalid input. Please use only numbers. For example: 10, 5.5, -20') + end + context "editing an enterprise fee" do # Given an enterprise fee let!(:fee) { create(:enterprise_fee) } @@ -106,6 +134,16 @@ expect(fee.reload.calculator_type).to eq("Calculator::PerItem") end + + it 'shows error flash when updating fee amount with invalid values' do + # When I fill in the calculator fields and click update + fill_in 'sets_enterprise_fee_set_collection_attributes_0_calculator_attributes_preferred_flat_percent', + with: "\'20.0'" + click_button 'Update' + + # Then I should see the flash error message + expect(flash_message).to eq('Invalid input. Please use only numbers. For example: 10, 5.5, -20') + end end it "deleting an enterprise fee" do