Updated on 2026-08-14

This commit is contained in:
Tangem 2026-07-17 07:34:43 -07:00
parent 3fd066750f
commit 172efa81b2
10 changed files with 124 additions and 63 deletions

View file

@ -1,10 +1,8 @@
package com.tangem.data.pay.repository
import arrow.core.Either
import arrow.core.right
import com.tangem.datasource.api.pay.TangemPayApi
import com.tangem.datasource.api.pay.models.response.CustomerOffersResponse
import com.tangem.datasource.local.datastore.RuntimeSharedStore
import com.tangem.domain.models.wallet.UserWalletId
import com.tangem.domain.pay.model.Offer
import com.tangem.domain.pay.model.OrderType
@ -18,18 +16,11 @@ internal class DefaultCustomerOffersRepository @Inject constructor(
private val requestHelper: TangemPayRequestPerformer,
) : CustomerOffersRepository {
private val offersCache = RuntimeSharedStore<Map<String, List<Offer>>>()
override suspend fun getOffers(userWalletId: UserWalletId): Either<VisaApiError, List<Offer>> {
val key = userWalletId.stringValue
offersCache.getSyncOrNull()?.get(key)?.let { return it.right() }
return requestHelper.performRequest(userWalletId) { authHeader ->
tangemPayApi.getCustomerOffers(authHeader = authHeader)
}.map { response ->
response.result.map { it.toDomain() }
}.onRight { offers ->
offersCache.update(default = emptyMap()) { it + (key to offers) }
}
}

View file

@ -57,7 +57,7 @@ internal class DefaultCustomerOffersRepositoryTest {
}
@Test
fun `GIVEN offers fetched once WHEN getOffers called twice THEN backend is hit only once`() = runTest {
fun `GIVEN offers response WHEN getOffers called twice THEN backend is hit each time`() = runTest {
// Arrange
val repository = createRepository()
@ -68,11 +68,11 @@ internal class DefaultCustomerOffersRepositoryTest {
// Assert
assertThat(first.isRight()).isTrue()
assertThat(second).isEqualTo(first)
coVerify(exactly = 1) { tangemPayApi.getCustomerOffers(any()) }
coVerify(exactly = 2) { tangemPayApi.getCustomerOffers(any()) }
}
@Test
fun `GIVEN backend error WHEN getOffers called again THEN error is not cached and backend is hit again`() = runTest {
fun `GIVEN backend error WHEN getOffers called again THEN backend is hit again`() = runTest {
// Arrange
coEvery { tangemPayApi.getCustomerOffers(any()) } returnsMany listOf(
ApiResponse.Error(ApiResponseError.NetworkException()) as ApiResponse<CustomerOffersResponse>,