Updated on 2026-08-14

This commit is contained in:
Tangem 2026-02-24 19:49:20 +04:00
parent 63698bed23
commit 189cb19ff2
4 changed files with 138 additions and 6 deletions

View file

@ -1,9 +1,9 @@
package com.tangem.tap.di.domain
import com.tangem.core.analytics.api.AnalyticsEventHandler
import com.tangem.domain.account.repository.AccountsCRUDRepository
import com.tangem.domain.common.wallets.UserWalletsListRepository
import com.tangem.domain.redux.ReduxStateHolder
import com.tangem.domain.tokens.repository.CurrenciesRepository
import com.tangem.domain.transaction.WalletAddressServiceRepository
import com.tangem.domain.transaction.usecase.ParseSharedAddressUseCase
import com.tangem.domain.transaction.usecase.ValidateWalletAddressUseCase
@ -272,11 +272,11 @@ internal object WalletsDomainModule {
@Singleton
fun providesSetNotificationsEnabledUseCase(
walletsRepository: WalletsRepository,
currenciesRepository: CurrenciesRepository,
accountsCRUDRepository: AccountsCRUDRepository,
): SetNotificationsEnabledUseCase {
return SetNotificationsEnabledUseCase(
walletsRepository = walletsRepository,
currenciesRepository = currenciesRepository,
accountsCRUDRepository = accountsCRUDRepository,
)
}

View file

@ -25,6 +25,7 @@ dependencies {
implementation(projects.domain.walletManager)
implementation(projects.libs.blockchainSdk)
implementation(projects.libs.tangemSdkApi)
implementation(projects.domain.account)
implementation(projects.domain.models)
implementation(projects.domain.tokens)
implementation(projects.domain.card)

View file

@ -1,13 +1,13 @@
package com.tangem.domain.wallets.usecase
import arrow.core.Either
import com.tangem.domain.tokens.repository.CurrenciesRepository
import com.tangem.domain.account.repository.AccountsCRUDRepository
import com.tangem.domain.models.wallet.UserWalletId
import com.tangem.domain.wallets.repository.WalletsRepository
class SetNotificationsEnabledUseCase(
private val walletsRepository: WalletsRepository,
private val currenciesRepository: CurrenciesRepository,
private val accountsCRUDRepository: AccountsCRUDRepository,
) {
suspend operator fun invoke(userWalletId: UserWalletId, isEnabled: Boolean): Either<Throwable, Unit> =
@ -16,7 +16,7 @@ class SetNotificationsEnabledUseCase(
userWalletId = userWalletId,
isEnabled = isEnabled,
)
currenciesRepository.syncTokens(userWalletId)
accountsCRUDRepository.syncTokens(userWalletId)
}.onLeft {
walletsRepository.setNotificationsEnabled(
userWalletId = userWalletId,

View file

@ -0,0 +1,131 @@
package com.tangem.domain.wallets.usecase
import com.google.common.truth.Truth.assertThat
import com.tangem.domain.account.repository.AccountsCRUDRepository
import com.tangem.domain.models.wallet.UserWalletId
import com.tangem.domain.wallets.repository.WalletsRepository
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.coVerifyOrder
import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
class SetNotificationsEnabledUseCaseTest {
private lateinit var useCase: SetNotificationsEnabledUseCase
private lateinit var walletsRepository: WalletsRepository
private lateinit var accountsCRUDRepository: AccountsCRUDRepository
@Before
fun setup() {
walletsRepository = mockk()
accountsCRUDRepository = mockk()
useCase = SetNotificationsEnabledUseCase(
walletsRepository = walletsRepository,
accountsCRUDRepository = accountsCRUDRepository,
)
}
@Test
fun `GIVEN notifications enabled successfully WHEN invoke THEN return Right with Unit`() = runTest {
// GIVEN
val userWalletId = UserWalletId("0A0B0C0D")
val isEnabled = true
coEvery { walletsRepository.setNotificationsEnabled(userWalletId, isEnabled) } just runs
coEvery { accountsCRUDRepository.syncTokens(userWalletId) } just runs
// WHEN
val result = useCase(userWalletId, isEnabled)
// THEN
assertThat(result.isRight()).isTrue()
coVerifyOrder {
walletsRepository.setNotificationsEnabled(userWalletId, isEnabled)
accountsCRUDRepository.syncTokens(userWalletId)
}
}
@Test
fun `GIVEN notifications disabled successfully WHEN invoke THEN return Right with Unit`() = runTest {
// GIVEN
val userWalletId = UserWalletId("0A0B0C0D")
val isEnabled = false
coEvery { walletsRepository.setNotificationsEnabled(userWalletId, isEnabled) } just runs
coEvery { accountsCRUDRepository.syncTokens(userWalletId) } just runs
// WHEN
val result = useCase(userWalletId, isEnabled)
// THEN
assertThat(result.isRight()).isTrue()
coVerifyOrder {
walletsRepository.setNotificationsEnabled(userWalletId, isEnabled)
accountsCRUDRepository.syncTokens(userWalletId)
}
}
@Test
fun `GIVEN setNotificationsEnabled throws exception WHEN invoke THEN return Left and revert notifications`() = runTest {
// GIVEN
val userWalletId = UserWalletId("0A0B0C0D")
val isEnabled = true
val exception = RuntimeException("Network error")
coEvery { walletsRepository.setNotificationsEnabled(userWalletId, isEnabled) } throws exception
coEvery { walletsRepository.setNotificationsEnabled(userWalletId, !isEnabled) } just runs
// WHEN
val result = useCase(userWalletId, isEnabled)
// THEN
assertThat(result.isLeft()).isTrue()
result.onLeft { throwable ->
assertThat(throwable).isEqualTo(exception)
}
coVerify { walletsRepository.setNotificationsEnabled(userWalletId, !isEnabled) }
}
@Test
fun `GIVEN syncTokens throws exception WHEN invoke THEN return Left and revert notifications`() = runTest {
// GIVEN
val userWalletId = UserWalletId("0A0B0C0D")
val isEnabled = true
val exception = RuntimeException("Sync error")
coEvery { walletsRepository.setNotificationsEnabled(userWalletId, isEnabled) } just runs
coEvery { accountsCRUDRepository.syncTokens(userWalletId) } throws exception
coEvery { walletsRepository.setNotificationsEnabled(userWalletId, !isEnabled) } just runs
// WHEN
val result = useCase(userWalletId, isEnabled)
// THEN
assertThat(result.isLeft()).isTrue()
result.onLeft { throwable ->
assertThat(throwable).isEqualTo(exception)
}
coVerify { walletsRepository.setNotificationsEnabled(userWalletId, !isEnabled) }
}
@Test
fun `GIVEN disabling notifications fails WHEN invoke THEN return Left and revert to enabled`() = runTest {
// GIVEN
val userWalletId = UserWalletId("0A0B0C0D")
val isEnabled = false
val exception = RuntimeException("Network error")
coEvery { walletsRepository.setNotificationsEnabled(userWalletId, isEnabled) } throws exception
coEvery { walletsRepository.setNotificationsEnabled(userWalletId, !isEnabled) } just runs
// WHEN
val result = useCase(userWalletId, isEnabled)
// THEN
assertThat(result.isLeft()).isTrue()
result.onLeft { throwable ->
assertThat(throwable).isEqualTo(exception)
}
coVerify { walletsRepository.setNotificationsEnabled(userWalletId, true) }
}
}