The personal finance app for everyone (by everyone)
Ruby
Allow API transaction update to opt into sync protection (user_modified) (#3483)
colinedwardwoode61fa5869a55
diff --git a/app/controllers/api/v1/transactions_controller.rb b/app/controllers/api/v1/transactions_controller.rbindex d4a9214613..734096891d 100644--- a/app/controllers/api/v1/transactions_controller.rb+++ b/app/controllers/api/v1/transactions_controller.rb@@ -153,6 +153,7 @@ class Api::V1::TransactionsController < Api::V1::BaseController@entry.sync_account_later@entry.lock_saved_attributes!+ @entry.mark_user_modified! if user_modified_requested?@transaction = @entry.transactionrender :show@@ -315,8 +316,8 @@ class Api::V1::TransactionsController < Api::V1::BaseController)end- # An API client can opt a transaction it creates into the same- # sync-protection a manual edit gets (Entry#protected_from_sync?) -+ # An API client can opt a transaction it creates or updates into the+ # same sync-protection a manual edit gets (Entry#protected_from_sync?) -# useful for a client that owns its own writes into an account also# linked to a bank-sync provider (Plaid/SimpleFin/etc.), so the next# sync only links its external_id to the entry instead of overwritingdiff --git a/docs/api/openapi.yaml b/docs/api/openapi.yamlindex e536d00e38..1eb7d95228 100644--- a/docs/api/openapi.yaml+++ b/docs/api/openapi.yaml@@ -7842,6 +7842,10 @@ paths:format: uuiddescription: Array of tag IDs to assign. Omit to preserve existingtags; use [] to clear all tags.+ user_modified:+ type: boolean+ description: Whether provider syncs should preserve user-supplied+ transaction changesrequired: truedelete:summary: Delete a transactiondiff --git a/spec/requests/api/v1/transactions_spec.rb b/spec/requests/api/v1/transactions_spec.rbindex 8228088d0d..fc138ebd09 100644--- a/spec/requests/api/v1/transactions_spec.rb+++ b/spec/requests/api/v1/transactions_spec.rb@@ -321,7 +321,8 @@ RSpec.describe 'API V1 Transactions', type: :request dotype: :array,items: { type: :string, format: :uuid },description: 'Array of tag IDs to assign. Omit to preserve existing tags; use [] to clear all tags.'- }+ },+ user_modified: { type: :boolean, description: 'Whether provider syncs should preserve user-supplied transaction changes' }}}}diff --git a/test/controllers/api/v1/transactions_controller_test.rb b/test/controllers/api/v1/transactions_controller_test.rbindex 8248319f41..334e5a12d5 100644--- a/test/controllers/api/v1/transactions_controller_test.rb+++ b/test/controllers/api/v1/transactions_controller_test.rb@@ -666,6 +666,38 @@ class Api::V1::TransactionsControllerTest < ActionDispatch::IntegrationTestassert_equal "Updated Transaction Name", response_data["name"]end+ test "should protect transaction from provider sync when updated with user_modified true" do+ update_params = {+ transaction: {+ name: "Client-owned Name",+ user_modified: true+ }+ }++ put api_v1_transaction_url(@transaction),+ params: update_params,+ headers: api_headers(@api_key)+ assert_response :success++ response_data = JSON.parse(response.body)+ assert_equal "Client-owned Name", response_data["name"]+ assert_equal true, response_data["user_modified"]++ entry = @transaction.entry.reload+ assert entry.user_modified?+ assert entry.protected_from_sync?+ end++ test "should not change user_modified on update by default" do+ put api_v1_transaction_url(@transaction),+ params: { transaction: { name: "Updated Name Only" } },+ headers: api_headers(@api_key)+ assert_response :success++ assert_equal false, JSON.parse(response.body)["user_modified"]+ assert_not @transaction.entry.reload.user_modified?+ end+test "should reject update with read-only API key" doupdate_params = {transaction: {