Files
sure/app/models/plaid_item/syncer.rb
LPW 140ea78b0e Add global sync summary component for all providers (#588)
* Add shared sync statistics collection and provider sync summary UI

- Introduced `SyncStats::Collector` concern to centralize sync statistics logic, including account, transaction, holdings, and health stats collection.
- Added collapsible `ProviderSyncSummary` component for displaying sync summaries across providers.
- Updated syncers (e.g., `LunchflowItem::Syncer`) to use the shared collector methods for consistent stats calculation.
- Added rake tasks under `dev:sync_stats` for testing and development purposes, including fake stats generation with optional issues.
- Enhanced provider-specific views to include sync summaries using the new shared component.

* Refactor `ProviderSyncSummary` to improve maintainability

- Extracted `severity_color_class` to simplify severity-to-CSS mapping.
- Replaced `holdings_label` with `holdings_label_key` for streamlined localization.
- Updated locale file to separate `found` and `processed` translations for clarity.

---------

Signed-off-by: Juan José Mata <juanjo.mata@gmail.com>
Co-authored-by: Josh Waldrep <joshua.waldrep5+github@gmail.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
2026-01-09 19:26:37 +01:00

68 lines
2.4 KiB
Ruby

class PlaidItem::Syncer
include SyncStats::Collector
attr_reader :plaid_item
def initialize(plaid_item)
@plaid_item = plaid_item
end
def perform_sync(sync)
# Phase 1: Import data from Plaid API
sync.update!(status_text: "Importing accounts from Plaid...") if sync.respond_to?(:status_text)
plaid_item.import_latest_plaid_data
# Phase 2: Collect setup statistics
sync.update!(status_text: "Checking account configuration...") if sync.respond_to?(:status_text)
collect_setup_stats(sync, provider_accounts: plaid_item.plaid_accounts)
# Check for unlinked accounts and update pending_account_setup flag
unlinked_count = plaid_item.plaid_accounts.count { |pa| pa.current_account.nil? }
if unlinked_count > 0
plaid_item.update!(pending_account_setup: true) if plaid_item.respond_to?(:pending_account_setup=)
sync.update!(status_text: "#{unlinked_count} accounts need setup...") if sync.respond_to?(:status_text)
else
plaid_item.update!(pending_account_setup: false) if plaid_item.respond_to?(:pending_account_setup=)
end
# Phase 3: Process the raw Plaid data and updates internal domain objects
linked_accounts = plaid_item.plaid_accounts.select { |pa| pa.current_account.present? }
if linked_accounts.any?
sync.update!(status_text: "Processing transactions...") if sync.respond_to?(:status_text)
mark_import_started(sync)
plaid_item.process_accounts
# Phase 4: Schedule balance calculations
sync.update!(status_text: "Calculating balances...") if sync.respond_to?(:status_text)
plaid_item.schedule_account_syncs(
parent_sync: sync,
window_start_date: sync.window_start_date,
window_end_date: sync.window_end_date
)
# Phase 5: Collect transaction and holdings statistics
account_ids = linked_accounts.filter_map { |pa| pa.current_account&.id }
collect_transaction_stats(sync, account_ids: account_ids, source: "plaid")
collect_holdings_stats(sync, holdings_count: count_holdings(linked_accounts), label: "processed")
end
# Mark sync health
collect_health_stats(sync, errors: nil)
rescue => e
collect_health_stats(sync, errors: [ { message: e.message, category: "sync_error" } ])
raise
end
def perform_post_sync
# no-op
end
private
def count_holdings(plaid_accounts)
plaid_accounts.sum do |pa|
Array(pa.raw_investments_payload).size
end
end
end