Address remaining CodeRabbit comments from PR #267 #351 (#451)

* Address remaining CodeRabbit comments from PR #267

This commit addresses the remaining unresolved code review comments:

1. Fix down migration in drop_was_merged_from_transactions.rb
   - Add null: false, default: false constraints to match original column
   - Ensures proper rollback compatibility

2. Fix bare rescue in maps_helper.rb compute_duplicate_only_flag
   - Replace bare rescue with rescue StandardError => e
   - Add proper logging for debugging
   - Follows Ruby best practices by being explicit about exception handling

These changes improve code quality and follow Rails/Ruby best practices.

* Refactor `SimplefinItemsController` and add tests for balances sync and account relinking behavior

- Replaced direct sync execution with `SyncJob` for asynchronous handling of balances sync.
- Updated account relinking logic to prevent disabling accounts with other active provider links.
- Removed unused `compute_relink_candidates` method.
- Added tests to verify `balances` action enqueues `SyncJob` and relinking respects account-provider relationships.

* Refactor balances sync to use runtime-only `balances_only` flag

- Replaced persistent `sync_stats` usage with runtime `balances_only?` predicate via `define_singleton_method`.
- Updated `SimplefinItemsController` `balances` action to pass `balances_only` flag to `SyncJob`.
- Enhanced `SyncJob` to attach transient `balances_only?` flag for execution.
- Adjusted `SimplefinItem::Syncer` logic to rely on the runtime `balances_only?` method.
- Updated controller tests to validate runtime flag usage in `SyncJob`.

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Josh Waldrep <joshua.waldrep5+github@gmail.com>
This commit is contained in:
LPW
2025-12-15 03:47:16 -05:00
committed by GitHub
parent 92cf98551b
commit 4d3d9d10df
6 changed files with 110 additions and 119 deletions

View File

@@ -28,6 +28,83 @@ class SimplefinItemsControllerTest < ActionDispatch::IntegrationTest
assert_redirected_to accounts_path
end
test "balances enqueues SyncJob and returns sync id as JSON" do
# Expect a Sync to be enqueued via SyncJob
SyncJob.expects(:perform_later).with do |sync, opts|
sync.is_a?(Sync) && opts.is_a?(Hash) && opts[:balances_only] == true
end.once
post balances_simplefin_item_url(@simplefin_item, format: :json)
assert_response :success
body = JSON.parse(@response.body)
assert_equal true, body["ok"], "expected ok: true"
assert body["sync_id"].present?, "expected sync_id to be present"
end
test "relink does not disable a previously linked account that still has other provider links" do
# Create two manual accounts A and B
account_a = Account.create!(
family: @family,
name: "Manual A",
balance: 0,
currency: "USD",
accountable_type: "Depository",
accountable: Depository.create!(subtype: "checking")
)
account_b = Account.create!(
family: @family,
name: "Manual B",
balance: 0,
currency: "USD",
accountable_type: "Depository",
accountable: Depository.create!(subtype: "savings")
)
# Create a SimpleFIN account under the same item
sfa_primary = SimplefinAccount.create!(
simplefin_item: @simplefin_item,
name: "SF A",
account_id: "sf_a",
account_type: "depository",
currency: "USD",
current_balance: 0
)
# Link the primary SimpleFIN provider to account A via AccountProvider (legacy link cleared by action)
AccountProvider.create!(account: account_a, provider: sfa_primary)
# Also link a different provider TYPE (Plaid) to account A so it is NOT orphaned
plaid_item = PlaidItem.create!(family: @family, name: "Plaid Conn", access_token: "test-token", plaid_id: "test-plaid-id")
plaid_acct = PlaidAccount.create!(
plaid_item: plaid_item,
plaid_id: "test-plaid-acct",
name: "Plaid A",
plaid_type: "depository",
currency: "USD",
current_balance: 0
)
AccountProvider.create!(account: account_a, provider: plaid_acct)
# Perform relink: point sfa_primary at account B
post link_existing_account_simplefin_items_path, params: {
account_id: account_b.id,
simplefin_account_id: sfa_primary.id
}
assert_response :see_other
# Reload and assert: account A should still be enabled (not disabled) because it has another provider link
account_a.reload
assert account_a.account_providers.any?, "expected previous account to still have provider links"
refute account_a.disabled?, "previous account should not be disabled when still linked to other providers"
# And the AccountProvider for sfa_primary should now point to account B
ap = AccountProvider.find_by(provider: sfa_primary)
assert_equal account_b.id, ap.account_id
end
test "should get edit" do
@simplefin_item.update!(status: :requires_update)
get edit_simplefin_item_url(@simplefin_item)