2024-02-10 16:18:56 -06:00
|
|
|
class ExchangeRate < ApplicationRecord
|
2024-03-15 12:21:59 -07:00
|
|
|
validates :base_currency, :converted_currency, presence: true
|
|
|
|
|
2024-02-10 16:18:56 -06:00
|
|
|
def self.convert(from, to, amount)
|
2024-02-26 20:20:38 +05:30
|
|
|
return amount unless EXCHANGE_RATE_ENABLED
|
|
|
|
|
2024-02-10 16:18:56 -06:00
|
|
|
rate = ExchangeRate.find_by(base_currency: from, converted_currency: to)
|
2024-03-07 17:46:36 -05:00
|
|
|
|
|
|
|
# TODO: Handle the case where the rate is not found
|
|
|
|
if rate.nil?
|
|
|
|
amount # Silently handle the error by returning the original amount
|
|
|
|
else
|
|
|
|
amount * rate.rate
|
|
|
|
end
|
2024-02-10 16:18:56 -06:00
|
|
|
end
|
|
|
|
end
|