diff --git a/app/controllers/api/v4/events_controller.rb b/app/controllers/api/v4/events_controller.rb index 2e8da46a2c..d5a575e592 100644 --- a/app/controllers/api/v4/events_controller.rb +++ b/app/controllers/api/v4/events_controller.rb @@ -53,34 +53,6 @@ def show require_oauth2_scope "organizations:read", :show - def transactions - authorize @event, :show_in_v4? - - @settled_transactions = TransactionGroupingEngine::Transaction::All.new(**filters).run - @pending_transactions = PendingTransactionEngine::PendingTransaction::All.new(**filters).run - - type_results = ::EventsController.filter_transaction_type(params[:type], settled_transactions: @settled_transactions, pending_transactions: @pending_transactions) - @settled_transactions = type_results[:settled_transactions] - @pending_transactions = type_results[:pending_transactions] - - @total_count = @pending_transactions.count + @settled_transactions.count - @transactions = paginate_transactions(@pending_transactions + @settled_transactions) - - if @transactions.any? - - page_settled = @transactions.select { |tx| tx.is_a?(CanonicalTransactionGrouped) } - page_pending = @transactions.select { |tx| tx.is_a?(CanonicalPendingTransaction) } - - if page_settled.any? - TransactionGroupingEngine::Transaction::AssociationPreloader.new(transactions: page_settled, event: @event).run! - end - - if page_pending.any? - PendingTransactionEngine::PendingTransaction::AssociationPreloader.new(pending_transactions: page_pending, event: @event).run! - end - end - end - def followers authorize @event, :show_in_v4? @followers = @event.followers @@ -91,54 +63,7 @@ def followers private def set_event - @event = Event.find_by_public_id(params[:id]) || Event.find_by!(slug: params[:id]) - end - - def paginate_transactions(transactions) - limit = params[:limit]&.to_i || 25 - start_index = if params[:after] - transactions.index { |tx| tx.local_hcb_code.public_id == params[:after] } + 1 - else - 0 - end - @has_more = transactions.length > start_index + limit - - transactions.slice(start_index, limit) - end - - def filters - filter_params = params.fetch(:filters, {}).permit( - :search, - :tag_id, - :expenses, - :revenue, - :minimum_amount, - :maximum_amount, - :start_date, - :end_date, - :user_id, - :missing_receipts, - :category, - :merchant, - :order_by - ) - - return { - event_id: @event.id, - search: filter_params[:search].presence, - tag_id: filter_params[:tag_id].presence, - expenses: filter_params[:expenses].presence, - revenue: filter_params[:revenue].presence, - minimum_amount: filter_params[:minimum_amount].presence ? Money.from_amount(filter_params[:minimum_amount].to_f) : nil, - maximum_amount: filter_params[:maximum_amount].presence ? Money.from_amount(filter_params[:maximum_amount].to_f) : nil, - start_date: filter_params[:start_date].presence, - end_date: filter_params[:end_date].presence, - user: filter_params[:user_id] ? @event.users.find_by_public_id(filter_params[:user_id]) : nil, - missing_receipts: filter_params[:missing_receipts].present?, - category: filter_params[:category].presence, - merchant: filter_params[:merchant].presence, - order_by: filter_params[:order_by].presence - } + @event = Event.find_by_public_id(params[:id]) || Event.find_by!(slug: params[:id]) # we don't use set_api_event here because it is passed as id in the url end end diff --git a/app/controllers/api/v4/transactions_controller.rb b/app/controllers/api/v4/transactions_controller.rb index 4f6c0fd9d5..ef21e0e6d5 100644 --- a/app/controllers/api/v4/transactions_controller.rb +++ b/app/controllers/api/v4/transactions_controller.rb @@ -9,6 +9,35 @@ class TransactionsController < ApplicationController before_action :set_api_event, only: [:update, :memo_suggestions] skip_after_action :verify_authorized, only: [:missing_receipt] + def index + @event = Event.find_by_public_id(params[:id]) || Event.find_by!(slug: params[:id]) # we don't use set_api_event here because it is passed as id in the url + + authorize @event, :show_in_v4? + + @settled_transactions = TransactionGroupingEngine::Transaction::All.new(**filters).run + @pending_transactions = PendingTransactionEngine::PendingTransaction::All.new(**filters).run + + type_results = ::EventsController.filter_transaction_type(params[:type], settled_transactions: @settled_transactions, pending_transactions: @pending_transactions) + @settled_transactions = type_results[:settled_transactions] + @pending_transactions = type_results[:pending_transactions] + + @total_count = @pending_transactions.count + @settled_transactions.count + @transactions = paginate_transactions(@pending_transactions + @settled_transactions) + + if @transactions.any? + page_settled = @transactions.select { |tx| tx.is_a?(CanonicalTransactionGrouped) } + page_pending = @transactions.select { |tx| tx.is_a?(CanonicalPendingTransaction) } + + if page_settled.any? + TransactionGroupingEngine::Transaction::AssociationPreloader.new(transactions: page_settled, event: @event).run! + end + + if page_pending.any? + PendingTransactionEngine::PendingTransaction::AssociationPreloader.new(pending_transactions: page_pending, event: @event).run! + end + end + end + def show @hcb_code = authorize HcbCode.find_by_public_id!(params[:id]) @@ -48,6 +77,55 @@ def memo_suggestions @suggested_memos = ::HcbCodeService::SuggestedMemos.new(hcb_code: @hcb_code, event: @event).run.first(4) end + private + + def paginate_transactions(transactions) + limit = params[:limit]&.to_i || 25 + start_index = if params[:after] + transactions.index { |tx| tx.local_hcb_code.public_id == params[:after] } + 1 + else + 0 + end + @has_more = transactions.length > start_index + limit + + transactions.slice(start_index, limit) + end + + def filters + filter_params = params.fetch(:filters, {}).permit( + :search, + :tag_id, + :expenses, + :revenue, + :minimum_amount, + :maximum_amount, + :start_date, + :end_date, + :user_id, + :missing_receipts, + :category, + :merchant, + :order_by + ) + + return { + event_id: @event.id, + search: filter_params[:search].presence, + tag_id: filter_params[:tag_id].presence, + expenses: filter_params[:expenses].presence, + revenue: filter_params[:revenue].presence, + minimum_amount: filter_params[:minimum_amount].presence ? Money.from_amount(filter_params[:minimum_amount].to_f) : nil, + maximum_amount: filter_params[:maximum_amount].presence ? Money.from_amount(filter_params[:maximum_amount].to_f) : nil, + start_date: filter_params[:start_date].presence, + end_date: filter_params[:end_date].presence, + user: filter_params[:user_id] ? @event.users.find_by_public_id(filter_params[:user_id]) : nil, + missing_receipts: filter_params[:missing_receipts].present?, + category: filter_params[:category].presence, + merchant: filter_params[:merchant].presence, + order_by: filter_params[:order_by].presence + } + end + end end end diff --git a/app/views/api/v4/events/transactions.json.jbuilder b/app/views/api/v4/transactions/index.json.jbuilder similarity index 100% rename from app/views/api/v4/events/transactions.json.jbuilder rename to app/views/api/v4/transactions/index.json.jbuilder diff --git a/config/routes.rb b/config/routes.rb index 339cc3903c..993f58c19a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -679,7 +679,7 @@ get "sub_organizations" post "sub_organizations", to: "events#create_sub_organization" - get "transactions" + get "transactions", to: "transactions#index" get :followers end end