Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljames-dj committed Jan 9, 2025
1 parent bd132ce commit e28d6cc
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/controllers/country_bands_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def index

def edit
@number = id_from_params
unless CountryBandDetail.distinct.pluck(:number).include?(@number)
unless CountryBandDetail.exists?(number: @number)
flash[:danger] = "Unknown band number"
return redirect_to country_bands_path
end
Expand Down
6 changes: 5 additions & 1 deletion db/migrate/20241225031242_populate_country_band_details.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class PopulateCountryBandDetails < ActiveRecord::Migration[7.2]
def change
def up
CountryBandDetail.create!(
number: 0,
start_date: '2018-01-01',
Expand Down Expand Up @@ -39,4 +39,8 @@ def change
due_percent_registration_fee: 15,
)
end

def down
CountryBandDetail.delete_all
end
end
38 changes: 38 additions & 0 deletions db/seeds/country_band_details.seeds.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

CountryBandDetail.create!(
number: 0,
start_date: '2018-01-01',
due_amount_per_competitor_in_cents: 0,
due_percent_registration_fee: 0,
)
CountryBandDetail.create!(
number: 1,
start_date: '2018-01-01',
due_amount_per_competitor_in_cents: 19,
due_percent_registration_fee: 5,
)
CountryBandDetail.create!(
number: 2,
start_date: '2018-01-01',
due_amount_per_competitor_in_cents: 32,
due_percent_registration_fee: 5,
)
CountryBandDetail.create!(
number: 3,
start_date: '2018-01-01',
due_amount_per_competitor_in_cents: 45,
due_percent_registration_fee: 15,
)
CountryBandDetail.create!(
number: 4,
start_date: '2018-01-01',
due_amount_per_competitor_in_cents: 228,
due_percent_registration_fee: 15,
)
CountryBandDetail.create!(
number: 5,
start_date: '2018-01-01',
due_amount_per_competitor_in_cents: 300,
due_percent_registration_fee: 15,
)
14 changes: 14 additions & 0 deletions lib/database_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,20 @@ def self.actions_to_column_sanitizers(columns_by_action)
),
),
}.freeze,
"country_band_details" => {
column_sanitizers: actions_to_column_sanitizers(
copy: %w(
id
number
start_date
end_date
due_amount_per_competitor_in_cents
due_percent_registration_fee
created_at
updated_at
),
),
}.freeze,
"user_roles" => {
where_clause: "JOIN user_groups ON user_groups.id=group_id WHERE NOT user_groups.is_hidden",
column_sanitizers: actions_to_column_sanitizers(
Expand Down
15 changes: 8 additions & 7 deletions lib/dues_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ def self.dues_for_n_competitors(country_iso2, base_entry_fee_lowest_denomination

def self.dues_per_competitor_in_usd(country_iso2, base_entry_fee_lowest_denomination, currency_code)
country_band = CountryBand.find_by(iso2: country_iso2)&.number
country_band_detail = CountryBandDetail.find_by(number: country_band)
registration_fees = Money.new(base_entry_fee_lowest_denomination, currency_code).exchange_to("USD")

DuesCalculator.update_exchange_rates_if_needed
input_money_us_dollars = Money.new(base_entry_fee_lowest_denomination, currency_code).exchange_to("USD")

country_band_detail = CountryBandDetail.find_by(number: country_band)
return nil unless country_band_detail
# Calculation of 'registration fee dues'
registration_fee_dues = Money.new(registration_fees * (country_band_detail&.due_percent_registration_fee.to_f || 0) / 100, "USD")

registration_fee_dues_us_dollars = input_money_us_dollars * country_band_detail.due_percent_registration_fee.to_f/100
# cent is given directly because Money require lowest currency subunit, which is cents for USD
country_band_dues_us_dollars_money = Money.new(country_band_detail.due_amount_per_competitor_in_cents, "USD")
# Calculation of 'country band dues'
country_band_dues = Money.new(country_band_detail&.due_amount_per_competitor_in_cents || 0, "USD")

[registration_fee_dues_us_dollars, country_band_dues_us_dollars_money].max
# The maximum of the two is the total dues per competitor
[registration_fee_dues, country_band_dues].max
rescue Money::Currency::UnknownCurrency, CurrencyUnavailable
nil
end
Expand Down
2 changes: 1 addition & 1 deletion spec/models/country_band_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@

it "invalidates band with invalid band id" do
cb = CountryBand.new(number: 6, iso2: "HELLO")
expect(cb).to be_invalid_with_errors(number: ["is not included in the list"])
expect(cb).to be_invalid_with_errors(country: ["must exist"])
end
end
1 change: 1 addition & 0 deletions spec/support/test_db_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class TestDbManager
groups_metadata_councils
groups_metadata_teams_committees
groups_metadata_translators
country_band_details
).freeze

def self.fill_tables
Expand Down

0 comments on commit e28d6cc

Please sign in to comment.