1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-04 21:15:19 +02:00

Implement auto family syncs on login (#1021)

This commit is contained in:
Zach Gollwitzer 2024-07-25 12:51:50 -04:00 committed by GitHub
parent c8590d53ba
commit ef4be7948a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 59 additions and 9 deletions

View file

@ -78,5 +78,6 @@ class Account::Sync < ApplicationRecord
partial: "shared/notification",
locals: { type: type, message: message }
)
broadcast_refresh_to account
end
end

View file

@ -11,6 +11,14 @@ module Account::Syncable
syncs.syncing.any?
end
def latest_sync_date
syncs.where.not(last_ran_at: nil).pluck(:last_ran_at).max&.to_date
end
def needs_sync?
latest_sync_date.nil? || latest_sync_date < Date.current
end
def sync_later(start_date: nil)
AccountSyncJob.perform_later(self, start_date: start_date)
end

View file

@ -34,10 +34,6 @@ class Demo::Generator
create_car_and_loan!
puts "accounts created"
family.sync
puts "balances synced"
puts "Demo data loaded successfully!"
end
end

View file

@ -105,6 +105,16 @@ class Family < ApplicationRecord
end
def sync(start_date: nil)
accounts.active.sync(start_date: start_date)
accounts.active.each do |account|
if account.needs_sync?
account.sync_later(start_date: start_date || account.last_sync_date)
end
end
update! last_synced_at: Time.now
end
def needs_sync?
last_synced_at.nil? || last_synced_at.to_date < Date.current
end
end