The personal finance app for everyone
Improve sync data management
Zach Gollwitzer867318cbc18a
diff --git a/app/models/plaid_item.rb b/app/models/plaid_item.rbindex 0d0a6b1b..a527dc3e 100644--- a/app/models/plaid_item.rb+++ b/app/models/plaid_item.rb@@ -42,7 +42,7 @@ class PlaidItem < ApplicationRecordbeginRails.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?# Schedule account syncs@@ -51,7 +51,6 @@ class PlaidItem < ApplicationRecordendRails.logger.info("Plaid data fetched and loaded")- plaid_datarescue Plaid::ApiError => ehandle_plaid_error(e)raise e@@ -120,7 +119,7 @@ class PlaidItem < ApplicationRecordendprivate- def fetch_and_load_plaid_data+ def fetch_and_load_plaid_data(sync)data = {}# Log what we're about to fetch@@ -147,6 +146,7 @@ class PlaidItem < ApplicationRecord# Accountsfetched_accounts = plaid_provider.get_item_accounts(self).accountsdata[:accounts] = fetched_accounts || []+ sync.update!(data: data)Rails.logger.info "Processing Plaid accounts (count: #{fetched_accounts.size})"internal_plaid_accounts = fetched_accounts.map do |account|@@ -158,6 +158,7 @@ class PlaidItem < ApplicationRecord# Transactionsfetched_transactions = safe_fetch_plaid_data(:get_item_transactions)data[:transactions] = fetched_transactions || []+ sync.update!(data: data)if fetched_transactionsRails.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# Investmentsfetched_investments = safe_fetch_plaid_data(:get_item_investments)data[:investments] = fetched_investments || []+ sync.update!(data: data)if fetched_investmentsRails.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# Liabilitiesfetched_liabilities = safe_fetch_plaid_data(:get_item_liabilities)data[:liabilities] = fetched_liabilities || []+ sync.update!(data: data)if fetched_liabilitiesRails.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 < ApplicationRecordendendend-- dataenddef safe_fetch_plaid_data(method)diff --git a/app/models/sync.rb b/app/models/sync.rbindex 0be26e5a..22b61829 100644--- a/app/models/sync.rb+++ b/app/models/sync.rb@@ -19,8 +19,7 @@ class Sync < ApplicationRecordstart!begin- data = syncable.sync_data(self, start_date: start_date)- update!(data: data) if data+ syncable.sync_data(self, start_date: start_date)unless has_pending_child_syncs?complete!