Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

Allow API transaction create to opt into sync protection (user_modified) (#3162)

colinedwardwoodb1df16b0a7aa
diff --git a/app/controllers/api/v1/transactions_controller.rb b/app/controllers/api/v1/transactions_controller.rb
index 2370ffa3e1..d4a9214613 100644
--- a/app/controllers/api/v1/transactions_controller.rb
+++ b/app/controllers/api/v1/transactions_controller.rb
@@ -99,9 +99,10 @@ class Api::V1::TransactionsController < Api::V1::BaseController
@entry = account.entries.new(entry_params_for_create)
if @entry.save
- @entry.sync_account_later
@entry.lock_saved_attributes!
@entry.transaction.lock_attr!(:tag_ids) if @entry.transaction.tags.any?
+ @entry.mark_user_modified! if user_modified_requested?
+ @entry.sync_account_later
@transaction = @entry.transaction
render :show, status: :created
@@ -310,10 +311,20 @@ class Api::V1::TransactionsController < Api::V1::BaseController
def transaction_params
params.require(:transaction).permit(
:date, :amount, :name, :description, :notes, :currency,
- :category_id, :merchant_id, :nature, tag_ids: []
+ :category_id, :merchant_id, :nature, :user_modified, tag_ids: []
)
end
+ # An API client can opt a transaction it creates 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 overwriting
+ # the category/name the client already set.
+ def user_modified_requested?
+ ActiveModel::Type::Boolean.new.cast(transaction_params[:user_modified])
+ end
+
def account_id_param
params.dig(:transaction, :account_id).presence
end
diff --git a/app/views/api/v1/transactions/_transaction.json.jbuilder b/app/views/api/v1/transactions/_transaction.json.jbuilder
index aa8674822a..dbf276ee86 100644
--- a/app/views/api/v1/transactions/_transaction.json.jbuilder
+++ b/app/views/api/v1/transactions/_transaction.json.jbuilder
@@ -19,6 +19,7 @@ json.name transaction.entry.name
json.notes transaction.entry.notes
json.external_id transaction.entry.external_id
json.source transaction.entry.source
+json.user_modified transaction.entry.user_modified
json.classification transaction.entry.classification
# Account information
diff --git a/test/controllers/api/v1/transactions_controller_test.rb b/test/controllers/api/v1/transactions_controller_test.rb
index 0e54c415d5..8248319f41 100644
--- a/test/controllers/api/v1/transactions_controller_test.rb
+++ b/test/controllers/api/v1/transactions_controller_test.rb
@@ -356,6 +356,53 @@ class Api::V1::TransactionsControllerTest < ActionDispatch::IntegrationTest
assert_equal response_data["id"], entry.transaction.id
end
+ test "should create transaction protected from provider sync when user_modified is true" do
+ transaction_params = {
+ transaction: {
+ account_id: @account.id,
+ name: "Imported Transaction",
+ amount: 25.00,
+ date: Date.current,
+ currency: "USD",
+ nature: "expense",
+ user_modified: true
+ }
+ }
+
+ assert_difference("@account.entries.count", 1) do
+ post api_v1_transactions_url,
+ params: transaction_params,
+ headers: api_headers(@api_key)
+ end
+
+ assert_response :created
+ response_data = JSON.parse(response.body)
+ assert_equal true, response_data["user_modified"]
+
+ entry = Transaction.find(response_data["id"]).entry
+ assert entry.user_modified?
+ assert entry.protected_from_sync?
+ end
+
+ test "should not mark transaction user_modified by default" do
+ transaction_params = {
+ transaction: {
+ account_id: @account.id,
+ name: "Imported Transaction",
+ amount: 25.00,
+ date: Date.current,
+ currency: "USD",
+ nature: "expense"
+ }
+ }
+
+ post api_v1_transactions_url, params: transaction_params, headers: api_headers(@api_key)
+
+ assert_response :created
+ response_data = JSON.parse(response.body)
+ assert_equal false, response_data["user_modified"]
+ end
+
test "should use default source when external_id provided without source" do
transaction_params = {
transaction: {