2024-11-15 13:49:37 -05:00
|
|
|
class PlaidAccount < ApplicationRecord
|
|
|
|
belongs_to :plaid_item
|
|
|
|
|
|
|
|
has_one :account, dependent: :destroy
|
|
|
|
|
2025-05-23 18:58:22 -04:00
|
|
|
validates :name, :plaid_type, :currency, presence: true
|
|
|
|
validate :has_balance
|
|
|
|
|
|
|
|
def upsert_plaid_snapshot!(account_snapshot)
|
|
|
|
assign_attributes(
|
|
|
|
current_balance: account_snapshot.balances.current,
|
|
|
|
available_balance: account_snapshot.balances.available,
|
|
|
|
currency: account_snapshot.balances.iso_currency_code,
|
|
|
|
plaid_type: account_snapshot.type,
|
|
|
|
plaid_subtype: account_snapshot.subtype,
|
|
|
|
name: account_snapshot.name,
|
|
|
|
mask: account_snapshot.mask,
|
|
|
|
raw_payload: account_snapshot
|
|
|
|
)
|
2025-05-07 13:56:20 -04:00
|
|
|
|
2025-05-23 18:58:22 -04:00
|
|
|
save!
|
2024-11-15 13:49:37 -05:00
|
|
|
end
|
|
|
|
|
2025-05-23 18:58:22 -04:00
|
|
|
def upsert_plaid_transactions_snapshot!(transactions_snapshot)
|
|
|
|
assign_attributes(
|
|
|
|
raw_transactions_payload: transactions_snapshot
|
2024-11-15 13:49:37 -05:00
|
|
|
)
|
|
|
|
|
2025-05-23 18:58:22 -04:00
|
|
|
save!
|
2024-11-15 13:49:37 -05:00
|
|
|
end
|
|
|
|
|
2025-05-23 18:58:22 -04:00
|
|
|
def upsert_plaid_investments_snapshot!(investments_snapshot)
|
|
|
|
assign_attributes(
|
|
|
|
raw_investments_payload: investments_snapshot
|
2024-11-15 13:49:37 -05:00
|
|
|
)
|
|
|
|
|
2025-05-23 18:58:22 -04:00
|
|
|
save!
|
2024-11-15 13:49:37 -05:00
|
|
|
end
|
|
|
|
|
2025-05-23 18:58:22 -04:00
|
|
|
def upsert_plaid_liabilities_snapshot!(liabilities_snapshot)
|
|
|
|
assign_attributes(
|
|
|
|
raw_liabilities_payload: liabilities_snapshot
|
2024-11-15 13:49:37 -05:00
|
|
|
)
|
|
|
|
|
2025-05-23 18:58:22 -04:00
|
|
|
save!
|
2024-11-15 13:49:37 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2025-05-23 18:58:22 -04:00
|
|
|
# Plaid guarantees at least one of these. This validation is a sanity check for that guarantee.
|
|
|
|
def has_balance
|
|
|
|
return if current_balance.present? || available_balance.present?
|
|
|
|
errors.add(:base, "Plaid account must have either current or available balance")
|
2024-12-10 18:54:09 -05:00
|
|
|
end
|
2024-11-15 13:49:37 -05:00
|
|
|
end
|