mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-02 20:15:22 +02:00
Add RejectedTransfer model, simplify auto matching (#1690)
* Allow transfers to match when inflow is after outflow * Simplify transfer auto matching with RejectedTransfer model * Validations * Reset migrations
This commit is contained in:
parent
0b4e314f58
commit
de90b29201
18 changed files with 221 additions and 79 deletions
|
@ -1,7 +1,7 @@
|
|||
require "test_helper"
|
||||
|
||||
class AccountTest < ActiveSupport::TestCase
|
||||
include SyncableInterfaceTest
|
||||
include SyncableInterfaceTest, Account::EntriesTestHelper
|
||||
|
||||
setup do
|
||||
@account = @syncable = accounts(:depository)
|
||||
|
@ -65,4 +65,38 @@ class AccountTest < ActiveSupport::TestCase
|
|||
assert_equal 0, @account.series(currency: "NZD").values.count
|
||||
end
|
||||
end
|
||||
|
||||
test "auto-matches transfers" do
|
||||
outflow_entry = create_transaction(date: 1.day.ago.to_date, account: @account, amount: 500)
|
||||
inflow_entry = create_transaction(date: Date.current, account: accounts(:credit_card), amount: -500)
|
||||
|
||||
assert_difference -> { Transfer.count } => 1 do
|
||||
@account.auto_match_transfers!
|
||||
end
|
||||
end
|
||||
|
||||
# In this scenario, our matching logic should find 4 potential matches. These matches should be ranked based on
|
||||
# days apart, then de-duplicated so that we aren't auto-matching the same transaction across multiple transfers.
|
||||
test "when 2 options exist, only auto-match one at a time, ranked by days apart" do
|
||||
yesterday_outflow = create_transaction(date: 1.day.ago.to_date, account: @account, amount: 500)
|
||||
yesterday_inflow = create_transaction(date: 1.day.ago.to_date, account: accounts(:credit_card), amount: -500)
|
||||
|
||||
today_outflow = create_transaction(date: Date.current, account: @account, amount: 500)
|
||||
today_inflow = create_transaction(date: Date.current, account: accounts(:credit_card), amount: -500)
|
||||
|
||||
assert_difference -> { Transfer.count } => 2 do
|
||||
@account.auto_match_transfers!
|
||||
end
|
||||
end
|
||||
|
||||
test "does not auto-match any transfers that have been rejected by user already" do
|
||||
outflow = create_transaction(date: Date.current, account: @account, amount: 500)
|
||||
inflow = create_transaction(date: Date.current, account: accounts(:credit_card), amount: -500)
|
||||
|
||||
RejectedTransfer.create!(inflow_transaction_id: inflow.entryable_id, outflow_transaction_id: outflow.entryable_id)
|
||||
|
||||
assert_no_difference -> { Transfer.count } do
|
||||
@account.auto_match_transfers!
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -14,15 +14,6 @@ class TransferTest < ActiveSupport::TestCase
|
|||
end
|
||||
end
|
||||
|
||||
test "auto matches transfers" do
|
||||
outflow_entry = create_transaction(date: 1.day.ago.to_date, account: accounts(:depository), amount: 500)
|
||||
inflow_entry = create_transaction(date: Date.current, account: accounts(:credit_card), amount: -500)
|
||||
|
||||
assert_difference -> { Transfer.count } => 1 do
|
||||
Transfer.auto_match_for_account(accounts(:depository))
|
||||
end
|
||||
end
|
||||
|
||||
test "transfer has different accounts, opposing amounts, and within 4 days of each other" do
|
||||
outflow_entry = create_transaction(date: 1.day.ago.to_date, account: accounts(:depository), amount: 500)
|
||||
inflow_entry = create_transaction(date: Date.current, account: accounts(:credit_card), amount: -500)
|
||||
|
@ -131,4 +122,16 @@ class TransferTest < ActiveSupport::TestCase
|
|||
transfer.save!
|
||||
end
|
||||
end
|
||||
|
||||
test "transaction can only belong to one transfer" do
|
||||
outflow_entry = create_transaction(date: Date.current, account: accounts(:depository), amount: 500)
|
||||
inflow_entry1 = create_transaction(date: Date.current, account: accounts(:credit_card), amount: -500)
|
||||
inflow_entry2 = create_transaction(date: Date.current, account: accounts(:credit_card), amount: -500)
|
||||
|
||||
Transfer.create!(inflow_transaction: inflow_entry1.account_transaction, outflow_transaction: outflow_entry.account_transaction)
|
||||
|
||||
assert_raises ActiveRecord::RecordInvalid do
|
||||
Transfer.create!(inflow_transaction: inflow_entry2.account_transaction, outflow_transaction: outflow_entry.account_transaction)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue