Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix snail issue for all enterprise fees #9791

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions app/controllers/admin/enterprise_fees_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
38 changes: 38 additions & 0 deletions spec/system/admin/enterprise_fees_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down Expand Up @@ -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
Expand Down