resloved issues raised by ai chatbot

This commit is contained in:
Shibu M
2026-06-04 10:15:38 +00:00
parent e73006af54
commit 7e6d1123dc
3 changed files with 56 additions and 7 deletions

View File

@@ -11,6 +11,7 @@ class Transfer < ApplicationRecord
validate :transfer_has_opposite_amounts_or_fees
validate :transfer_within_date_range
validate :transfer_has_same_family
validate :fees_must_be_non_negative
class << self
def kind_for_account(account)
@@ -148,14 +149,19 @@ class Transfer < ApplicationRecord
inflow_amount = inflow_entry.amount
outflow_amount = outflow_entry.amount
errors.add(:base, :opposite_amounts) unless inflow_amount.negative? && outflow_amount.positive?
if inflow_entry.currency == outflow_entry.currency
total_fee = source_fee_amount.to_d + destination_fee_amount.to_d
errors.add(:base, :opposite_amounts) if inflow_amount + outflow_amount != total_fee
else
errors.add(:base, :opposite_amounts) unless inflow_amount.negative? && outflow_amount.positive?
end
end
def fees_must_be_non_negative
errors.add(:source_fee_amount, :greater_than_or_equal_to, count: 0) if source_fee_amount.to_d.negative?
errors.add(:destination_fee_amount, :greater_than_or_equal_to, count: 0) if destination_fee_amount.to_d.negative?
end
def transfer_within_date_range
return unless inflow_transaction&.entry && outflow_transaction&.entry

View File

@@ -35,13 +35,13 @@
</dl>
<dl class="flex items-center gap-2 justify-between">
<dt class="text-secondary"><%= t(".amount") %></dt>
<dd class="font-medium text-red-500 privacy-sensitive"><%= format_money @transfer.outflow_transaction.entry.amount_money * -1 %></dd>
<dd class="font-medium text-destructive privacy-sensitive"><%= format_money @transfer.outflow_transaction.entry.amount_money * -1 %></dd>
</dl>
</div>
<% if @transfer.has_source_fee? %>
<dl class="flex items-center gap-2 justify-between">
<dt class="text-secondary"><%= t(".source_fee") %></dt>
<dd class="font-medium text-red-500 privacy-sensitive">
<dd class="font-medium text-destructive privacy-sensitive">
-<%= format_money Money.new(@transfer.source_fee_amount, @transfer.from_account.currency) %>
</dd>
</dl>
@@ -50,7 +50,7 @@
<% if @transfer.has_destination_fee? %>
<dl class="flex items-center gap-2 justify-between">
<dt class="text-secondary"><%= t(".destination_fee") %></dt>
<dd class="font-medium text-red-500 privacy-sensitive">
<dd class="font-medium text-destructive privacy-sensitive">
-<%= format_money Money.new(@transfer.destination_fee_amount, @transfer.to_account.currency) %>
</dd>
</dl>
@@ -71,7 +71,7 @@
</dl>
<dl class="flex items-center gap-2 justify-between">
<dt class="text-secondary"><%= t(".amount") %></dt>
<dd class="font-medium text-green-500 privacy-sensitive">+<%= format_money @transfer.inflow_transaction.entry.amount_money * -1 %></dd>
<dd class="font-medium text-success privacy-sensitive">+<%= format_money @transfer.inflow_transaction.entry.amount_money * -1 %></dd>
</dl>
</div>
</div>
@@ -115,7 +115,7 @@
<%= button_to t(".delete"),
transfer_path(@transfer),
method: :delete,
class: "rounded-lg px-3 py-2 whitespace-nowrap text-red-500 text-sm
class: "rounded-lg px-3 py-2 whitespace-nowrap text-destructive text-sm
font-medium border border-secondary",
data: { turbo_confirm: true, turbo_frame: "_top" } %>
</div>

View File

@@ -203,4 +203,47 @@ class TransferTest < ActiveSupport::TestCase
transfer.update!(source_fee_amount: 3, destination_fee_amount: 2)
assert_equal 5, transfer.total_fee
end
test "destination fee larger than amount inverts inflow sign and fails validation" do
outflow_entry = create_transaction(date: Date.current, account: accounts(:depository), amount: 100)
inflow_entry = create_transaction(date: Date.current, account: accounts(:credit_card), amount: 50)
transfer = Transfer.new(
inflow_transaction: inflow_entry.transaction,
outflow_transaction: outflow_entry.transaction,
destination_fee_amount: 150
)
# inflow amount (50) is positive, which means destination is also outflowing
assert transfer.invalid?
assert_equal "Must have opposite amounts", transfer.errors.full_messages.first
end
test "negative source fee is rejected" do
outflow_entry = create_transaction(date: Date.current, account: accounts(:depository), amount: 500)
inflow_entry = create_transaction(date: Date.current, account: accounts(:credit_card), amount: -500)
transfer = Transfer.new(
inflow_transaction: inflow_entry.transaction,
outflow_transaction: outflow_entry.transaction,
source_fee_amount: -5
)
assert transfer.invalid?
assert_equal [ "Source fee amount must be greater than or equal to 0" ], transfer.errors.full_messages
end
test "negative destination fee is rejected" do
outflow_entry = create_transaction(date: Date.current, account: accounts(:depository), amount: 500)
inflow_entry = create_transaction(date: Date.current, account: accounts(:credit_card), amount: -500)
transfer = Transfer.new(
inflow_transaction: inflow_entry.transaction,
outflow_transaction: outflow_entry.transaction,
destination_fee_amount: -5
)
assert transfer.invalid?
assert_equal [ "Destination fee amount must be greater than or equal to 0" ], transfer.errors.full_messages
end
end