The personal finance app for everyone (by everyone)
Ruby
fix(api): include disabled-account transaction history (#1723)
ghost325084e34255
diff --git a/app/controllers/api/v1/transactions_controller.rb b/app/controllers/api/v1/transactions_controller.rbindex 5dd65aba31..f208d83b27 100644--- a/app/controllers/api/v1/transactions_controller.rb+++ b/app/controllers/api/v1/transactions_controller.rb@@ -10,8 +10,11 @@ class Api::V1::TransactionsController < Api::V1::BaseControllerdef indexfamily = current_resource_owner.family- accessible_account_ids = family.accounts.accessible_by(current_resource_owner).select(:id)- transactions_query = family.transactions.visible+ accessible_account_ids = family.accounts+ .accessible_by(current_resource_owner)+ .where.not(status: "pending_deletion")+ .select(:id)+ transactions_query = family.transactions.joins(:entry).where(entries: { account_id: accessible_account_ids })# Apply filters@@ -198,6 +201,8 @@ endprivatedef set_transaction+ raise ActiveRecord::RecordNotFound unless valid_uuid?(params[:id])+family = current_resource_owner.family@transaction = family.transactions.joins(entry: :account)diff --git a/docs/api/openapi.yaml b/docs/api/openapi.yamlindex ab6a787bbe..ebc4c44253 100644--- a/docs/api/openapi.yaml+++ b/docs/api/openapi.yaml@@ -6505,6 +6505,8 @@ paths:- Transactionssecurity:- apiKeyAuth: []+ description: Returns global ledger history for accessible accounts, including+ disabled accounts but excluding accounts pending deletion.parameters:- name: pagein: query@@ -6716,10 +6718,11 @@ paths:parameters:- name: idin: path- required: true- description: Transaction IDschema:type: string+ format: uuid+ required: true+ description: Transaction IDget:summary: Retrieve a transactiontags:diff --git a/spec/requests/api/v1/transactions_spec.rb b/spec/requests/api/v1/transactions_spec.rbindex d57a40b6a8..2a346906c9 100644--- a/spec/requests/api/v1/transactions_spec.rb+++ b/spec/requests/api/v1/transactions_spec.rb@@ -89,6 +89,7 @@ RSpec.describe 'API V1 Transactions', type: :request doget 'List transactions' dotags 'Transactions'security [ { apiKeyAuth: [] } ]+ description 'Returns global ledger history for accessible accounts, including disabled accounts but excluding accounts pending deletion.'produces 'application/json'parameter name: :page, in: :query, type: :integer, required: false,description: 'Page number (default: 1)'@@ -268,7 +269,7 @@ RSpec.describe 'API V1 Transactions', type: :request doendpath '/api/v1/transactions/{id}' do- parameter name: :id, in: :path, type: :string, required: true, description: 'Transaction ID'+ parameter name: :id, in: :path, schema: { type: :string, format: :uuid }, required: true, description: 'Transaction ID'get 'Retrieve a transaction' dotags 'Transactions'diff --git a/test/controllers/api/v1/transactions_controller_test.rb b/test/controllers/api/v1/transactions_controller_test.rbindex 002c80c305..1f5123d9ed 100644--- a/test/controllers/api/v1/transactions_controller_test.rb+++ b/test/controllers/api/v1/transactions_controller_test.rb@@ -66,6 +66,44 @@ class Api::V1::TransactionsControllerTest < ActionDispatch::IntegrationTestendend+ test "should include disabled account transactions in index history" do+ disabled_transaction = create_disabled_account_transaction(name: "Closed Account Grocery")++ get api_v1_transactions_url, headers: api_headers(@api_key)+ assert_response :success++ response_data = JSON.parse(response.body)+ transaction_ids = response_data["transactions"].map { |transaction| transaction["id"] }+ assert_includes transaction_ids, disabled_transaction.id+ end++ test "should exclude pending deletion account transactions from index history" do+ pending_deletion_transaction = create_account_transaction(+ status: "pending_deletion",+ name: "Pending Delete Account Grocery"+ )++ get api_v1_transactions_url, headers: api_headers(@api_key)+ assert_response :success++ response_data = JSON.parse(response.body)+ transaction_ids = response_data["transactions"].map { |transaction| transaction["id"] }+ assert_not_includes transaction_ids, pending_deletion_transaction.id+ end++ test "should filter disabled account transactions by account_id" do+ disabled_transaction = create_disabled_account_transaction(name: "Closed Account Filter")+ disabled_account = disabled_transaction.entry.account++ get api_v1_transactions_url,+ params: { account_id: disabled_account.id },+ headers: api_headers(@api_key)+ assert_response :success++ response_data = JSON.parse(response.body)+ assert_equal [ disabled_transaction.id ], response_data["transactions"].map { |transaction| transaction["id"] }+ end+test "should filter transactions by date range" dostart_date = 1.month.ago.to_dateend_date = Date.current@@ -83,6 +121,22 @@ class Api::V1::TransactionsControllerTest < ActionDispatch::IntegrationTestendend+ test "should filter disabled account transactions by date range" do+ disabled_transaction = create_disabled_account_transaction(+ name: "Closed Account Date Range",+ date: Date.current - 3.days+ )++ get api_v1_transactions_url,+ params: { start_date: Date.current - 4.days, end_date: Date.current - 2.days },+ headers: api_headers(@api_key)+ assert_response :success++ response_data = JSON.parse(response.body)+ transaction_ids = response_data["transactions"].map { |transaction| transaction["id"] }+ assert_includes transaction_ids, disabled_transaction.id+ end+test "should search transactions" do# Create a transaction with a specific name for testingentry = @account.entries.create!(@@ -103,6 +157,19 @@ class Api::V1::TransactionsControllerTest < ActionDispatch::IntegrationTestassert_not_nil found_transaction, "Should find the coffee transaction"end+ test "should search disabled account transactions" do+ disabled_transaction = create_disabled_account_transaction(name: "Closed Account Coffee")++ get api_v1_transactions_url,+ params: { search: "Closed Account Coffee" },+ headers: api_headers(@api_key)+ assert_response :success++ response_data = JSON.parse(response.body)+ found_transaction = response_data["transactions"].find { |transaction| transaction["id"] == disabled_transaction.id }+ assert_not_nil found_transaction, "Should find disabled account transactions in global history search"+ end+test "should paginate transactions" doget api_v1_transactions_url,params: { page: 1, per_page: 5 },@@ -144,9 +211,33 @@ class Api::V1::TransactionsControllerTest < ActionDispatch::IntegrationTestassert_response :successend- test "should return 404 for non-existent transaction" do+ test "should show disabled account transaction" do+ disabled_transaction = create_disabled_account_transaction(name: "Closed Account Show")++ get api_v1_transaction_url(disabled_transaction), headers: api_headers(@api_key)+ assert_response :success++ response_data = JSON.parse(response.body)+ assert_equal disabled_transaction.id, response_data["id"]+ assert_equal disabled_transaction.entry.account_id, response_data["account"]["id"]+ end++ test "should return 404 for valid missing transaction id" do+ get api_v1_transaction_url(SecureRandom.uuid), headers: api_headers(@api_key)+ assert_response :not_found++ response_data = JSON.parse(response.body)+ assert_equal "not_found", response_data["error"]+ assert_equal "Transaction not found", response_data["message"]+ end++ test "should return 404 for malformed id" doget api_v1_transaction_url(999999), headers: api_headers(@api_key)assert_response :not_found++ response_data = JSON.parse(response.body)+ assert_equal "not_found", response_data["error"]+ assert_equal "Transaction not found", response_data["message"]endtest "should reject show request without API key" do@@ -689,4 +780,28 @@ end"non-income transactions should have non-positive signed_amount_cents"endend++ def create_disabled_account_transaction(name:, date: Date.current)+ create_account_transaction(status: "disabled", name: name, date: date)+ end++ def create_account_transaction(status:, name:, date: Date.current)+ account = @family.accounts.create!(+ name: "#{status.titleize} Checking #{SecureRandom.hex(4)}",+ balance: 0,+ currency: "USD",+ status: status,+ accountable: Depository.new+ )++ entry = account.entries.create!(+ name: name,+ amount: 12.34,+ currency: "USD",+ date: date,+ entryable: Transaction.new+ )++ entry.transaction+ endend