Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Skip account sync for descriptive transaction edits (#4001)

GFRff26de1bfd31
diff --git a/app/controllers/api/v1/transactions_controller.rb b/app/controllers/api/v1/transactions_controller.rb
index 734096891d..dbb5e4b2b7 100644
--- a/app/controllers/api/v1/transactions_controller.rb
+++ b/app/controllers/api/v1/transactions_controller.rb
@@ -143,6 +143,8 @@ class Api::V1::TransactionsController < Api::V1::BaseController
Entry.transaction do
if @entry.update(entry_params_for_update)
+ sync_needed = @entry.saved_changes_affect_balances?
+
# Handle tags separately - only when explicitly provided in the request
# This allows clearing tags with tag_ids: [] while preserving tags when not specified
if tags_provided?
@@ -151,7 +153,7 @@ class Api::V1::TransactionsController < Api::V1::BaseController
@entry.transaction.lock_attr!(:tag_ids) if @entry.transaction.tags.any?
end
- @entry.sync_account_later
+ @entry.sync_account_later if sync_needed
@entry.lock_saved_attributes!
@entry.mark_user_modified! if user_modified_requested?
diff --git a/app/controllers/transactions_controller.rb b/app/controllers/transactions_controller.rb
index 38b44c47bb..d59b29e27f 100644
--- a/app/controllers/transactions_controller.rb
+++ b/app/controllers/transactions_controller.rb
@@ -184,6 +184,7 @@ class TransactionsController < ApplicationController
def update
if @entry.update(permitted_entry_params)
+ sync_needed = @entry.saved_changes_affect_balances?
transaction = @entry.transaction
transaction.record_category_usage!
@@ -198,7 +199,7 @@ class TransactionsController < ApplicationController
@entry.lock_saved_attributes!
@entry.mark_user_modified!
@entry.transaction.lock_attr!(:tag_ids) if @entry.transaction.tags.any?
- @entry.sync_account_later
+ @entry.sync_account_later if sync_needed
notes_changed = @entry.saved_change_to_notes?
@@ -262,7 +263,6 @@ class TransactionsController < ApplicationController
@entry.lock_saved_attributes!
@entry.mark_user_modified!
@entry.transaction.lock_attr!(:tag_ids)
- @entry.sync_account_later
respond_to do |format|
# JSON stays first so Accept: */* callers keep the original response.
diff --git a/app/models/entry.rb b/app/models/entry.rb
index 77303521a3..25593fe97b 100644
--- a/app/models/entry.rb
+++ b/app/models/entry.rb
@@ -4,6 +4,13 @@ class Entry < ApplicationRecord
TRUTHY_VALUES = [ true, "true", "1", 1 ].freeze
private_constant :TRUTHY_VALUES
+ # Attributes that only describe an entry. Balance materialization and
+ # transfer matching never read them, so an edit limited to them doesn't need
+ # an account sync. Reports and budgets pick such edits up through
+ # Family#entries_cache_version, which the save itself bumps.
+ DESCRIPTIVE_ATTRIBUTES = %w[name notes updated_at].freeze
+ DESCRIPTIVE_TRANSACTION_ATTRIBUTES = %w[category_id merchant_id updated_at].freeze
+
attr_accessor :unsplitting
monetize :amount
@@ -311,6 +318,15 @@ class Entry < ApplicationRecord
account.sync_later(window_start_date: sync_start_date)
end
+ # Whether the last save changed anything a sync depends on. Call it right
+ # after the save, before any follow-up write resets saved_changes.
+ def saved_changes_affect_balances?
+ return true if (saved_changes.keys - DESCRIPTIVE_ATTRIBUTES).any?
+
+ descriptive = entryable.is_a?(Transaction) ? DESCRIPTIVE_TRANSACTION_ATTRIBUTES : %w[updated_at]
+ (entryable.saved_changes.keys - descriptive).any?
+ end
+
def entryable_name_short
entryable_type.demodulize.underscore
end
diff --git a/test/controllers/api/v1/transactions_controller_test.rb b/test/controllers/api/v1/transactions_controller_test.rb
index a7d57ab6f3..4d9b6e71f0 100644
--- a/test/controllers/api/v1/transactions_controller_test.rb
+++ b/test/controllers/api/v1/transactions_controller_test.rb
@@ -666,6 +666,33 @@ class Api::V1::TransactionsControllerTest < ActionDispatch::IntegrationTest
assert_equal "Updated Transaction Name", response_data["name"]
end
+ test "should not sync the account when only notes change" do
+ assert_no_enqueued_jobs only: SyncJob do
+ put api_v1_transaction_url(@transaction),
+ params: { transaction: { notes: "Just a note" } },
+ headers: api_headers(@api_key)
+ end
+ assert_response :success
+ end
+
+ test "should sync the account when the amount changes" do
+ assert_enqueued_with(job: SyncJob) do
+ put api_v1_transaction_url(@transaction),
+ params: { transaction: { amount: 42.00 } },
+ headers: api_headers(@api_key)
+ end
+ assert_response :success
+ end
+
+ test "should sync the account when notes and amount change together" do
+ assert_enqueued_with(job: SyncJob) do
+ put api_v1_transaction_url(@transaction),
+ params: { transaction: { notes: "Just a note", amount: 42.00 } },
+ headers: api_headers(@api_key)
+ end
+ assert_response :success
+ end
+
test "should protect transaction from provider sync when updated with user_modified true" do
update_params = {
transaction: {
diff --git a/test/controllers/transactions_controller_test.rb b/test/controllers/transactions_controller_test.rb
index 9f6cceb3a3..216db9ac16 100644
--- a/test/controllers/transactions_controller_test.rb
+++ b/test/controllers/transactions_controller_test.rb
@@ -1699,15 +1699,37 @@ end
Rails.cache = original_cache
end
- test "an unconfirmed auto-matched transfer keeps its category menu in the list" do
- outflow = create_transaction(account: accounts(:depository), amount: 500, name: "PENDING_MATCH_OUT").transaction
- inflow = create_transaction(account: accounts(:credit_card), amount: -500).transaction
- Transfer.create!(outflow_transaction: outflow, inflow_transaction: inflow)
+ test "category-only update does not sync the account" do
+ assert_no_enqueued_jobs only: SyncJob do
+ patch transaction_url(@entry), params: {
+ entry: { entryable_attributes: { id: @entry.entryable_id, category_id: categories(:food_and_drink).id } }
+ }
+ end
- get transactions_url
+ assert_equal categories(:food_and_drink).id, @entry.reload.entryable.category_id
+ end
- assert_response :success
- assert_select "##{dom_id(outflow, "category_menu_desktop")} turbo-frame#category_dropdown"
+ test "amount update syncs the account" do
+ assert_enqueued_with(job: SyncJob) do
+ patch transaction_url(@entry), params: { entry: { amount: @entry.amount + 10 } }
+ end
+ end
+
+ test "category and date updated together still sync the account" do
+ assert_enqueued_with(job: SyncJob) do
+ patch transaction_url(@entry), params: {
+ entry: {
+ date: @entry.date - 1.day,
+ entryable_attributes: { id: @entry.entryable_id, category_id: categories(:food_and_drink).id }
+ }
+ }
+ end
+ end
+
+ test "toggling a tag does not sync the account" do
+ assert_no_enqueued_jobs only: SyncJob do
+ patch tags_transaction_url(@entry), params: { toggle_tag_id: tags(:two).id }, as: :turbo_stream
+ end
end
test "index with ai_status=current renders the AI filter badge" do
@@ -1765,6 +1787,17 @@ end
assert_no_match(/ai_status/, response.location)
end
+ test "an unconfirmed auto-matched transfer keeps its category menu in the list" do
+ outflow = create_transaction(account: accounts(:depository), amount: 500, name: "PENDING_MATCH_OUT").transaction
+ inflow = create_transaction(account: accounts(:credit_card), amount: -500).transaction
+ Transfer.create!(outflow_transaction: outflow, inflow_transaction: inflow)
+
+ get transactions_url
+
+ assert_response :success
+ assert_select "##{dom_id(outflow, "category_menu_desktop")} turbo-frame#category_dropdown"
+ end
+
test "update drops a category sent for a matched transfer's inflow leg" do
outflow = Transaction.create!(kind: "investment_contribution")
Entry.create!(account: accounts(:depository), entryable: outflow, name: "Contribution", amount: 500, currency: "USD", date: Date.current)
diff --git a/test/models/entry_test.rb b/test/models/entry_test.rb
index 6709b89244..2e935c2180 100644
--- a/test/models/entry_test.rb
+++ b/test/models/entry_test.rb
@@ -47,4 +47,43 @@ class EntryTest < ActiveSupport::TestCase
assert_not_nil category.reload.last_used_at
end
+
+ test "descriptive edits do not affect balances" do
+ entry = create_transaction(account: accounts(:depository), amount: 100)
+ entry = Entry.find(entry.id)
+
+ entry.update!(name: "Renamed", notes: "note", entryable_attributes: { id: entry.entryable_id, category_id: categories(:food_and_drink).id })
+
+ assert_not entry.saved_changes_affect_balances?
+ end
+
+ # A descriptive change must never mask a balance-relevant change made in
+ # the same update.
+ test "descriptive edits combined with a balance edit still affect balances" do
+ entry = create_transaction(account: accounts(:depository), amount: 100)
+
+ entry = Entry.find(entry.id)
+ entry.update!(notes: "x", amount: 150)
+ assert entry.saved_changes_affect_balances?
+
+ entry = Entry.find(entry.id)
+ entry.update!(date: 3.days.ago.to_date, entryable_attributes: { id: entry.entryable_id, category_id: categories(:food_and_drink).id })
+ assert entry.saved_changes_affect_balances?
+ end
+
+ test "amount, date and exchange rate edits affect balances" do
+ entry = create_transaction(account: accounts(:depository), amount: 100)
+
+ entry = Entry.find(entry.id)
+ entry.update!(amount: 150)
+ assert entry.saved_changes_affect_balances?
+
+ entry = Entry.find(entry.id)
+ entry.update!(date: 3.days.ago.to_date)
+ assert entry.saved_changes_affect_balances?
+
+ entry = Entry.find(entry.id)
+ entry.update!(entryable_attributes: { id: entry.entryable_id, exchange_rate: 1.2 })
+ assert entry.saved_changes_affect_balances?
+ end
end