Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigoliveirac committed Aug 23, 2024
1 parent c093bbb commit c77c729
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class BalanceBuilder {
* Updates `balance` with deposits from income and transfers in.
*
* @param incomes Deposit amounts from income sources.
* @param transferIn Deposit amounts from transfer in sources.
* @param transfersIn Deposit amounts from transfer in sources.
*/
fun processDeposits(
incomes: Map<AssetCode, PositiveDouble>,
transferIn: Map<AssetCode, PositiveDouble>,
transfersIn: Map<AssetCode, PositiveDouble>,
) {
combine(incomes, transferIn).forEach { (asset, amount) ->
combine(incomes, transfersIn).forEach { (asset, amount) ->
NonZeroDouble
.from((balance[asset]?.value ?: 0.0) + amount.value)
.onRight { newValue ->
Expand All @@ -31,13 +31,13 @@ class BalanceBuilder {
* Updates `balance` with withdrawals from expenses and transfers out.
*
* @param expenses Deposit amounts from expense sources.
* @param transferOut Deposit amounts from transfer out sources.
* @param transfersOut Deposit amounts from transfer out sources.
*/
fun processWithdrawals(
expenses: Map<AssetCode, PositiveDouble>,
transferOut: Map<AssetCode, PositiveDouble>,
transfersOut: Map<AssetCode, PositiveDouble>,
) {
combine(expenses, transferOut).forEach { (asset, amount) ->
combine(expenses, transfersOut).forEach { (asset, amount) ->
NonZeroDouble
.from((balance[asset]?.value ?: 0.0) - amount.value)
.onRight { newValue ->
Expand All @@ -48,9 +48,7 @@ class BalanceBuilder {

/**
* Combines two maps by summing their values. If the sum results in an error,
* the value from the `c` map is used.
*
* Note: `c` is a copy of `a`.
* the value from the `a` map is used.
*
* @return A map with combined values from the two input maps.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,17 @@ class AccountBalanceUseCase @Inject constructor(
transactions = transactionRepository.findAll()
)

val balance = BalanceBuilder()

balance.processDeposits(
incomes = accountStats.income.values,
transferIn = accountStats.transfersIn.values
)

balance.processWithdrawals(
expenses = accountStats.expense.values,
transferOut = accountStats.transfersOut.values
)

return balance.build()
return BalanceBuilder().run {
processDeposits(
incomes = accountStats.income.values,
transfersIn = accountStats.transfersIn.values
)
processWithdrawals(
expenses = accountStats.expense.values,
transfersOut = accountStats.transfersOut.values
)
build()
}
}
}

Expand Down

0 comments on commit c77c729

Please sign in to comment.