mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Merge pull request #9791 from vsmay98/8250-fix-snail-for-all-enterprise-fee
Fix snail issue for all enterprise fees
This commit is contained in:
@@ -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 @@ module Admin
|
||||
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 @@ module Admin
|
||||
]
|
||||
)
|
||||
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
|
||||
|
||||
@@ -58,6 +58,34 @@ describe '
|
||||
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 @@ describe '
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user