Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
24.3 KB
Raw Blame
# frozen_string_literal: true

class RedbarkItem::Importer
  include SyncStats::Collector
  include RedbarkAccount::DataHelpers
  include CurrencyNormalizable

  attr_reader :redbark_item, :redbark_provider, :sync, :fetched_at

  # `fetched_at` stamps the account-details snapshot. It is injected rather
  # than read here so that one sync's fetch and its processing share a clock;
  # see RedbarkItem::Syncer.
  def initialize(redbark_item, redbark_provider:, sync: nil, fetched_at: Time.current)
    @redbark_item = redbark_item
    @redbark_provider = redbark_provider
    @sync = sync
    @fetched_at = fetched_at
  end

  def import
    Rails.logger.info "RedbarkItem::Importer - Starting import for item #{redbark_item.id}"

    # Step 1: Fetch and store all accounts (with connection metadata for institutions)
    import_accounts

    # Step 2: For linked accounts only, fetch transactions and balances.
    # Unlinked accounts just need basic info (name, institution) for the setup modal.
    # `account_provider: :account` is preloaded because the account-type filter
    # in import_account_details reads `current_account` for every linked
    # account, which is a query each without it.
    linked_accounts = redbark_item.linked_redbark_accounts.includes(account_provider: :account).to_a

    Rails.logger.info "RedbarkItem::Importer - Found #{linked_accounts.count} linked accounts to process"

    linked_accounts.each do |redbark_account|
      import_transactions(redbark_account)
    end

    import_balances(linked_accounts)
    import_account_details(linked_accounts)

    # Store import stats on the item as the raw snapshot
    redbark_item.upsert_redbark_snapshot!(stats)

    stats
  rescue Provider::Redbark::AuthenticationError
    redbark_item.update!(status: :requires_update)
    raise
  end

  private

    def stats
      @stats ||= {}
    end

    def persist_stats!
      return unless sync&.respond_to?(:sync_stats)
      merged = (sync.sync_stats || {}).merge(stats)
      sync.update_columns(sync_stats: merged)
    end

    def connections_by_id
      @connections_by_id ||= begin
        stats["api_requests"] = stats.fetch("api_requests", 0) + 1
        redbark_provider.list_connections.index_by { |c| c[:id].to_s }
      rescue Provider::Redbark::AuthenticationError
        raise
      rescue => e
        capture_failure("Connections fetch failed; institution metadata will be limited", e)
        {}
      end
    end

    def import_accounts
      Rails.logger.info "RedbarkItem::Importer - Fetching accounts"

      accounts_data = redbark_provider.list_accounts

      stats["api_requests"] = stats.fetch("api_requests", 0) + 1
      stats["total_accounts"] = accounts_data.size

      # Fetch connections once, before the per-account rescue below - an auth
      # failure here must propagate and mark the item requires_update, not get
      # swallowed as N per-account errors
      connections_by_id

      upstream_account_ids = []

      accounts_data.each do |account_data|
        begin
          account_id = account_data[:id]&.to_s
          next if account_id.blank?
          next if account_data[:name].blank?

          # Only banking and documents connections carry transactions;
          # brokerage accounts belong to /v1/trades and are out of scope here
          connection = connections_by_id[account_data[:connectionId].to_s]
          next unless transactable_connection?(connection)

          upstream_account_ids << account_id

          redbark_account = redbark_item.redbark_accounts.find_or_initialize_by(
            redbark_account_id: account_id
          )
          redbark_account.upsert_from_redbark!(account_data, connection_data: connection)

          stats["accounts_imported"] = stats.fetch("accounts_imported", 0) + 1
        rescue => e
          capture_failure("Account import failed", e, account_id: account_data[:id])
          stats["accounts_skipped"] = stats.fetch("accounts_skipped", 0) + 1
          register_error(e, account_id: account_data[:id])
        end
      end

      persist_stats!

      @upstream_account_ids = upstream_account_ids

      prune_removed_accounts(upstream_account_ids)
    end

    def import_transactions(redbark_account)
      Rails.logger.info "RedbarkItem::Importer - Fetching transactions for account #{redbark_account.id}"

      if redbark_account.connection_id.blank?
        Rails.logger.warn "RedbarkItem::Importer - Account #{redbark_account.id} has no connection_id, skipping transactions"
        return
      end

      # A linked account can still sit on a non-transactable connection
      # (e.g. brokerage rows linked before this guard existed) - the
      # transactions endpoint rejects those outright
      connection = connections_by_id[redbark_account.connection_id.to_s]
      unless transactable_connection?(connection)
        Rails.logger.info "RedbarkItem::Importer - Skipping transactions for non-banking account #{redbark_account.id}"
        return
      end

      begin
        start_date = calculate_transaction_start_date(redbark_account)

        transactions_data = redbark_provider.get_transactions(
          connection_id: redbark_account.connection_id,
          account_id: redbark_account.redbark_account_id,
          start_date: start_date,
          end_date: Date.current,
          include_pending: Rails.configuration.x.redbark.include_pending
        )

        stats["api_requests"] = stats.fetch("api_requests", 0) + 1

        if transactions_data.any?
          transactions_hashes = transactions_data.map { |t| sdk_object_to_hash(t) }
          stored = redbark_account.raw_transactions_payload || []

          RedbarkAccount.transaction do
            dropped_stored_ids, kept_ids_by_alias = apply_reissued_ids(redbark_account, stored, transactions_hashes, start_date)

            merged = merge_transactions(
              stored.reject { |t| dropped_stored_ids.include?(transaction_key(t)) },
              fold_aliases(transactions_hashes, kept_ids_by_alias, dropped_stored_ids),
              window_start: start_date
            )
            redbark_account.upsert_redbark_transactions_snapshot!(merged)
          end
          stats["transactions_found"] = stats.fetch("transactions_found", 0) + transactions_data.size
        end
      rescue Provider::Redbark::AuthenticationError
        raise
      rescue => e
        capture_failure("Transactions fetch failed", e, account_id: redbark_account.redbark_account_id)
        register_error(e, context: "transactions", account_id: redbark_account.id)
      end
    end

    # One balances call covers every eligible linked account. A failed fetch
    # leaves current_balance untouched so the processor keeps the previous
    # balance instead of writing zeros.
    def import_balances(linked_accounts)
      eligible_accounts = balance_eligible_accounts(linked_accounts)
      account_ids = eligible_accounts.map(&:redbark_account_id).compact
      return if account_ids.empty?

      begin
        balances = fetch_balances_with_fallback(account_ids)

        balances_by_id = balances.index_by { |b| b[:accountId].to_s }

        eligible_accounts.each do |redbark_account|
          balance_data = balances_by_id[redbark_account.redbark_account_id]
          next unless balance_data

          amount = parse_decimal(balance_data[:currentBalance])
          next if amount.nil?

          redbark_account.update!(
            current_balance: amount,
            currency: extract_currency(balance_data, fallback: redbark_account.currency)
          )
          stats["balances_updated"] = stats.fetch("balances_updated", 0) + 1
        end
      rescue Provider::Redbark::AuthenticationError
        raise
      rescue => e
        capture_failure("Balances fetch failed; keeping previous balances", e)
        register_error(e, context: "balances")
      end
    end

    # Loans, and credit cards that still need an APR, and only when there are
    # any: `account-details` is the one endpoint that reports a rate, and for
    # every other account it would be a request per sync for something nothing
    # reads. Cards are a batch of their own (store_card_account_details).
    #
    # A failure here leaves the payload untouched and the sync green, exactly as
    # a failed balances fetch does. The processor treats "never fetched" and
    # "fetch failed" alike, because for a rate they mean the same thing:
    # nothing was reported.
    def import_account_details(linked_accounts)
      eligible_accounts = balance_eligible_accounts(linked_accounts)
      loans = eligible_accounts.select { |redbark_account| redbark_account.current_account&.accountable_type == "Loan" }
      cards = eligible_accounts.select { |redbark_account| card_details_due?(redbark_account) }

      store_loan_account_details(loans)
      store_card_account_details(cards)
    end

    def store_loan_account_details(eligible_accounts)
      account_ids = eligible_accounts.map(&:redbark_account_id).compact
      return if account_ids.empty?

      begin
        details_by_id = fetch_account_details_with_fallback(account_ids)
                          .index_by { |detail| detail[:accountId].to_s }

        eligible_accounts.each do |redbark_account|
          detail = details_by_id[redbark_account.redbark_account_id]
          # An account the provider has no detail for is omitted from `data`
          # rather than erroring, so leave the last payload in place -- and
          # leave the stamp alone with it, so the processor knows the snapshot
          # is not from this sync.
          next if detail.blank?

          redbark_account.update!(
            raw_account_details_payload: detail,
            account_details_fetched_at: fetched_at
          )
          stats["account_details_updated"] = stats.fetch("account_details_updated", 0) + 1
        end
      rescue Provider::Redbark::AuthenticationError
        raise
      rescue => e
        capture_failure("Account details fetch failed; keeping previous details", e)
        register_error(e, context: "account_details")
      end
    end

    def card_details_due?(redbark_account)
      account = redbark_account.current_account
      return false unless account&.accountable_type == "CreditCard"
      return false unless RedbarkAccount::CreditCardDetailsProcessor.needs_apr?(account.accountable)

      last_attempt_at = redbark_account.account_details_fetched_at
      last_attempt_at.blank? || last_attempt_at <= fetched_at - RedbarkAccount::CreditCardDetailsProcessor::REFRESH_WINDOW
    end

    # A card's product details rarely change, so each card is tried at most
    # once per window: its stamp records every attempt -- stored, left out by
    # the provider, or failed -- not only a stored payload. The payload is
    # replaced on every attempt, cleared when there is none, so a stamp never
    # vouches for details from an earlier attempt. A batch of its own,
    # so a card id the endpoint rejects cannot push the loans into per-account
    # retries; a failure is logged once per window, never a sync error.
    def store_card_account_details(cards)
      return if cards.empty?

      begin
        details_by_id = fetch_account_details_with_fallback(cards.map(&:redbark_account_id), log: method(:warn_card_failure))
                          .index_by { |detail| detail[:accountId].to_s }

        cards.each do |redbark_account|
          detail = details_by_id[redbark_account.redbark_account_id]
          redbark_account.update!(raw_account_details_payload: detail.presence, account_details_fetched_at: fetched_at)
          stats["account_details_updated"] = stats.fetch("account_details_updated", 0) + 1 if detail.present?
        end
      rescue Provider::Redbark::AuthenticationError
        raise
      rescue => e
        warn_card_failure("Card account details fetch failed; clearing previous details", e)
        RedbarkAccount.where(id: cards.map(&:id)).update_all(raw_account_details_payload: nil, account_details_fetched_at: fetched_at)
      end
    end

    def warn_card_failure(message, error, account_id: nil)
      RedbarkAccount::CreditCardDetailsProcessor.warn_once(
        message, source: self.class.name, family: redbark_item.family,
        key: account_id ? { account_id: account_id } : { redbark_item_id: redbark_item.id },
        error_class: error.class.name, error: error.message
      )
    end

    # The batch is rejected whole when one id is stale or of the wrong
    # category, exactly as the balances batch is, so one bad account must not
    # cost every other loan its rate. Mirrors fetch_balances_with_fallback.
    def fetch_account_details_with_fallback(account_ids, log: method(:capture_failure))
      stats["api_requests"] = stats.fetch("api_requests", 0) + 1
      redbark_provider.get_account_details(account_ids: account_ids)
    rescue Provider::Redbark::AuthenticationError
      raise
    rescue Provider::Redbark::Error => e
      raise unless %i[not_found bad_request].include?(e.error_type)
      raise if account_ids.size <= 1

      log.call("Batched account details call rejected; retrying per account", e)

      account_ids.flat_map do |account_id|
        begin
          stats["api_requests"] = stats.fetch("api_requests", 0) + 1
          redbark_provider.get_account_details(account_ids: [ account_id ])
        rescue Provider::Redbark::AuthenticationError
          raise
        rescue Provider::Redbark::Error => account_error
          log.call("Account details fetch failed for account", account_error, account_id: account_id)
          []
        end
      end
    end

    # The balances endpoint rejects the WHOLE batch when any requested id is
    # unknown (404) or non-banking (400), so exclude accounts that no longer
    # exist upstream and accounts on non-banking connections up front.
    def balance_eligible_accounts(linked_accounts)
      linked_accounts.select do |redbark_account|
        next false if redbark_account.redbark_account_id.blank?

        if @upstream_account_ids.present? && !@upstream_account_ids.include?(redbark_account.redbark_account_id)
          next false
        end

        connection = connections_by_id[redbark_account.connection_id.to_s]
        next true if connection.nil? # no connection metadata - let the fallback sort it out

        category = connection[:category].to_s
        category.blank? || category == "banking"
      end
    end

    # If the batch is still rejected (stale id or category we could not see),
    # fall back to per-account requests so one bad account cannot freeze
    # balance updates for the rest of the item.
    def fetch_balances_with_fallback(account_ids)
      stats["api_requests"] = stats.fetch("api_requests", 0) + 1
      redbark_provider.get_balances(account_ids: account_ids)
    rescue Provider::Redbark::AuthenticationError
      raise
    rescue Provider::Redbark::Error => e
      raise unless %i[not_found bad_request].include?(e.error_type)
      raise if account_ids.size <= 1

      capture_failure("Batched balances call rejected; retrying per account", e)

      account_ids.flat_map do |account_id|
        begin
          stats["api_requests"] = stats.fetch("api_requests", 0) + 1
          redbark_provider.get_balances(account_ids: [ account_id ])
        rescue Provider::Redbark::AuthenticationError
          raise
        rescue Provider::Redbark::Error => account_error
          capture_failure("Balance fetch failed for account; keeping previous balance", account_error, account_id: account_id)
          []
        end
      end
    end

    def calculate_transaction_start_date(redbark_account)
      has_stored_transactions = (redbark_account.raw_transactions_payload || []).any?

      if has_stored_transactions && redbark_item.last_synced_at.present?
        # Incremental: go back 7 days from last sync to catch late-posting
        # transactions. The user's sync_start_date governs the initial
        # backfill only - re-fetching the whole window every sync would blow
        # through the API's row ceiling on busy accounts.
        (redbark_item.last_synced_at - 7.days).to_date
      elsif redbark_account.sync_start_date.present?
        # The stored date was validated against the rolling lookback window
        # when it was set, but time passes between then and this first real
        # fetch (a delayed initial sync, a long-stuck job). Clamp to today's
        # window so an expired date doesn't get sent to Redbark and rejected
        # outright -- the user still gets as much history as is still
        # available, instead of nothing.
        [ redbark_account.sync_start_date, RedbarkAccount::MAX_SYNC_START_DATE_LOOKBACK_DAYS.days.ago.to_date ].max
      else
        # First sync for this account: pull the default history window
        RedbarkAccount::DEFAULT_INITIAL_HISTORY_DAYS.days.ago.to_date
      end
    end

    # The snapshot is bounded to the current fetch window - durable history
    # lives in entries (deduped by external_id), so older rows are dropped
    # rather than accumulated forever. Rows without a parseable date are kept.
    # A pending row absent from the refetch has settled (possibly under a new
    # id) - keeping it would duplicate the posted transaction.
    def merge_transactions(existing, new_transactions, window_start: nil)
      new_keys = new_transactions.map { |t| transaction_key(t) }.to_set

      by_id = {}
      existing.each do |t|
        next unless within_window?(t, window_start)
        next if stale_pending?(t, new_keys)
        by_id[transaction_key(t)] = t
      end
      new_transactions.each { |t| by_id[transaction_key(t)] = t }
      by_id.values
    end

    # Points existing entries at the ids a bank re-issued (see
    # RedbarkAccount::TransactionIdReissue). Returns the stored ids to leave
    # out of the snapshot, so the processor can't import the old row as a
    # second entry, and { generated id => kept id } for fold_aliases.
    #
    # A re-key moves only external_id: a protected entry keeps everything
    # else, and an unprotected one is refreshed from the new row by the
    # processor as usual. A generated id is recorded as an alias on the entry
    # it duplicates, which keeps its id for when the bank restores it; with no
    # such entry it is handled like any other new id.
    def apply_reissued_ids(redbark_account, stored, fetched, window_start)
      entries = redbark_account.current_account&.entries&.where(source: "redbark")
      return [ Set.new, {} ] if entries.nil?

      kept_ids_by_alias = entries.where(entryable_type: "Transaction")
        .joins("INNER JOIN transactions ON transactions.id = entries.entryable_id")
        .where("transactions.extra -> 'redbark' -> 'alias_ids' IS NOT NULL")
        .pluck(:external_id, Arel.sql("transactions.extra -> 'redbark' -> 'alias_ids'"))
        .each_with_object({}) do |(external_id, alias_ids), map|
          Array(alias_ids).each { |alias_id| map[alias_id.to_s] = external_id.delete_prefix("redbark_") }
        end

      result = RedbarkAccount::TransactionIdReissue.new(
        stored: stored, fetched: fetched, window_start: window_start, known_alias_ids: kept_ids_by_alias.keys
      ).call

      dropped_stored_ids = Set.new

      result.aliases.each do |kept_id, generated_id|
        entry = entries.find_by(external_id: "redbark_#{kept_id}", entryable_type: "Transaction")

        if entry
          record_alias(entry.entryable, generated_id)
          kept_ids_by_alias[generated_id] = kept_id
        else
          dropped_stored_ids << kept_id
        end
      end

      result.re_keys.each do |old_id, new_id|
        dropped_stored_ids << old_id

        if entries.exists?(external_id: "redbark_#{new_id}")
          DebugLogEntry.capture(
            category: "provider_sync",
            level: "warn",
            message: "Redbark re-issued a transaction id that already has an entry; left both entries as they are",
            source: self.class.name,
            provider_key: "redbark",
            family: redbark_item.family,
            account_provider: redbark_account.account_provider,
            metadata: { redbark_account_id: redbark_account.id, old_id: old_id, new_id: new_id }
          )
          next
        end

        entries.where(external_id: "redbark_#{old_id}")
          .update_all(external_id: "redbark_#{new_id}", updated_at: Time.current)
      end

      [ dropped_stored_ids, kept_ids_by_alias ]
    end

    # A generated id stands in for its kept id. While it is the only row of
    # the two served, its payload is the current one, so it goes into the
    # snapshot under the kept id and the processor refreshes the kept entry
    # from it. It is dropped once the kept id is served again, or when the
    # kept row left the snapshot this sync (re-keyed away).
    def fold_aliases(fetched, kept_ids_by_alias, dropped_stored_ids)
      fetched_ids = fetched.map { |t| transaction_key(t) }.to_set

      fetched.filter_map do |t|
        kept_id = kept_ids_by_alias[transaction_key(t)]
        next t unless kept_id
        next if fetched_ids.include?(kept_id) || dropped_stored_ids.include?(kept_id)

        t.merge(t.key?(:id) ? { id: kept_id } : { "id" => kept_id })
      end
    end

    def record_alias(transaction, generated_id)
      extra = (transaction.extra || {}).deep_dup
      redbark = extra["redbark"] || {}
      alias_ids = Array(redbark["alias_ids"])
      return if alias_ids.include?(generated_id)

      extra["redbark"] = redbark.merge("alias_ids" => alias_ids + [ generated_id ])
      transaction.update_columns(extra: extra)
    end

    def within_window?(transaction, window_start)
      return true if window_start.nil?

      t = transaction.is_a?(Hash) ? transaction.with_indifferent_access : transaction
      date = Date.parse(t[:date].to_s) rescue nil
      date.nil? || date >= window_start
    end

    def stale_pending?(transaction, new_keys)
      t = transaction.is_a?(Hash) ? transaction.with_indifferent_access : transaction
      t[:status].to_s == "pending" && !new_keys.include?(transaction_key(t))
    end

    def transaction_key(transaction)
      transaction = transaction.with_indifferent_access if transaction.is_a?(Hash)
      transaction[:id].presence ||
        [ transaction[:date], transaction[:amount], transaction[:description] ].join("-")
    end

    # Removes records that no longer exist upstream and are not linked to any
    # Account. Guarded to a non-empty upstream list so a transient empty or
    # failed response can never wipe out all accounts.
    def prune_removed_accounts(upstream_account_ids)
      return if upstream_account_ids.empty?

      scope = redbark_item.redbark_accounts.includes(:account_provider)
      orphaned = scope
        .where.not(redbark_account_id: upstream_account_ids)
        .or(scope.where(redbark_account_id: nil))

      orphaned.each do |redbark_account|
        if redbark_account.account_provider.present?
          Rails.logger.info "RedbarkItem::Importer - Keeping stale RedbarkAccount #{redbark_account.id} (still linked to an Account)"
          next
        end

        begin
          Rails.logger.info "RedbarkItem::Importer - Pruning orphaned RedbarkAccount #{redbark_account.id} (no longer exists upstream)"
          redbark_account.destroy
          stats["accounts_pruned"] = stats.fetch("accounts_pruned", 0) + 1
        rescue => e
          capture_failure("Failed to prune orphaned account", e, redbark_account_id: redbark_account.id)
        end
      end
    end

    # Banking and documents connections serve /v1/transactions; anything else
    # (brokerage) does not. Unknown connections get the benefit of the doubt.
    def transactable_connection?(connection)
      return true if connection.nil?

      category = connection[:category].to_s
      category.blank? || %w[banking documents].include?(category)
    end

    def register_error(error, **context)
      stats["errors"] ||= []
      stats["errors"] << {
        message: error.message,
        context: context.to_s,
        timestamp: Time.current.iso8601
      }
    end

    # Surfaces provider failures in the /settings/debug super-admin UI so
    # support can diagnose without log access
    def capture_failure(message, error, **metadata)
      DebugLogEntry.capture(
        category: "provider_sync",
        level: "warn",
        message: message,
        source: self.class.name,
        provider_key: "redbark",
        family: redbark_item.family,
        metadata: { error_class: error.class.name, error: error.message }.merge(metadata)
      )
      Rails.logger.warn "RedbarkItem::Importer - #{message}: #{error.class}: #{error.message}"
    end
end