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

Fix Plaid cash balance double counting (#2222)
Some checks are pending
Publish Docker image / ci (push) Waiting to run
Publish Docker image / Build docker image (push) Blocked by required conditions

* Fix Plaid cash balance double counting

* Fix today's cash balance

* Simplify balance trends in activity view
This commit is contained in:
Zach Gollwitzer 2025-05-08 12:25:53 -04:00 committed by GitHub
parent 42207e487e
commit 1e5edd9f2f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 72 additions and 97 deletions

View file

@ -11,6 +11,7 @@ class PlaidInvestmentSync
@securities = securities
PlaidAccount.transaction do
normalize_cash_balance!
sync_transactions!
sync_holdings!
end
@ -19,6 +20,23 @@ class PlaidInvestmentSync
private
attr_reader :transactions, :holdings, :securities
# Plaid considers "brokerage cash" and "cash equivalent holdings" to all be part of "cash balance"
# Internally, we DO NOT.
# Maybe clearly distinguishes between "brokerage cash" vs. "holdings (i.e. invested cash)"
# For this reason, we must back out cash + cash equivalent holdings from the reported cash balance to avoid double counting
def normalize_cash_balance!
excludable_cash_holdings = holdings.select do |h|
internal_security, plaid_security = get_security(h.security_id, securities)
internal_security.present? && (plaid_security&.is_cash_equivalent || plaid_security&.type == "cash")
end
excludable_cash_holdings_value = excludable_cash_holdings.sum { |h| h.quantity * h.institution_price }
plaid_account.account.update!(
cash_balance: plaid_account.account.cash_balance - excludable_cash_holdings_value
)
end
def sync_transactions!
transactions.each do |transaction|
security, plaid_security = get_security(transaction.security_id, securities)