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)
* Fix Plaid cash balance double counting * Fix today's cash balance * Simplify balance trends in activity view
This commit is contained in:
parent
42207e487e
commit
1e5edd9f2f
8 changed files with 72 additions and 97 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue