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

Improve sync data management

This commit is contained in:
Zach Gollwitzer 2025-05-08 12:52:40 -04:00
parent 1e5edd9f2f
commit 867318cbc1
2 changed files with 7 additions and 7 deletions

View file

@ -42,7 +42,7 @@ class PlaidItem < ApplicationRecord
begin begin
Rails.logger.info("Fetching and loading Plaid data") Rails.logger.info("Fetching and loading Plaid data")
plaid_data = fetch_and_load_plaid_data fetch_and_load_plaid_data(sync)
update!(status: :good) if requires_update? update!(status: :good) if requires_update?
# Schedule account syncs # Schedule account syncs
@ -51,7 +51,6 @@ class PlaidItem < ApplicationRecord
end end
Rails.logger.info("Plaid data fetched and loaded") Rails.logger.info("Plaid data fetched and loaded")
plaid_data
rescue Plaid::ApiError => e rescue Plaid::ApiError => e
handle_plaid_error(e) handle_plaid_error(e)
raise e raise e
@ -120,7 +119,7 @@ class PlaidItem < ApplicationRecord
end end
private private
def fetch_and_load_plaid_data def fetch_and_load_plaid_data(sync)
data = {} data = {}
# Log what we're about to fetch # Log what we're about to fetch
@ -147,6 +146,7 @@ class PlaidItem < ApplicationRecord
# Accounts # Accounts
fetched_accounts = plaid_provider.get_item_accounts(self).accounts fetched_accounts = plaid_provider.get_item_accounts(self).accounts
data[:accounts] = fetched_accounts || [] data[:accounts] = fetched_accounts || []
sync.update!(data: data)
Rails.logger.info "Processing Plaid accounts (count: #{fetched_accounts.size})" Rails.logger.info "Processing Plaid accounts (count: #{fetched_accounts.size})"
internal_plaid_accounts = fetched_accounts.map do |account| internal_plaid_accounts = fetched_accounts.map do |account|
@ -158,6 +158,7 @@ class PlaidItem < ApplicationRecord
# Transactions # Transactions
fetched_transactions = safe_fetch_plaid_data(:get_item_transactions) fetched_transactions = safe_fetch_plaid_data(:get_item_transactions)
data[:transactions] = fetched_transactions || [] data[:transactions] = fetched_transactions || []
sync.update!(data: data)
if fetched_transactions if fetched_transactions
Rails.logger.info "Processing Plaid transactions (added: #{fetched_transactions.added.size}, modified: #{fetched_transactions.modified.size}, removed: #{fetched_transactions.removed.size})" Rails.logger.info "Processing Plaid transactions (added: #{fetched_transactions.added.size}, modified: #{fetched_transactions.modified.size}, removed: #{fetched_transactions.removed.size})"
@ -177,6 +178,7 @@ class PlaidItem < ApplicationRecord
# Investments # Investments
fetched_investments = safe_fetch_plaid_data(:get_item_investments) fetched_investments = safe_fetch_plaid_data(:get_item_investments)
data[:investments] = fetched_investments || [] data[:investments] = fetched_investments || []
sync.update!(data: data)
if fetched_investments if fetched_investments
Rails.logger.info "Processing Plaid investments (transactions: #{fetched_investments.transactions.size}, holdings: #{fetched_investments.holdings.size}, securities: #{fetched_investments.securities.size})" Rails.logger.info "Processing Plaid investments (transactions: #{fetched_investments.transactions.size}, holdings: #{fetched_investments.holdings.size}, securities: #{fetched_investments.securities.size})"
@ -194,6 +196,7 @@ class PlaidItem < ApplicationRecord
# Liabilities # Liabilities
fetched_liabilities = safe_fetch_plaid_data(:get_item_liabilities) fetched_liabilities = safe_fetch_plaid_data(:get_item_liabilities)
data[:liabilities] = fetched_liabilities || [] data[:liabilities] = fetched_liabilities || []
sync.update!(data: data)
if fetched_liabilities if fetched_liabilities
Rails.logger.info "Processing Plaid liabilities (credit: #{fetched_liabilities.credit&.size || 0}, mortgage: #{fetched_liabilities.mortgage&.size || 0}, student: #{fetched_liabilities.student&.size || 0})" Rails.logger.info "Processing Plaid liabilities (credit: #{fetched_liabilities.credit&.size || 0}, mortgage: #{fetched_liabilities.mortgage&.size || 0}, student: #{fetched_liabilities.student&.size || 0})"
@ -209,8 +212,6 @@ class PlaidItem < ApplicationRecord
end end
end end
end end
data
end end
def safe_fetch_plaid_data(method) def safe_fetch_plaid_data(method)

View file

@ -19,8 +19,7 @@ class Sync < ApplicationRecord
start! start!
begin begin
data = syncable.sync_data(self, start_date: start_date) syncable.sync_data(self, start_date: start_date)
update!(data: data) if data
unless has_pending_child_syncs? unless has_pending_child_syncs?
complete! complete!