mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-02 20:15:22 +02:00
Pass transactions cursor when fetching plaid transactions
This commit is contained in:
parent
6935ffa3d1
commit
aecb5aafd8
6 changed files with 67 additions and 20 deletions
|
@ -5,12 +5,10 @@ class PlaidAccount::Importer
|
|||
end
|
||||
|
||||
def import
|
||||
PlaidAccount.transaction do
|
||||
import_account_info
|
||||
import_transactions if account_snapshot.transactions_data.present?
|
||||
import_investments if account_snapshot.investments_data.present?
|
||||
import_liabilities if account_snapshot.liabilities_data.present?
|
||||
end
|
||||
import_account_info
|
||||
import_transactions if account_snapshot.transactions_data.present?
|
||||
import_investments if account_snapshot.investments_data.present?
|
||||
import_liabilities if account_snapshot.liabilities_data.present?
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -20,6 +20,11 @@ class PlaidItem::AccountsSnapshot
|
|||
)
|
||||
end
|
||||
|
||||
def transactions_cursor
|
||||
return nil unless transactions_data
|
||||
transactions_data.cursor
|
||||
end
|
||||
|
||||
private
|
||||
attr_reader :plaid_item, :plaid_provider
|
||||
|
||||
|
@ -68,7 +73,11 @@ class PlaidItem::AccountsSnapshot
|
|||
|
||||
def transactions_data
|
||||
return nil unless can_fetch_transactions?
|
||||
@transactions_data ||= plaid_provider.get_transactions(plaid_item.access_token)
|
||||
|
||||
@transactions_data ||= plaid_provider.get_transactions(
|
||||
plaid_item.access_token,
|
||||
next_cursor: plaid_item.next_cursor
|
||||
)
|
||||
end
|
||||
|
||||
def can_fetch_investments?
|
||||
|
|
|
@ -39,15 +39,20 @@ class PlaidItem::Importer
|
|||
def fetch_and_import_accounts_data
|
||||
snapshot = PlaidItem::AccountsSnapshot.new(plaid_item, plaid_provider: plaid_provider)
|
||||
|
||||
snapshot.accounts.each do |raw_account|
|
||||
plaid_account = plaid_item.plaid_accounts.find_or_initialize_by(
|
||||
plaid_id: raw_account.account_id
|
||||
)
|
||||
PlaidItem.transaction do
|
||||
snapshot.accounts.each do |raw_account|
|
||||
plaid_account = plaid_item.plaid_accounts.find_or_initialize_by(
|
||||
plaid_id: raw_account.account_id
|
||||
)
|
||||
|
||||
PlaidAccount::Importer.new(
|
||||
plaid_account,
|
||||
account_snapshot: snapshot.get_account_data(raw_account.account_id)
|
||||
).import
|
||||
PlaidAccount::Importer.new(
|
||||
plaid_account,
|
||||
account_snapshot: snapshot.get_account_data(raw_account.account_id)
|
||||
).import
|
||||
end
|
||||
|
||||
# Once we know all data has been imported, save the cursor to avoid re-fetching the same data next time
|
||||
plaid_item.update!(next_cursor: snapshot.transactions_cursor)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue