1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-10 07:55:21 +02:00

Fix matching logic, tests

This commit is contained in:
Zach Gollwitzer 2025-06-20 12:49:02 -04:00
parent d48779a09f
commit ec7882b09c
6 changed files with 42 additions and 12 deletions

View file

@ -17,7 +17,8 @@ class TransactionsController < ApplicationController
.reverse_chronological
.includes(
{ entry: :account },
:category, :merchant, :tags
:category, :merchant, :tags,
:transfer_as_inflow, :transfer_as_outflow
)
@pagy, @transactions = pagy(base_scope, limit: per_page, params: ->(p) { p.except(:focused_record_id) })

View file

@ -8,7 +8,12 @@ class TransferMatchesController < ApplicationController
def create
@transfer = build_transfer
@transfer.save!
Transfer.transaction do
@transfer.save!
@transfer.outflow_transaction.update!(kind: Transfer.kind_for_account(@transfer.outflow_transaction.entry.account))
@transfer.inflow_transaction.update!(kind: "funds_movement")
end
@transfer.sync_account_later
redirect_back_or_to transactions_path, notice: "Transfer created"

View file

@ -53,6 +53,9 @@ module Family::AutoTransferMatchable
outflow_transaction_id: match.outflow_transaction_id,
)
Transaction.find(match.inflow_transaction_id).update!(kind: "funds_movement")
Transaction.find(match.outflow_transaction_id).update!(kind: Transfer.kind_for_account(Transaction.find(match.outflow_transaction_id).entry.account))
used_transaction_ids << match.inflow_transaction_id
used_transaction_ids << match.outflow_transaction_id
end

View file

@ -12,6 +12,18 @@ class Transfer < ApplicationRecord
validate :transfer_within_date_range
validate :transfer_has_same_family
class << self
def kind_for_account(account)
if account.loan?
"loan_payment"
elsif account.liability?
"cc_payment"
else
"funds_movement"
end
end
end
def reject!
Transfer.transaction do
RejectedTransfer.find_or_create_by!(inflow_transaction_id: inflow_transaction_id, outflow_transaction_id: outflow_transaction_id)
@ -19,6 +31,15 @@ class Transfer < ApplicationRecord
end
end
# Once transfer is destroyed, we need to mark the denormalized kind fields on the transactions
def destroy!
Transfer.transaction do
inflow_transaction.update!(kind: "standard")
outflow_transaction.update!(kind: "standard")
super
end
end
def confirm!
update!(status: "confirmed")
end

View file

@ -10,7 +10,7 @@
<div class="pr-4 lg:pr-10 flex items-center gap-3 lg:gap-4 col-span-8 lg:col-span-6">
<%= check_box_tag dom_id(entry, "selection"),
disabled: transaction.transfer?,
disabled: transaction.transfer.present?,
class: "checkbox checkbox--light",
data: {
id: entry.id,
@ -39,7 +39,7 @@
<% if transaction.transfer? %>
<%= link_to(
entry.name,
entry_path(entry),
transaction.transfer.present? ? transfer_path(transaction.transfer) : entry_path(entry),
data: {
turbo_frame: "drawer",
turbo_prefetch: false
@ -64,7 +64,7 @@
</span>
<% end %>
<% if transaction.transfer? %>
<% if transaction.transfer.present? %>
<%= render "transactions/transfer_match", transaction: transaction %>
<% end %>
</div>
@ -91,7 +91,7 @@
<%= render "transactions/transaction_category", transaction: transaction %>
</div>
<div class="col-span-4 lg:col-span-2 ml-auto text-right">
<div class="col-span-4 ml-auto text-right">
<%= content_tag :p,
transaction.transfer? && view_ctx == "global" ? "+/- #{format_money(entry.amount_money.abs)}" : format_money(-entry.amount_money),
class: ["text-green-600": entry.amount.negative?] %>
@ -101,7 +101,7 @@
<% if balance_trend&.trend %>
<%= tag.p format_money(balance_trend.trend.current),
class: "font-medium text-sm text-primary" %>
<% else %>
<% elsif view_ctx != "global" %>
<%= tag.p "--", class: "font-medium text-sm text-gray-400" %>
<% end %>
</div>

View file

@ -313,13 +313,13 @@ end
accountable: Depository.new
)
transfer = Transfer.from_accounts(
from_account: from_account,
to_account: to_account,
transfer = Transfer::Creator.new(
family: @family,
source_account_id: from_account.id,
destination_account_id: to_account.id,
date: Date.current,
amount: 100
)
transfer.save!
).create
get api_v1_transaction_url(transfer.inflow_transaction), headers: api_headers(@api_key)
assert_response :success