Updated on 2026-08-14
This commit is contained in:
parent
4215c91a37
commit
df186a5f5d
9 changed files with 74 additions and 9 deletions
|
|
@ -5,6 +5,7 @@ import com.tangem.domain.account.models.AccountList
|
|||
import com.tangem.domain.account.models.ArchivedAccount
|
||||
import com.tangem.domain.models.account.Account
|
||||
import com.tangem.domain.models.account.AccountId
|
||||
import com.tangem.domain.models.account.AccountName
|
||||
import com.tangem.domain.models.wallet.UserWallet
|
||||
import com.tangem.domain.models.wallet.UserWalletId
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
|
@ -113,4 +114,12 @@ interface AccountsCRUDRepository {
|
|||
|
||||
/** Synchronously retrieves all user wallets */
|
||||
fun getUserWalletsSync(): List<UserWallet>
|
||||
|
||||
/** Checks if the provided account name is the default name within the given account list
|
||||
*
|
||||
* @param accountList the list of accounts to check against
|
||||
* @param accountName the account name to be checked
|
||||
* @throws IllegalArgumentException if the account name matches the default name
|
||||
*/
|
||||
fun checkDefaultAccountName(accountList: AccountList, accountName: AccountName)
|
||||
}
|
||||
|
|
@ -1,10 +1,12 @@
|
|||
package com.tangem.domain.account.usecase
|
||||
|
||||
import arrow.core.Either
|
||||
import arrow.core.Either.Companion.catch
|
||||
import arrow.core.getOrElse
|
||||
import arrow.core.raise.Raise
|
||||
import arrow.core.raise.catch
|
||||
import arrow.core.raise.either
|
||||
import arrow.core.raise.withError
|
||||
import com.tangem.domain.account.fetcher.SingleAccountListFetcher
|
||||
import com.tangem.domain.account.models.AccountList
|
||||
import com.tangem.domain.account.repository.AccountsCRUDRepository
|
||||
|
|
@ -49,8 +51,10 @@ class AddCryptoPortfolioUseCase(
|
|||
|
||||
val newAccount = createAccount(userWalletId, accountName, icon, derivationIndex)
|
||||
|
||||
val updatedAccounts = (accountList + newAccount).getOrElse {
|
||||
raise(Error.AccountListRequirementsNotMet(it))
|
||||
val updatedAccounts = withError({ Error.AccountListRequirementsNotMet(it) }) {
|
||||
checkDefaultName(accountList, accountName)
|
||||
|
||||
(accountList + newAccount).bind()
|
||||
}
|
||||
|
||||
saveAccounts(accountList = updatedAccounts)
|
||||
|
|
@ -81,6 +85,12 @@ class AddCryptoPortfolioUseCase(
|
|||
)
|
||||
}
|
||||
|
||||
private fun Raise<AccountList.Error>.checkDefaultName(accountList: AccountList, accountName: AccountName) {
|
||||
withError({ AccountList.Error.DuplicateAccountNames }) {
|
||||
catch { crudRepository.checkDefaultAccountName(accountList, accountName) }.bind()
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun Raise<Error>.getAccountList(userWalletId: UserWalletId): AccountList {
|
||||
return catch(
|
||||
block = { crudRepository.getAccountListSync(userWalletId = userWalletId) },
|
||||
|
|
|
|||
|
|
@ -2,10 +2,7 @@ package com.tangem.domain.account.usecase
|
|||
|
||||
import arrow.core.Either
|
||||
import arrow.core.getOrElse
|
||||
import arrow.core.raise.Raise
|
||||
import arrow.core.raise.catch
|
||||
import arrow.core.raise.either
|
||||
import arrow.core.raise.ensure
|
||||
import arrow.core.raise.*
|
||||
import com.tangem.domain.account.models.AccountList
|
||||
import com.tangem.domain.account.repository.AccountsCRUDRepository
|
||||
import com.tangem.domain.models.account.Account
|
||||
|
|
@ -50,8 +47,12 @@ class UpdateCryptoPortfolioUseCase(
|
|||
.setName(name = accountName)
|
||||
.setIcon(icon = icon)
|
||||
|
||||
val updatedAccounts = (accountList + updatedAccount).getOrElse {
|
||||
raise(Error.AccountListRequirementsNotMet(it))
|
||||
val updatedAccounts = withError({ Error.AccountListRequirementsNotMet(it) }) {
|
||||
if (accountName != null) {
|
||||
checkDefaultName(accountList, accountName)
|
||||
}
|
||||
|
||||
(accountList + updatedAccount).bind()
|
||||
}
|
||||
|
||||
saveAccounts(updatedAccounts)
|
||||
|
|
@ -88,6 +89,12 @@ class UpdateCryptoPortfolioUseCase(
|
|||
return if (icon != null) this.copy(icon = icon) else this
|
||||
}
|
||||
|
||||
private fun Raise<AccountList.Error>.checkDefaultName(accountList: AccountList, accountName: AccountName) {
|
||||
withError({ AccountList.Error.DuplicateAccountNames }) {
|
||||
Either.Companion.catch { crudRepository.checkDefaultAccountName(accountList, accountName) }.bind()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents possible errors that can occur during the update operation
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -69,6 +69,7 @@ class AddCryptoPortfolioUseCaseTest {
|
|||
coVerifySequence {
|
||||
singleAccountListFetcher(SingleAccountListFetcher.Params(userWalletId))
|
||||
crudRepository.getAccountListSync(userWalletId)
|
||||
crudRepository.checkDefaultAccountName(accountList, newAccount.accountName)
|
||||
crudRepository.saveAccounts(updatedAccountList)
|
||||
mainAccountTokensMigration.migrate(userWalletId, newAccount.derivationIndex)
|
||||
}
|
||||
|
|
@ -177,6 +178,7 @@ class AddCryptoPortfolioUseCaseTest {
|
|||
coVerifySequence {
|
||||
singleAccountListFetcher(SingleAccountListFetcher.Params(userWalletId))
|
||||
crudRepository.getAccountListSync(userWalletId)
|
||||
crudRepository.checkDefaultAccountName(accountList, newAccount.accountName)
|
||||
}
|
||||
|
||||
coVerify(inverse = true) {
|
||||
|
|
@ -249,6 +251,7 @@ class AddCryptoPortfolioUseCaseTest {
|
|||
coVerifySequence {
|
||||
singleAccountListFetcher(SingleAccountListFetcher.Params(userWalletId))
|
||||
crudRepository.getAccountListSync(userWalletId)
|
||||
crudRepository.checkDefaultAccountName(accountList, newAccount.accountName)
|
||||
crudRepository.saveAccounts(updatedAccountList)
|
||||
}
|
||||
|
||||
|
|
@ -288,6 +291,7 @@ class AddCryptoPortfolioUseCaseTest {
|
|||
coVerifySequence {
|
||||
singleAccountListFetcher(SingleAccountListFetcher.Params(userWalletId))
|
||||
crudRepository.getAccountListSync(userWalletId)
|
||||
crudRepository.checkDefaultAccountName(accountList, newAccount.accountName)
|
||||
crudRepository.saveAccounts(updatedAccountList)
|
||||
mainAccountTokensMigration.migrate(userWalletId, newAccount.derivationIndex)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue