Skip to content

armgit / sure

Public Imported from GitHub
0

The personal finance app for everyone (by everyone)

Ruby

update transactions_controller (#1953)

William Wei Ming34dd5fbc6274
diff --git a/app/controllers/api/v1/transactions_controller.rb b/app/controllers/api/v1/transactions_controller.rb
index f208d83b27..2370ffa3e1 100644
--- a/app/controllers/api/v1/transactions_controller.rb
+++ b/app/controllers/api/v1/transactions_controller.rb
@@ -127,7 +127,7 @@ class Api::V1::TransactionsController < Api::V1::BaseController
error: "internal_server_error",
message: "An unexpected error occurred"
}, status: :internal_server_error
-end
+ end
def update
if @entry.split_child?
@@ -227,12 +227,12 @@ end
def apply_filters(query)
# Account filtering
if params[:account_id].present?
- query = query.joins(:entry).where(entries: { account_id: params[:account_id] })
+ query = query.where(entries: { account_id: params[:account_id] })
end
if params[:account_ids].present?
account_ids = Array(params[:account_ids])
- query = query.joins(:entry).where(entries: { account_id: account_ids })
+ query = query.where(entries: { account_id: account_ids })
end
# Category filtering
@@ -257,37 +257,39 @@ end
# Date range filtering
if params[:start_date].present?
- query = query.joins(:entry).where("entries.date >= ?", Date.parse(params[:start_date]))
+ query = query.where("entries.date >= ?", Date.parse(params[:start_date]))
end
if params[:end_date].present?
- query = query.joins(:entry).where("entries.date <= ?", Date.parse(params[:end_date]))
+ query = query.where("entries.date <= ?", Date.parse(params[:end_date]))
end
# Amount filtering
if params[:min_amount].present?
min_amount = params[:min_amount].to_f
- query = query.joins(:entry).where("entries.amount >= ?", min_amount)
+ query = query.where("entries.amount >= ?", min_amount)
end
if params[:max_amount].present?
max_amount = params[:max_amount].to_f
- query = query.joins(:entry).where("entries.amount <= ?", max_amount)
+ query = query.where("entries.amount <= ?", max_amount)
end
# Tag filtering
if params[:tag_ids].present?
tag_ids = Array(params[:tag_ids])
- query = query.joins(:tags).where(tags: { id: tag_ids })
+ query = query.where(
+ id: query.joins(:tags).where(tags: { id: tag_ids }).distinct.select(:id)
+ )
end
# Transaction type filtering (income/expense)
if params[:type].present?
case params[:type].downcase
when "income"
- query = query.joins(:entry).where("entries.amount < 0")
+ query = query.where("entries.amount < 0")
when "expense"
- query = query.joins(:entry).where("entries.amount > 0")
+ query = query.where("entries.amount > 0")
end
end
@@ -297,13 +299,13 @@ end
def apply_search(query)
search_term = "%#{params[:search]}%"
- query.joins(:entry)
- .left_joins(:merchant)
- .where(
- "entries.name ILIKE ? OR entries.notes ILIKE ? OR merchants.name ILIKE ?",
- search_term, search_term, search_term
- )
-end
+ query
+ .left_joins(:merchant)
+ .where(
+ "entries.name ILIKE ? OR entries.notes ILIKE ? OR merchants.name ILIKE ?",
+ search_term, search_term, search_term
+ )
+ end
def transaction_params
params.require(:transaction).permit(
diff --git a/test/controllers/api/v1/transactions_controller_test.rb b/test/controllers/api/v1/transactions_controller_test.rb
index ce7398e3cd..0e54c415d5 100644
--- a/test/controllers/api/v1/transactions_controller_test.rb
+++ b/test/controllers/api/v1/transactions_controller_test.rb
@@ -3,6 +3,8 @@
require "test_helper"
class Api::V1::TransactionsControllerTest < ActionDispatch::IntegrationTest
+ include EntriesTestHelper
+
setup do
@user = users(:family_admin)
@family = @user.family
@@ -51,6 +53,30 @@ class Api::V1::TransactionsControllerTest < ActionDispatch::IntegrationTest
assert response_data["pagination"].key?("total_pages")
end
+ test "index avoids per-transaction transfer queries" do
+ from_account = @family.accounts.first
+ to_account = @family.accounts.second || @family.accounts.create!(
+ name: "Second Account",
+ balance: 0,
+ currency: @family.currency,
+ accountable: Depository.new
+ )
+
+ create_transfer(from_account: from_account, to_account: to_account, amount: 10)
+ baseline_queries = count_db_queries do
+ get api_v1_transactions_url, params: { per_page: 200 }, headers: api_headers(@api_key)
+ assert_response :success
+ end
+
+ 5.times { create_transfer(from_account: from_account, to_account: to_account, amount: 10) }
+ expanded_queries = count_db_queries do
+ get api_v1_transactions_url, params: { per_page: 200 }, headers: api_headers(@api_key)
+ assert_response :success
+ end
+
+ assert_equal baseline_queries, expanded_queries
+ end
+
test "should get index with read-only API key" do
get api_v1_transactions_url, headers: api_headers(@read_only_api_key)
assert_response :success
@@ -121,6 +147,37 @@ class Api::V1::TransactionsControllerTest < ActionDispatch::IntegrationTest
end
end
+ test "should filter transactions by tag_ids without error" do
+ tag_one = tags(:one)
+ tag_two = tags(:two)
+ tagged_entry = @account.entries.create!(
+ name: "Tagged Transaction",
+ amount: 12.34,
+ currency: "USD",
+ date: Date.current,
+ entryable: Transaction.new(tags: [ tag_one, tag_two ])
+ )
+
+ untagged_entry = @account.entries.create!(
+ name: "Untagged Transaction",
+ amount: 12.34,
+ currency: "USD",
+ date: Date.current,
+ entryable: Transaction.new
+ )
+
+ get api_v1_transactions_url,
+ params: { tag_ids: [ tag_one.id, tag_two.id ], per_page: 200 },
+ headers: api_headers(@api_key)
+ assert_response :success
+
+ response_data = JSON.parse(response.body)
+ transaction_ids = response_data["transactions"].map { |t| t["id"] }
+ assert_equal 1, transaction_ids.count(tagged_entry.transaction.id)
+ assert_includes transaction_ids, tagged_entry.transaction.id
+ assert_not_includes transaction_ids, untagged_entry.transaction.id
+ end
+
test "should filter disabled account transactions by date range" do
disabled_transaction = create_disabled_account_transaction(
name: "Closed Account Date Range",
@@ -764,6 +821,19 @@ end
{ "X-Api-Key" => api_key.display_key }
end
+ def count_db_queries(&block)
+ queries = 0
+ callback = lambda do |_name, _started, _finished, _unique_id, payload|
+ return if payload[:cached]
+ return if payload[:name].in?(%w[SCHEMA TRANSACTION])
+
+ queries += 1
+ end
+
+ ActiveSupport::Notifications.subscribed(callback, "sql.active_record", &block)
+ queries
+ end
+
def create_transfer_between_accounts
from_account = @family.accounts.create!(
name: "Transfer From Account",