Updated on 2026-08-14
This commit is contained in:
parent
41164b1baf
commit
54e612cf30
9 changed files with 200 additions and 18 deletions
|
|
@ -0,0 +1,8 @@
|
|||
package com.tangem.domain.notifications.models
|
||||
|
||||
sealed class NotificationsError {
|
||||
|
||||
data class DataError(val message: String) : NotificationsError()
|
||||
|
||||
data object ApplicationIdNotFound : NotificationsError()
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
package com.tangem.domain.notifications
|
||||
|
||||
import com.tangem.domain.notifications.repository.PushNotificationsRepository
|
||||
|
||||
class ClearApplicationIdUseCase(
|
||||
private val pushNotificationsRepository: PushNotificationsRepository,
|
||||
) {
|
||||
|
||||
suspend operator fun invoke() {
|
||||
pushNotificationsRepository.clearApplicationId()
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,9 @@
|
|||
package com.tangem.domain.notifications
|
||||
|
||||
import arrow.core.Either
|
||||
import arrow.core.raise.either
|
||||
import com.tangem.domain.notifications.models.ApplicationId
|
||||
import com.tangem.domain.notifications.models.NotificationsError
|
||||
import com.tangem.domain.notifications.repository.PushNotificationsRepository
|
||||
import com.tangem.utils.notifications.PushNotificationsTokenProvider
|
||||
|
||||
|
|
@ -10,8 +12,8 @@ class SendPushTokenUseCase(
|
|||
private val pushNotificationsTokenProvider: PushNotificationsTokenProvider,
|
||||
) {
|
||||
|
||||
suspend operator fun invoke(applicationId: ApplicationId): Either<Throwable, Unit> = Either.catch {
|
||||
suspend operator fun invoke(applicationId: ApplicationId): Either<NotificationsError, Unit> = either {
|
||||
val token = pushNotificationsTokenProvider.getToken()
|
||||
pushNotificationsRepository.sendPushToken(applicationId, token)
|
||||
pushNotificationsRepository.sendPushToken(applicationId, token).bind()
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,9 @@
|
|||
package com.tangem.domain.notifications.repository
|
||||
|
||||
import arrow.core.Either
|
||||
import com.tangem.domain.notifications.models.ApplicationId
|
||||
import com.tangem.domain.notifications.models.NotificationsEligibleNetwork
|
||||
import com.tangem.domain.notifications.models.NotificationsError
|
||||
|
||||
interface PushNotificationsRepository {
|
||||
|
||||
|
|
@ -12,8 +14,9 @@ interface PushNotificationsRepository {
|
|||
|
||||
suspend fun getApplicationId(): ApplicationId?
|
||||
|
||||
@Throws
|
||||
suspend fun sendPushToken(appId: ApplicationId, pushToken: String)
|
||||
suspend fun clearApplicationId()
|
||||
|
||||
suspend fun sendPushToken(appId: ApplicationId, pushToken: String): Either<NotificationsError, Unit>
|
||||
|
||||
@Throws
|
||||
suspend fun getEligibleNetworks(): List<NotificationsEligibleNetwork>
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package com.tangem.domain.notifications
|
|||
import arrow.core.Either
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import com.tangem.domain.notifications.models.ApplicationId
|
||||
import com.tangem.domain.notifications.models.NotificationsError
|
||||
import com.tangem.domain.notifications.repository.PushNotificationsRepository
|
||||
import com.tangem.utils.notifications.PushNotificationsTokenProvider
|
||||
import io.mockk.coEvery
|
||||
|
|
@ -34,7 +35,7 @@ class SendPushTokenUseCaseTest {
|
|||
val applicationId = ApplicationId("test-app-id")
|
||||
val token = "test-token"
|
||||
coEvery { pushNotificationsTokenProvider.getToken() } returns token
|
||||
coEvery { pushNotificationsRepository.sendPushToken(applicationId, token) } returns Unit
|
||||
coEvery { pushNotificationsRepository.sendPushToken(applicationId, token) } returns Either.Right(Unit)
|
||||
|
||||
// WHEN
|
||||
val result = sendPushTokenUseCase(applicationId)
|
||||
|
|
@ -45,13 +46,13 @@ class SendPushTokenUseCaseTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN repository throws error WHEN invoke THEN returns error`() = runTest {
|
||||
fun `GIVEN repository returns DataError WHEN invoke THEN returns DataError`() = runTest {
|
||||
// GIVEN
|
||||
val applicationId = ApplicationId("test-app-id")
|
||||
val token = "test-token"
|
||||
val expectedError = RuntimeException("Network error")
|
||||
val expectedError = NotificationsError.DataError("Network error")
|
||||
coEvery { pushNotificationsTokenProvider.getToken() } returns token
|
||||
coEvery { pushNotificationsRepository.sendPushToken(applicationId, token) } throws expectedError
|
||||
coEvery { pushNotificationsRepository.sendPushToken(applicationId, token) } returns Either.Left(expectedError)
|
||||
|
||||
// WHEN
|
||||
val result = sendPushTokenUseCase(applicationId)
|
||||
|
|
@ -60,4 +61,38 @@ class SendPushTokenUseCaseTest {
|
|||
assertThat(result).isEqualTo(Either.Left(expectedError))
|
||||
coVerify(exactly = 1) { pushNotificationsRepository.sendPushToken(applicationId, token) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN repository returns ApplicationIdNotFound WHEN invoke THEN returns ApplicationIdNotFound`() = runTest {
|
||||
// GIVEN
|
||||
val applicationId = ApplicationId("test-app-id")
|
||||
val token = "test-token"
|
||||
coEvery { pushNotificationsTokenProvider.getToken() } returns token
|
||||
coEvery {
|
||||
pushNotificationsRepository.sendPushToken(applicationId, token)
|
||||
} returns Either.Left(NotificationsError.ApplicationIdNotFound)
|
||||
|
||||
// WHEN
|
||||
val result = sendPushTokenUseCase(applicationId)
|
||||
|
||||
// THEN
|
||||
assertThat(result).isEqualTo(Either.Left(NotificationsError.ApplicationIdNotFound))
|
||||
coVerify(exactly = 1) { pushNotificationsRepository.sendPushToken(applicationId, token) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN empty token WHEN invoke THEN sends empty token to repository`() = runTest {
|
||||
// GIVEN
|
||||
val applicationId = ApplicationId("test-app-id")
|
||||
val emptyToken = ""
|
||||
coEvery { pushNotificationsTokenProvider.getToken() } returns emptyToken
|
||||
coEvery { pushNotificationsRepository.sendPushToken(applicationId, emptyToken) } returns Either.Right(Unit)
|
||||
|
||||
// WHEN
|
||||
val result = sendPushTokenUseCase(applicationId)
|
||||
|
||||
// THEN
|
||||
assertThat(result).isEqualTo(Either.Right(Unit))
|
||||
coVerify(exactly = 1) { pushNotificationsRepository.sendPushToken(applicationId, emptyToken) }
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue