feat: scope Mercury account uniqueness to mercury_item (#1032)

* feat: scope Mercury account uniqueness to mercury_item

* feat: extend to all other providers

* fix: add uniqueness test

* fix: lint

* fix: test

* fix: coderabbit comment

* fix: coderabbit comment

* fix: coderabbit comment

* fix: update

* fix: lint

* fix: update

* fix: update
This commit is contained in:
Clayton
2026-03-19 09:17:55 -05:00
committed by GitHub
parent b68c767b34
commit 1191d9f7d8
17 changed files with 461 additions and 117 deletions

View File

@@ -15,6 +15,7 @@ class CoinbaseAccount < ApplicationRecord
has_one :linked_account, through: :account_provider, source: :account
validates :name, :currency, presence: true
validates :account_id, uniqueness: { scope: :coinbase_item_id, allow_nil: true }
# Helper to get account using account_providers system
def current_account

View File

@@ -16,6 +16,7 @@ class EnableBankingAccount < ApplicationRecord
validates :name, :currency, presence: true
validates :uid, presence: true, uniqueness: { scope: :enable_banking_item_id }
# account_id is not uniquely scoped: uid already enforces one-account-per-identifier per item
# Helper to get account using account_providers system
def current_account

View File

@@ -12,6 +12,7 @@ class IndexaCapitalAccount < ApplicationRecord
has_one :linked_account, through: :account_provider, source: :account
validates :name, :currency, presence: true
validates :indexa_capital_account_id, uniqueness: { scope: :indexa_capital_item_id, allow_nil: true }
# Scopes
scope :with_linked, -> { joins(:account_provider) }

View File

@@ -15,6 +15,7 @@ class LunchflowAccount < ApplicationRecord
has_one :linked_account, through: :account_provider, source: :account
validates :name, :currency, presence: true
validates :account_id, uniqueness: { scope: :lunchflow_item_id, allow_nil: true }
# Helper to get account using account_providers system
def current_account

View File

@@ -15,6 +15,7 @@ class MercuryAccount < ApplicationRecord
has_one :linked_account, through: :account_provider, source: :account
validates :name, :currency, presence: true
validates :account_id, uniqueness: { scope: :mercury_item_id }
# Helper to get account using account_providers system
def current_account

View File

@@ -19,6 +19,7 @@ class PlaidAccount < ApplicationRecord
has_one :linked_account, through: :account_provider, source: :account
validates :name, :plaid_type, :currency, presence: true
validates :plaid_id, uniqueness: { scope: :plaid_item_id }
validate :has_balance
# Helper to get account using new system first, falling back to legacy

View File

@@ -17,6 +17,8 @@ class SnaptradeAccount < ApplicationRecord
has_one :linked_account, through: :account_provider, source: :account
validates :name, :currency, presence: true
validates :account_id, uniqueness: { scope: :snaptrade_item_id, allow_nil: true }
validates :snaptrade_account_id, uniqueness: { scope: :snaptrade_item_id, allow_nil: true }
# Enqueue cleanup job after destruction to avoid blocking transaction with API call
after_destroy :enqueue_connection_cleanup