Skip to content

Commit

Permalink
feat: connect to upgrade-personal-to-team API #WPB-11992 (#3625)
Browse files Browse the repository at this point in the history
Co-authored-by: ohassine <[email protected]>
  • Loading branch information
damian-kaczmarek and ohassine authored Nov 21, 2024
1 parent 7cd4bd6 commit c911271
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 7 deletions.
8 changes: 8 additions & 0 deletions app/src/main/kotlin/com/wire/android/di/CoreLogicModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -492,4 +492,12 @@ class UseCaseModule {
@Provides
fun provideSendFCMTokenToAPIUseCase(@KaliumCoreLogic coreLogic: CoreLogic, @CurrentAccount currentAccount: UserId) =
coreLogic.getSessionScope(currentAccount).debug.sendFCMTokenToServer

@ViewModelScoped
@Provides
fun provideMigrateFromPersonalToTeamUseCase(
@KaliumCoreLogic coreLogic: CoreLogic,
@CurrentAccount currentAccount: UserId
) =
coreLogic.getSessionScope(currentAccount).migrateFromPersonalToTeam
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
package com.wire.android.ui.userprofile.teammigration

import androidx.compose.foundation.text.input.TextFieldState
import com.wire.kalium.logic.CoreFailure

data class TeamMigrationState(
val teamNameTextState: TextFieldState = TextFieldState(),
val shouldShowMigrationLeaveDialog: Boolean = false
val shouldShowMigrationLeaveDialog: Boolean = false,
val migrationFailure: CoreFailure? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,25 @@
*/
package com.wire.android.ui.userprofile.teammigration

import androidx.compose.foundation.text.input.setTextAndSelectAll
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wire.android.feature.analytics.AnonymousAnalyticsManager
import com.wire.android.feature.analytics.model.AnalyticsEvent
import com.wire.kalium.logic.CoreFailure
import com.wire.kalium.logic.feature.user.migration.MigrateFromPersonalToTeamResult
import com.wire.kalium.logic.feature.user.migration.MigrateFromPersonalToTeamUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class TeamMigrationViewModel @Inject constructor(
private val anonymousAnalyticsManager: AnonymousAnalyticsManager
private val anonymousAnalyticsManager: AnonymousAnalyticsManager,
private val migrateFromPersonalToTeam: MigrateFromPersonalToTeamUseCase
) : ViewModel() {

var teamMigrationState by mutableStateOf(TeamMigrationState())
Expand Down Expand Up @@ -81,4 +88,31 @@ class TeamMigrationViewModel @Inject constructor(
)
)
}

fun migrateFromPersonalToTeamAccount(onSuccess: () -> Unit) {
viewModelScope.launch {
migrateFromPersonalToTeam.invoke(
teamMigrationState.teamNameTextState.text.toString(),
).let { result ->
when (result) {
is MigrateFromPersonalToTeamResult.Success -> {
teamMigrationState.teamNameTextState.setTextAndSelectAll(result.teamName)
onSuccess()
}

is MigrateFromPersonalToTeamResult.Error -> {
onMigrationFailure(result.failure)
}
}
}
}
}

fun failureHandled() {
teamMigrationState = teamMigrationState.copy(migrationFailure = null)
}

private fun onMigrationFailure(failure: CoreFailure) {
teamMigrationState = teamMigrationState.copy(migrationFailure = failure)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ import com.wire.android.navigation.style.SlideNavigationAnimation
import com.wire.android.ui.common.WireCheckbox
import com.wire.android.ui.common.colorsScheme
import com.wire.android.ui.common.dimensions
import com.wire.android.ui.common.error.CoreFailureErrorDialog
import com.wire.android.ui.destinations.TeamMigrationDoneStepScreenDestination
import com.wire.android.ui.theme.WireTheme
import com.wire.android.ui.theme.wireTypography
import com.wire.android.ui.userprofile.teammigration.PersonalToTeamMigrationNavGraph
import com.wire.android.ui.userprofile.teammigration.TeamMigrationState
import com.wire.android.ui.userprofile.teammigration.TeamMigrationViewModel
import com.wire.android.ui.userprofile.teammigration.common.BottomLineButtons
import com.wire.android.ui.userprofile.teammigration.common.BulletList
Expand All @@ -66,21 +68,43 @@ fun TeamMigrationConfirmationStepScreen(
navigator: DestinationsNavigator,
teamMigrationViewModel: TeamMigrationViewModel
) {
val state = remember { teamMigrationViewModel.teamMigrationState }

TeamMigrationConfirmationStepScreenContent(
onContinueButtonClicked = {
// TODO: call the API to migrate the user to the team, if successful navigate to next screen
navigator.navigate(TeamMigrationDoneStepScreenDestination)
teamMigrationViewModel.migrateFromPersonalToTeamAccount(
onSuccess = {
navigator.navigate(TeamMigrationDoneStepScreenDestination)
},
)
},
onBackPressed = {
navigator.popBackStack()
}
)

HandleErrors(state, teamMigrationViewModel::failureHandled)

LaunchedEffect(Unit) {
teamMigrationViewModel.sendPersonalTeamCreationFlowStartedEvent(3)
}
}

@Composable
private fun HandleErrors(
teamMigrationState: TeamMigrationState,
onFailureHandled: () -> Unit
) {
val failure = teamMigrationState.migrationFailure ?: return
// TODO handle error WPB-14281
CoreFailureErrorDialog(
coreFailure = failure,
onDialogDismiss = {
onFailureHandled()
}
)
}

@Composable
private fun TeamMigrationConfirmationStepScreenContent(
modifier: Modifier = Modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,26 @@
*/
package com.wire.android.ui.userprofile.teammigration

import androidx.compose.foundation.text.input.setTextAndPlaceCursorAtEnd
import com.wire.android.config.CoroutineTestExtension
import com.wire.android.feature.analytics.AnonymousAnalyticsManager
import com.wire.android.feature.analytics.model.AnalyticsEvent
import com.wire.kalium.logic.NetworkFailure
import com.wire.kalium.logic.feature.user.migration.MigrateFromPersonalToTeamResult
import com.wire.kalium.logic.feature.user.migration.MigrateFromPersonalToTeamUseCase
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.impl.annotations.MockK
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.internal.assertEquals
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

@ExtendWith(CoroutineTestExtension::class)
class TeamMigrationViewModelTest {

@Test
Expand Down Expand Up @@ -157,17 +168,75 @@ class TeamMigrationViewModelTest {
}
}

@Test
fun `given team name, when migrateFromPersonalToTeamAccount return success, then call use case and onSuccess`() =
runTest {
val (arrangement, viewModel) = Arrangement()
.withMigrateFromPersonalToTeamSuccess()
.arrange()

val onSuccess = mockk<() -> Unit>(relaxed = true)

viewModel.migrateFromPersonalToTeamAccount(onSuccess)

coVerify(exactly = 1) {
arrangement.migrateFromPersonalToTeam(Arrangement.TEAM_NAME)
}
verify(exactly = 1) { onSuccess() }
}

@Test
fun `given team name, when migrateFromPersonalToTeamAccount return failure, then call use case and handle the failure`() =
runTest {
val (arrangement, viewModel) = Arrangement()
.withMigrateFromPersonalToTeamError()
.arrange()

val onSuccess = {}

viewModel.migrateFromPersonalToTeamAccount(onSuccess)

coVerify(exactly = 1) {
arrangement.migrateFromPersonalToTeam(Arrangement.TEAM_NAME)
}
Assertions.assertNotNull(viewModel.teamMigrationState.migrationFailure)
viewModel.failureHandled()
Assertions.assertNull(viewModel.teamMigrationState.migrationFailure)
}

private class Arrangement {

@MockK
lateinit var anonymousAnalyticsManager: AnonymousAnalyticsManager

@MockK
lateinit var migrateFromPersonalToTeam: MigrateFromPersonalToTeamUseCase

init {
MockKAnnotations.init(this, relaxUnitFun = true)
}

fun arrange() = this to TeamMigrationViewModel(
anonymousAnalyticsManager = anonymousAnalyticsManager
)
anonymousAnalyticsManager = anonymousAnalyticsManager,
migrateFromPersonalToTeam = migrateFromPersonalToTeam,
).also { viewModel ->
viewModel.teamMigrationState.teamNameTextState.setTextAndPlaceCursorAtEnd(TEAM_NAME)
}

fun withMigrateFromPersonalToTeamSuccess() = apply {
coEvery { migrateFromPersonalToTeam(any()) } returns MigrateFromPersonalToTeamResult.Success(
TEAM_NAME
)
}

fun withMigrateFromPersonalToTeamError() = apply {
coEvery { migrateFromPersonalToTeam(any()) } returns MigrateFromPersonalToTeamResult.Error(
NetworkFailure.NoNetworkConnection(null)
)
}

companion object {
const val TEAM_NAME = "teamName"
}
}
}
2 changes: 1 addition & 1 deletion kalium
Submodule kalium updated 36 files
+12 βˆ’0 calling/build.gradle.kts
+9 βˆ’0 cryptography/build.gradle.kts
+3 βˆ’0 data/src/commonMain/kotlin/com/wire/kalium/logic/data/user/CreateUserTeam.kt
+4 βˆ’0 logic/src/commonMain/kotlin/com/wire/kalium/logic/configuration/UserConfigRepository.kt
+16 βˆ’0 logic/src/commonMain/kotlin/com/wire/kalium/logic/data/user/UserRepository.kt
+16 βˆ’10 logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/UserSessionScope.kt
+5 βˆ’0 logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/call/CallsScope.kt
+49 βˆ’0 .../src/commonMain/kotlin/com/wire/kalium/logic/feature/call/usecase/ObserveConferenceCallingEnabledUseCase.kt
+52 βˆ’0 logic/src/commonMain/kotlin/com/wire/kalium/logic/feature/user/migration/MigrateFromPersonalToTeamUseCase.kt
+69 βˆ’13 logic/src/commonTest/kotlin/com/wire/kalium/logic/data/user/UserRepositoryTest.kt
+127 βˆ’0 .../commonTest/kotlin/com/wire/kalium/logic/feature/call/usecase/ObserveConferenceCallingEnabledUseCaseTest.kt
+151 βˆ’0 .../src/commonTest/kotlin/com/wire/kalium/logic/feature/user/migration/MigrateFromPersonalToTeamUseCaseTest.kt
+68 βˆ’0 mocks/src/commonMain/kotlin/com/wire/kalium/mocks/responses/MigrationUserToTeamResponseJson.kt
+27 βˆ’0 network-model/src/commonMain/kotlin/com/wire/kalium/network/api/authenticated/user/CreateUserTeamDTO.kt
+34 βˆ’0 network/build.gradle.kts
+30 βˆ’12 network/src/commonMain/kotlin/com/wire/kalium/network/BackendMetaDataUtil.kt
+30 βˆ’0 network/src/commonMain/kotlin/com/wire/kalium/network/api/base/authenticated/UpgradePersonalToTeamApi.kt
+33 βˆ’0 network/src/commonMain/kotlin/com/wire/kalium/network/api/v0/authenticated/UpgradePersonalToTeamApiV0.kt
+7 βˆ’0 ...ain/kotlin/com/wire/kalium/network/api/v0/authenticated/networkContainer/AuthenticatedNetworkContainerV0.kt
+26 βˆ’0 network/src/commonMain/kotlin/com/wire/kalium/network/api/v2/authenticated/UpgradePersonalToTeamApiV2.kt
+7 βˆ’0 ...ain/kotlin/com/wire/kalium/network/api/v2/authenticated/networkContainer/AuthenticatedNetworkContainerV2.kt
+26 βˆ’0 network/src/commonMain/kotlin/com/wire/kalium/network/api/v3/authenticated/UpgradePersonalToTeamApiV3.kt
+7 βˆ’0 ...ain/kotlin/com/wire/kalium/network/api/v3/authenticated/networkContainer/AuthenticatedNetworkContainerV3.kt
+26 βˆ’0 network/src/commonMain/kotlin/com/wire/kalium/network/api/v4/authenticated/UpgradePersonalToTeamApiV4.kt
+7 βˆ’0 ...ain/kotlin/com/wire/kalium/network/api/v4/authenticated/networkContainer/AuthenticatedNetworkContainerV4.kt
+26 βˆ’0 network/src/commonMain/kotlin/com/wire/kalium/network/api/v5/authenticated/UpgradePersonalToTeamApiV5.kt
+7 βˆ’0 ...ain/kotlin/com/wire/kalium/network/api/v5/authenticated/networkContainer/AuthenticatedNetworkContainerV5.kt
+26 βˆ’0 network/src/commonMain/kotlin/com/wire/kalium/network/api/v6/authenticated/UpgradePersonalToTeamApiV6.kt
+8 βˆ’0 ...ain/kotlin/com/wire/kalium/network/api/v6/authenticated/networkContainer/AuthenticatedNetworkContainerV6.kt
+54 βˆ’0 network/src/commonMain/kotlin/com/wire/kalium/network/api/v7/authenticated/UpgradePersonalToTeamApiV7.kt
+7 βˆ’0 ...ain/kotlin/com/wire/kalium/network/api/v7/authenticated/networkContainer/AuthenticatedNetworkContainerV7.kt
+5 βˆ’0 network/src/commonMain/kotlin/com/wire/kalium/network/networkContainer/AuthenticatedNetworkContainer.kt
+2 βˆ’0 network/src/commonMain/kotlin/com/wire/kalium/network/networkContainer/UnauthenticatedNetworkContainer.kt
+97 βˆ’0 network/src/commonTest/kotlin/com/wire/kalium/api/v7/UpgradePersonalToTeamApiV7Test.kt
+16 βˆ’0 persistence/src/commonMain/kotlin/com/wire/kalium/persistence/config/UserConfigStorage.kt
+112 βˆ’0 scripts/generate_new_api_version.sh

0 comments on commit c911271

Please sign in to comment.