The personal finance app for everyone (by everyone)
Ruby
Fix double-counted entry when its date moves later (#3967)
JulienGourmete656f1ec9f89
diff --git a/app/models/entry.rb b/app/models/entry.rbindex 3cfd7f25b9..2d4801f060 100644--- a/app/models/entry.rb+++ b/app/models/entry.rb@@ -35,6 +35,7 @@ class Entry < ApplicationRecordvalidate :split_child_date_matches_parentbefore_destroy :prevent_individual_child_deletion, if: :split_child?+ after_save :track_earliest_saved_date, if: :saved_change_to_date?scope :visible, -> {joins(:account).where(accounts: { status: [ "draft", "active" ] })@@ -297,8 +298,19 @@ class Entry < ApplicationRecordentryable.lock_saved_attributes!end+ # Enqueues an account sync whose window starts at the earliest date this+ # entry has occupied since it was loaded, so balances are recomputed from+ # wherever the entry used to sit.+ #+ # Later saves (lock_saved_attributes!, mark_user_modified!) reset+ # date_previously_was, so the earliest date tracked across saves is used too.+ # Starting the window after the old date would seed the incremental balance+ # calculation from a balance that still includes this entry.+ #+ # @return [void]def sync_account_later- sync_start_date = [ date_previously_was, date ].compact.min unless destroyed?+ sync_start_date = [ @earliest_saved_date, date_previously_was, date ].compact.min unless destroyed?+ @earliest_saved_date = nilaccount.sync_later(window_start_date: sync_start_date)end@@ -571,6 +583,14 @@ class Entry < ApplicationRecordprivate+ # Remembers the earliest date this entry had before any save that changed+ # it, for sync_account_later to use as the sync window start.+ #+ # @return [Date]+ def track_earliest_saved_date+ @earliest_saved_date = [ @earliest_saved_date, date_before_last_save ].compact.min+ end+def cannot_unexclude_split_parentreturn unless excluded_changed?(from: true, to: false) && split_parent?diff --git a/test/controllers/transactions_controller_test.rb b/test/controllers/transactions_controller_test.rbindex 8930aef5ef..fc3fd91577 100644--- a/test/controllers/transactions_controller_test.rb+++ b/test/controllers/transactions_controller_test.rb@@ -291,6 +291,21 @@ class TransactionsControllerTest < ActionDispatch::IntegrationTestassert_response :unprocessable_entityend+ # Regression: the sync window must start at the old date, or the incremental+ # balance calculation counts the moved transaction twice.+ test "moving a transaction later syncs from its original date" do+ original_date = 5.days.ago.to_date+ @entry.update!(date: original_date)++ Account.any_instance.expects(:sync_later).with(window_start_date: original_date)++ patch transaction_url(@entry), params: {+ entry: { date: 2.days.ago.to_date, entryable_type: @entry.entryable_type }+ }++ assert_equal 2.days.ago.to_date, @entry.reload.date+ end+test "updates with transaction details" doassert_no_difference [ "Entry.count", "Transaction.count" ] dopatch transaction_url(@entry), params: {diff --git a/test/models/entry_test.rb b/test/models/entry_test.rbindex 77c4b036b3..6709b89244 100644--- a/test/models/entry_test.rb+++ b/test/models/entry_test.rb@@ -23,6 +23,21 @@ class EntryTest < ActiveSupport::TestCaseassert_equal entry_ids.sort.reverse, Entry.where(id: entry_ids).reverse_chronological.pluck(:id)end+ # Regression: lock_saved_attributes! and mark_user_modified! save again and used+ # to drop the original date, so moving an entry later double-counted it.+ test "sync_account_later keeps the original date across later saves" do+ entry = create_transaction(account: accounts(:depository), date: 5.days.ago.to_date)+ original_date = entry.date++ entry.update!(date: 2.days.ago.to_date)+ entry.update!(date: 1.day.ago.to_date)+ entry.lock_saved_attributes!+ entry.mark_user_modified!++ entry.account.expects(:sync_later).with(window_start_date: original_date)+ entry.sync_account_later+ end+test "bulk_update! touches the assigned category's last_used_at" doentry = create_transaction(account: accounts(:depository))category = categories(:income)