Skip to content

Commit

Permalink
Merge pull request #13072 from woocommerce/issue/13061-integrate-api-…
Browse files Browse the repository at this point in the history
…change

[Woo POS][non-simple products] Integrate "include_types" parameter for product endpoint
  • Loading branch information
AnirudhBhat authored Dec 12, 2024
2 parents 7c2f5cc + 7f911b1 commit df6be4c
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ class ProductListRepository @Inject constructor(
offset = offset,
sortType = sortType ?: productSortingChoice,
filterOptions = productFilterOptions,
excludedProductIds = excludedProductIds.orEmpty()
excludedProductIds = excludedProductIds.orEmpty(),
includeTypes = emptyList()
).let { result ->
if (result.isError) {
AnalyticsTracker.track(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import org.wordpress.android.fluxc.store.WCProductStore
import org.wordpress.android.fluxc.store.WCProductStore.ProductFilterOption
import org.wordpress.android.fluxc.store.WCProductStore.SkuSearchOptions
import java.util.concurrent.atomic.AtomicBoolean
Expand Down Expand Up @@ -48,6 +49,7 @@ class ProductListHandler @Inject constructor(private val repository: ProductSele
searchQuery: String = "",
filters: Map<ProductFilterOption, String> = emptyMap(),
searchType: SearchType,
includeType: List<WCProductStore.IncludeType> = emptyList(),
): Result<Unit> = mutex.withLock {
offset.value = 0
searchResults.value = emptyList()
Expand All @@ -68,7 +70,7 @@ class ProductListHandler @Inject constructor(private val repository: ProductSele
}
}
} else {
fetchProducts(forceRefresh)
fetchProducts(forceRefresh, includeType)
}
}

Expand All @@ -84,8 +86,17 @@ class ProductListHandler @Inject constructor(private val repository: ProductSele
}
}

private suspend fun fetchProducts(forceRefresh: Boolean = false): Result<Unit> {
return repository.fetchProducts(forceRefresh, offset.value, PAGE_SIZE, productFilters.value).onSuccess {
private suspend fun fetchProducts(
forceRefresh: Boolean = false,
includeTypes: List<WCProductStore.IncludeType> = emptyList(),
): Result<Unit> {
return repository.fetchProducts(
forceRefresh,
offset.value,
PAGE_SIZE,
productFilters.value,
includeTypes
).onSuccess {
canLoadMore.set(it)
offset.value += PAGE_SIZE
}.map { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,15 @@ class ProductSelectorRepository @Inject constructor(
forceRefresh: Boolean = false,
offset: Int,
pageSize: Int,
filterOptions: Map<ProductFilterOption, String>
filterOptions: Map<ProductFilterOption, String>,
includeType: List<WCProductStore.IncludeType>,
): Result<Boolean> {
return productStore.fetchProducts(
site = selectedSite.get(),
offset = offset,
pageSize = pageSize,
filterOptions = filterOptions,
includeTypes = includeType,
forceRefresh = forceRefresh,
)
.let { result ->
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package com.woocommerce.android.ui.woopos.home.items.products

import com.woocommerce.android.model.Product
import com.woocommerce.android.ui.products.ProductStatus
import com.woocommerce.android.ui.products.ProductType
import com.woocommerce.android.ui.products.selector.ProductListHandler
import com.woocommerce.android.ui.woopos.featureflags.IsNonSimpleProductTypesEnabled
import com.woocommerce.android.util.WooLog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
Expand All @@ -22,7 +20,6 @@ import javax.inject.Singleton
@Singleton
class WooPosProductsDataSource @Inject constructor(
private val handler: ProductListHandler,
private val isNonSimpleProductTypesEnabled: IsNonSimpleProductTypesEnabled,
) {
private var productCache: List<Product> = emptyList()
private val cacheMutex = Mutex()
Expand All @@ -40,15 +37,8 @@ class WooPosProductsDataSource @Inject constructor(
val result = handler.loadFromCacheAndFetch(
forceRefresh = forceRefreshProducts,
searchType = ProductListHandler.SearchType.DEFAULT,
filters =
if (isNonSimpleProductTypesEnabled()) {
mapOf(WCProductStore.ProductFilterOption.STATUS to ProductStatus.PUBLISH.value)
} else {
mapOf(
WCProductStore.ProductFilterOption.TYPE to ProductType.SIMPLE.value,
WCProductStore.ProductFilterOption.STATUS to ProductStatus.PUBLISH.value
)
}
includeType = listOf(WCProductStore.IncludeType.Simple),
filters = mapOf(WCProductStore.ProductFilterOption.STATUS to ProductStatus.PUBLISH.value)
)

if (result.isSuccess) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ enum class FeatureFlag {
ENDLESS_CAMPAIGNS_SUPPORT,
REVAMP_WOO_SHIPPING,
OBJECTIVE_SECTION,
POS_NON_SIMPLE_PRODUCT_TYPES,
POS_CASH_PAYMENTS,
POS_RECEIPTS,
PRODUCT_GLOBAL_UNIQUE_IDENTIFIER_SUPPORT;
Expand All @@ -29,7 +28,6 @@ enum class FeatureFlag {
BETTER_CUSTOMER_SEARCH_M2,
ORDER_CREATION_AUTO_TAX_RATE,
REVAMP_WOO_SHIPPING,
POS_NON_SIMPLE_PRODUCT_TYPES,
POS_CASH_PAYMENTS,
POS_RECEIPTS,
PRODUCT_GLOBAL_UNIQUE_IDENTIFIER_SUPPORT -> PackageUtils.isDebugBuild()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal class ProductListHandlerTest : BaseUnitTest() {
on(it.observeProducts(any())) doReturn flow { emit(generateSampleProducts()) }

onBlocking {
(it.fetchProducts(any(), any(), any(), any()))
(it.fetchProducts(any(), any(), any(), any(), any()))
} doReturn Result.success(true)
}

Expand All @@ -34,7 +34,7 @@ internal class ProductListHandlerTest : BaseUnitTest() {

@Test
fun `when load invoked, then emits first 25 products from db`() = testBlocking {
whenever(repo.fetchProducts(any(), any(), any(), any())).doReturn(Result.success(true))
whenever(repo.fetchProducts(any(), any(), any(), any(), any())).doReturn(Result.success(true))
val handler = ProductListHandler(repo)
handler.loadFromCacheAndFetch(searchType = SearchType.DEFAULT)

Expand All @@ -51,17 +51,23 @@ internal class ProductListHandlerTest : BaseUnitTest() {
fun `when load invoked, then side fetches first 25 products from backend`() = testBlocking {
val handler = ProductListHandler(repo)
handler.loadFromCacheAndFetch(searchType = SearchType.DEFAULT)
verify(repo).fetchProducts(false, 0, 25, emptyMap())
verify(repo).fetchProducts(false, 0, 25, emptyMap(), emptyList())
}

@Test
fun `when load more invoked, then fetches next 25 products`() = testBlocking {
val handler = ProductListHandler(repo)
handler.loadFromCacheAndFetch(searchType = SearchType.DEFAULT)
handler.loadFromCacheAndFetch(
false,
"",
emptyMap(),
searchType = SearchType.DEFAULT,
emptyList()
)

handler.loadMore()

verify(repo).fetchProducts(false, 25, 25, emptyMap())
verify(repo).fetchProducts(false, 25, 25, emptyMap(), emptyList())

handler.productsFlow.test {
val products = awaitItem()
Expand Down
Loading

0 comments on commit df6be4c

Please sign in to comment.