Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ohassine committed Nov 20, 2024
1 parent 63633ba commit 39d8710
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ import com.wire.kalium.logic.functional.fold
import com.wire.kalium.logic.functional.map
import com.wire.kalium.logic.wrapApiRequest
import com.wire.kalium.logic.wrapStorageRequest
import com.wire.kalium.network.api.unbound.configuration.ApiVersionDTO
import com.wire.kalium.network.api.base.authenticated.UpgradePersonalToTeamApi.Companion.MIN_API_VERSION
import com.wire.kalium.network.api.base.unbound.versioning.VersionApi
import com.wire.kalium.network.api.unbound.configuration.ApiVersionDTO
import com.wire.kalium.persistence.daokaliumdb.ServerConfigurationDAO
import com.wire.kalium.util.KaliumDispatcher
import com.wire.kalium.util.KaliumDispatcherImpl
import io.ktor.http.Url
import kotlinx.coroutines.withContext

interface ServerConfigRepository {
val minimumApiVersionForPersonalToTeamAccountMigration: Int

suspend fun getOrFetchMetadata(serverLinks: ServerConfig.Links): Either<CoreFailure, ServerConfig>
suspend fun storeConfig(links: ServerConfig.Links, metadata: ServerConfig.MetaData): Either<StorageFailure, ServerConfig>

Expand Down Expand Up @@ -73,6 +76,8 @@ internal class ServerConfigDataSource(
private val dispatchers: KaliumDispatcher = KaliumDispatcherImpl
) : ServerConfigRepository {

override val minimumApiVersionForPersonalToTeamAccountMigration = MIN_API_VERSION

override suspend fun getOrFetchMetadata(serverLinks: ServerConfig.Links): Either<CoreFailure, ServerConfig> =
wrapStorageRequest { dao.configByLinks(serverConfigMapper.toEntity(serverLinks)) }.fold({
fetchApiVersionAndStore(serverLinks)
Expand Down Expand Up @@ -128,8 +133,8 @@ internal class ServerConfigDataSource(
}

override suspend fun updateConfigApiVersion(serverConfig: ServerConfig): Either<CoreFailure, Unit> =
fetchMetadata(serverConfig.links)
.flatMap { wrapStorageRequest { dao.updateApiVersion(serverConfig.id, it.commonApiVersion.version) } }
fetchMetadata(serverConfig.links)
.flatMap { wrapStorageRequest { dao.updateApiVersion(serverConfig.id, it.commonApiVersion.version) } }

override suspend fun configForUser(userId: UserId): Either<StorageFailure, ServerConfig> =
wrapStorageRequest { dao.configForUser(userId.toDao()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package com.wire.kalium.logic.feature.personaltoteamaccount
import com.wire.kalium.logic.configuration.server.ServerConfigRepository
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.functional.fold
import com.wire.kalium.network.api.base.authenticated.UpgradePersonalToTeamApi.Companion.MIN_API_VERSION

/**
* Use case to check if the backend supports personal to team account migration.
Expand All @@ -36,7 +35,7 @@ internal class IsPersonalToTeamAccountSupportedByBackendUseCaseImpl(
override suspend fun invoke(): Boolean {
return serverConfigRepository.commonApiVersion(userId.domain).fold(
{ false },
{ it >= MIN_API_VERSION }
{ it >= serverConfigRepository.minimumApiVersionForPersonalToTeamAccountMigration }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import com.wire.kalium.logic.functional.Either
import io.mockative.Mock
import io.mockative.coEvery
import io.mockative.coVerify
import io.mockative.every
import io.mockative.mock
import io.mockative.once
import kotlinx.coroutines.runBlocking
Expand All @@ -38,7 +39,8 @@ class IsPersonalToTeamAccountSupportedByBackendUseCaseTest {
fun givenAPIVersionBelowMinimum_whenInvoking_thenReturnsFalse() = runTest {
// Given
val (arrangement, useCase) = Arrangement()
.withRepositoryReturning(Either.Right(6))
.withRepositoryReturningMinimumApiVersion()
.withRepositoryReturningCommonApiVersion(Either.Right(6))
.arrange()

// When
Expand All @@ -55,7 +57,8 @@ class IsPersonalToTeamAccountSupportedByBackendUseCaseTest {
fun givenAPIVersionEqualToMinimum_whenInvoking_thenReturnsTrue() = runBlocking {
// Given
val (arrangement, useCase) = Arrangement()
.withRepositoryReturning(Either.Right(7))
.withRepositoryReturningMinimumApiVersion()
.withRepositoryReturningCommonApiVersion(Either.Right(7))
.arrange()

// When
Expand All @@ -72,7 +75,8 @@ class IsPersonalToTeamAccountSupportedByBackendUseCaseTest {
fun givenAPIVersionAboveMinimum_whenInvoking_thenReturnsTrue() = runBlocking {
// Given
val (arrangement, useCase) = Arrangement()
.withRepositoryReturning(Either.Right(8))
.withRepositoryReturningMinimumApiVersion()
.withRepositoryReturningCommonApiVersion(Either.Right(8))
.arrange()

// When
Expand All @@ -89,7 +93,8 @@ class IsPersonalToTeamAccountSupportedByBackendUseCaseTest {
fun givenErrorFetchingAPIVersion_whenInvoking_thenReturnsFalse() = runTest {
// Given
val (arrangement, useCase) = Arrangement()
.withRepositoryReturning(Either.Left(CoreFailure.SyncEventOrClientNotFound))
.withRepositoryReturningMinimumApiVersion()
.withRepositoryReturningCommonApiVersion(Either.Left(CoreFailure.SyncEventOrClientNotFound))
.arrange()

// When
Expand All @@ -107,15 +112,25 @@ class IsPersonalToTeamAccountSupportedByBackendUseCaseTest {
@Mock
val serverConfigRepository = mock(ServerConfigRepository::class)

suspend fun withRepositoryReturning(value: Either<CoreFailure, Int>) = apply {
suspend fun withRepositoryReturningCommonApiVersion(value: Either<CoreFailure, Int>) = apply {
coEvery {
serverConfigRepository.commonApiVersion(TestUser.USER_ID.domain)
}.returns(value)
}

fun withRepositoryReturningMinimumApiVersion() = apply {
every {
serverConfigRepository.minimumApiVersionForPersonalToTeamAccountMigration
}.returns(MIN_API_VERSION)
}

fun arrange() = this to IsPersonalToTeamAccountSupportedByBackendUseCaseImpl(
serverConfigRepository = serverConfigRepository,
userId = TestUser.USER_ID
)
}

companion object {
private const val MIN_API_VERSION = 7
}
}

0 comments on commit 39d8710

Please sign in to comment.