Skip to content

Commit

Permalink
Merge pull request #13018 from woocommerce/issue/13015-remove-stale-p…
Browse files Browse the repository at this point in the history
…roducts

[Woo POS][non-simple products] Force refresh products while fetching from POS
  • Loading branch information
AnirudhBhat authored Nov 29, 2024
2 parents 2ea0c82 + 1aed772 commit 7f56f0a
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class ProductListHandler @Inject constructor(private val repository: ProductSele
}.flatMapLatest { it }

suspend fun loadFromCacheAndFetch(
forceRefresh: Boolean = false,
searchQuery: String = "",
filters: Map<ProductFilterOption, String> = emptyMap(),
searchType: SearchType,
Expand All @@ -67,7 +68,7 @@ class ProductListHandler @Inject constructor(private val repository: ProductSele
}
}
} else {
fetchProducts()
fetchProducts(forceRefresh)
}
}

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

private suspend fun fetchProducts(): Result<Unit> {
return repository.fetchProducts(offset.value, PAGE_SIZE, productFilters.value).onSuccess {
private suspend fun fetchProducts(forceRefresh: Boolean = false): Result<Unit> {
return repository.fetchProducts(forceRefresh, offset.value, PAGE_SIZE, productFilters.value).onSuccess {
canLoadMore.set(it)
offset.value += PAGE_SIZE
}.map { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class ProductSelectorRepository @Inject constructor(
}

suspend fun fetchProducts(
forceRefresh: Boolean = false,
offset: Int,
pageSize: Int,
filterOptions: Map<ProductFilterOption, String>
Expand All @@ -81,7 +82,7 @@ class ProductSelectorRepository @Inject constructor(
offset = offset,
pageSize = pageSize,
filterOptions = filterOptions,
forceRefresh = false,
forceRefresh = forceRefresh,
)
.let { result ->
if (result.isError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class WooPosProductsDataSource @Inject constructor(
emit(ProductsResult.Cached(productCache))

val result = handler.loadFromCacheAndFetch(
forceRefresh = forceRefreshProducts,
searchType = ProductListHandler.SearchType.DEFAULT,
filters =
if (isNonSimpleProductTypesEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,10 @@ class WooPosTotalsRepository @Inject constructor(
val itemData = itemClickedDataList.find { it.id == id }!!
when (itemData) {
is WooPosItemsViewModel.ItemClickedData.SimpleProduct -> createSimpleProductOrderItem(
id,
quantity,
itemData
)
is WooPosItemsViewModel.ItemClickedData.Variation -> createVariationOrderItem(
id,
quantity,
itemData
)
Expand All @@ -78,14 +76,13 @@ class WooPosTotalsRepository @Inject constructor(
}

private suspend fun createSimpleProductOrderItem(
id: Long,
quantity: Int,
itemData: WooPosItemsViewModel.ItemClickedData.SimpleProduct
): Order.Item {
val productResult = getProductById(itemData.id)!!
return Order.Item.EMPTY.copy(
itemId = 0L,
productId = id,
productId = itemData.id,
variationId = 0L,
quantity = quantity.toFloat(),
total = EMPTY_TOTALS_SUBTOTAL_VALUE,
Expand All @@ -96,7 +93,6 @@ class WooPosTotalsRepository @Inject constructor(
}

private suspend fun createVariationOrderItem(
id: Long,
quantity: Int,
itemData: WooPosItemsViewModel.ItemClickedData.Variation
): Order.Item {
Expand All @@ -107,12 +103,14 @@ class WooPosTotalsRepository @Inject constructor(
)!!
return Order.Item.EMPTY.copy(
itemId = 0L,
productId = id,
productId = itemData.productId,
variationId = variationResult.remoteVariationId,
quantity = quantity.toFloat(),
total = EMPTY_TOTALS_SUBTOTAL_VALUE,
subtotal = EMPTY_TOTALS_SUBTOTAL_VALUE,
attributesList = emptyList(),
attributesList = variationResult.attributes
.filterNot { it.name.isNullOrEmpty() || it.option.isNullOrEmpty() }
.map { Order.Item.Attribute(it.name!!, it.option!!) },
name = variationResult.getName(productResult),
)
}
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()))
(it.fetchProducts(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())).doReturn(Result.success(true))
whenever(repo.fetchProducts(any(), any(), any(), any())).doReturn(Result.success(true))
val handler = ProductListHandler(repo)
handler.loadFromCacheAndFetch(searchType = SearchType.DEFAULT)

Expand All @@ -51,7 +51,7 @@ 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(0, 25, emptyMap())
verify(repo).fetchProducts(false, 0, 25, emptyMap())
}

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

handler.loadMore()

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

handler.productsFlow.test {
val products = awaitItem()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class WooPosProductsDataSourceTest {
// GIVEN
whenever(handler.canLoadMore).thenReturn(AtomicBoolean(true))
whenever(handler.productsFlow).thenReturn(flowOf(sampleProducts))
whenever(handler.loadFromCacheAndFetch(any(), any(), any())).thenReturn(Result.success(Unit))
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)

// WHEN
Expand All @@ -121,7 +121,7 @@ class WooPosProductsDataSourceTest {
// GIVEN
whenever(handler.canLoadMore).thenReturn(AtomicBoolean(true))
whenever(handler.productsFlow).thenReturn(flowOf(sampleProducts))
whenever(handler.loadFromCacheAndFetch(any(), any(), any())).thenReturn(Result.success(Unit))
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)

// WHEN
Expand All @@ -144,14 +144,14 @@ class WooPosProductsDataSourceTest {
whenever(handler.canLoadMore).thenReturn(AtomicBoolean(true))
whenever(handler.productsFlow).thenReturn(flowOf(sampleProducts))
val exception = Exception("Remote load failed")
whenever(handler.loadFromCacheAndFetch(any(), any(), any())).thenReturn(Result.success(Unit))
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.success(Unit))

val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)

// Prepopulate the cache by calling loadSimpleProducts once
sut.loadSimpleProducts(forceRefreshProducts = false).first()

whenever(handler.loadFromCacheAndFetch(any(), any(), any())).thenReturn(Result.failure(exception))
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.failure(exception))

// WHEN
val flow = sut.loadSimpleProducts(forceRefreshProducts = false).toList()
Expand Down Expand Up @@ -224,7 +224,7 @@ class WooPosProductsDataSourceTest {
whenever(handler.canLoadMore).thenReturn(AtomicBoolean(true))
whenever(handler.productsFlow).thenReturn(flowOf(emptyList()))
val exception = Exception("Remote load failed")
whenever(handler.loadFromCacheAndFetch(any(), any(), any())).thenReturn(Result.failure(exception))
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.failure(exception))

val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)

Expand All @@ -246,7 +246,7 @@ class WooPosProductsDataSourceTest {
// GIVEN
whenever(handler.canLoadMore).thenReturn(AtomicBoolean(true))
whenever(handler.productsFlow).thenReturn(flowOf(emptyList()))
whenever(handler.loadFromCacheAndFetch(any(), any(), any())).thenReturn(Result.success(Unit))
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)

// WHEN
Expand Down Expand Up @@ -286,7 +286,7 @@ class WooPosProductsDataSourceTest {
)
)
)
whenever(handler.loadFromCacheAndFetch(any(), any(), any())).thenReturn(Result.success(Unit))
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)

// WHEN
Expand Down Expand Up @@ -323,7 +323,7 @@ class WooPosProductsDataSourceTest {
)
)
)
whenever(handler.loadFromCacheAndFetch(any(), any(), any())).thenReturn(Result.success(Unit))
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)

// WHEN
Expand Down Expand Up @@ -361,7 +361,7 @@ class WooPosProductsDataSourceTest {
)
)
)
whenever(handler.loadFromCacheAndFetch(any(), any(), any())).thenReturn(Result.success(Unit))
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)

