mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-09 07:25:19 +02:00
Put sync implementations in own concerns
This commit is contained in:
parent
64147758a3
commit
3207077f9e
5 changed files with 55 additions and 46 deletions
|
@ -81,21 +81,6 @@ class Account < ApplicationRecord
|
|||
DestroyJob.perform_later(self)
|
||||
end
|
||||
|
||||
def sync_data(sync, start_date: nil)
|
||||
Rails.logger.info("Processing balances (#{linked? ? 'reverse' : 'forward'})")
|
||||
sync_balances
|
||||
end
|
||||
|
||||
def post_sync(sync)
|
||||
family.remove_syncing_notice!
|
||||
|
||||
accountable.post_sync(sync)
|
||||
|
||||
unless sync.child?
|
||||
family.auto_match_transfers!
|
||||
end
|
||||
end
|
||||
|
||||
def current_holdings
|
||||
holdings.where(currency: currency, date: holdings.maximum(:date)).order(amount: :desc)
|
||||
end
|
||||
|
@ -172,10 +157,4 @@ class Account < ApplicationRecord
|
|||
def long_subtype_label
|
||||
accountable_class.long_subtype_label_for(subtype) || accountable_class.display_name
|
||||
end
|
||||
|
||||
private
|
||||
def sync_balances
|
||||
strategy = linked? ? :reverse : :forward
|
||||
Balance::Syncer.new(self, strategy: strategy).sync_balances
|
||||
end
|
||||
end
|
||||
|
|
26
app/models/account/syncable.rb
Normal file
26
app/models/account/syncable.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
module Account::Syncable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
include Syncable
|
||||
|
||||
def sync_data(sync, start_date: nil)
|
||||
Rails.logger.info("Processing balances (#{linked? ? 'reverse' : 'forward'})")
|
||||
sync_balances
|
||||
end
|
||||
|
||||
def post_sync(sync)
|
||||
family.remove_syncing_notice!
|
||||
|
||||
accountable.post_sync(sync)
|
||||
|
||||
unless sync.child?
|
||||
family.auto_match_transfers!
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def sync_balances
|
||||
strategy = linked? ? :reverse : :forward
|
||||
Balance::Syncer.new(self, strategy: strategy).sync_balances
|
||||
end
|
||||
end
|
|
@ -1,8 +1,7 @@
|
|||
module Family::Syncable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
# Top-level, generic Syncable concern
|
||||
include ::Syncable
|
||||
include Syncable
|
||||
|
||||
def sync_data(sync, start_date: nil)
|
||||
# We don't rely on this value to guard the app, but keep it eventually consistent
|
||||
|
|
|
@ -22,24 +22,6 @@ class PlaidItem < ApplicationRecord
|
|||
scope :ordered, -> { order(created_at: :desc) }
|
||||
scope :needs_update, -> { where(status: :requires_update) }
|
||||
|
||||
def sync_data(sync, start_date: nil)
|
||||
begin
|
||||
Rails.logger.info("Fetching and loading Plaid data")
|
||||
fetch_and_load_plaid_data(sync)
|
||||
update!(status: :good) if requires_update?
|
||||
|
||||
# Schedule account syncs
|
||||
accounts.each do |account|
|
||||
account.sync_later(start_date: start_date, parent_sync: sync)
|
||||
end
|
||||
|
||||
Rails.logger.info("Plaid data fetched and loaded")
|
||||
rescue Plaid::ApiError => e
|
||||
handle_plaid_error(e)
|
||||
raise e
|
||||
end
|
||||
end
|
||||
|
||||
def get_update_link_token(webhooks_url:, redirect_url:)
|
||||
begin
|
||||
family.get_link_token(
|
||||
|
@ -61,11 +43,6 @@ class PlaidItem < ApplicationRecord
|
|||
end
|
||||
end
|
||||
|
||||
def post_sync(sync)
|
||||
auto_match_categories!
|
||||
family.broadcast_refresh
|
||||
end
|
||||
|
||||
def destroy_later
|
||||
update!(scheduled_for_deletion: true)
|
||||
DestroyJob.perform_later(self)
|
||||
|
|
28
app/models/plaid_item/syncable.rb
Normal file
28
app/models/plaid_item/syncable.rb
Normal file
|
@ -0,0 +1,28 @@
|
|||
module PlaidItem::Syncable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
include Syncable
|
||||
|
||||
def sync_data(sync, start_date: nil)
|
||||
begin
|
||||
Rails.logger.info("Fetching and loading Plaid data")
|
||||
fetch_and_load_plaid_data(sync)
|
||||
update!(status: :good) if requires_update?
|
||||
|
||||
# Schedule account syncs
|
||||
accounts.each do |account|
|
||||
account.sync_later(start_date: start_date, parent_sync: sync)
|
||||
end
|
||||
|
||||
Rails.logger.info("Plaid data fetched and loaded")
|
||||
rescue Plaid::ApiError => e
|
||||
handle_plaid_error(e)
|
||||
raise e
|
||||
end
|
||||
end
|
||||
|
||||
def post_sync(sync)
|
||||
auto_match_categories!
|
||||
family.broadcast_refresh
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue