From b2a56aefc19646cc3bc483fc7d4709fd8764557e Mon Sep 17 00:00:00 2001 From: Zach Gollwitzer Date: Tue, 10 Dec 2024 18:54:09 -0500 Subject: [PATCH] Update Plaid cash balance on each sync --- app/models/plaid_account.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/app/models/plaid_account.rb b/app/models/plaid_account.rb index 5a6d03e7..8fe342a7 100644 --- a/app/models/plaid_account.rb +++ b/app/models/plaid_account.rb @@ -37,7 +37,9 @@ class PlaidAccount < ApplicationRecord plaid_subtype: plaid_account_data.subtype, account_attributes: { id: account.id, - balance: plaid_account_data.balances.current + # Plaid guarantees at least 1 of these + balance: plaid_account_data.balances.current || plaid_account_data.balances.available, + cash_balance: derive_plaid_cash_balance(plaid_account_data.balances) } ) end @@ -208,4 +210,13 @@ class PlaidAccount < ApplicationRecord family.merchants.find_or_create_by!(name: plaid_merchant_name) end + + def derive_plaid_cash_balance(plaid_balances) + if account.investment? + plaid_balances.available || 0 + else + # For now, we will not distinguish between "cash" and "overall" balance for non-investment accounts + plaid_balances.current || plaid_balances.available + end + end end