Add SnapTrade brokerage integration with full trade history support (#737)

* Introduce SnapTrade integration with models, migrations, views, and activity processing logic.

* Refactor SnapTrade activities processing: improve activity fetching flow, handle pending states, and update UI elements for enhanced user feedback.

* Update Brakeman ignore file to include intentional redirect for SnapTrade OAuth portal.

* Refactor SnapTrade models, views, and processing logic: add currency extraction helper, improve pending state handling, optimize migration checks, and enhance user feedback in UI.

* Remove encryption for SnapTrade `snaptrade_user_id`, as it is an identifier, not a secret.

* Introduce `SnaptradeConnectionCleanupJob` to asynchronously handle SnapTrade connection cleanup and improve i18n for SnapTrade item status messages.

* Update SnapTrade encryption: make `snaptrade_user_secret` non-deterministic to enhance security.

---------

Signed-off-by: Juan José Mata <juanjo.mata@gmail.com>
Co-authored-by: luckyPipewrench <luckypipewrench@proton.me>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
This commit is contained in:
LPW
2026-01-22 14:52:49 -05:00
committed by GitHub
parent 179552657c
commit a83f70425f
52 changed files with 4417 additions and 25 deletions

View File

@@ -0,0 +1,79 @@
class CreateSnaptradeItemsAndAccounts < ActiveRecord::Migration[7.2]
def change
# Create provider items table (stores per-family connection credentials)
create_table :snaptrade_items, id: :uuid, if_not_exists: true do |t|
t.references :family, null: false, foreign_key: true, type: :uuid
t.string :name
# Institution metadata
t.string :institution_id
t.string :institution_name
t.string :institution_domain
t.string :institution_url
t.string :institution_color
# Status and lifecycle
t.string :status, default: "good"
t.boolean :scheduled_for_deletion, default: false
t.boolean :pending_account_setup, default: false
# Sync settings
t.datetime :sync_start_date
t.datetime :last_synced_at
# Raw data storage
t.jsonb :raw_payload
t.jsonb :raw_institution_payload
# Provider-specific credential fields
t.string :client_id
t.string :consumer_key
t.string :snaptrade_user_id
t.string :snaptrade_user_secret
t.timestamps
end
add_index :snaptrade_items, :status unless index_exists?(:snaptrade_items, :status)
# Create provider accounts table (stores individual account data from provider)
create_table :snaptrade_accounts, id: :uuid, if_not_exists: true do |t|
t.references :snaptrade_item, null: false, foreign_key: true, type: :uuid
# Account identification
t.string :name
# SnapTrade-specific IDs (snaptrade_account_id is SnapTrade's UUID for this account)
t.string :snaptrade_account_id
t.string :snaptrade_authorization_id
t.string :account_number
t.string :brokerage_name
# Account details
t.string :currency
t.decimal :current_balance, precision: 19, scale: 4
t.decimal :cash_balance, precision: 19, scale: 4
t.string :account_status
t.string :account_type
t.string :provider
# Metadata and raw data
t.jsonb :institution_metadata
t.jsonb :raw_payload
t.jsonb :raw_transactions_payload
t.jsonb :raw_holdings_payload, default: []
t.jsonb :raw_activities_payload, default: []
# Sync tracking
t.datetime :last_holdings_sync
t.datetime :last_activities_sync
t.boolean :activities_fetch_pending, default: false
t.timestamps
end
unless index_exists?(:snaptrade_accounts, :snaptrade_account_id)
add_index :snaptrade_accounts, :snaptrade_account_id, unique: true
end
end
end

View File

@@ -0,0 +1,7 @@
class AddActivitiesFetchPendingToSnaptradeAccounts < ActiveRecord::Migration[7.2]
def change
unless column_exists?(:snaptrade_accounts, :activities_fetch_pending)
add_column :snaptrade_accounts, :activities_fetch_pending, :boolean, default: false
end
end
end

View File

@@ -0,0 +1,7 @@
class AddSyncStartDateToSnaptradeAccounts < ActiveRecord::Migration[7.2]
def change
unless column_exists?(:snaptrade_accounts, :sync_start_date)
add_column :snaptrade_accounts, :sync_start_date, :date
end
end
end

59
db/schema.rb generated
View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[7.2].define(version: 2026_01_21_101345) do
ActiveRecord::Schema[7.2].define(version: 2026_01_22_160000) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
@@ -1158,6 +1158,61 @@ ActiveRecord::Schema[7.2].define(version: 2026_01_21_101345) do
t.index ["status"], name: "index_simplefin_items_on_status"
end
create_table "snaptrade_accounts", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.uuid "snaptrade_item_id", null: false
t.string "name"
t.string "account_id"
t.string "snaptrade_account_id"
t.string "snaptrade_authorization_id"
t.string "account_number"
t.string "brokerage_name"
t.string "currency"
t.decimal "current_balance", precision: 19, scale: 4
t.decimal "cash_balance", precision: 19, scale: 4
t.string "account_status"
t.string "account_type"
t.string "provider"
t.jsonb "institution_metadata"
t.jsonb "raw_payload"
t.jsonb "raw_transactions_payload"
t.jsonb "raw_holdings_payload", default: []
t.jsonb "raw_activities_payload", default: []
t.datetime "last_holdings_sync"
t.datetime "last_activities_sync"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "activities_fetch_pending", default: false
t.date "sync_start_date"
t.index ["account_id"], name: "index_snaptrade_accounts_on_account_id", unique: true
t.index ["snaptrade_account_id"], name: "index_snaptrade_accounts_on_snaptrade_account_id", unique: true
t.index ["snaptrade_item_id"], name: "index_snaptrade_accounts_on_snaptrade_item_id"
end
create_table "snaptrade_items", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.uuid "family_id", null: false
t.string "name"
t.string "institution_id"
t.string "institution_name"
t.string "institution_domain"
t.string "institution_url"
t.string "institution_color"
t.string "status", default: "good"
t.boolean "scheduled_for_deletion", default: false
t.boolean "pending_account_setup", default: false
t.datetime "sync_start_date"
t.datetime "last_synced_at"
t.jsonb "raw_payload"
t.jsonb "raw_institution_payload"
t.string "client_id"
t.string "consumer_key"
t.string "snaptrade_user_id"
t.string "snaptrade_user_secret"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["family_id"], name: "index_snaptrade_items_on_family_id"
t.index ["status"], name: "index_snaptrade_items_on_status"
end
create_table "sso_audit_logs", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.uuid "user_id"
t.string "event_type", null: false
@@ -1428,6 +1483,8 @@ ActiveRecord::Schema[7.2].define(version: 2026_01_21_101345) do
add_foreign_key "sessions", "users"
add_foreign_key "simplefin_accounts", "simplefin_items"
add_foreign_key "simplefin_items", "families"
add_foreign_key "snaptrade_accounts", "snaptrade_items"
add_foreign_key "snaptrade_items", "families"
add_foreign_key "sso_audit_logs", "users"
add_foreign_key "subscriptions", "families"
add_foreign_key "syncs", "syncs", column: "parent_id"