mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-02 20:15:22 +02:00
Account Sync should happen at login, 1x per day, OR on-demand (#594)
* Add last_sync_date to accounts table * Always sync Account after Valuation or Transaction creation, update, or deletion. Skip sync if user clicks "sync" button without changing anything * Sync user accounts daily based on last_login_at
This commit is contained in:
parent
4f0b2de4ef
commit
315c4bf1ec
8 changed files with 48 additions and 8 deletions
|
@ -14,12 +14,23 @@ module Account::Syncable
|
|||
calculator.calculate
|
||||
self.balances.upsert_all(calculator.daily_balances, unique_by: :index_account_balances_on_account_id_date_currency_unique)
|
||||
self.balances.where("date < ?", effective_start_date).delete_all
|
||||
update!(status: "ok")
|
||||
update!(status: "ok", last_sync_date: Date.today)
|
||||
rescue => e
|
||||
update!(status: "error")
|
||||
Rails.logger.error("Failed to sync account #{id}: #{e.message}")
|
||||
end
|
||||
|
||||
def can_sync?
|
||||
# Skip account sync if account is not active or the sync process is already running
|
||||
return false unless is_active
|
||||
return false if syncing?
|
||||
# If last_sync_date is blank (i.e. the account has never been synced before) allow syncing
|
||||
return true if last_sync_date.blank?
|
||||
|
||||
# If last_sync_date is not today, allow syncing
|
||||
last_sync_date != Date.today
|
||||
end
|
||||
|
||||
# The earliest date we can calculate a balance for
|
||||
def effective_start_date
|
||||
first_valuation_date = self.valuations.order(:date).pluck(:date).first
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue