mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-23 07:09:39 +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
|
@ -38,11 +38,14 @@ class TransfersController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
Transfer.transaction do
|
if transfer_update_params[:status] == "rejected"
|
||||||
@transfer.update!(transfer_update_params.except(:category_id))
|
@transfer.reject!
|
||||||
@transfer.outflow_transaction.update!(category_id: transfer_update_params[:category_id])
|
elsif transfer_update_params[:status] == "confirmed"
|
||||||
|
@transfer.confirm!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@transfer.outflow_transaction.update!(category_id: transfer_update_params[:category_id])
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html { redirect_back_or_to transactions_url, notice: t(".success") }
|
format.html { redirect_back_or_to transactions_url, notice: t(".success") }
|
||||||
format.turbo_stream
|
format.turbo_stream
|
||||||
|
|
|
@ -159,4 +159,62 @@ class Account < ApplicationRecord
|
||||||
entryable: Account::Valuation.new
|
entryable: Account::Valuation.new
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def transfer_match_candidates
|
||||||
|
Account::Entry.select([
|
||||||
|
"inflow_candidates.entryable_id as inflow_transaction_id",
|
||||||
|
"outflow_candidates.entryable_id as outflow_transaction_id",
|
||||||
|
"ABS(inflow_candidates.date - outflow_candidates.date) as date_diff"
|
||||||
|
]).from("account_entries inflow_candidates")
|
||||||
|
.joins("
|
||||||
|
JOIN account_entries outflow_candidates ON (
|
||||||
|
inflow_candidates.amount < 0 AND
|
||||||
|
outflow_candidates.amount > 0 AND
|
||||||
|
inflow_candidates.amount = -outflow_candidates.amount AND
|
||||||
|
inflow_candidates.currency = outflow_candidates.currency AND
|
||||||
|
inflow_candidates.account_id <> outflow_candidates.account_id AND
|
||||||
|
inflow_candidates.date BETWEEN outflow_candidates.date - 4 AND outflow_candidates.date + 4
|
||||||
|
)
|
||||||
|
").joins("
|
||||||
|
LEFT JOIN transfers existing_transfers ON (
|
||||||
|
existing_transfers.inflow_transaction_id = inflow_candidates.entryable_id OR
|
||||||
|
existing_transfers.outflow_transaction_id = outflow_candidates.entryable_id
|
||||||
|
)
|
||||||
|
")
|
||||||
|
.joins("LEFT JOIN rejected_transfers ON (
|
||||||
|
rejected_transfers.inflow_transaction_id = inflow_candidates.entryable_id AND
|
||||||
|
rejected_transfers.outflow_transaction_id = outflow_candidates.entryable_id
|
||||||
|
)")
|
||||||
|
.joins("JOIN accounts inflow_accounts ON inflow_accounts.id = inflow_candidates.account_id")
|
||||||
|
.joins("JOIN accounts outflow_accounts ON outflow_accounts.id = outflow_candidates.account_id")
|
||||||
|
.where("inflow_accounts.family_id = ? AND outflow_accounts.family_id = ?", self.family_id, self.family_id)
|
||||||
|
.where("inflow_candidates.entryable_type = 'Account::Transaction' AND outflow_candidates.entryable_type = 'Account::Transaction'")
|
||||||
|
.where(existing_transfers: { id: nil })
|
||||||
|
.order("date_diff ASC") # Closest matches first
|
||||||
|
end
|
||||||
|
|
||||||
|
def auto_match_transfers!
|
||||||
|
# Exclude already matched transfers
|
||||||
|
candidates_scope = transfer_match_candidates.where(rejected_transfers: { id: nil })
|
||||||
|
|
||||||
|
# Track which transactions we've already matched to avoid duplicates
|
||||||
|
used_transaction_ids = Set.new
|
||||||
|
|
||||||
|
candidates = []
|
||||||
|
|
||||||
|
Transfer.transaction do
|
||||||
|
candidates_scope.each do |match|
|
||||||
|
next if used_transaction_ids.include?(match.inflow_transaction_id) ||
|
||||||
|
used_transaction_ids.include?(match.outflow_transaction_id)
|
||||||
|
|
||||||
|
Transfer.create!(
|
||||||
|
inflow_transaction_id: match.inflow_transaction_id,
|
||||||
|
outflow_transaction_id: match.outflow_transaction_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
used_transaction_ids << match.inflow_transaction_id
|
||||||
|
used_transaction_ids << match.outflow_transaction_id
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -77,12 +77,20 @@ class Account::Entry < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def transfer_match_candidates
|
def transfer_match_candidates
|
||||||
account.family.entries
|
candidates_scope = account.transfer_match_candidates
|
||||||
.where.not(account_id: account_id)
|
|
||||||
.where.not(id: id)
|
candidates_scope = if amount.negative?
|
||||||
.where(amount: -amount)
|
candidates_scope.where("inflow_candidates.entryable_id = ?", entryable_id)
|
||||||
.where(currency: currency)
|
else
|
||||||
.where(date: (date - 4.days)..(date + 4.days))
|
candidates_scope.where("outflow_candidates.entryable_id = ?", entryable_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
candidates_scope.map do |pm|
|
||||||
|
Transfer.new(
|
||||||
|
inflow_transaction_id: pm.inflow_transaction_id,
|
||||||
|
outflow_transaction_id: pm.outflow_transaction_id,
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
|
|
@ -5,7 +5,7 @@ class Account::Syncer
|
||||||
end
|
end
|
||||||
|
|
||||||
def run
|
def run
|
||||||
Transfer.auto_match_for_account(account)
|
account.auto_match_transfers!
|
||||||
|
|
||||||
holdings = sync_holdings
|
holdings = sync_holdings
|
||||||
balances = sync_balances(holdings)
|
balances = sync_balances(holdings)
|
||||||
|
|
|
@ -9,6 +9,10 @@ class Account::Transaction < ApplicationRecord
|
||||||
has_one :transfer_as_inflow, class_name: "Transfer", foreign_key: "inflow_transaction_id", dependent: :destroy
|
has_one :transfer_as_inflow, class_name: "Transfer", foreign_key: "inflow_transaction_id", dependent: :destroy
|
||||||
has_one :transfer_as_outflow, class_name: "Transfer", foreign_key: "outflow_transaction_id", dependent: :destroy
|
has_one :transfer_as_outflow, class_name: "Transfer", foreign_key: "outflow_transaction_id", dependent: :destroy
|
||||||
|
|
||||||
|
# We keep track of rejected transfers to avoid auto-matching them again
|
||||||
|
has_one :rejected_transfer_as_inflow, class_name: "RejectedTransfer", foreign_key: "inflow_transaction_id", dependent: :destroy
|
||||||
|
has_one :rejected_transfer_as_outflow, class_name: "RejectedTransfer", foreign_key: "outflow_transaction_id", dependent: :destroy
|
||||||
|
|
||||||
accepts_nested_attributes_for :taggings, allow_destroy: true
|
accepts_nested_attributes_for :taggings, allow_destroy: true
|
||||||
|
|
||||||
scope :active, -> { where(excluded: false) }
|
scope :active, -> { where(excluded: false) }
|
||||||
|
@ -24,6 +28,6 @@ class Account::Transaction < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def transfer?
|
def transfer?
|
||||||
transfer.present? && transfer.status != "rejected"
|
transfer.present?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
4
app/models/rejected_transfer.rb
Normal file
4
app/models/rejected_transfer.rb
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
class RejectedTransfer < ApplicationRecord
|
||||||
|
belongs_to :inflow_transaction, class_name: "Account::Transaction"
|
||||||
|
belongs_to :outflow_transaction, class_name: "Account::Transaction"
|
||||||
|
end
|
|
@ -2,13 +2,15 @@ class Transfer < ApplicationRecord
|
||||||
belongs_to :inflow_transaction, class_name: "Account::Transaction"
|
belongs_to :inflow_transaction, class_name: "Account::Transaction"
|
||||||
belongs_to :outflow_transaction, class_name: "Account::Transaction"
|
belongs_to :outflow_transaction, class_name: "Account::Transaction"
|
||||||
|
|
||||||
enum :status, { pending: "pending", confirmed: "confirmed", rejected: "rejected" }
|
enum :status, { pending: "pending", confirmed: "confirmed" }
|
||||||
|
|
||||||
|
validates :inflow_transaction_id, uniqueness: true
|
||||||
|
validates :outflow_transaction_id, uniqueness: true
|
||||||
|
|
||||||
validate :transfer_has_different_accounts
|
validate :transfer_has_different_accounts
|
||||||
validate :transfer_has_opposite_amounts
|
validate :transfer_has_opposite_amounts
|
||||||
validate :transfer_within_date_range
|
validate :transfer_within_date_range
|
||||||
validate :transfer_has_same_family
|
validate :transfer_has_same_family
|
||||||
validate :inflow_on_or_after_outflow
|
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def from_accounts(from_account:, to_account:, date:, amount:)
|
def from_accounts(from_account:, to_account:, date:, amount:)
|
||||||
|
@ -42,43 +44,17 @@ class Transfer < ApplicationRecord
|
||||||
status: "confirmed"
|
status: "confirmed"
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def auto_match_for_account(account)
|
def reject!
|
||||||
matches = Account::Entry.select([
|
|
||||||
"inflow_candidates.entryable_id as inflow_transaction_id",
|
|
||||||
"outflow_candidates.entryable_id as outflow_transaction_id"
|
|
||||||
]).from("account_entries inflow_candidates")
|
|
||||||
.joins("
|
|
||||||
JOIN account_entries outflow_candidates ON (
|
|
||||||
inflow_candidates.amount < 0 AND
|
|
||||||
outflow_candidates.amount > 0 AND
|
|
||||||
inflow_candidates.amount = -outflow_candidates.amount AND
|
|
||||||
inflow_candidates.currency = outflow_candidates.currency AND
|
|
||||||
inflow_candidates.account_id <> outflow_candidates.account_id AND
|
|
||||||
inflow_candidates.date BETWEEN outflow_candidates.date - 4 AND outflow_candidates.date + 4 AND
|
|
||||||
inflow_candidates.date >= outflow_candidates.date
|
|
||||||
)
|
|
||||||
").joins("
|
|
||||||
LEFT JOIN transfers existing_transfers ON (
|
|
||||||
existing_transfers.inflow_transaction_id = inflow_candidates.entryable_id OR
|
|
||||||
existing_transfers.outflow_transaction_id = outflow_candidates.entryable_id
|
|
||||||
)
|
|
||||||
")
|
|
||||||
.joins("JOIN accounts inflow_accounts ON inflow_accounts.id = inflow_candidates.account_id")
|
|
||||||
.joins("JOIN accounts outflow_accounts ON outflow_accounts.id = outflow_candidates.account_id")
|
|
||||||
.where("inflow_accounts.family_id = ? AND outflow_accounts.family_id = ?", account.family_id, account.family_id)
|
|
||||||
.where("inflow_candidates.entryable_type = 'Account::Transaction' AND outflow_candidates.entryable_type = 'Account::Transaction'")
|
|
||||||
.where(existing_transfers: { id: nil })
|
|
||||||
|
|
||||||
Transfer.transaction do
|
Transfer.transaction do
|
||||||
matches.each do |match|
|
RejectedTransfer.find_or_create_by!(inflow_transaction_id: inflow_transaction_id, outflow_transaction_id: outflow_transaction_id)
|
||||||
Transfer.create!(
|
destroy!
|
||||||
inflow_transaction_id: match.inflow_transaction_id,
|
|
||||||
outflow_transaction_id: match.outflow_transaction_id,
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def confirm!
|
||||||
|
update!(status: "confirmed")
|
||||||
end
|
end
|
||||||
|
|
||||||
def sync_account_later
|
def sync_account_later
|
||||||
|
@ -119,11 +95,6 @@ class Transfer < ApplicationRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def inflow_on_or_after_outflow
|
|
||||||
return unless inflow_transaction.present? && outflow_transaction.present?
|
|
||||||
errors.add(:base, :inflow_must_be_on_or_after_outflow) if inflow_transaction.entry.date < outflow_transaction.entry.date
|
|
||||||
end
|
|
||||||
|
|
||||||
def transfer_has_different_accounts
|
def transfer_has_different_accounts
|
||||||
return unless inflow_transaction.present? && outflow_transaction.present?
|
return unless inflow_transaction.present? && outflow_transaction.present?
|
||||||
errors.add(:base, :must_be_from_different_accounts) if inflow_transaction.entry.account == outflow_transaction.entry.account
|
errors.add(:base, :must_be_from_different_accounts) if inflow_transaction.entry.account == outflow_transaction.entry.account
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<%# locals: (entry:) %>
|
<%# locals: (entry:) %>
|
||||||
|
|
||||||
<div id="<%= dom_id(entry, "category_menu") %>">
|
<div id="<%= dom_id(entry, "category_menu") %>">
|
||||||
<% if entry.account_transaction.transfer&.categorizable? || entry.account_transaction.transfer.nil? || entry.account_transaction.transfer&.rejected? %>
|
<% if entry.account_transaction.transfer&.categorizable? || entry.account_transaction.transfer.nil? %>
|
||||||
<%= render "categories/menu", transaction: entry.account_transaction %>
|
<%= render "categories/menu", transaction: entry.account_transaction %>
|
||||||
<% else %>
|
<% else %>
|
||||||
<%= render "categories/badge", category: entry.account_transaction.transfer&.payment? ? payment_category : transfer_category %>
|
<%= render "categories/badge", category: entry.account_transaction.transfer&.payment? ? payment_category : transfer_category %>
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
<%= button_to transfer_path(entry.account_transaction.transfer, transfer: { status: "rejected" }),
|
<%= button_to transfer_path(entry.account_transaction.transfer, transfer: { status: "rejected" }),
|
||||||
method: :patch,
|
method: :patch,
|
||||||
|
data: { turbo: false },
|
||||||
class: "text-gray-500 hover:text-gray-800 flex items-center justify-center",
|
class: "text-gray-500 hover:text-gray-800 flex items-center justify-center",
|
||||||
title: "Reject match" do %>
|
title: "Reject match" do %>
|
||||||
<%= lucide_icon "x", class: "w-4 h-4 text-gray-400 hover:text-gray-600" %>
|
<%= lucide_icon "x", class: "w-4 h-4 text-gray-400 hover:text-gray-600" %>
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
) %>
|
) %>
|
||||||
<% else %>
|
<% else %>
|
||||||
<%= render "account/transfer_matches/matching_fields",
|
<%= render "account/transfer_matches/matching_fields",
|
||||||
form: f, entry: @entry, candidates: @transfer_match_candidates, accounts: @accounts %>
|
form: f, entry: @entry, candidates: @transfer_match_candidates.map { |pm| pm.outflow_transaction.entry }, accounts: @accounts %>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
@ -50,7 +50,7 @@
|
||||||
) %>
|
) %>
|
||||||
<% else %>
|
<% else %>
|
||||||
<%= render "account/transfer_matches/matching_fields",
|
<%= render "account/transfer_matches/matching_fields",
|
||||||
form: f, entry: @entry, candidates: @transfer_match_candidates, accounts: @accounts %>
|
form: f, entry: @entry, candidates: @transfer_match_candidates.map { |pm| pm.inflow_transaction.entry }, accounts: @accounts %>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
|
@ -24,10 +24,6 @@
|
||||||
<span title="<%= transfer.payment? ? "Payment" : "Transfer" %> is confirmed">
|
<span title="<%= transfer.payment? ? "Payment" : "Transfer" %> is confirmed">
|
||||||
<%= lucide_icon "link-2", class: "w-4 h-4 text-indigo-600" %>
|
<%= lucide_icon "link-2", class: "w-4 h-4 text-indigo-600" %>
|
||||||
</span>
|
</span>
|
||||||
<% elsif transfer.status == "rejected" %>
|
|
||||||
<span class="inline-flex items-center rounded-full bg-red-50 px-2 py-0.5 text-xs font-medium text-red-700">
|
|
||||||
Rejected
|
|
||||||
</span>
|
|
||||||
<% else %>
|
<% else %>
|
||||||
<span class="inline-flex items-center rounded-full bg-indigo-50 px-2 py-0.5 text-xs font-medium text-indigo-700">
|
<span class="inline-flex items-center rounded-full bg-indigo-50 px-2 py-0.5 text-xs font-medium text-indigo-700">
|
||||||
Auto-matched
|
Auto-matched
|
||||||
|
@ -42,6 +38,7 @@
|
||||||
|
|
||||||
<%= button_to transfer_path(transfer, transfer: { status: "rejected" }),
|
<%= button_to transfer_path(transfer, transfer: { status: "rejected" }),
|
||||||
method: :patch,
|
method: :patch,
|
||||||
|
data: { turbo: false },
|
||||||
class: "text-gray-500 hover:text-gray-800 flex items-center justify-center",
|
class: "text-gray-500 hover:text-gray-800 flex items-center justify-center",
|
||||||
title: "Reject match" do %>
|
title: "Reject match" do %>
|
||||||
<%= lucide_icon "x", class: "w-4 h-4 text-gray-400 hover:text-gray-600" %>
|
<%= lucide_icon "x", class: "w-4 h-4 text-gray-400 hover:text-gray-600" %>
|
||||||
|
|
|
@ -1,17 +1,19 @@
|
||||||
<%= turbo_stream.replace @transfer %>
|
<% unless @transfer.destroyed? %>
|
||||||
|
<%= turbo_stream.replace @transfer %>
|
||||||
|
|
||||||
<%= turbo_stream.replace "category_menu_account_entry_#{@transfer.inflow_transaction.entry.id}",
|
<%= turbo_stream.replace "category_menu_account_entry_#{@transfer.inflow_transaction.entry.id}",
|
||||||
partial: "account/transactions/transaction_category",
|
partial: "account/transactions/transaction_category",
|
||||||
locals: { entry: @transfer.inflow_transaction.entry } %>
|
locals: { entry: @transfer.inflow_transaction.entry } %>
|
||||||
|
|
||||||
<%= turbo_stream.replace "category_menu_account_entry_#{@transfer.outflow_transaction.entry.id}",
|
<%= turbo_stream.replace "category_menu_account_entry_#{@transfer.outflow_transaction.entry.id}",
|
||||||
partial: "account/transactions/transaction_category",
|
partial: "account/transactions/transaction_category",
|
||||||
locals: { entry: @transfer.outflow_transaction.entry } %>
|
locals: { entry: @transfer.outflow_transaction.entry } %>
|
||||||
|
|
||||||
<%= turbo_stream.replace "transfer_match_account_entry_#{@transfer.inflow_transaction.entry.id}",
|
<%= turbo_stream.replace "transfer_match_account_entry_#{@transfer.inflow_transaction.entry.id}",
|
||||||
partial: "account/transactions/transfer_match",
|
partial: "account/transactions/transfer_match",
|
||||||
locals: { entry: @transfer.inflow_transaction.entry } %>
|
locals: { entry: @transfer.inflow_transaction.entry } %>
|
||||||
|
|
||||||
<%= turbo_stream.replace "transfer_match_account_entry_#{@transfer.outflow_transaction.entry.id}",
|
<%= turbo_stream.replace "transfer_match_account_entry_#{@transfer.outflow_transaction.entry.id}",
|
||||||
partial: "account/transactions/transfer_match",
|
partial: "account/transactions/transfer_match",
|
||||||
locals: { entry: @transfer.outflow_transaction.entry } %>
|
locals: { entry: @transfer.outflow_transaction.entry } %>
|
||||||
|
<% end %>
|
||||||
|
|
|
@ -13,7 +13,8 @@ en:
|
||||||
amounts
|
amounts
|
||||||
must_have_single_currency: Transfer must have a single currency
|
must_have_single_currency: Transfer must have a single currency
|
||||||
must_be_from_same_family: Transfer must be from the same family
|
must_be_from_same_family: Transfer must be from the same family
|
||||||
inflow_must_be_on_or_after_outflow: Inflow transaction must be on or after outflow transaction
|
inflow_cannot_be_in_multiple_transfers: Inflow transaction cannot be part of multiple transfers
|
||||||
|
outflow_cannot_be_in_multiple_transfers: Outflow transaction cannot be part of multiple transfers
|
||||||
transfer:
|
transfer:
|
||||||
name: Transfer to %{to_account}
|
name: Transfer to %{to_account}
|
||||||
payment_name: Payment to %{to_account}
|
payment_name: Payment to %{to_account}
|
||||||
|
|
44
db/migrate/20250124224316_create_rejected_transfers.rb
Normal file
44
db/migrate/20250124224316_create_rejected_transfers.rb
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
class CreateRejectedTransfers < ActiveRecord::Migration[7.2]
|
||||||
|
def change
|
||||||
|
create_table :rejected_transfers, id: :uuid do |t|
|
||||||
|
t.references :inflow_transaction, null: false, foreign_key: { to_table: :account_transactions }, type: :uuid
|
||||||
|
t.references :outflow_transaction, null: false, foreign_key: { to_table: :account_transactions }, type: :uuid
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
|
||||||
|
add_index :rejected_transfers, [ :inflow_transaction_id, :outflow_transaction_id ], unique: true
|
||||||
|
|
||||||
|
reversible do |dir|
|
||||||
|
dir.up do
|
||||||
|
execute <<~SQL
|
||||||
|
INSERT INTO rejected_transfers (inflow_transaction_id, outflow_transaction_id, created_at, updated_at)
|
||||||
|
SELECT
|
||||||
|
inflow_transaction_id,
|
||||||
|
outflow_transaction_id,
|
||||||
|
created_at,
|
||||||
|
updated_at
|
||||||
|
FROM transfers
|
||||||
|
WHERE status = 'rejected'
|
||||||
|
SQL
|
||||||
|
|
||||||
|
execute <<~SQL
|
||||||
|
DELETE FROM transfers
|
||||||
|
WHERE status = 'rejected'
|
||||||
|
SQL
|
||||||
|
end
|
||||||
|
|
||||||
|
dir.down do
|
||||||
|
execute <<~SQL
|
||||||
|
INSERT INTO transfers (inflow_transaction_id, outflow_transaction_id, status, created_at, updated_at)
|
||||||
|
SELECT
|
||||||
|
inflow_transaction_id,
|
||||||
|
outflow_transaction_id,
|
||||||
|
'rejected',
|
||||||
|
created_at,
|
||||||
|
updated_at
|
||||||
|
FROM rejected_transfers
|
||||||
|
SQL
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
14
db/schema.rb
generated
14
db/schema.rb
generated
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema[7.2].define(version: 2025_01_20_210449) do
|
ActiveRecord::Schema[7.2].define(version: 2025_01_24_224316) do
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "pgcrypto"
|
enable_extension "pgcrypto"
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
|
@ -527,6 +527,16 @@ ActiveRecord::Schema[7.2].define(version: 2025_01_20_210449) do
|
||||||
t.string "area_unit"
|
t.string "area_unit"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "rejected_transfers", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
|
||||||
|
t.uuid "inflow_transaction_id", null: false
|
||||||
|
t.uuid "outflow_transaction_id", null: false
|
||||||
|
t.datetime "created_at", null: false
|
||||||
|
t.datetime "updated_at", null: false
|
||||||
|
t.index ["inflow_transaction_id", "outflow_transaction_id"], name: "idx_on_inflow_transaction_id_outflow_transaction_id_412f8e7e26", unique: true
|
||||||
|
t.index ["inflow_transaction_id"], name: "index_rejected_transfers_on_inflow_transaction_id"
|
||||||
|
t.index ["outflow_transaction_id"], name: "index_rejected_transfers_on_outflow_transaction_id"
|
||||||
|
end
|
||||||
|
|
||||||
create_table "securities", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
|
create_table "securities", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
|
||||||
t.string "ticker"
|
t.string "ticker"
|
||||||
t.string "name"
|
t.string "name"
|
||||||
|
@ -691,6 +701,8 @@ ActiveRecord::Schema[7.2].define(version: 2025_01_20_210449) do
|
||||||
add_foreign_key "merchants", "families"
|
add_foreign_key "merchants", "families"
|
||||||
add_foreign_key "plaid_accounts", "plaid_items"
|
add_foreign_key "plaid_accounts", "plaid_items"
|
||||||
add_foreign_key "plaid_items", "families"
|
add_foreign_key "plaid_items", "families"
|
||||||
|
add_foreign_key "rejected_transfers", "account_transactions", column: "inflow_transaction_id"
|
||||||
|
add_foreign_key "rejected_transfers", "account_transactions", column: "outflow_transaction_id"
|
||||||
add_foreign_key "security_prices", "securities"
|
add_foreign_key "security_prices", "securities"
|
||||||
add_foreign_key "sessions", "impersonation_sessions", column: "active_impersonator_session_id"
|
add_foreign_key "sessions", "impersonation_sessions", column: "active_impersonator_session_id"
|
||||||
add_foreign_key "sessions", "users"
|
add_foreign_key "sessions", "users"
|
||||||
|
|
|
@ -25,8 +25,8 @@ class TransfersControllerTest < ActionDispatch::IntegrationTest
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "can destroy transfer" do
|
test "soft deletes transfer" do
|
||||||
assert_difference -> { Transfer.count } => -1, -> { Account::Transaction.count } => 0 do
|
assert_difference -> { Transfer.count }, -1 do
|
||||||
delete transfer_url(transfers(:one))
|
delete transfer_url(transfers(:one))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
require "test_helper"
|
require "test_helper"
|
||||||
|
|
||||||
class AccountTest < ActiveSupport::TestCase
|
class AccountTest < ActiveSupport::TestCase
|
||||||
include SyncableInterfaceTest
|
include SyncableInterfaceTest, Account::EntriesTestHelper
|
||||||
|
|
||||||
setup do
|
setup do
|
||||||
@account = @syncable = accounts(:depository)
|
@account = @syncable = accounts(:depository)
|
||||||
|
@ -65,4 +65,38 @@ class AccountTest < ActiveSupport::TestCase
|
||||||
assert_equal 0, @account.series(currency: "NZD").values.count
|
assert_equal 0, @account.series(currency: "NZD").values.count
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
@ -14,15 +14,6 @@ class TransferTest < ActiveSupport::TestCase
|
||||||
end
|
end
|
||||||
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
|
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)
|
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)
|
inflow_entry = create_transaction(date: Date.current, account: accounts(:credit_card), amount: -500)
|
||||||
|
@ -131,4 +122,16 @@ class TransferTest < ActiveSupport::TestCase
|
||||||
transfer.save!
|
transfer.save!
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue