-
Notifications
You must be signed in to change notification settings - Fork 25
Add Interactive Brokers Provider #1722
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9ba090d
Display multi-currency holdings correctly
gian-reto 681f830
Implement IBKR provider
gian-reto db1051d
Fix: Use historical exchange rate for historical prices
gian-reto 572d766
Add brokerage exchange rate for trades
gian-reto ccff867
Sync historical balances from IBKR
gian-reto 1c5c1fd
Add logos in activity history
gian-reto 49f1f6a
Fix privacy mode blur in account view
gian-reto 762f6b7
Improve IBKR XML Flex report parser errors
gian-reto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,236 @@ | ||
| class IbkrItemsController < ApplicationController | ||
| before_action :set_ibkr_item, only: [ :update, :destroy, :sync, :setup_accounts, :complete_account_setup ] | ||
| before_action :require_admin!, only: [ :create, :select_accounts, :select_existing_account, :link_existing_account, :update, :destroy, :sync, :setup_accounts, :complete_account_setup ] | ||
|
|
||
| def create | ||
| @ibkr_item = Current.family.ibkr_items.build(ibkr_item_params) | ||
| @ibkr_item.name ||= t("ibkr_items.defaults.name") | ||
|
|
||
| if @ibkr_item.save | ||
| @ibkr_item.sync_later | ||
|
|
||
| if turbo_frame_request? | ||
| flash.now[:notice] = t(".success") | ||
| render turbo_stream: [ | ||
| turbo_stream.replace( | ||
| "ibkr-providers-panel", | ||
| partial: "settings/providers/ibkr_panel" | ||
| ), | ||
| *flash_notification_stream_items | ||
| ] | ||
| else | ||
| redirect_to accounts_path, notice: t(".success"), status: :see_other | ||
| end | ||
| else | ||
| @error_message = @ibkr_item.errors.full_messages.join(", ") | ||
|
|
||
| if turbo_frame_request? | ||
| render turbo_stream: turbo_stream.replace( | ||
| "ibkr-providers-panel", | ||
| partial: "settings/providers/ibkr_panel", | ||
| locals: { error_message: @error_message } | ||
| ), status: :unprocessable_entity | ||
| else | ||
| redirect_to settings_providers_path, alert: @error_message, status: :see_other | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def update | ||
| attrs = ibkr_item_params.to_h | ||
| attrs["query_id"] = @ibkr_item.query_id if attrs["query_id"].blank? | ||
| attrs["token"] = @ibkr_item.token if attrs["token"].blank? | ||
|
|
||
| if @ibkr_item.update(attrs.merge(status: :good)) | ||
| @ibkr_item.sync_later unless @ibkr_item.syncing? | ||
|
|
||
| if turbo_frame_request? | ||
| flash.now[:notice] = t(".success") | ||
| render turbo_stream: [ | ||
| turbo_stream.replace( | ||
| "ibkr-providers-panel", | ||
| partial: "settings/providers/ibkr_panel" | ||
| ), | ||
| *flash_notification_stream_items | ||
| ] | ||
| else | ||
| redirect_to accounts_path, notice: t(".success"), status: :see_other | ||
| end | ||
| else | ||
| @error_message = @ibkr_item.errors.full_messages.join(", ") | ||
|
|
||
| if turbo_frame_request? | ||
| render turbo_stream: turbo_stream.replace( | ||
| "ibkr-providers-panel", | ||
| partial: "settings/providers/ibkr_panel", | ||
| locals: { error_message: @error_message } | ||
| ), status: :unprocessable_entity | ||
| else | ||
| redirect_to settings_providers_path, alert: @error_message, status: :see_other | ||
| end | ||
| end | ||
| end | ||
|
|
||
| def destroy | ||
| begin | ||
| @ibkr_item.unlink_all!(dry_run: false) | ||
| rescue => e | ||
| Rails.logger.warn("IBKR unlink during destroy failed: #{e.class} - #{e.message}") | ||
| end | ||
|
|
||
| @ibkr_item.destroy_later | ||
| redirect_to settings_providers_path, notice: t(".success"), status: :see_other | ||
| end | ||
|
|
||
| def sync | ||
| @ibkr_item.sync_later unless @ibkr_item.syncing? | ||
|
|
||
| respond_to do |format| | ||
| format.html { redirect_back_or_to accounts_path } | ||
| format.json { head :ok } | ||
| end | ||
| end | ||
|
|
||
| def select_accounts | ||
| ibkr_item = current_ibkr_item | ||
| unless ibkr_item | ||
| redirect_to settings_providers_path, alert: t(".not_configured") | ||
| return | ||
| end | ||
|
|
||
| redirect_to setup_accounts_ibkr_item_path(ibkr_item) | ||
| end | ||
|
|
||
| def select_existing_account | ||
| @account = Current.family.accounts.find(params[:account_id]) | ||
| @available_ibkr_accounts = Current.family.ibkr_items | ||
| .includes(ibkr_accounts: { account_provider: :account }) | ||
| .flat_map(&:ibkr_accounts) | ||
| .select { |ibkr_account| ibkr_account.account_provider.nil? } | ||
| .sort_by { |ibkr_account| ibkr_account.updated_at || ibkr_account.created_at } | ||
| .reverse | ||
|
|
||
| render :select_existing_account, layout: false | ||
| end | ||
|
|
||
| def link_existing_account | ||
| account = Current.family.accounts.find_by(id: params[:account_id]) | ||
| ibkr_account = Current.family.ibkr_items | ||
| .joins(:ibkr_accounts) | ||
| .where(ibkr_accounts: { id: params[:ibkr_account_id] }) | ||
| .first | ||
| &.ibkr_accounts | ||
| &.find_by(id: params[:ibkr_account_id]) | ||
|
|
||
| if account.blank? || ibkr_account.blank? | ||
| redirect_to settings_providers_path, alert: t(".not_found") | ||
| return | ||
| end | ||
|
|
||
| if account.accountable_type != "Investment" || account.account_providers.any? || account.plaid_account_id.present? || account.simplefin_account_id.present? | ||
| redirect_to account_path(account), alert: t(".only_manual_investment") | ||
| return | ||
| end | ||
|
|
||
| provider = nil | ||
|
|
||
| ibkr_account.with_lock do | ||
| if ibkr_account.current_account.present? | ||
| redirect_to account_path(account), alert: t(".already_linked") | ||
| return | ||
| end | ||
|
|
||
| provider = ibkr_account.ensure_account_provider!(account) | ||
| end | ||
|
|
||
| raise "Failed to create AccountProvider link" unless provider | ||
|
|
||
| begin | ||
| IbkrAccount::Processor.new(ibkr_account.reload).process | ||
| rescue => e | ||
| Rails.logger.error("Failed to process linked IBKR account #{ibkr_account.id}: #{e.class} - #{e.message}") | ||
| end | ||
|
|
||
| ibkr_account.ibkr_item.sync_later unless ibkr_account.ibkr_item.syncing? | ||
| redirect_to account_path(account), notice: t(".success"), status: :see_other | ||
| rescue => e | ||
| Rails.logger.error("Failed to link existing IBKR account: #{e.class} - #{e.message}") | ||
| redirect_to settings_providers_path, alert: t(".failed"), status: :see_other | ||
| end | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| def setup_accounts | ||
| @ibkr_accounts = @ibkr_item.ibkr_accounts.includes(account_provider: :account) | ||
| @linked_accounts = @ibkr_accounts.select { |ibkr_account| ibkr_account.current_account.present? } | ||
| @unlinked_accounts = @ibkr_accounts.reject { |ibkr_account| ibkr_account.current_account.present? } | ||
|
|
||
| no_accounts = @linked_accounts.blank? && @unlinked_accounts.blank? | ||
| latest_sync = @ibkr_item.syncs.ordered.first | ||
| should_sync = latest_sync.nil? || !latest_sync.completed? | ||
|
|
||
| if no_accounts && !@ibkr_item.syncing? && should_sync | ||
| @ibkr_item.sync_later | ||
| end | ||
|
|
||
| @linkable_accounts = Current.family.accounts | ||
| .visible | ||
| .where(accountable_type: "Investment") | ||
| .left_joins(:account_providers) | ||
| .where(account_providers: { id: nil }) | ||
| .order(:name) | ||
|
|
||
| @syncing = @ibkr_item.syncing? | ||
| @waiting_for_sync = no_accounts && @syncing | ||
| @no_accounts_found = no_accounts && !@syncing && @ibkr_item.last_synced_at.present? | ||
| end | ||
|
|
||
| def complete_account_setup | ||
| selected_accounts = Array(params[:account_ids]).reject(&:blank?) | ||
| created_accounts = [] | ||
|
|
||
| selected_accounts.each do |ibkr_account_id| | ||
| ibkr_account = @ibkr_item.ibkr_accounts.find_by(id: ibkr_account_id) | ||
| next unless ibkr_account | ||
|
|
||
| ibkr_account.with_lock do | ||
| next if ibkr_account.current_account.present? | ||
|
|
||
| account = Account.create_from_ibkr_account(ibkr_account) | ||
| ibkr_account.ensure_account_provider!(account) | ||
| created_accounts << account | ||
| end | ||
|
|
||
| begin | ||
| IbkrAccount::Processor.new(ibkr_account.reload).process | ||
| rescue => e | ||
| Rails.logger.error("Failed to process IBKR account #{ibkr_account.id} after setup: #{e.class} - #{e.message}") | ||
| end | ||
| end | ||
|
|
||
| @ibkr_item.update!(pending_account_setup: @ibkr_item.unlinked_accounts_count.positive?) | ||
| @ibkr_item.sync_later if created_accounts.any? | ||
|
|
||
| if created_accounts.any? | ||
| redirect_to accounts_path, notice: t(".success", count: created_accounts.count), status: :see_other | ||
| elsif selected_accounts.empty? | ||
| redirect_to setup_accounts_ibkr_item_path(@ibkr_item), alert: t(".none_selected"), status: :see_other | ||
| else | ||
| redirect_to setup_accounts_ibkr_item_path(@ibkr_item), alert: t(".none_created"), status: :see_other | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def set_ibkr_item | ||
| @ibkr_item = Current.family.ibkr_items.find(params[:id]) | ||
| end | ||
|
|
||
| def current_ibkr_item | ||
| active_items = Current.family.ibkr_items.active | ||
|
|
||
| active_items.syncable.ordered.first || active_items.ordered.first | ||
| end | ||
|
|
||
| def ibkr_item_params | ||
| params.require(:ibkr_item).permit(:name, :query_id, :token) | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.