mirror of
https://github.com/we-promise/sure.git
synced 2026-04-19 12:04:08 +00:00
Rename raw_investments_payload to raw_holdings_payload for Plaid accounts (#760)
* refactor: rename `raw_investments_payload` to `raw_holdings_payload` - Update references and models to use consistent naming. - Adjust migrations, tests, and encryption setup accordingly. * fix: improve safety when accessing raw_holdings_payload keys - Use `dig` with safe navigation to prevent potential nil errors. - Add support for decryption from the old column name `raw_investments_payload`. - Adjust related methods and calculations for consistency. --------- Co-authored-by: luckyPipewrench <luckypipewrench@proton.me>
This commit is contained in:
@@ -5,7 +5,8 @@ class PlaidAccount < ApplicationRecord
|
||||
if encryption_ready?
|
||||
encrypts :raw_payload
|
||||
encrypts :raw_transactions_payload
|
||||
encrypts :raw_investments_payload
|
||||
# Support reading data encrypted under the old column name after rename
|
||||
encrypts :raw_holdings_payload, previous: { attribute: :raw_investments_payload }
|
||||
encrypts :raw_liabilities_payload
|
||||
end
|
||||
|
||||
@@ -48,9 +49,9 @@ class PlaidAccount < ApplicationRecord
|
||||
save!
|
||||
end
|
||||
|
||||
def upsert_plaid_investments_snapshot!(investments_snapshot)
|
||||
def upsert_plaid_holdings_snapshot!(holdings_snapshot)
|
||||
assign_attributes(
|
||||
raw_investments_payload: investments_snapshot
|
||||
raw_holdings_payload: holdings_snapshot
|
||||
)
|
||||
|
||||
save!
|
||||
|
||||
@@ -23,7 +23,7 @@ class PlaidAccount::Importer
|
||||
end
|
||||
|
||||
def import_investments
|
||||
plaid_account.upsert_plaid_investments_snapshot!(account_snapshot.investments_data)
|
||||
plaid_account.upsert_plaid_holdings_snapshot!(account_snapshot.investments_data)
|
||||
end
|
||||
|
||||
def import_liabilities
|
||||
|
||||
@@ -44,7 +44,7 @@ class PlaidAccount::Investments::BalanceCalculator
|
||||
attr_reader :plaid_account, :security_resolver
|
||||
|
||||
def holdings
|
||||
plaid_account.raw_investments_payload["holdings"] || []
|
||||
plaid_account.raw_holdings_payload&.dig("holdings") || []
|
||||
end
|
||||
|
||||
def calculate_investment_brokerage_cash
|
||||
|
||||
@@ -51,7 +51,7 @@ class PlaidAccount::Investments::HoldingsProcessor
|
||||
end
|
||||
|
||||
def holdings
|
||||
plaid_account.raw_investments_payload&.[]("holdings") || []
|
||||
plaid_account.raw_holdings_payload&.[]("holdings") || []
|
||||
end
|
||||
|
||||
def parse_decimal(value)
|
||||
|
||||
@@ -43,7 +43,7 @@ class PlaidAccount::Investments::SecurityResolver
|
||||
Response = Struct.new(:security, :cash_equivalent?, :brokerage_cash?, keyword_init: true)
|
||||
|
||||
def securities
|
||||
plaid_account.raw_investments_payload["securities"] || []
|
||||
plaid_account.raw_holdings_payload&.dig("securities") || []
|
||||
end
|
||||
|
||||
# Tries to find security, or returns the "proxy security" (common with options contracts that have underlying securities)
|
||||
|
||||
@@ -98,7 +98,7 @@ class PlaidAccount::Investments::TransactionsProcessor
|
||||
end
|
||||
|
||||
def transactions
|
||||
plaid_account.raw_investments_payload["transactions"] || []
|
||||
plaid_account.raw_holdings_payload&.dig("transactions") || []
|
||||
end
|
||||
|
||||
# Plaid unfortunately returns incorrect signage on some `quantity` values. They claim all "sell" transactions
|
||||
|
||||
@@ -61,7 +61,7 @@ class PlaidItem::Syncer
|
||||
|
||||
def count_holdings(plaid_accounts)
|
||||
plaid_accounts.sum do |pa|
|
||||
Array(pa.raw_investments_payload).size
|
||||
pa.raw_holdings_payload&.dig("holdings")&.size || 0
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user