Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(transfers): remove unused "amount summaries" functions #355

Merged
merged 1 commit into from
Jan 17, 2025
Merged
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
58 changes: 0 additions & 58 deletions apps/app/lib/app/transfers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -143,64 +143,6 @@ defmodule App.Transfers do
|> offset(^offset)
end

## Amount summary

@typep amount_summary :: %{
type: MoneyTransfer.type(),
amount: Money.t(),
count: non_neg_integer()
}

@doc """
Returns the total amount of money transfers linked to a book, grouped by type.

The `:reimbursement` type is not included. If a book has no money transfers of a type,
the amount for this type will be `Money.zero(:EUR)` and the count is `0`.
"""
@spec amounts_summary_for_book(Book.t()) :: %{MoneyTransfer.type() => amount_summary()}
def amounts_summary_for_book(book) do
amounts_summary_query()
|> where([money_transfer: money_transfer], money_transfer.book_id == ^book.id)
|> Repo.all()
|> Map.new(&{&1.type, &1})
|> fill_grouped_amounts_missing_types()
end

@doc """
Returns the total amount of money transfers tenanted by a member, grouped by type.

The `:reimbursement` type is not included. If a book has no money transfers of a type,
the amount for this type will be `Money.zero(:EUR)` and the count is `0`.
"""
@spec amounts_summary_for_tenant(BookMember.t()) :: %{MoneyTransfer.type() => amount_summary()}
def amounts_summary_for_tenant(member) do
amounts_summary_query()
|> where([money_transfer: money_transfer], money_transfer.tenant_id == ^member.id)
|> Repo.all()
|> Map.new(&{&1.type, &1})
|> fill_grouped_amounts_missing_types()
end

defp amounts_summary_query do
from money_transfer in MoneyTransfer,
as: :money_transfer,
where: money_transfer.type != :reimbursement,
select: %{
type: money_transfer.type,
amount: sum(money_transfer.amount),
count: count(money_transfer.id)
},
group_by: money_transfer.type
end

defp fill_grouped_amounts_missing_types(grouped_amounts) do
Enum.reduce([:payment, :income], grouped_amounts, fn type, amounts ->
Map.put_new_lazy(amounts, type, fn ->
%{type: type, amount: Money.zero(:EUR), count: 0}
end)
end)
end

## CRUD

@doc """
Expand Down