1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-06 05:55:21 +02:00

Multi-Currency Part 2 (#543)

* Support all currencies, handle outside DB

* Remove currencies from seed

* Fix account balance namespace

* Set default currency on authentication

* Cache currency instances

* Implement multi-currency syncs with tests

* Series fallback, passing tests

* Fix conflicts

* Make value group concrete class that works with currency values

* Fix migration conflict

* Update tests to expect multi-currency results

* Update account list to use group method

* Namespace updates

* Fetch unknown exchange rates from API

* Fix date range bug

* Ensure demo data works without external API

* Enforce cascades only at DB level
This commit is contained in:
Zach Gollwitzer 2024-03-21 13:39:10 -04:00 committed by GitHub
parent de0cba9fed
commit 110855d077
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
55 changed files with 1226 additions and 714 deletions

View file

@ -1,19 +0,0 @@
class ConvertCurrencyJob < ApplicationJob
queue_as :default
def perform(family)
family = Family.find(family.id)
# Convert all account balances to new currency
family.accounts.each do |account|
if account.currency == family.currency
account.converted_balance = account.balance
account.converted_currency = account.currency
else
account.converted_balance = ExchangeRate.convert(account.currency, family.currency, account.balance)
account.converted_currency = family.currency
end
account.save!
end
end
end

View file

@ -1,34 +0,0 @@
class DailyExchangeRateJob < ApplicationJob
queue_as :default
def perform
# Get the last date for which exchange rates were fetched for each currency
last_fetched_dates = ExchangeRate.group(:base_currency).maximum(:date)
# Loop through each currency and fetch exchange rates for each
Currency.all.each do |currency|
last_fetched_date = last_fetched_dates[currency.iso_code] || Date.yesterday
next_day = last_fetched_date + 1.day
response = Faraday.get("https://api.synthfinance.com/rates/historical") do |req|
req.headers["Authorization"] = "Bearer #{ENV["SYNTH_API_KEY"]}"
req.params["date"] = next_day.to_s
req.params["from"] = currency.iso_code
req.params["to"] = Currency.where.not(iso_code: currency.iso_code).pluck(:iso_code).join(",")
end
if response.success?
rates = JSON.parse(response.body)["rates"]
rates.each do |currency_iso_code, value|
ExchangeRate.find_or_create_by(date: Date.today, base_currency: currency.iso_code, converted_currency: currency_iso_code) do |exchange_rate|
exchange_rate.rate = value
end
puts "#{currency.iso_code} to #{currency_iso_code} on #{Date.today}: #{value}"
end
else
puts "Failed to fetch exchange rates for #{currency.iso_code} on #{Date.today}: #{response.status}"
end
end
end
end