Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion feature/client-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ android {

dependencies {

implementation(project(":infra:auth-store"))
implementation(project(":infra:config-store"))
implementation(project(":infra:config-sync"))
implementation(project(":infra:events"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import com.simprints.feature.clientapi.usecases.GetCurrentSessionIdUseCase
import com.simprints.feature.clientapi.usecases.GetEnrolmentCreationEventForSubjectUseCase
import com.simprints.feature.clientapi.usecases.IsFlowCompletedWithErrorUseCase
import com.simprints.feature.clientapi.usecases.SimpleEventReporter
import com.simprints.infra.authstore.AuthStore
import com.simprints.infra.config.store.models.Project
import com.simprints.infra.config.store.models.TokenKeyType
import com.simprints.infra.config.store.tokenization.TokenizationProcessor
Expand Down Expand Up @@ -55,7 +54,6 @@ class ClientApiViewModel @Inject internal constructor(
private val getEnrolmentCreationEventForSubject: GetEnrolmentCreationEventForSubjectUseCase,
private val deleteSessionEventsIfNeeded: DeleteSessionEventsIfNeededUseCase,
private val isFlowCompletedWithError: IsFlowCompletedWithErrorUseCase,
private val authStore: AuthStore,
private val configManager: ConfigManager,
private val timeHelper: TimeHelper,
private val persistentLogger: PersistentLogger,
Expand All @@ -73,7 +71,7 @@ class ClientApiViewModel @Inject internal constructor(
get() = _showAlert
private val _showAlert = MutableLiveData<LiveDataEventWithContent<ClientApiError>>()

private suspend fun getProject() = runCatching { configManager.getProject(authStore.signedInProjectId) }.getOrNull()
private suspend fun getProject() = runCatching { configManager.getProject() }.getOrNull()

suspend fun handleIntent(
action: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.simprints.feature.clientapi.usecases

import com.simprints.core.SessionCoroutineScope
import com.simprints.infra.authstore.AuthStore
import com.simprints.infra.config.store.models.ProjectState
import com.simprints.infra.config.store.models.canSyncDataToSimprints
import com.simprints.infra.config.sync.ConfigManager
Expand All @@ -11,13 +10,12 @@ import kotlinx.coroutines.launch
import javax.inject.Inject

internal class DeleteSessionEventsIfNeededUseCase @Inject constructor(
private val authStore: AuthStore,
private val configManager: ConfigManager,
private val eventRepository: EventRepository,
@SessionCoroutineScope private val sessionCoroutineScope: CoroutineScope,
) {
operator fun invoke(sessionId: String) = sessionCoroutineScope.launch {
val projectNotRunning = configManager.getProject(authStore.signedInProjectId).state != ProjectState.RUNNING
val projectNotRunning = configManager.getProject()?.state != ProjectState.RUNNING
val simprintsDataSyncDisabled = !configManager.getProjectConfiguration().canSyncDataToSimprints()

if (simprintsDataSyncDisabled || projectNotRunning) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import com.simprints.feature.clientapi.usecases.GetCurrentSessionIdUseCase
import com.simprints.feature.clientapi.usecases.GetEnrolmentCreationEventForSubjectUseCase
import com.simprints.feature.clientapi.usecases.IsFlowCompletedWithErrorUseCase
import com.simprints.feature.clientapi.usecases.SimpleEventReporter
import com.simprints.infra.authstore.AuthStore
import com.simprints.infra.config.store.models.Project
import com.simprints.infra.config.store.models.TokenKeyType
import com.simprints.infra.config.store.tokenization.TokenizationProcessor
Expand Down Expand Up @@ -70,9 +69,6 @@ internal class ClientApiViewModelTest {
@MockK
lateinit var isFlowCompletedWithError: IsFlowCompletedWithErrorUseCase

@MockK
lateinit var authStore: AuthStore

@MockK
lateinit var configManager: ConfigManager

Expand Down Expand Up @@ -108,7 +104,6 @@ internal class ClientApiViewModelTest {
getEnrolmentCreationEventForSubject = getEnrolmentCreationEventForSubject,
deleteSessionEventsIfNeeded = deleteSessionEventsIfNeeded,
isFlowCompletedWithError = isFlowCompletedWithError,
authStore = authStore,
configManager = configManager,
timeHelper = timeHelper,
persistentLogger = persistentLogger,
Expand Down Expand Up @@ -323,9 +318,7 @@ internal class ClientApiViewModelTest {
project: Project,
returnValue: TokenizableString,
) {
val projectId = "projectId"
every { authStore.signedInProjectId } returns projectId
coEvery { configManager.getProject(projectId) } returns project
coEvery { configManager.getProject() } returns project
every {
tokenizationProcessor.decrypt(
encrypted = any(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package com.simprints.feature.clientapi.usecases

import com.simprints.infra.authstore.AuthStore
import com.simprints.infra.config.store.models.ProjectState
import com.simprints.infra.config.store.models.UpSynchronizationConfiguration
import com.simprints.infra.config.sync.ConfigManager
import com.simprints.infra.events.EventRepository
import com.simprints.testtools.common.coroutines.TestCoroutineRule
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.*
import io.mockk.impl.annotations.MockK
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.test.runTest
Expand All @@ -21,9 +17,6 @@ class DeleteSessionEventsIfNeededUseCaseTest {
@get:Rule
val testCoroutineRule = TestCoroutineRule()

@MockK
private lateinit var authStore: AuthStore

@MockK
private lateinit var configManager: ConfigManager

Expand All @@ -36,19 +29,30 @@ class DeleteSessionEventsIfNeededUseCaseTest {
fun setUp() {
MockKAnnotations.init(this, relaxed = true)

every { authStore.signedInProjectId } returns "projectId"

deleteUseCase = DeleteSessionEventsIfNeededUseCase(
authStore,
configManager,
eventRepository,
CoroutineScope(testCoroutineRule.testCoroutineDispatcher),
)
}

@Test
fun `deletes session events if project missing`() = runTest {
coEvery { configManager.getProject()?.state } returns null
coEvery {
configManager
.getProjectConfiguration()
.synchronization.up.simprints.kind
} returns UpSynchronizationConfiguration.UpSynchronizationKind.ALL

deleteUseCase("sessionId")

coVerify { eventRepository.deleteEventScope("sessionId") }
}

@Test
fun `deletes session events if project paused`() = runTest {
coEvery { configManager.getProject(any()).state } returns ProjectState.PROJECT_PAUSED
coEvery { configManager.getProject()?.state } returns ProjectState.PROJECT_PAUSED
coEvery {
configManager
.getProjectConfiguration()
Expand All @@ -62,7 +66,7 @@ class DeleteSessionEventsIfNeededUseCaseTest {

@Test
fun `deletes session events if project ending`() = runTest {
coEvery { configManager.getProject(any()).state } returns ProjectState.PROJECT_ENDING
coEvery { configManager.getProject()?.state } returns ProjectState.PROJECT_ENDING
coEvery {
configManager
.getProjectConfiguration()
Expand All @@ -76,7 +80,7 @@ class DeleteSessionEventsIfNeededUseCaseTest {

@Test
fun `deletes session events if project ended`() = runTest {
coEvery { configManager.getProject(any()).state } returns ProjectState.PROJECT_ENDED
coEvery { configManager.getProject()?.state } returns ProjectState.PROJECT_ENDED
coEvery {
configManager
.getProjectConfiguration()
Expand All @@ -90,7 +94,7 @@ class DeleteSessionEventsIfNeededUseCaseTest {

@Test
fun `deletes session events if data sync disabled in running project`() = runTest {
coEvery { configManager.getProject(any()).state } returns ProjectState.RUNNING
coEvery { configManager.getProject()?.state } returns ProjectState.RUNNING
coEvery {
configManager
.getProjectConfiguration()
Expand All @@ -104,7 +108,7 @@ class DeleteSessionEventsIfNeededUseCaseTest {

@Test
fun `does not delete session events if data sync enabled in running project`() = runTest {
coEvery { configManager.getProject(any()).state } returns ProjectState.RUNNING
coEvery { configManager.getProject()?.state } returns ProjectState.RUNNING
coEvery {
configManager
.getProjectConfiguration()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.simprints.core.domain.tokenization.TokenizableString
import com.simprints.infra.authstore.AuthStore
import com.simprints.infra.config.store.models.TokenKeyType
import com.simprints.infra.config.store.tokenization.TokenizationProcessor
import com.simprints.infra.config.sync.ConfigManager
Expand All @@ -17,7 +16,6 @@ import javax.inject.Inject
@HiltViewModel
internal class ProjectDetailsViewModel @Inject constructor(
private val configManager: ConfigManager,
private val authStore: AuthStore,
private val recentUserActivityManager: RecentUserActivityManager,
private val tokenizationProcessor: TokenizationProcessor,
) : ViewModel() {
Expand All @@ -31,23 +29,23 @@ internal class ProjectDetailsViewModel @Inject constructor(

fun load() = viewModelScope.launch {
val state = try {
val projectId = authStore.signedInProjectId
val cachedProject = configManager.getProject(projectId)
val recentUserActivity = recentUserActivityManager.getRecentUserActivity()
val decryptedUserId = when (val userId = recentUserActivity.lastUserUsed) {
is TokenizableString.Raw -> userId
is TokenizableString.Tokenized -> tokenizationProcessor.decrypt(
encrypted = userId,
tokenKeyType = TokenKeyType.AttendantId,
project = cachedProject,
configManager.getProject()?.let { cachedProject ->
val recentUserActivity = recentUserActivityManager.getRecentUserActivity()
val decryptedUserId = when (val userId = recentUserActivity.lastUserUsed) {
is TokenizableString.Raw -> userId
is TokenizableString.Tokenized -> tokenizationProcessor.decrypt(
encrypted = userId,
tokenKeyType = TokenKeyType.AttendantId,
project = cachedProject,
)
}
DashboardProjectState(
title = cachedProject.name,
lastUser = decryptedUserId.value,
lastScanner = recentUserActivity.lastScannerUsed,
isLoaded = true,
)
}
DashboardProjectState(
title = cachedProject.name,
lastUser = decryptedUserId.value,
lastScanner = recentUserActivity.lastScannerUsed,
isLoaded = true,
)
} ?: DashboardProjectState(isLoaded = false)
} catch (_: Throwable) {
DashboardProjectState(isLoaded = false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,8 @@ internal class SyncInfoViewModel @Inject constructor(
}

syncOrchestrator.stopEventSync()
val projectState = try {
configManager.getProject(authStore.signedInProjectId).state
} catch (_: Exception) {
// If the device is compromised, project data is deleted. Access attempts will throw an exception,
// effectively appearing to the user as if the project has ended.
ProjectState.PROJECT_ENDED
}

val isDownSyncAllowed = !isPreLogoutUpSync && projectState == ProjectState.RUNNING
val isDownSyncAllowed = !isPreLogoutUpSync && configManager.getProject()?.state == ProjectState.RUNNING
syncOrchestrator.startEventSync(isDownSyncAllowed)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.simprints.core.ExternalScope
import com.simprints.core.domain.tokenization.TokenizableString
import com.simprints.core.domain.tokenization.asTokenizableRaw
import com.simprints.feature.dashboard.settings.syncinfo.moduleselection.exceptions.NoModuleSelectedException
import com.simprints.feature.dashboard.settings.syncinfo.moduleselection.exceptions.TooManyModulesSelectedException
import com.simprints.feature.dashboard.settings.syncinfo.moduleselection.repository.Module
import com.simprints.feature.dashboard.settings.syncinfo.moduleselection.repository.ModuleRepository
import com.simprints.infra.authstore.AuthStore
import com.simprints.infra.config.store.models.SettingsPasswordConfig
import com.simprints.infra.config.store.models.TokenKeyType
import com.simprints.infra.config.store.tokenization.TokenizationProcessor
Expand All @@ -23,7 +23,6 @@ import javax.inject.Inject

@HiltViewModel
internal class ModuleSelectionViewModel @Inject constructor(
private val authStore: AuthStore,
private val moduleRepository: ModuleRepository,
private val syncOrchestrator: SyncOrchestrator,
private val configManager: ConfigManager,
Expand Down Expand Up @@ -51,11 +50,13 @@ internal class ModuleSelectionViewModel @Inject constructor(
moduleRepository.getModules().map { module ->
val decryptedName = when (val name = module.name) {
is TokenizableString.Raw -> name
is TokenizableString.Tokenized -> tokenizationProcessor.decrypt(
encrypted = name,
tokenKeyType = TokenKeyType.ModuleId,
project = configManager.getProject(authStore.signedInProjectId),
)
is TokenizableString.Tokenized -> configManager.getProject()?.let {
tokenizationProcessor.decrypt(
encrypted = name,
tokenKeyType = TokenKeyType.ModuleId,
project = it,
)
} ?: "".asTokenizableRaw()
}
module.copy(name = decryptedName)
}
Expand Down Expand Up @@ -99,11 +100,13 @@ internal class ModuleSelectionViewModel @Inject constructor(
externalScope.launch {
val modules = modules.map { module ->
val encryptedName = when (val name = module.name) {
is TokenizableString.Raw -> tokenizationProcessor.encrypt(
decrypted = name,
tokenKeyType = TokenKeyType.ModuleId,
project = configManager.getProject(authStore.signedInProjectId),
)
is TokenizableString.Raw -> configManager.getProject()?.let { project ->
tokenizationProcessor.encrypt(
decrypted = name,
tokenKeyType = TokenKeyType.ModuleId,
project = project,
)
} ?: "".asTokenizableRaw()

is TokenizableString.Tokenized -> name
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,7 @@ internal class ObserveSyncInfoUseCase @Inject constructor(
else -> DownSyncCounts(0, isLowerBound = false)
}

val project = try {
projectId.takeUnless { it.isBlank() }?.let { configManager.getProject(it) }
} catch (_: Exception) {
// If the device is compromised, project data is deleted. Access attempts will throw an exception,
// effectively appearing to the user as if the project has ended.
null
}

val project = configManager.getProject()
val isProjectRunning = project?.state == ProjectState.RUNNING
val moduleCounts = if (project != null) {
deviceConfig.selectedModules.map { moduleName ->
Expand Down
Loading