Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 1 addition & 76 deletions app/controllers/api/v4/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
78 changes: 78 additions & 0 deletions app/controllers/api/v4/transactions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down