mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-22 22:59:39 +02:00
Breaks our Plaid sync process out into more manageable classes. Notably, this moves the sync process to a distinct, 2-step flow: 1. Import stage - we first make API calls and import Plaid data to "mirror" tables 2. Processing stage - read the raw data, apply business rules, build internal domain models and sync balances This provides several benefits: - Plaid syncs can now be "replayed" without fetching API data again - Mirror tables provide better audit and debugging capabilities - Eliminates the "all or nothing" sync behavior that is currently in place, which is brittle
54 lines
1.4 KiB
Ruby
54 lines
1.4 KiB
Ruby
class PlaidAccount < ApplicationRecord
|
|
belongs_to :plaid_item
|
|
|
|
has_one :account, dependent: :destroy
|
|
|
|
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
|
|
)
|
|
|
|
save!
|
|
end
|
|
|
|
def upsert_plaid_transactions_snapshot!(transactions_snapshot)
|
|
assign_attributes(
|
|
raw_transactions_payload: transactions_snapshot
|
|
)
|
|
|
|
save!
|
|
end
|
|
|
|
def upsert_plaid_investments_snapshot!(investments_snapshot)
|
|
assign_attributes(
|
|
raw_investments_payload: investments_snapshot
|
|
)
|
|
|
|
save!
|
|
end
|
|
|
|
def upsert_plaid_liabilities_snapshot!(liabilities_snapshot)
|
|
assign_attributes(
|
|
raw_liabilities_payload: liabilities_snapshot
|
|
)
|
|
|
|
save!
|
|
end
|
|
|
|
private
|
|
# 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")
|
|
end
|
|
end
|