The personal finance app for everyone (by everyone)
Ruby
fix(snaptrade): sign bare TRANSFER activities by provider direction (#2792)
Erkan Doğan32ab402d3824
diff --git a/app/models/snaptrade_account/activities_processor.rb b/app/models/snaptrade_account/activities_processor.rbindex 96b35cbb42..bcce376296 100644--- a/app/models/snaptrade_account/activities_processor.rb+++ b/app/models/snaptrade_account/activities_processor.rb@@ -250,6 +250,15 @@ class SnaptradeAccount::ActivitiesProcessoramount.abs # Money out should be positive in Surewhen "CONTRIBUTION", "TRANSFER_IN", "DIVIDEND", "DIV", "INTEREST", "CASH"-amount.abs # Money in should be negative in Sure+ when "TRANSFER"+ # Direction is not encoded in the type (unlike TRANSFER_IN/TRANSFER_OUT), so the+ # provider's sign is the only directional signal available. SnapTrade signs these+ # from the account's perspective (positive = money in), which is the inverse of+ # Sure's convention, so invert it rather than passing it through unchanged.+ # Passing it through stored a 401k contribution as a positive amount, which+ # Entry#classification reads as an expense and ReverseCalculator reads as a+ # value decrease — a correct current balance with a declining history (issue #2756).+ -amountelseamountenddiff --git a/test/models/snaptrade_account/activities_processor_test.rb b/test/models/snaptrade_account/activities_processor_test.rbindex 2c5639952c..b4b8419c05 100644--- a/test/models/snaptrade_account/activities_processor_test.rb+++ b/test/models/snaptrade_account/activities_processor_test.rb@@ -161,6 +161,37 @@ class SnaptradeAccount::ActivitiesProcessorTest < ActiveSupport::TestCaseassert_equal "Transfer", transfer_out.entryable.investment_activity_labelend+ test "normalizes bare TRANSFER from the provider sign" do+ # Regression test for issue #2756. Unlike TRANSFER_IN/TRANSFER_OUT, a bare "TRANSFER"+ # carries no direction in the type, so SnapTrade's sign is the only directional signal+ # available and must be inverted into Sure's convention rather than passed through.+ @snaptrade_account.update!(raw_activities_payload: [+ build_cash_activity(+ id: "transfer_generic_in",+ type: "TRANSFER",+ amount: 1320.75,+ settlement_date: Date.current.to_s+ ),+ build_cash_activity(+ id: "transfer_generic_out",+ type: "TRANSFER",+ amount: -500.00,+ settlement_date: Date.current.to_s+ )+ ])++ processor = SnaptradeAccount::ActivitiesProcessor.new(@snaptrade_account)+ processor.process++ inbound = @account.entries.find_by(external_id: "transfer_generic_in", source: "snaptrade")+ outbound = @account.entries.find_by(external_id: "transfer_generic_out", source: "snaptrade")++ assert_not_nil inbound+ assert_not_nil outbound+ assert_equal(-1320.75, inbound.amount.to_f, "money in must be stored negative on an asset account")+ assert_equal 500.00, outbound.amount.to_f, "money out must be stored positive on an asset account"+ end+test "maps all known activity types correctly" dotype_mappings = {"BUY" => "Buy",