Skip to content

Commit

Permalink
Merge pull request #1889 from trade-tariff/ott-309
Browse files Browse the repository at this point in the history
Monkey patch for allowing ZWL rate only be saved as ZIG
  • Loading branch information
shjohnson authored Jun 17, 2024
2 parents ae12aef + d69526a commit 90a5c98
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ DEPENDENCIES
webmock

RUBY VERSION
ruby 3.2.2p53
ruby 3.2.3p157

BUNDLED WITH
2.3.8
31 changes: 24 additions & 7 deletions app/services/exchange_rates/update_currency_rates_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,26 @@ def call
response = @xe_api.get_all_historic_rates

ExchangeRateCurrencyRate.db.transaction do
rates = build_rates(response)
# This will only get rates for the current live rates so you cant pull historic data
included_rates = @type == ExchangeRateCurrencyRate::MONTHLY_RATE_TYPE ? ExchangeRateCountryCurrency.live_currency_codes : ExchangeRateCountryCurrency::SPOT_RATE_CURRENCY_CODES
rates = rates.select { |rate| included_rates.include?(rate.currency_code) }

rates = build_rates(response).select { |rate| included_rates.include?(rate.currency_code) }
upsert_rates(rates)
end
end

private

def monthly_rate_type?
@type == ExchangeRateCurrencyRate::MONTHLY_RATE_TYPE
end

def included_rates
# This will only get rates for the current live rates so you cant pull historic data
if monthly_rate_type?
ExchangeRateCountryCurrency.live_currency_codes
else
ExchangeRateCountryCurrency::SPOT_RATE_CURRENCY_CODES
end
end

def build_rates(response)
response['to'].map do |currency_data|
build_rate(currency_data)
Expand All @@ -41,16 +50,24 @@ def build_rate(currency_data)
currency_code = currency_data['quotecurrency']
rate = currency_data['mid']

validity_start_date = @type == ExchangeRateCurrencyRate::MONTHLY_RATE_TYPE ? @date.beginning_of_month : @date.end_of_month
validity_start_date = monthly_rate_type? ? @date.beginning_of_month : @date.end_of_month
validity_end_date = @date.end_of_month

ExchangeRateCurrencyRate.new(
currency_code:,
currency_code: currency_overide(currency_code),
validity_start_date:,
validity_end_date:,
rate:,
rate_type: @type,
)
end

def currency_overide(currency_code)
currency_overides = { 'ZWL' => 'ZIG' }

return currency_code unless currency_overides.keys.include?(currency_code)

currency_overides[currency_code]
end
end
end
17 changes: 17 additions & 0 deletions db/data_migrations/20240606125159_add_new_zimbabwe_gold.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

Sequel.migration do
up do
if TradeTariffBackend.uk?
new_zimbabwe_country_currency = ExchangeRateCountryCurrency.where(country_description: 'Zimbabwe',
currency_code: 'ZIG',
country_code:'ZW').first

zimbabwe_country_currency.update(country_description: 'Zimbabwe Gold')
end
end

down do
# We don't want to rollback to incorrect Zimbabwe rates.
end
end
82 changes: 80 additions & 2 deletions spec/services/exchange_rates/update_currency_rates_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,23 @@
let(:sample_date) { Time.zone.today }

before do
create(:exchange_rate_country_currency, currency_code: 'AED')
create(:exchange_rate_country_currency, currency_code: 'EUR')
create(:exchange_rate_country_currency,
country_code: 'AE',
country_description: 'UAE',
currency_description: 'Dirham',
currency_code: 'AED')
create(:exchange_rate_country_currency,
country_code: 'EU',
country_description: 'Eurozone',
currency_description: 'Euro',
currency_code: 'EUR')

# Only added for the Zimbabwe monkey patch situation
create(:exchange_rate_country_currency,
country_code: 'ZW',
country_description: 'Zimbabwe',
currency_description: 'Zimbabwe Gold',
currency_code: 'ZIG')
end

it 'only inserts rates that exist as currencies' do
Expand All @@ -60,6 +75,69 @@

expect(new_rates).to include_json(expected_rates)
end

# Below test is in place to protect us against XE
# not porviding ZIG currnecy even though its accepted across over 90% banks in Zimbabwe
context 'when XE provides ZWL and ZIG' do
let(:response) do
{
'to' => [
{ 'quotecurrency' => 'ZWL', 'mid' => 16.8567 },
{ 'quotecurrency' => 'ZIG', 'mid' => 16.8567 },
],
}
end

let(:expected_rates) do
[
{
currency_code: 'ZIG',
validity_start_date: date.beginning_of_month,
validity_end_date: date.end_of_month,
rate: 16.8567,
rate_type: ExchangeRateCurrencyRate::MONTHLY_RATE_TYPE,
},
].as_json
end

it 'will create a ZIG rate' do
service.call

new_rates = ExchangeRateCurrencyRate.all.map(&:values).as_json

expect(new_rates).to include_json(expected_rates)
end
end

context 'when XE provides ZWL' do
let(:response) do
{
'to' => [
{ 'quotecurrency' => 'ZWL', 'mid' => 16.8567 },
],
}
end

let(:expected_rates) do
[
{
currency_code: 'ZIG',
validity_start_date: date.beginning_of_month,
validity_end_date: date.end_of_month,
rate: 16.8567,
rate_type: ExchangeRateCurrencyRate::MONTHLY_RATE_TYPE,
},
].as_json
end

it 'will create a ZIG rate' do
service.call

new_rates = ExchangeRateCurrencyRate.all.map(&:values).as_json

expect(new_rates).to include_json(expected_rates)
end
end
end

context 'when type is SPOT_RATE_TYPE' do
Expand Down

0 comments on commit 90a5c98

Please sign in to comment.