-
Notifications
You must be signed in to change notification settings - Fork 628
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Domain] Build AccountBalanceUseCase (#3418)
* Calculates the all balance in all assets that it have * Create None Balance * impl calculate method to return ExchangedAccountBalance * create a BalanceBuilder * improve the logic * add NoneBalance * drop unnecessary code * return NoneBalance if the balance is empty * refactor * get new value from merge * refactor * rename methods params * refactor * renaming * Add KDocs * solve detekt issues * refactor * Create BalanceBuilderTest * solve detekt issues * unit test - when stats are empty * unit test - process one deposit come from incomes * unit test - process two deposits in different currencies come from incomes * unit test - process two deposits in same currency come from incomes and transfersIn * unit test - process two deposits in different currencies come from incomes and transfersIn * unit test - process two deposits in different currencies come from incomes and one transfersIn * improve the logic when the balance should be empty * unit tests - refactor and add new cases * improve the BalanceBuilder * unit test - add two more cases
- Loading branch information
1 parent
a116fff
commit 2db1bdd
Showing
3 changed files
with
451 additions
and
5 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
shared/domain/src/main/java/com/ivy/domain/usecase/BalanceBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.ivy.domain.usecase | ||
|
||
import com.ivy.data.model.primitive.AssetCode | ||
import com.ivy.data.model.primitive.NonZeroDouble | ||
import com.ivy.data.model.primitive.PositiveDouble | ||
|
||
class BalanceBuilder { | ||
|
||
private val balance = mutableMapOf<AssetCode, NonZeroDouble>() | ||
|
||
/** | ||
* Updates `balance` with deposits from income and transfers in. | ||
* | ||
* @param incomes Deposit amounts from income sources. | ||
* @param transfersIn Deposit amounts from transfer in sources. | ||
*/ | ||
fun processDeposits( | ||
incomes: Map<AssetCode, PositiveDouble>, | ||
transfersIn: Map<AssetCode, PositiveDouble>, | ||
) { | ||
combine(incomes, transfersIn).forEach { (asset, amount) -> | ||
NonZeroDouble | ||
.from((balance[asset]?.value ?: 0.0) + amount.value) | ||
.onRight { newValue -> | ||
balance[asset] = newValue | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Updates `balance` with withdrawals from expenses and transfers out. | ||
* | ||
* @param expenses Deposit amounts from expense sources. | ||
* @param transfersOut Deposit amounts from transfer out sources. | ||
*/ | ||
fun processWithdrawals( | ||
expenses: Map<AssetCode, PositiveDouble>, | ||
transfersOut: Map<AssetCode, PositiveDouble>, | ||
) { | ||
combine(expenses, transfersOut).forEach { (asset, amount) -> | ||
val sub = (balance[asset]?.value ?: 0.0) - amount.value | ||
NonZeroDouble | ||
.from(sub) | ||
.onLeft { | ||
if (sub == 0.0) { | ||
balance.remove(asset) | ||
} | ||
} | ||
.onRight { newValue -> | ||
balance[asset] = newValue | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Combines two maps by summing their values. If the sum results in an error, | ||
* the value from the `a` map is used. | ||
* | ||
* @return A map with combined values from the two input maps. | ||
*/ | ||
private fun combine( | ||
a: Map<AssetCode, PositiveDouble>, | ||
b: Map<AssetCode, PositiveDouble>, | ||
): Map<AssetCode, PositiveDouble> { | ||
val c = a.toMutableMap() | ||
b.forEach { (asset, amount) -> | ||
c.merge(asset, amount) { b, c -> | ||
PositiveDouble | ||
.from(b.value + c.value) | ||
.fold( | ||
ifLeft = { c }, | ||
ifRight = { it } | ||
) | ||
} | ||
} | ||
return c | ||
} | ||
|
||
fun build(): Map<AssetCode, NonZeroDouble> = balance | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.