Skip to content

Commit

Permalink
fix(finance): ordergroup overview total balances (#1051)
Browse files Browse the repository at this point in the history
* Introduce tests for deleted ordergroup and foodcoop transactions.
* Use the filtered @ordergroups for the @total_balances calculation
* This is also faster as the sum_of_class of the ordergroups are already precomputed.
  • Loading branch information
yksflip authored Mar 8, 2024
1 parent 8836697 commit a5861f5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/controllers/finance/ordergroups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def index
@ordergroups = @ordergroups.where('groups.name LIKE ?', "%#{params[:query]}%") unless params[:query].nil?
@ordergroups = @ordergroups.page(params[:page]).per(@per_page)

@total_balances = FinancialTransactionClass.sorted.each_with_object({}) do |c, tmp|
tmp[c.id] = c.financial_transactions.reduce(0) { |sum, t| sum + t.amount }
@total_balances = FinancialTransactionClass.sorted.each_with_object({}) do |transaction_class, total_balances|
total_balances[transaction_class.id] = @ordergroups.reduce(0) { |sum, o| o["sum_of_class_#{transaction_class.id}"] + sum }
end
end
end
44 changes: 40 additions & 4 deletions spec/controllers/finance/ordergroups_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@
end
let(:fin_trans3) do
create(:financial_transaction,
user: user,
amount: 42.23,
ordergroup: user.ordergroup,
financial_transaction_type: fin_trans_type2)
end
let(:fin_trans_foodcoop) do
create(:financial_transaction,
amount: 111,
ordergroup: nil,
financial_transaction_type: fin_trans_type2)
end

Expand All @@ -49,9 +53,41 @@
get_with_defaults :index
expect(response).to have_http_status(:success)

assert_select "#total_balance#{fin_trans_type1.financial_transaction_class_id}", number_to_currency(300)
assert_select "#total_balance#{fin_trans_type2.financial_transaction_class_id}", number_to_currency(42.23)
assert_total_balance_of_transaction_type1(300)
assert_total_balance_of_transaction_type2(42.23)
assert_total_balance_sum(342.23)
end

it 'ignores deleted ordergroups' do
user.ordergroup.mark_as_deleted
get_with_defaults :index
assert_total_balance_of_transaction_type1(0)
assert_total_balance_of_transaction_type2(42.23)
assert_select '#total_balance_sum', number_to_currency(42.23)
end

it 'ignores foodcoop transactions' do
fin_trans_foodcoop
get_with_defaults :index
assert_total_balance_of_transaction_type1(300)
assert_total_balance_of_transaction_type2(42.23)
assert_select '#total_balance_sum', number_to_currency(342.23)
end
end

def assert_total_balance_sum(amount)
assert_select '#total_balance_sum', number_to_currency(amount)
end

def assert_total_balance_of_transaction_type1(amount)
assert_total_balanceof_transaction_type(fin_trans_type1.financial_transaction_class_id, amount)
end

def assert_total_balance_of_transaction_type2(amount)
assert_total_balanceof_transaction_type(fin_trans_type2.financial_transaction_class_id, amount)
end

def assert_total_balanceof_transaction_type(type, amount)
assert_select "#total_balance#{type}", number_to_currency(amount)
end
end

0 comments on commit a5861f5

Please sign in to comment.