// WHEN
Expand Down Expand Up @@ -400,7 +400,7 @@ class WooPosProductsDataSourceTest {
)
)
)
whenever(handler.loadFromCacheAndFetch(any(), any(), any())).thenReturn(Result.success(Unit))
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)

// WHEN
Expand Down Expand Up @@ -437,7 +437,7 @@ class WooPosProductsDataSourceTest {
)
)
)
whenever(handler.loadFromCacheAndFetch(any(), any(), any())).thenReturn(Result.success(Unit))
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)

// WHEN
Expand Down Expand Up @@ -475,7 +475,7 @@ class WooPosProductsDataSourceTest {
)
)
)
whenever(handler.loadFromCacheAndFetch(any(), any(), any())).thenReturn(Result.success(Unit))
whenever(handler.loadFromCacheAndFetch(any(), any(), any(), any())).thenReturn(Result.success(Unit))
val sut = WooPosProductsDataSource(handler, isNonSimpleProductTypesEnabled)

// WHEN
Expand All @@ -499,6 +499,7 @@ class WooPosProductsDataSourceTest {

// THEN
verify(handler).loadFromCacheAndFetch(
forceRefresh = true,
searchType = ProductListHandler.SearchType.DEFAULT,
filters = mapOf(
WCProductStore.ProductFilterOption.TYPE to ProductType.SIMPLE.value,
Expand All @@ -520,6 +521,7 @@ class WooPosProductsDataSourceTest {

// THEN
verify(handler).loadFromCacheAndFetch(
forceRefresh = true,
searchType = ProductListHandler.SearchType.DEFAULT,
filters = mapOf(
WCProductStore.ProductFilterOption.STATUS to ProductStatus.PUBLISH.value
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ stripe-terminal = '3.7.1'
tinder-statemachine = '0.2.0'
wiremock = '2.26.3'
wordpress-aztec = 'v2.1.4'
wordpress-fluxc = 'trunk-b1086fc19b68a57592f6b337d8044b39296ed12c'
wordpress-fluxc = 'trunk-3d4558601eaa4fc1d4a0254c6caff5f7c87732bb'
wordpress-login = '1.19.0'
wordpress-libaddressinput = '0.0.2'
wordpress-mediapicker = '0.3.1'
Expand Down

0 comments on commit 7f56f0a

Please sign in to comment.