Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

FIX pull fresh accounts from API (#385)

soky srmba266986d475
diff --git a/app/models/lunchflow_account.rb b/app/models/lunchflow_account.rb
index d79daff70c..35d74dab95 100644
--- a/app/models/lunchflow_account.rb
+++ b/app/models/lunchflow_account.rb
@@ -28,7 +28,7 @@ class LunchflowAccount < ApplicationRecord
snapshot[:name]
end
- update!(
+ assign_attributes(
current_balance: nil, # Balance not provided by accounts endpoint
currency: parse_currency(snapshot[:currency]) || "USD",
name: display_name,
@@ -41,6 +41,8 @@ class LunchflowAccount < ApplicationRecord
}.compact,
raw_payload: account_snapshot
)
+
+ save!
end
def upsert_lunchflow_transactions_snapshot!(transactions_snapshot)
diff --git a/app/models/lunchflow_item/importer.rb b/app/models/lunchflow_item/importer.rb
index 54310e66e3..6d4c63e908 100644
--- a/app/models/lunchflow_item/importer.rb
+++ b/app/models/lunchflow_item/importer.rb
@@ -24,37 +24,56 @@ class LunchflowItem::Importer
# Continue with import even if snapshot storage fails
end
- # Step 2: Update only previously selected accounts (don't create new ones)
+ # Step 2: Update linked accounts and create records for new accounts from API
accounts_updated = 0
+ accounts_created = 0
accounts_failed = 0
if accounts_data[:accounts].present?
- # Get only linked lunchflow account IDs (ones actually imported/used by the user)
- # This prevents updating orphaned accounts from old behavior that saved everything
- existing_account_ids = lunchflow_item.lunchflow_accounts
- .joins(:account_provider)
- .pluck(:account_id)
- .map(&:to_s)
+ # Get linked lunchflow account IDs (ones actually imported/used by the user)
+ linked_account_ids = lunchflow_item.lunchflow_accounts
+ .joins(:account_provider)
+ .pluck(:account_id)
+ .map(&:to_s)
+
+ # Get all existing lunchflow account IDs (linked or not)
+ all_existing_ids = lunchflow_item.lunchflow_accounts.pluck(:account_id).map(&:to_s)
accounts_data[:accounts].each do |account_data|
account_id = account_data[:id]&.to_s
next unless account_id.present?
+ next if account_data[:name].blank?
- # Only update if this account was previously selected (exists in our DB)
- next unless existing_account_ids.include?(account_id)
-
- begin
- import_account(account_data)
- accounts_updated += 1
- rescue => e
- accounts_failed += 1
- Rails.logger.error "LunchflowItem::Importer - Failed to update account #{account_id}: #{e.message}"
- # Continue updating other accounts even if one fails
+ if linked_account_ids.include?(account_id)
+ # Update existing linked accounts
+ begin
+ import_account(account_data)
+ accounts_updated += 1
+ rescue => e
+ accounts_failed += 1
+ Rails.logger.error "LunchflowItem::Importer - Failed to update account #{account_id}: #{e.message}"
+ end
+ elsif !all_existing_ids.include?(account_id)
+ # Create new unlinked lunchflow_account records for accounts we haven't seen before
+ # This allows users to link them later via "Setup new accounts"
+ begin
+ lunchflow_account = lunchflow_item.lunchflow_accounts.build(
+ account_id: account_id,
+ name: account_data[:name],
+ currency: account_data[:currency] || "USD"
+ )
+ lunchflow_account.upsert_lunchflow_snapshot!(account_data)
+ accounts_created += 1
+ Rails.logger.info "LunchflowItem::Importer - Created new unlinked account record for #{account_id}"
+ rescue => e
+ accounts_failed += 1
+ Rails.logger.error "LunchflowItem::Importer - Failed to create account #{account_id}: #{e.message}"
+ end
end
end
end
- Rails.logger.info "LunchflowItem::Importer - Updated #{accounts_updated} accounts (#{accounts_failed} failed)"
+ Rails.logger.info "LunchflowItem::Importer - Updated #{accounts_updated} accounts, created #{accounts_created} new (#{accounts_failed} failed)"
# Step 3: Fetch transactions only for linked accounts with active status
transactions_imported = 0
@@ -75,11 +94,12 @@ class LunchflowItem::Importer
end
end
- Rails.logger.info "LunchflowItem::Importer - Completed import for item #{lunchflow_item.id}: #{accounts_updated} accounts updated, #{transactions_imported} transactions"
+ Rails.logger.info "LunchflowItem::Importer - Completed import for item #{lunchflow_item.id}: #{accounts_updated} accounts updated, #{accounts_created} new accounts discovered, #{transactions_imported} transactions"
{
success: accounts_failed == 0 && transactions_failed == 0,
accounts_updated: accounts_updated,
+ accounts_created: accounts_created,
accounts_failed: accounts_failed,
transactions_imported: transactions_imported,
transactions_failed: transactions_failed