Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby
12.0 KB
Raw Blame
# frozen_string_literal: true

class CoinspotItemsController < ApplicationController
  include SafeReturnPath

  before_action :set_coinspot_item, only: %i[update destroy sync setup_accounts complete_account_setup]
  before_action :require_admin!, only: %i[
    create select_accounts link_accounts select_existing_account link_existing_account
    update destroy sync setup_accounts complete_account_setup
  ]

  # Creates a new CoinSpot connection from the settings panel form, sets its
  # institution branding, and queues the first sync.
  def create
    @coinspot_item = Current.family.coinspot_items.build(coinspot_item_params)
    @coinspot_item.name ||= t(".default_name")

    if @coinspot_item.save
      @coinspot_item.set_coinspot_institution_defaults!
      @coinspot_item.sync_later
      redirect_to settings_providers_path, notice: t(".success"), status: :see_other
    else
      render_provider_panel("coinspot", alert: @coinspot_item.errors.full_messages.join(", "))
    end
  end

  # Updates a connection's editable fields (name, sync start date, and
  # optionally new credentials).
  def update
    if @coinspot_item.update(coinspot_item_params)
      render_provider_panel("coinspot", notice: t(".success"))
    else
      render_provider_panel("coinspot", alert: @coinspot_item.errors.full_messages.join(", "))
    end
  end

  # Unlinks every account from this connection, then schedules the
  # connection itself for asynchronous deletion.
  def destroy
    # unlink_all! captures a per-account failure on the result rather than
    # raising, so discarding the results scheduled the connection for deletion
    # even when accounts were still linked to it -- taking their provider
    # links down with it.
    unlink_results = @coinspot_item.unlink_all!(dry_run: false)
    if unlink_results.any? { |result| result[:error].present? }
      redirect_to settings_providers_path, alert: t(".unlink_failed")
      return
    end

    @coinspot_item.destroy_later
    redirect_to settings_providers_path, notice: t(".success")
  end

  # Queues a manual sync, unless one is already running.
  def sync
    @coinspot_item.sync_later unless @coinspot_item.syncing?
    return render_provider_panel("coinspot", notice: t("settings.providers.sync_provider_in_progress")) if provider_panel_form?

    respond_to do |format|
      format.html { redirect_back_or_to settings_providers_path }
      format.json { head :ok }
    end
  end

  # Entry point for adding a new CoinSpot-backed account: resolves which
  # connection to use (from the param, or the family's only credentialed
  # one) and redirects into account setup, or back with a selection prompt
  # when the connection is ambiguous or missing.
  def select_accounts
    account_flow = coinspot_item_account_flow_context
    coinspot_item = account_flow[:coinspot_item]

    unless coinspot_item
      message = coinspot_item_selection_message(account_flow[:credentialed_items])
      frame_redirect_to(settings_providers_path, alert: message)
      return
    end

    redirect_to setup_accounts_coinspot_item_path(coinspot_item, return_to: safe_return_to_path), status: :see_other
  end

  # Entry point for linking an already-existing manual account to a CoinSpot
  # connection: resolves the connection and redirects into account setup.
  def link_accounts
    coinspot_item = coinspot_item_account_flow_context[:coinspot_item]
    unless coinspot_item
      redirect_to settings_providers_path, alert: t(".select_connection")
      return
    end

    redirect_to setup_accounts_coinspot_item_path(coinspot_item), status: :see_other
  end

  # Renders the dialog for picking which unlinked CoinSpot account to attach
  # to an existing manual Crypto exchange account.
  def select_existing_account
    @account = Current.family.accounts.find(params[:account_id])
    # require_admin! is a family-role check, not an account one: an admin who
    # neither owns this account nor holds full_control on it still reaches
    # here. auto_share_with_family! only grants members read_write, so without
    # this the dialog exposes a co-member's account to be linked.
    return unless require_account_permission!(@account, :write, redirect_path: accounts_path)

    account_flow = coinspot_item_account_flow_context
    @coinspot_item = account_flow[:coinspot_item]

    unless manual_crypto_exchange_account?(@account)
      redirect_to accounts_path, alert: t("coinspot_items.link_existing_account.errors.only_manual")
      return
    end

    unless @coinspot_item
      redirect_to settings_providers_path, alert: coinspot_item_selection_message(account_flow[:credentialed_items])
      return
    end

    @available_coinspot_accounts = @coinspot_item.coinspot_accounts
      .left_joins(:account_provider)
      .where(account_providers: { id: nil })
      .order(:name)

    render :select_existing_account, layout: false
  end

  # Links a chosen unlinked CoinSpot account to an existing manual account
  # and queues a sync so its history imports immediately.
  def link_existing_account
    @account = Current.family.accounts.find(params[:account_id])
    # Must precede the AccountProvider.create! below: family scoping alone
    # would let an admin attach a provider to a co-member's account they only
    # hold read_write on. Same guard as select_existing_account.
    return unless require_account_permission!(@account, :write, redirect_path: accounts_path)

    coinspot_item = coinspot_item_account_flow_context[:coinspot_item]

    unless manual_crypto_exchange_account?(@account)
      return redirect_or_flash_error(t(".errors.only_manual"), account_path(@account))
    end

    unless coinspot_item
      redirect_to settings_providers_path, alert: t(".select_connection")
      return
    end

    coinspot_account = coinspot_item.coinspot_accounts.find_by(id: params[:coinspot_account_id])
    unless coinspot_account
      return redirect_or_flash_error(t(".errors.invalid_coinspot_account"), account_path(@account))
    end
    # Two locks, outer on @account, because the unique index is on
    # (account_id, provider_type): locking only the coinspot row lets two
    # requests pick DIFFERENT coinspot accounts for the SAME @account, both
    # pass their own check, and the second create! raise RecordNotUnique as an
    # unrescued 500. The eligibility check is repeated inside the lock, since
    # manual_crypto_exchange_account? read account_providers before it.
    already_linked = false
    @account.with_lock do
      if @account.reload.account_providers.any?
        already_linked = true
      else
        coinspot_account.with_lock do
          if coinspot_account.reload.account_provider.present?
            already_linked = true
          else
            AccountProvider.create!(account: @account, provider: coinspot_account)
          end
        end
      end
    end

    if already_linked
      return redirect_or_flash_error(t(".errors.coinspot_account_already_linked"), account_path(@account))
    end

    coinspot_item.sync_later

    redirect_to accounts_path, notice: t(".success")
  end

  # Renders the dialog listing this connection's not-yet-imported accounts
  # for the user to select from.
  def setup_accounts
    @coinspot_accounts = unlinked_accounts_for(@coinspot_item)
  end

  # Creates a Sure account for each selected, still-unlinked CoinSpot
  # account and processes its initial activity. A single account's failure
  # is logged and skipped rather than aborting the rest of the selection;
  # a created account whose provider link fails to save is torn back down
  # instead of left half-set-up.
  def complete_account_setup
    selected_accounts = Array(params[:selected_accounts]).reject(&:blank?)
    return_to = safe_return_to_path
    created_accounts = []
    failed_accounts = 0

    selected_accounts.each do |coinspot_account_id|
      coinspot_account = @coinspot_item.coinspot_accounts.find_by(id: coinspot_account_id)
      next unless coinspot_account

      coinspot_account.with_lock do
        if coinspot_account.account_provider.blank?
          account = Account.create_from_coinspot_account(coinspot_account)
          provider_link = coinspot_account.ensure_account_provider!(account)
          provider_link ? created_accounts << account : account.destroy!
        end
      end

      CoinspotAccount::Processor.new(coinspot_account.reload).process
    rescue StandardError => e
      failed_accounts += 1
      DebugLogEntry.capture(
        category: "provider_sync_error",
        level: "error",
        message: "Failed to setup account for CoinspotAccount: #{e.message}",
        source: self.class.name,
        provider_key: "coinspot",
        family: @coinspot_item.family,
        metadata: { coinspot_account_id: coinspot_account_id, error_class: e.class.name }
      )
    end

    @coinspot_item.update!(pending_account_setup: unlinked_accounts_for(@coinspot_item).exists?)
    @coinspot_item.sync_later if created_accounts.any?

    notice = if created_accounts.any?
      t(".success", count: created_accounts.count)
    elsif selected_accounts.empty?
      t(".none_selected")
    elsif failed_accounts == selected_accounts.count
      t(".setup_failed", count: failed_accounts)
    else
      t(".no_accounts")
    end

    redirect_to return_to || accounts_path, notice: notice, status: :see_other
  end

  private

    # Loads the connection scoped to the current family, 404ing if it
    # doesn't belong to them.
    def set_coinspot_item
      @coinspot_item = Current.family.coinspot_items.find(params[:id])
    end

    # Permitted connection attributes. Blank credential fields are dropped
    # on update so re-submitting the edit form without touching them doesn't
    # overwrite already-stored credentials with blanks.
    def coinspot_item_params
      permitted = params.require(:coinspot_item).permit(:name, :sync_start_date, :api_key, :api_secret)
      if @coinspot_item&.persisted?
        permitted.delete(:api_key) if permitted[:api_key].blank?
        permitted.delete(:api_secret) if permitted[:api_secret].blank?
      end
      permitted
    end

    # Resolves which connection an account-linking flow should act on: the
    # one named by params[:coinspot_item_id], or the family's only
    # credentialed connection when there's exactly one and no id was given.
    # Also returns every credentialed connection, for the selection prompt
    # when the target is ambiguous.
    def coinspot_item_account_flow_context
      credentialed_items = Current.family.coinspot_items
        .active.credentials_configured.ordered
        .select(&:credentials_configured?)
      item = if params[:coinspot_item_id].present?
        credentialed_items.find { |candidate| candidate.id.to_s == params[:coinspot_item_id].to_s }
      elsif credentialed_items.one?
        credentialed_items.first
      end

      { coinspot_item: item, credentialed_items: credentialed_items }
    end

    # A connection's CoinSpot accounts not yet linked to a Sure account.
    def unlinked_accounts_for(coinspot_item)
      coinspot_item.coinspot_accounts.left_joins(:account_provider).where(account_providers: { id: nil }).order(:name)
    end

    # Explains why no connection could be resolved: ambiguous (multiple
    # connections, none specified) versus none configured at all.
    def coinspot_item_selection_message(credentialed_items)
      if credentialed_items.count > 1 && params[:coinspot_item_id].blank?
        t("coinspot_items.select_accounts.select_connection")
      else
        t("coinspot_items.select_accounts.no_credentials_configured")
      end
    end

    # Only a manual Crypto exchange account with no existing provider link
    # is eligible to be linked to a CoinSpot account.
    def manual_crypto_exchange_account?(account)
      account.manual_crypto_exchange?
    end

    # Reports an error from a linking action: a flash Turbo Stream for a
    # frame request, or a redirect with an alert otherwise.
    def redirect_or_flash_error(message, fallback_path)
      if turbo_frame_request?
        flash.now[:alert] = message
        render turbo_stream: Array(flash_notification_stream_items)
      else
        redirect_to fallback_path, alert: message
      end
    end
end