Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

feat: Import pending transactions from Enable Banking only if option is enabled (#1476)

Alessio Cappa156694494d9d
diff --git a/app/models/enable_banking_item/importer.rb b/app/models/enable_banking_item/importer.rb
index 24fbefc271..504b22c9aa 100644
--- a/app/models/enable_banking_item/importer.rb
+++ b/app/models/enable_banking_item/importer.rb
@@ -225,8 +225,13 @@ class EnableBankingItem::Importer
existing
end
+ def include_pending?
+ Setting.syncs_include_pending
+ end
+
def fetch_and_store_transactions(enable_banking_account)
start_date = determine_sync_start_date(enable_banking_account)
+ include_pending = include_pending?
all_transactions = fetch_paginated_transactions(
enable_banking_account,
@@ -235,13 +240,16 @@ class EnableBankingItem::Importer
psu_headers: enable_banking_item.build_psu_headers
)
- # Also fetch pending transactions (visible for 1-3 days before they become BOOK)
- pending_transactions = fetch_paginated_transactions(
- enable_banking_account,
- start_date: start_date,
- transaction_status: "PDNG",
- psu_headers: enable_banking_item.build_psu_headers
- )
+ pending_transactions = []
+ if include_pending
+ # Also fetch pending transactions (visible for 1-3 days before they become BOOK) if setting is enabled
+ pending_transactions = fetch_paginated_transactions(
+ enable_banking_account,
+ start_date: start_date,
+ transaction_status: "PDNG",
+ psu_headers: enable_banking_item.build_psu_headers
+ )
+ end
book_ids = all_transactions
.map { |tx| tx.with_indifferent_access[:transaction_id].presence }
@@ -269,8 +277,18 @@ class EnableBankingItem::Importer
transactions_count = all_transactions.count
+ existing_transactions = enable_banking_account.raw_transactions_payload.to_a
+
+ removed_pending = false
+
+ unless include_pending
+ removed_pending = existing_transactions.reject! do |tx|
+ tx = tx.with_indifferent_access
+ tx.dig(:extra, :enable_banking, :pending) || tx[:_pending]
+ end
+ end
+
if all_transactions.any?
- existing_transactions = enable_banking_account.raw_transactions_payload.to_a
# C4: Remove stored PDNG entries that have now settled as BOOK.
# When a BOOK transaction arrives with the same transaction_id as a stored
@@ -286,11 +304,16 @@ class EnableBankingItem::Importer
.map { |tx| tx.with_indifferent_access[:entry_reference].presence }
.compact.to_set
- removed_pending = existing_transactions.reject! do |tx|
- tx = tx.with_indifferent_access
- pending_flag = tx.dig(:extra, :enable_banking, :pending) || tx[:_pending]
- next false unless pending_flag
- tx[:transaction_id].present? ? book_ids.include?(tx[:transaction_id]) : book_entry_refs.include?(tx[:entry_reference].presence)
+ if include_pending
+ removed_pending ||= existing_transactions.reject! do |tx|
+ tx = tx.with_indifferent_access
+ pending_flag = tx.dig(:extra, :enable_banking, :pending) || tx[:_pending]
+ next false unless pending_flag
+
+ tx[:transaction_id].present? ?
+ book_ids.include?(tx[:transaction_id]) :
+ book_entry_refs.include?(tx[:entry_reference].presence)
+ end
end
existing_ids = existing_transactions.map { |tx|
@@ -307,6 +330,10 @@ class EnableBankingItem::Importer
if new_transactions.any? || removed_pending
enable_banking_account.upsert_enable_banking_transactions_snapshot!(existing_transactions + new_transactions)
end
+ elsif removed_pending
+ enable_banking_account.upsert_enable_banking_transactions_snapshot!(
+ existing_transactions
+ )
end
{ success: true, transactions_count: transactions_count }