Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

fix(sync): keep a locked date and amount when a provider sync re-imports a transaction (#3989)

GFRa760d130c479
diff --git a/app/models/account/provider_import_adapter.rb b/app/models/account/provider_import_adapter.rb
index 2508c44fd8..524f0a62a9 100644
--- a/app/models/account/provider_import_adapter.rb
+++ b/app/models/account/provider_import_adapter.rb
@@ -216,11 +216,16 @@ class Account::ProviderImportAdapter
date
end
- entry.assign_attributes(
- amount: amount,
- currency: currency,
- date: effective_date
- )
+ # A date or amount the user changed without marking the entry user_modified
+ # (an API PATCH, for example) is still locked by lock_saved_attributes!.
+ # Respect that lock the way enrich_attribute does for name and category.
+ # Amount and currency only make sense together, so a lock on either keeps both.
+ financial_attributes = { amount: amount, currency: currency, date: effective_date }
+ if entry.persisted?
+ financial_attributes.delete(:date) if entry.locked?(:date)
+ financial_attributes.except!(:amount, :currency) if entry.locked?(:amount) || entry.locked?(:currency)
+ end
+ entry.assign_attributes(financial_attributes)
# Use enrichment pattern to respect user overrides
entry.enrich_attribute(:name, name, source: source)
@@ -244,7 +249,7 @@ class Account::ProviderImportAdapter
# Auto-detect investment activity labels for investment accounts
detected_label = investment_activity_label
if account.investment? && detected_label.nil? && entry.entryable.is_a?(Transaction)
- detected_label = detect_activity_label(name, amount)
+ detected_label = detect_activity_label(name, entry.amount)
end
# Determine the transaction kind. Activity-label and account-type classification
@@ -261,7 +266,7 @@ class Account::ProviderImportAdapter
elsif detected_label == "Contribution"
auto_kind = "investment_contribution"
auto_category = account.family.investment_contributions_category
- elsif account.accountable_type == "Loan" && amount.negative?
+ elsif account.accountable_type == "Loan" && entry.amount.negative?
auto_kind = "loan_payment"
end
auto_kind ||= kind.presence
diff --git a/test/controllers/api/v1/transactions_controller_test.rb b/test/controllers/api/v1/transactions_controller_test.rb
index 334e5a12d5..a7d57ab6f3 100644
--- a/test/controllers/api/v1/transactions_controller_test.rb
+++ b/test/controllers/api/v1/transactions_controller_test.rb
@@ -688,6 +688,34 @@ class Api::V1::TransactionsControllerTest < ActionDispatch::IntegrationTest
assert entry.protected_from_sync?
end
+ test "date changed via API survives the next provider sync without user_modified" do
+ entry = Account::ProviderImportAdapter.new(@account).import_transaction(
+ external_id: "plaid_api_date_edit",
+ amount: 42.00,
+ currency: "USD",
+ date: Date.current - 5.days,
+ name: "Card payment",
+ source: "plaid"
+ )
+
+ put api_v1_transaction_url(entry.transaction),
+ params: { transaction: { date: (Date.current - 2.days).to_s } },
+ headers: api_headers(@api_key)
+ assert_response :success
+ assert_not entry.reload.user_modified?
+
+ Account::ProviderImportAdapter.new(@account).import_transaction(
+ external_id: "plaid_api_date_edit",
+ amount: 42.00,
+ currency: "USD",
+ date: Date.current - 5.days,
+ name: "Card payment",
+ source: "plaid"
+ )
+
+ assert_equal Date.current - 2.days, entry.reload.date
+ end
+
test "should not change user_modified on update by default" do
put api_v1_transaction_url(@transaction),
params: { transaction: { name: "Updated Name Only" } },
diff --git a/test/models/account/provider_import_adapter_test.rb b/test/models/account/provider_import_adapter_test.rb
index 7d612021ab..e19dc3d9d9 100644
--- a/test/models/account/provider_import_adapter_test.rb
+++ b/test/models/account/provider_import_adapter_test.rb
@@ -114,6 +114,93 @@ class Account::ProviderImportAdapterTest < ActiveSupport::TestCase
end
end
+ test "keeps a locked date and amount on re-import" do
+ entry = @adapter.import_transaction(
+ external_id: "plaid_locked_financials",
+ amount: 100.00,
+ currency: "USD",
+ date: Date.current - 3.days,
+ name: "Original Name",
+ source: "plaid"
+ )
+
+ # A user edit that locks the fields without marking the entry user_modified,
+ # as PATCH /api/v1/transactions/:id does when user_modified is not sent.
+ entry.update!(date: Date.current - 1.day, amount: 80.00)
+ entry.lock_saved_attributes!
+ assert_not entry.reload.user_modified?
+
+ updated_entry = @adapter.import_transaction(
+ external_id: "plaid_locked_financials",
+ amount: 100.00,
+ currency: "EUR",
+ date: Date.current - 3.days,
+ name: "Provider Name",
+ source: "plaid"
+ )
+
+ assert_equal entry.id, updated_entry.id
+ assert_equal Date.current - 1.day, updated_entry.reload.date
+ assert_equal 80.00, updated_entry.amount
+ assert_equal "USD", updated_entry.currency
+ assert_equal "Provider Name", updated_entry.name
+ end
+
+ test "classifies a re-imported loan entry by its locked amount, not the provider's" do
+ loan_adapter = Account::ProviderImportAdapter.new(accounts(:loan))
+ entry = loan_adapter.import_transaction(
+ external_id: "plaid_loan_locked_amount",
+ amount: -200.00,
+ currency: "USD",
+ date: Date.current,
+ name: "Loan Repayment",
+ source: "plaid"
+ )
+ assert_equal "loan_payment", entry.transaction.kind
+
+ entry.update!(amount: 50.00)
+ entry.lock_saved_attributes!
+ entry.transaction.update!(kind: "standard")
+
+ loan_adapter.import_transaction(
+ external_id: "plaid_loan_locked_amount",
+ amount: -200.00,
+ currency: "USD",
+ date: Date.current,
+ name: "Loan Repayment",
+ source: "plaid"
+ )
+
+ assert_equal 50.00, entry.reload.amount
+ assert_equal "standard", entry.transaction.kind
+ end
+
+ test "updates an unlocked amount while keeping a locked date" do
+ entry = @adapter.import_transaction(
+ external_id: "plaid_locked_date_only",
+ amount: 100.00,
+ currency: "USD",
+ date: Date.current - 3.days,
+ name: "Original Name",
+ source: "plaid"
+ )
+
+ entry.update!(date: Date.current - 1.day)
+ entry.lock_saved_attributes!
+
+ updated_entry = @adapter.import_transaction(
+ external_id: "plaid_locked_date_only",
+ amount: 120.00,
+ currency: "USD",
+ date: Date.current - 3.days,
+ name: "Original Name",
+ source: "plaid"
+ )
+
+ assert_equal Date.current - 1.day, updated_entry.reload.date
+ assert_equal 120.00, updated_entry.amount
+ end
+
test "allows same external_id from different sources without collision" do
# Create transaction from SimpleFin with ID "transaction_123"
simplefin_entry = @adapter.import_transaction(