mirror of
https://github.com/we-promise/sure.git
synced 2026-04-19 12:04:08 +00:00
Plaid sync domain improvements (#2267)
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
This commit is contained in:
60
app/models/plaid_account/transactions/processor.rb
Normal file
60
app/models/plaid_account/transactions/processor.rb
Normal file
@@ -0,0 +1,60 @@
|
||||
class PlaidAccount::Transactions::Processor
|
||||
def initialize(plaid_account)
|
||||
@plaid_account = plaid_account
|
||||
end
|
||||
|
||||
def process
|
||||
# Each entry is processed inside a transaction, but to avoid locking up the DB when
|
||||
# there are hundreds or thousands of transactions, we process them individually.
|
||||
modified_transactions.each do |transaction|
|
||||
PlaidEntry::Processor.new(
|
||||
transaction,
|
||||
plaid_account: plaid_account,
|
||||
category_matcher: category_matcher
|
||||
).process
|
||||
end
|
||||
|
||||
PlaidAccount.transaction do
|
||||
removed_transactions.each do |transaction|
|
||||
remove_plaid_transaction(transaction)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
attr_reader :plaid_account
|
||||
|
||||
def category_matcher
|
||||
@category_matcher ||= PlaidAccount::Transactions::CategoryMatcher.new(family_categories)
|
||||
end
|
||||
|
||||
def family_categories
|
||||
@family_categories ||= begin
|
||||
if account.family.categories.none?
|
||||
account.family.categories.bootstrap!
|
||||
end
|
||||
|
||||
account.family.categories
|
||||
end
|
||||
end
|
||||
|
||||
def account
|
||||
plaid_account.account
|
||||
end
|
||||
|
||||
def remove_plaid_transaction(raw_transaction)
|
||||
account.entries.find_by(plaid_id: raw_transaction["transaction_id"])&.destroy
|
||||
end
|
||||
|
||||
# Since we find_or_create_by transactions, we don't need a distinction between added/modified
|
||||
def modified_transactions
|
||||
modified = plaid_account.raw_transactions_payload["modified"] || []
|
||||
added = plaid_account.raw_transactions_payload["added"] || []
|
||||
|
||||
modified + added
|
||||
end
|
||||
|
||||
def removed_transactions
|
||||
plaid_account.raw_transactions_payload["removed"] || []
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user