The personal finance app for everyone (by everyone)
Ruby
Keep rule-assigned categories on Trade Republic cash deposits (#3791)
Niels Eulink095fa56e9094
diff --git a/app/models/trade_republic_account/activities_processor.rb b/app/models/trade_republic_account/activities_processor.rbindex 48353541ce..1ea357958a 100644--- a/app/models/trade_republic_account/activities_processor.rb+++ b/app/models/trade_republic_account/activities_processor.rb@@ -334,6 +334,7 @@ class TradeRepublicAccount::ActivitiesProcessoramount = parse_decimal(detail[:amount])return false unless amount && !amount.zero?+ legacy_contribution = legacy_contribution_external_ids.include?(external_id)entry = import_adapter.import_transaction(external_id: external_id,# The normalized category is the source of truth for direction. TR@@ -362,7 +363,7 @@ class TradeRepublicAccount::ActivitiesProcessor}.compact})- reset_legacy_contribution!(entry, event)+ reset_legacy_contribution!(entry, event) if legacy_contributiontrueend@@ -375,21 +376,34 @@ class TradeRepublicAccount::ActivitiesProcessorend# Earlier syncs stored cash-account deposits as investment contributions.- # The adapter neither downgrades that kind nor clears the category and- # label it assigned with it, so reset untouched inflows outside a matched- # transfer.+ # The adapter never clears the category and label it assigned with that+ # kind, so reset untouched inflows outside a matched transfer. Callers only+ # pass rows stored with that kind: a family can assign the same category or+ # label through a Rule, which must survive a sync.def reset_legacy_contribution!(entry, event)- return unless @trade_republic_account.cash?-transaction = entry&.entryablereturn unless transaction.is_a?(Transaction) && entry.amount.negative?return if entry.protected_from_sync? || transaction.transfer.present?- attrs = {}- attrs[:kind] = transfer_event?(event) ? "funds_movement" : "standard" if transaction.investment_contribution?+ attrs = { kind: transfer_event?(event) ? "funds_movement" : "standard" }attrs[:category_id] = nil if investment_contribution_category_ids.include?(transaction.category_id)attrs[:investment_activity_label] = nil if transaction.investment_activity_label == "Contribution"- transaction.update!(attrs) if attrs.any?+ transaction.update!(attrs)+ end++ # Read before the first cash movement is imported: the adapter overwrites+ # the kind of transfer events, so the stored kind is gone afterwards.+ def legacy_contribution_external_ids+ @legacy_contribution_external_ids ||=+ if @trade_republic_account.cash?+ Transaction.investment_contribution+ .joins(:entry)+ .where(entries: { account_id: account.id, source: "trade_republic" })+ .pluck("entries.external_id")+ .to_set+ else+ Set.new+ endenddef investment_contribution_category_idsdiff --git a/test/models/trade_republic_account_activities_processor_test.rb b/test/models/trade_republic_account_activities_processor_test.rbindex 4df30dd523..2f73b4e53b 100644--- a/test/models/trade_republic_account_activities_processor_test.rb+++ b/test/models/trade_republic_account_activities_processor_test.rb@@ -850,6 +850,28 @@ class TradeRepublicAccountActivitiesProcessorTest < ActiveSupport::TestCaseassert_nil legacy.transaction.investment_activity_labelend+ test "keeps a rule-assigned investment contributions category and label on cash account deposits" do+ cash_account, cash_sure = create_linked_cash_account!+ category = @family.investment_contributions_category+ cash_account.update!(raw_timeline_payload: [+ deposit_event.merge(id: "evt_rule_dep", eventType: "BANK_TRANSACTION_INCOMING", title: "Employer")+ ])+ TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process++ transaction = cash_sure.entries.find_by!(external_id: "trade_republic_event_evt_rule_dep").transaction+ assert_equal "standard", transaction.kind+ assert transaction.enrich_attribute(:category_id, category.id, source: "rule")+ assert transaction.enrich_attribute(:investment_activity_label, "Contribution", source: "rule")+ assert_not transaction.entry.reload.protected_from_sync?++ TradeRepublicAccount::ActivitiesProcessor.new(cash_account.reload).process++ transaction.reload+ assert_equal "standard", transaction.kind+ assert_equal category.id, transaction.category_id+ assert_equal "Contribution", transaction.investment_activity_label+ end+test "savings-plan invoice imports as a portfolio trade and a cash outflow" docash_account, cash_sure = create_linked_cash_account!event = savings_plan_invoice_event