2024-02-29 08:32:52 -05:00
|
|
|
module Account::Syncable
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
def sync_later
|
|
|
|
AccountSyncJob.perform_later self
|
|
|
|
end
|
|
|
|
|
|
|
|
def sync
|
2024-03-11 16:32:13 -04:00
|
|
|
update!(status: "syncing")
|
2024-02-29 08:32:52 -05:00
|
|
|
synced_daily_balances = Account::BalanceCalculator.new(self).daily_balances
|
|
|
|
self.balances.upsert_all(synced_daily_balances, unique_by: :index_account_balances_on_account_id_and_date)
|
|
|
|
self.balances.where("date < ?", effective_start_date).delete_all
|
2024-03-11 16:32:13 -04:00
|
|
|
update!(status: "ok")
|
2024-02-29 08:32:52 -05:00
|
|
|
rescue => e
|
2024-03-11 16:32:13 -04:00
|
|
|
update!(status: "error")
|
2024-02-29 08:32:52 -05:00
|
|
|
Rails.logger.error("Failed to sync account #{id}: #{e.message}")
|
|
|
|
end
|
|
|
|
|
|
|
|
# The earliest date we can calculate a balance for
|
|
|
|
def effective_start_date
|
|
|
|
first_valuation_date = self.valuations.order(:date).pluck(:date).first
|
|
|
|
first_transaction_date = self.transactions.order(:date).pluck(:date).first
|
|
|
|
|
|
|
|
[ first_valuation_date, first_transaction_date&.prev_day ].compact.min || Date.current
|
|
|
|
end
|
|
|
|
end
|