mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-22 06:39:39 +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,13 +5,11 @@ class PlaidAccount::Importer
|
||||||
end
|
end
|
||||||
|
|
||||||
def import
|
def import
|
||||||
PlaidAccount.transaction do
|
|
||||||
import_account_info
|
import_account_info
|
||||||
import_transactions if account_snapshot.transactions_data.present?
|
import_transactions if account_snapshot.transactions_data.present?
|
||||||
import_investments if account_snapshot.investments_data.present?
|
import_investments if account_snapshot.investments_data.present?
|
||||||
import_liabilities if account_snapshot.liabilities_data.present?
|
import_liabilities if account_snapshot.liabilities_data.present?
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
attr_reader :plaid_account, :account_snapshot
|
attr_reader :plaid_account, :account_snapshot
|
||||||
|
|
|
@ -20,6 +20,11 @@ class PlaidItem::AccountsSnapshot
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def transactions_cursor
|
||||||
|
return nil unless transactions_data
|
||||||
|
transactions_data.cursor
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
attr_reader :plaid_item, :plaid_provider
|
attr_reader :plaid_item, :plaid_provider
|
||||||
|
|
||||||
|
@ -68,7 +73,11 @@ class PlaidItem::AccountsSnapshot
|
||||||
|
|
||||||
def transactions_data
|
def transactions_data
|
||||||
return nil unless can_fetch_transactions?
|
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
|
end
|
||||||
|
|
||||||
def can_fetch_investments?
|
def can_fetch_investments?
|
||||||
|
|
|
@ -39,6 +39,7 @@ class PlaidItem::Importer
|
||||||
def fetch_and_import_accounts_data
|
def fetch_and_import_accounts_data
|
||||||
snapshot = PlaidItem::AccountsSnapshot.new(plaid_item, plaid_provider: plaid_provider)
|
snapshot = PlaidItem::AccountsSnapshot.new(plaid_item, plaid_provider: plaid_provider)
|
||||||
|
|
||||||
|
PlaidItem.transaction do
|
||||||
snapshot.accounts.each do |raw_account|
|
snapshot.accounts.each do |raw_account|
|
||||||
plaid_account = plaid_item.plaid_accounts.find_or_initialize_by(
|
plaid_account = plaid_item.plaid_accounts.find_or_initialize_by(
|
||||||
plaid_id: raw_account.account_id
|
plaid_id: raw_account.account_id
|
||||||
|
@ -49,5 +50,9 @@ class PlaidItem::Importer
|
||||||
account_snapshot: snapshot.get_account_data(raw_account.account_id)
|
account_snapshot: snapshot.get_account_data(raw_account.account_id)
|
||||||
).import
|
).import
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
<%# locals: (message:, description:) %>
|
<%# locals: (message:, description:) %>
|
||||||
|
|
||||||
<div id="cta">
|
<div id="cta">
|
||||||
<%= tag.div class: "relative flex gap-3 rounded-lg bg-container-inset p-4 group w-full md:max-w-80 shadow-border-xs", data: { controller: "element-removal" } do %>
|
<%= tag.div class: "relative flex gap-3 rounded-lg bg-container p-4 group w-full md:max-w-80 shadow-border-xs", data: { controller: "element-removal" } do %>
|
||||||
<div class="h-5 w-5 shrink-0 p-px text-primary">
|
<div class="h-5 w-5 shrink-0 p-px text-primary">
|
||||||
<div class="flex h-full items-center justify-center rounded-full bg-success">
|
<div class="flex h-full items-center justify-center rounded-full bg-success text-white">
|
||||||
<%= icon "check", size: "xs" %>
|
<%= icon "check", size: "xs", color: "current" %>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,14 @@ class PlaidItem::AccountsSnapshotTest < ActiveSupport::TestCase
|
||||||
)
|
)
|
||||||
]).at_least_once
|
]).at_least_once
|
||||||
|
|
||||||
@plaid_provider.expects(:get_transactions).with(@plaid_item.access_token).once
|
@plaid_provider.expects(:get_transactions).with(@plaid_item.access_token, next_cursor: nil).returns(
|
||||||
|
OpenStruct.new(
|
||||||
|
added: [],
|
||||||
|
modified: [],
|
||||||
|
removed: [],
|
||||||
|
cursor: "test_cursor_1"
|
||||||
|
)
|
||||||
|
).once
|
||||||
@plaid_provider.expects(:get_item_investments).never
|
@plaid_provider.expects(:get_item_investments).never
|
||||||
@plaid_provider.expects(:get_item_liabilities).never
|
@plaid_provider.expects(:get_item_liabilities).never
|
||||||
|
|
||||||
|
@ -45,6 +52,31 @@ class PlaidItem::AccountsSnapshotTest < ActiveSupport::TestCase
|
||||||
@snapshot.get_account_data("123")
|
@snapshot.get_account_data("123")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "updates next_cursor when fetching transactions" do
|
||||||
|
@plaid_item.update!(available_products: [ "transactions" ], billed_products: [], next_cursor: "test_cursor_1")
|
||||||
|
|
||||||
|
@snapshot.expects(:accounts).returns([
|
||||||
|
OpenStruct.new(
|
||||||
|
account_id: "123",
|
||||||
|
type: "depository"
|
||||||
|
)
|
||||||
|
]).at_least_once
|
||||||
|
|
||||||
|
@plaid_provider.expects(:get_transactions).with(@plaid_item.access_token, next_cursor: "test_cursor_1").returns(
|
||||||
|
OpenStruct.new(
|
||||||
|
added: [],
|
||||||
|
modified: [],
|
||||||
|
removed: [],
|
||||||
|
cursor: "test_cursor_2"
|
||||||
|
)
|
||||||
|
).once
|
||||||
|
|
||||||
|
@plaid_provider.expects(:get_item_investments).never
|
||||||
|
@plaid_provider.expects(:get_item_liabilities).never
|
||||||
|
|
||||||
|
@snapshot.get_account_data("123")
|
||||||
|
end
|
||||||
|
|
||||||
test "fetches investments data if item supports investments and investment accounts present" do
|
test "fetches investments data if item supports investments and investment accounts present" do
|
||||||
@plaid_item.update!(available_products: [ "investments" ], billed_products: [])
|
@plaid_item.update!(available_products: [ "investments" ], billed_products: [])
|
||||||
|
|
||||||
|
|
|
@ -33,14 +33,17 @@ class PlaidItem::ImporterTest < ActiveSupport::TestCase
|
||||||
PlaidItem::AccountsSnapshot.any_instance.expects(:accounts).returns([
|
PlaidItem::AccountsSnapshot.any_instance.expects(:accounts).returns([
|
||||||
OpenStruct.new(
|
OpenStruct.new(
|
||||||
account_id: "acc_1",
|
account_id: "acc_1",
|
||||||
type: "depository"
|
type: "depository",
|
||||||
)
|
)
|
||||||
]).at_least_once
|
]).at_least_once
|
||||||
|
|
||||||
|
PlaidItem::AccountsSnapshot.any_instance.expects(:transactions_cursor).returns("test_cursor_1")
|
||||||
|
|
||||||
PlaidItem::AccountsSnapshot.any_instance.expects(:get_account_data).with("acc_1").once
|
PlaidItem::AccountsSnapshot.any_instance.expects(:get_account_data).with("acc_1").once
|
||||||
|
|
||||||
PlaidAccount::Importer.any_instance.expects(:import).once
|
PlaidAccount::Importer.any_instance.expects(:import).once
|
||||||
|
|
||||||
|
@plaid_item.expects(:update!).with(next_cursor: "test_cursor_1")
|
||||||
@plaid_item.expects(:upsert_plaid_snapshot!).with(item_data)
|
@plaid_item.expects(:upsert_plaid_snapshot!).with(item_data)
|
||||||
@plaid_item.expects(:upsert_plaid_institution_snapshot!).with(institution_data)
|
@plaid_item.expects(:upsert_plaid_institution_snapshot!).with(institution_data)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue