Skip to content

Commit ae1a066

Browse files
authored
Merge pull request #14821 from woocommerce/woomob-1576-woo-poslocal-catalog-reuse-wooposfullsyncstatuschecker-in
[Woo POS][Local Catalog] Consolidate requirements for local catalog support
2 parents c6c68cf + 0868295 commit ae1a066

File tree

6 files changed

+141
-195
lines changed

6 files changed

+141
-195
lines changed

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosFullSyncStatusChecker.kt

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.woocommerce.android.ui.woopos.localcatalog
22

33
import com.woocommerce.android.tools.SelectedSite
44
import com.woocommerce.android.ui.woopos.common.util.WooPosLogWrapper
5-
import com.woocommerce.android.ui.woopos.featureflags.WooPosLocalCatalogM1Enabled
65
import com.woocommerce.android.ui.woopos.util.WooPosNetworkStatus
76
import com.woocommerce.android.ui.woopos.util.datastore.WooPosPreferencesRepository
87
import com.woocommerce.android.ui.woopos.util.datastore.WooPosSyncTimestampManager
@@ -15,31 +14,20 @@ class WooPosFullSyncStatusChecker @Inject constructor(
1514
private val syncTimestampManager: WooPosSyncTimestampManager,
1615
private val selectedSite: SelectedSite,
1716
private val networkStatus: WooPosNetworkStatus,
18-
private val wooPosLocalCatalogM1Enabled: WooPosLocalCatalogM1Enabled,
1917
private val localCatalogStore: WooPosLocalCatalogStore,
2018
private val prefsRepo: WooPosPreferencesRepository,
2119
private val checkCatalogSizeAction: WooPosCheckCatalogSizeAction,
22-
private val isVariationsEndpointAvailable: WooPosIsLocalCatalogVariationsEndpointAvailable,
20+
private val isLocalCatalogSupported: WooPosIsLocalCatalogSupported,
2321
private val wooPosLogWrapper: WooPosLogWrapper
2422
) {
2523
@Suppress("ReturnCount")
2624
suspend fun checkSyncRequirement(): WooPosFullSyncRequirement {
27-
if (!wooPosLocalCatalogM1Enabled()) {
28-
wooPosLogWrapper.d("Full sync check skipped: Local catalog feature not enabled")
29-
return WooPosFullSyncRequirement.LocalCatalogDisabled("Local catalog feature not enabled")
30-
}
31-
32-
if (!isVariationsEndpointAvailable()) {
33-
wooPosLogWrapper.d("Full sync check skipped: Variations endpoint not available")
34-
return WooPosFullSyncRequirement.LocalCatalogDisabled("Variations endpoint not available")
35-
}
36-
3725
val site = selectedSite.getOrNull()
38-
if (site == null) error("No site selected")
26+
?: error("No site selected")
3927

40-
if (!prefsRepo.isPeriodicSyncEnabledForSite(site.siteId)) {
41-
wooPosLogWrapper.d("Full sync check skipped: Periodic Sync disabled for site.")
42-
return WooPosFullSyncRequirement.LocalCatalogDisabled("Periodic Sync disabled for site.")
28+
if (!isLocalCatalogSupported(site.siteId)) {
29+
wooPosLogWrapper.d("Full sync check skipped: Local catalog not supported for site")
30+
return WooPosFullSyncRequirement.LocalCatalogDisabled("Local catalog not supported for site")
4331
}
4432

4533
if (localCatalogStore.getProductCount(site.localId()).getOrNull() == 0) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.woocommerce.android.ui.woopos.localcatalog
2+
3+
import com.woocommerce.android.ui.woopos.featureflags.WooPosLocalCatalogM1Enabled
4+
import com.woocommerce.android.ui.woopos.util.datastore.WooPosPreferencesRepository
5+
import javax.inject.Inject
6+
7+
/**
8+
* Checks if local catalog is supported and enabled for a given site.
9+
*/
10+
class WooPosIsLocalCatalogSupported @Inject constructor(
11+
private val wooPosLocalCatalogM1Enabled: WooPosLocalCatalogM1Enabled,
12+
private val prefsRepo: WooPosPreferencesRepository,
13+
private val isVariationsEndpointAvailable: WooPosIsLocalCatalogVariationsEndpointAvailable,
14+
) {
15+
@Suppress("ReturnCount")
16+
suspend operator fun invoke(siteId: Long): Boolean {
17+
if (!wooPosLocalCatalogM1Enabled()) {
18+
return false
19+
}
20+
21+
if (!isVariationsEndpointAvailable()) {
22+
return false
23+
}
24+
25+
if (!prefsRepo.isPeriodicSyncEnabledForSite(siteId)) {
26+
return false
27+
}
28+
29+
return true
30+
}
31+
}

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosLocalCatalogSyncWorker.kt

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import androidx.hilt.work.HiltWorker
55
import androidx.work.CoroutineWorker
66
import androidx.work.WorkerParameters
77
import com.woocommerce.android.tools.SelectedSite
8-
import com.woocommerce.android.ui.login.AccountRepository
98
import com.woocommerce.android.ui.woopos.common.util.WooPosLogWrapper
10-
import com.woocommerce.android.ui.woopos.featureflags.WooPosLocalCatalogM1Enabled
119
import com.woocommerce.android.ui.woopos.tab.WooPosTabShouldBeVisible
1210
import com.woocommerce.android.ui.woopos.util.datastore.WooPosPreferencesRepository
1311
import dagger.assisted.Assisted
@@ -22,15 +20,13 @@ class WooPosLocalCatalogSyncWorker
2220
constructor(
2321
@Assisted appContext: Context,
2422
@Assisted workerParams: WorkerParameters,
25-
private val accountRepository: AccountRepository,
2623
private val selectedSite: SelectedSite,
27-
private val featureFlagM1Enabled: WooPosLocalCatalogM1Enabled,
2824
private val preferencesRepository: WooPosPreferencesRepository,
2925
private val syncRepository: WooPosLocalCatalogSyncRepository,
3026
private val logger: WooPosLogWrapper,
3127
private val timeProvider: DateTimeProvider,
3228
private val wooPosTabShouldBeVisible: WooPosTabShouldBeVisible,
33-
private val isVariationsEndpointAvailable: WooPosIsLocalCatalogVariationsEndpointAvailable,
29+
private val isLocalCatalogSupported: WooPosIsLocalCatalogSupported,
3430
) : CoroutineWorker(appContext, workerParams) {
3531

3632
companion object {
@@ -104,29 +100,14 @@ constructor(
104100

105101
@Suppress("ReturnCount")
106102
private suspend fun isCatalogSyncSupported(): SiteModel? {
107-
if (!featureFlagM1Enabled.invoke()) {
108-
logger.d("Feature flag disabled, skipping local catalog sync")
109-
return null
110-
}
111-
112-
if (!isVariationsEndpointAvailable()) {
113-
logger.d("Variations endpoint not available, skipping local catalog sync")
114-
return null
115-
}
116-
117-
if (!accountRepository.isUserLoggedIn()) {
118-
logger.d("User not logged in, skipping local catalog sync")
119-
return null
120-
}
121-
122103
val site = selectedSite.getOrNull()
123104
if (site == null) {
124105
logger.w("No selected WooCommerce site found, skipping local catalog sync")
125106
return null
126107
}
127108

128-
if (!preferencesRepository.isPeriodicSyncEnabledForSite(site.siteId)) {
129-
logger.w("Periodic sync permanently disabled for site ${site.url}, skipping local catalog sync.")
109+
if (!isLocalCatalogSupported(site.siteId)) {
110+
logger.d("Local catalog not supported for site")
130111
return null
131112
}
132113

WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosFullSyncStatusCheckerTest.kt

Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package com.woocommerce.android.ui.woopos.localcatalog
22

33
import com.woocommerce.android.tools.SelectedSite
44
import com.woocommerce.android.ui.woopos.common.util.WooPosLogWrapper
5-
import com.woocommerce.android.ui.woopos.featureflags.WooPosLocalCatalogM1Enabled
65
import com.woocommerce.android.ui.woopos.util.WooPosCoroutineTestRule
76
import com.woocommerce.android.ui.woopos.util.WooPosNetworkStatus
87
import com.woocommerce.android.ui.woopos.util.datastore.WooPosPreferencesRepository
98
import com.woocommerce.android.ui.woopos.util.datastore.WooPosSyncTimestampManager
10-
import com.woocommerce.android.util.GetWooCorePluginCachedVersion
119
import kotlinx.coroutines.ExperimentalCoroutinesApi
1210
import kotlinx.coroutines.test.runTest
1311
import org.assertj.core.api.Assertions.assertThat
@@ -33,17 +31,11 @@ class WooPosFullSyncStatusCheckerTest {
3331
private val syncTimestampManager: WooPosSyncTimestampManager = mock()
3432
private val selectedSite: SelectedSite = mock()
3533
private val networkStatus: WooPosNetworkStatus = mock()
36-
private val wooPosLocalCatalogM1Enabled: WooPosLocalCatalogM1Enabled = mock()
3734
private val localCatalogStore: WooPosLocalCatalogStore = mock()
3835
private val wooPosLogWrapper: WooPosLogWrapper = mock()
3936
private val prefsRepo: WooPosPreferencesRepository = mock()
4037
private val checkCatalogSizeAction: WooPosCheckCatalogSizeAction = mock()
41-
private val getWooVersion: GetWooCorePluginCachedVersion = mock()
42-
private val variationsEndpointChecker: WooPosIsLocalCatalogVariationsEndpointAvailable =
43-
WooPosIsLocalCatalogVariationsEndpointAvailable(
44-
getWooVersion = getWooVersion,
45-
logger = wooPosLogWrapper
46-
)
38+
private val isLocalCatalogSupported: WooPosIsLocalCatalogSupported = mock()
4739

4840
private val siteModel = SiteModel().apply {
4941
id = 123
@@ -54,21 +46,18 @@ class WooPosFullSyncStatusCheckerTest {
5446
syncTimestampManager = syncTimestampManager,
5547
selectedSite = selectedSite,
5648
networkStatus = networkStatus,
57-
wooPosLocalCatalogM1Enabled = wooPosLocalCatalogM1Enabled,
5849
localCatalogStore = localCatalogStore,
5950
prefsRepo = prefsRepo,
6051
checkCatalogSizeAction = checkCatalogSizeAction,
61-
isVariationsEndpointAvailable = variationsEndpointChecker,
52+
isLocalCatalogSupported = isLocalCatalogSupported,
6253
wooPosLogWrapper = wooPosLogWrapper
6354
)
6455

6556
@Before
6657
fun setup() = runTest {
6758
val recentTimestamp = System.currentTimeMillis() - 1.days.inWholeMilliseconds
68-
whenever(wooPosLocalCatalogM1Enabled()).thenReturn(true)
69-
whenever(getWooVersion()).thenReturn("10.3.0")
7059
whenever(selectedSite.getOrNull()).thenReturn(siteModel)
71-
whenever(prefsRepo.isPeriodicSyncEnabledForSite(any())).thenReturn(true)
60+
whenever(isLocalCatalogSupported(siteModel.siteId)).thenReturn(true)
7261
whenever(syncTimestampManager.getFullSyncLastCompletedTimestamp()).thenReturn(recentTimestamp)
7362
whenever(networkStatus.isConnected()).thenReturn(true)
7463
whenever(localCatalogStore.getProductCount(LocalOrRemoteId.LocalId(siteModel.id)))
@@ -78,10 +67,10 @@ class WooPosFullSyncStatusCheckerTest {
7867
}
7968

8069
@Test
81-
fun `given feature flag disabled, when checkSyncRequirement called, then should return LocalCatalogDisabled`() =
70+
fun `given local catalog not supported, when checkSyncRequirement called, then should return LocalCatalogDisabled`() =
8271
runTest {
8372
// GIVEN
84-
whenever(wooPosLocalCatalogM1Enabled()).thenReturn(false)
73+
whenever(isLocalCatalogSupported(siteModel.siteId)).thenReturn(false)
8574

8675
val sut = createSut()
8776

@@ -106,21 +95,6 @@ class WooPosFullSyncStatusCheckerTest {
10695
// Exception is expected
10796
}
10897

109-
@Test
110-
fun `given periodic sync disabled for site, when checkSyncRequirement called, then should return LocalCatalogDisabled`() =
111-
runTest {
112-
// GIVEN
113-
whenever(prefsRepo.isPeriodicSyncEnabledForSite(any())).thenReturn(false)
114-
115-
val sut = createSut()
116-
117-
// WHEN
118-
val result = sut.checkSyncRequirement()
119-
120-
// THEN
121-
assertThat(result).isInstanceOf(WooPosFullSyncRequirement.LocalCatalogDisabled::class.java)
122-
}
123-
12498
@Test
12599
fun `given never synced before and network connected, when checkSyncRequirement called, then should return BlockingRequired`() =
126100
runTest {
@@ -254,36 +228,6 @@ class WooPosFullSyncStatusCheckerTest {
254228
assertThat(result).isEqualTo(WooPosFullSyncRequirement.NotRequired)
255229
}
256230

257-
@Test
258-
fun `given variations endpoint not available, when checkSyncRequirement called, then should return LocalCatalogDisabled`() =
259-
runTest {
260-
// GIVEN
261-
whenever(getWooVersion()).thenReturn("10.2.0") // Version below 10.3.0
262-
263-
val sut = createSut()
264-
265-
// WHEN
266-
val result = sut.checkSyncRequirement()
267-
268-
// THEN
269-
assertThat(result).isInstanceOf(WooPosFullSyncRequirement.LocalCatalogDisabled::class.java)
270-
}
271-
272-
@Test
273-
fun `given woo version null, when checkSyncRequirement called, then should return LocalCatalogDisabled`() =
274-
runTest {
275-
// GIVEN
276-
whenever(getWooVersion()).thenReturn(null)
277-
278-
val sut = createSut()
279-
280-
// WHEN
281-
val result = sut.checkSyncRequirement()
282-
283-
// THEN
284-
assertThat(result).isInstanceOf(WooPosFullSyncRequirement.LocalCatalogDisabled::class.java)
285-
}
286-
287231
@Test
288232
fun `given empty catalog and catalog too large, when checkSyncRequirement called, then should disable sync and return LocalCatalogDisabled`() =
289233
runTest {
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.woocommerce.android.ui.woopos.localcatalog
2+
3+
import com.woocommerce.android.ui.woopos.common.util.WooPosLogWrapper
4+
import com.woocommerce.android.ui.woopos.featureflags.WooPosLocalCatalogM1Enabled
5+
import com.woocommerce.android.ui.woopos.util.datastore.WooPosPreferencesRepository
6+
import com.woocommerce.android.util.GetWooCorePluginCachedVersion
7+
import com.woocommerce.android.viewmodel.BaseUnitTest
8+
import kotlinx.coroutines.ExperimentalCoroutinesApi
9+
import org.assertj.core.api.Assertions.assertThat
10+
import org.junit.Before
11+
import org.junit.Test
12+
import org.mockito.kotlin.any
13+
import org.mockito.kotlin.mock
14+
import org.mockito.kotlin.whenever
15+
16+
@ExperimentalCoroutinesApi
17+
class WooPosIsLocalCatalogSupportedTest : BaseUnitTest() {
18+
19+
private var featureFlagM1Enabled: WooPosLocalCatalogM1Enabled = mock()
20+
private var preferencesRepository: WooPosPreferencesRepository = mock()
21+
private var getWooVersion: GetWooCorePluginCachedVersion = mock()
22+
private var logger: WooPosLogWrapper = mock()
23+
24+
private lateinit var isLocalCatalogSupported: WooPosIsLocalCatalogSupported
25+
26+
companion object {
27+
private const val SITE_ID = 123L
28+
}
29+
30+
@Before
31+
fun setup() = testBlocking {
32+
whenever(featureFlagM1Enabled.invoke()).thenReturn(true)
33+
whenever(getWooVersion()).thenReturn("10.3.0")
34+
whenever(preferencesRepository.isPeriodicSyncEnabledForSite(any())).thenReturn(true)
35+
36+
val variationsEndpointChecker = WooPosIsLocalCatalogVariationsEndpointAvailable(
37+
getWooVersion = getWooVersion,
38+
logger = logger
39+
)
40+
41+
isLocalCatalogSupported = WooPosIsLocalCatalogSupported(
42+
wooPosLocalCatalogM1Enabled = featureFlagM1Enabled,
43+
prefsRepo = preferencesRepository,
44+
isVariationsEndpointAvailable = variationsEndpointChecker
45+
)
46+
}
47+
48+
@Test
49+
fun `given all conditions met, when check invoked, then returns true`() = testBlocking {
50+
// WHEN
51+
val result = isLocalCatalogSupported(SITE_ID)
52+
53+
// THEN
54+
assertThat(result).isTrue()
55+
}
56+
57+
@Test
58+
fun `given feature flag disabled, when check invoked, then returns false`() = testBlocking {
59+
// GIVEN
60+
whenever(featureFlagM1Enabled.invoke()).thenReturn(false)
61+
62+
// WHEN
63+
val result = isLocalCatalogSupported(SITE_ID)
64+
65+
// THEN
66+
assertThat(result).isFalse()
67+
}
68+
69+
@Test
70+
fun `given variations endpoint not available, when check invoked, then returns false`() = testBlocking {
71+
// GIVEN
72+
whenever(getWooVersion()).thenReturn("10.2.9")
73+
74+
// WHEN
75+
val result = isLocalCatalogSupported(SITE_ID)
76+
77+
// THEN
78+
assertThat(result).isFalse()
79+
}
80+
81+
@Test
82+
fun `given periodic sync disabled, when check invoked, then returns false`() = testBlocking {
83+
// GIVEN
84+
whenever(preferencesRepository.isPeriodicSyncEnabledForSite(SITE_ID)).thenReturn(false)
85+
86+
// WHEN
87+
val result = isLocalCatalogSupported(SITE_ID)
88+
89+
// THEN
90+
assertThat(result).isFalse()
91+
}
92+
}

0 commit comments

Comments
 (0)