Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

FIX: Refunds or credits incorrectly recorded as cc_payment (#3062)

Derek Brownea0aa6c562d0
diff --git a/app/models/account/provider_import_adapter.rb b/app/models/account/provider_import_adapter.rb
index 89c4194094..fb67a2a5fb 100644
--- a/app/models/account/provider_import_adapter.rb
+++ b/app/models/account/provider_import_adapter.rb
@@ -224,8 +224,6 @@ class Account::ProviderImportAdapter
auto_category = account.family.investment_contributions_category
elsif account.accountable_type == "Loan" && amount.negative?
auto_kind = "loan_payment"
- elsif account.accountable_type == "CreditCard" && amount.negative?
- auto_kind = "cc_payment"
end
auto_kind ||= kind.presence
diff --git a/app/models/brex_entry/processor.rb b/app/models/brex_entry/processor.rb
index 4e31fc46a2..e284e61358 100644
--- a/app/models/brex_entry/processor.rb
+++ b/app/models/brex_entry/processor.rb
@@ -26,6 +26,7 @@ class BrexEntry::Processor
date: date,
name: name,
source: "brex",
+ kind: transaction_kind,
merchant: merchant,
notes: notes,
extra: extra
@@ -84,6 +85,10 @@ class BrexEntry::Processor
note_parts.any? ? note_parts.join(" - ") : nil
end
+ def transaction_kind
+ "cc_payment" if brex_account.account_kind == "card" && data[:type] == "COLLECTION" && amount.negative?
+ end
+
def merchant
merchant_name = merchant_payload[:raw_descriptor].presence || merchant_payload[:name].presence
return @merchant if instance_variable_defined?(:@merchant)
diff --git a/test/models/brex_entry/processor_test.rb b/test/models/brex_entry/processor_test.rb
index aa36765dc6..5910a88f3e 100644
--- a/test/models/brex_entry/processor_test.rb
+++ b/test/models/brex_entry/processor_test.rb
@@ -43,6 +43,22 @@ class BrexEntry::ProcessorTest < ActiveSupport::TestCase
assert_equal "cc_payment", entry.transaction.kind
end
+ test "does not classify non-negative collection as card payment" do
+ entry = BrexEntry::Processor.new(card_transaction(id: "tx_collection_positive", amount: 50_00, type: "COLLECTION"), brex_account: @brex_account).process
+
+ assert_equal BigDecimal("50.0"), entry.amount
+ assert_equal "standard", entry.transaction.kind
+ end
+
+ test "does not classify non-card collection as card payment" do
+ @brex_account.update!(account_kind: "cash")
+
+ entry = BrexEntry::Processor.new(card_transaction(id: "tx_collection_cash", amount: -50_00, type: "COLLECTION"), brex_account: @brex_account).process
+
+ assert_equal BigDecimal("-50.0"), entry.amount
+ assert_equal "standard", entry.transaction.kind
+ end
+
test "is idempotent by external id and source" do
transaction = card_transaction(id: "tx_duplicate", amount: 12_34)