From b8c8be4e38e9fcd7d9ca268418ae1c3a208f637e Mon Sep 17 00:00:00 2001 From: malinajirka Date: Mon, 8 Sep 2025 08:53:57 +0200 Subject: [PATCH 1/4] Migrate WooPosCartItemsUpdater --- .../home/cart/WooPosCartItemsUpdater.kt | 18 ++++++++++++- .../home/cart/WooPosCartItemsUpdaterTest.kt | 26 ++++++++++--------- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/cart/WooPosCartItemsUpdater.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/cart/WooPosCartItemsUpdater.kt index ea930ddc5417..15446e1bc466 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/cart/WooPosCartItemsUpdater.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/cart/WooPosCartItemsUpdater.kt @@ -2,6 +2,7 @@ package com.woocommerce.android.ui.woopos.home.cart import com.automattic.android.tracks.crashlogging.CrashLogging import com.woocommerce.android.ui.woopos.common.data.WooPosProductsCache +import com.woocommerce.android.ui.woopos.common.data.models.WooPosProductModelVersion2 import com.woocommerce.android.ui.woopos.common.util.WooPosLogWrapper import com.woocommerce.android.ui.woopos.home.ParentToChildrenEvent import com.woocommerce.android.ui.woopos.home.cart.WooPosCartItemViewState.Coupon.CouponValidationState @@ -108,11 +109,26 @@ class WooPosCartItemsUpdater @Inject constructor( updatedItem: WooPosCartItemViewState.Product, updatedProduct: ParentToChildrenEvent.OrderCreated.ProductInfo ) { + // TBD Local Catalog The app should update cache based on the data coming from backend not from the view layer productsCache.getProductById(updatedItem.id)?.let { product -> productsCache.updateProduct( product.copy( name = updatedItem.name, - price = updatedProduct.subtotalPricePerItem(), + pricing = when (product.pricing) { + WooPosProductModelVersion2.WooPosPricing.NoPricing -> + WooPosProductModelVersion2.WooPosPricing.NoPricing + + is WooPosProductModelVersion2.WooPosPricing.RegularPricing -> + WooPosProductModelVersion2.WooPosPricing.RegularPricing( + updatedProduct.subtotalPricePerItem() + ) + + is WooPosProductModelVersion2.WooPosPricing.SalePricing -> + WooPosProductModelVersion2.WooPosPricing.SalePricing( + product.pricing.regularPrice, + updatedProduct.subtotalPricePerItem() + ) + } ) ) } diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/cart/WooPosCartItemsUpdaterTest.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/cart/WooPosCartItemsUpdaterTest.kt index 72238e19e482..9fe315219930 100644 --- a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/cart/WooPosCartItemsUpdaterTest.kt +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/cart/WooPosCartItemsUpdaterTest.kt @@ -2,13 +2,14 @@ package com.woocommerce.android.ui.woopos.home.cart import androidx.arch.core.executor.testing.InstantTaskExecutorRule import com.automattic.android.tracks.crashlogging.CrashLogging -import com.woocommerce.android.model.Product import com.woocommerce.android.ui.woopos.common.data.WooPosProductsCache +import com.woocommerce.android.ui.woopos.common.data.models.WooPosProductModelVersion2 import com.woocommerce.android.ui.woopos.common.util.WooPosLogWrapper import com.woocommerce.android.ui.woopos.home.ParentToChildrenEvent import com.woocommerce.android.ui.woopos.home.cart.WooPosCartItemViewState.Coupon.CouponValidationState import com.woocommerce.android.ui.woopos.util.WooPosCoroutineTestRule import com.woocommerce.android.ui.woopos.util.format.WooPosFormatPrice +import com.woocommerce.android.ui.woopos.util.generateWooPosProduct import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.runTest import org.assertj.core.api.Assertions.assertThat @@ -45,7 +46,8 @@ class WooPosCartItemsUpdaterTest { formatPrice = formatPrice, productsCache = productsCache, wooPosLogWrapper = logger, - crashLogger = crashLogger + crashLogger = crashLogger, + ) @Test @@ -67,7 +69,7 @@ class WooPosCartItemsUpdaterTest { basePrice = BigDecimal("10.0"), quantity = 1f ) - val cachedProduct = mock() + val cachedProduct = generateWooPosProduct() whenever(productsCache.getProductById(1L)).thenReturn(cachedProduct) // WHEN @@ -82,7 +84,7 @@ class WooPosCartItemsUpdaterTest { verify(productsCache).updateProduct( cachedProduct.copy( name = "Updated Name", - price = BigDecimal("10.0") + pricing = WooPosProductModelVersion2.WooPosPricing.RegularPricing(BigDecimal("10.0")) ) ) } @@ -108,7 +110,7 @@ class WooPosCartItemsUpdaterTest { basePrice = BigDecimal("10.0"), quantity = 1f ) - val cachedProduct = mock() + val cachedProduct = generateWooPosProduct() whenever(productsCache.getProductById(1L)).thenReturn(cachedProduct) // WHEN @@ -123,7 +125,7 @@ class WooPosCartItemsUpdaterTest { verify(productsCache).updateProduct( cachedProduct.copy( name = "Updated Variation", - price = BigDecimal("10.0") + pricing = WooPosProductModelVersion2.WooPosPricing.RegularPricing(BigDecimal("10.0")) ) ) } @@ -179,7 +181,7 @@ class WooPosCartItemsUpdaterTest { basePrice = BigDecimal("10.0"), quantity = 1f ) - val cachedProduct = mock() + val cachedProduct = generateWooPosProduct() whenever(productsCache.getProductById(1L)).thenReturn(cachedProduct) // WHEN @@ -199,7 +201,7 @@ class WooPosCartItemsUpdaterTest { verify(productsCache).updateProduct( cachedProduct.copy( name = "Updated Product 1", - price = BigDecimal("10.0") + pricing = WooPosProductModelVersion2.WooPosPricing.RegularPricing(BigDecimal("10.0")) ) ) verify(productsCache).deleteProduct(2L) @@ -224,7 +226,7 @@ class WooPosCartItemsUpdaterTest { basePrice = BigDecimal("10.0"), quantity = 1f ) - val cachedProduct = mock() + val cachedProduct = generateWooPosProduct() whenever(productsCache.getProductById(1L)).thenReturn(cachedProduct) // WHEN @@ -245,7 +247,7 @@ class WooPosCartItemsUpdaterTest { verify(productsCache).updateProduct( cachedProduct.copy( name = "Updated Product", - price = BigDecimal("10.0") + pricing = WooPosProductModelVersion2.WooPosPricing.RegularPricing(BigDecimal("10.0")) ) ) verify(productsCache).deleteProduct(1L) @@ -320,7 +322,7 @@ class WooPosCartItemsUpdaterTest { basePrice = BigDecimal("10.0"), quantity = 1f ) - val cachedProduct = mock() + val cachedProduct = generateWooPosProduct() whenever(productsCache.getProductById(1L)).thenReturn(cachedProduct) // WHEN @@ -330,7 +332,7 @@ class WooPosCartItemsUpdaterTest { verify(productsCache).updateProduct( cachedProduct.copy( name = "Updated Name", - price = BigDecimal("10.0") + pricing = WooPosProductModelVersion2.WooPosPricing.RegularPricing(BigDecimal("10.0")) ) ) } From 00bb4705c7919ba6b6480cf7e6ad06c2c2e09d12 Mon Sep 17 00:00:00 2001 From: malinajirka Date: Mon, 8 Sep 2025 08:54:09 +0200 Subject: [PATCH 2/4] Migrate WooPosSearchByIdentifierProcessVariationResult --- ...earchByIdentifierProcessVariationResult.kt | 6 ++-- ...hByIdentifierProcessVariationResultTest.kt | 32 ++++++------------- .../ui/woopos/util/WooPosProductUtils.kt | 3 +- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierProcessVariationResult.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierProcessVariationResult.kt index cd0840e64419..e439dd85faaa 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierProcessVariationResult.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierProcessVariationResult.kt @@ -1,6 +1,6 @@ package com.woocommerce.android.ui.woopos.common.data.searchbyidentifier -import com.woocommerce.android.model.Product +import com.woocommerce.android.ui.woopos.common.data.models.WooPosProductModelVersion2 import kotlinx.coroutines.async import kotlinx.coroutines.coroutineScope import javax.inject.Inject @@ -9,11 +9,11 @@ class WooPosSearchByIdentifierProcessVariationResult @Inject constructor( private val variationFetch: WooPosSearchByIdentifierVariationFetch, private val productGetOrFetch: WooPosSearchByIdentifierProductGetOrFetch ) { - suspend operator fun invoke(product: Product): WooPosSearchByIdentifierResult = coroutineScope { + suspend operator fun invoke(product: WooPosProductModelVersion2): WooPosSearchByIdentifierResult = coroutineScope { val parentId = product.parentId val variationId = product.remoteId - if (parentId <= 0) { + if (parentId == null || parentId <= 0) { return@coroutineScope WooPosSearchByIdentifierResult.Failure( WooPosSearchByIdentifierResult.Error.NotFound ) diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierProcessVariationResultTest.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierProcessVariationResultTest.kt index e68482d7f569..504bdaae4563 100644 --- a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierProcessVariationResultTest.kt +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierProcessVariationResultTest.kt @@ -1,7 +1,8 @@ package com.woocommerce.android.ui.woopos.common.data.searchbyidentifier -import com.woocommerce.android.model.Product import com.woocommerce.android.model.ProductVariation +import com.woocommerce.android.ui.woopos.common.data.models.WooPosProductModelVersion2 +import com.woocommerce.android.ui.woopos.util.generateWooPosProduct import kotlinx.coroutines.runBlocking import kotlinx.coroutines.test.runTest import org.junit.Assert.assertEquals @@ -25,11 +26,9 @@ class WooPosSearchByIdentifierProcessVariationResultTest { // GIVEN val parentId = 123L val variationId = 456L - val product: Product = mock { - on { this.parentId }.thenReturn(parentId) - on { remoteId }.thenReturn(variationId) - } - val parentProduct: Product = mock() + val product: WooPosProductModelVersion2 = generateWooPosProduct(productId = variationId, parentId = parentId) + + val parentProduct: WooPosProductModelVersion2 = generateWooPosProduct() val variation: ProductVariation = mock() runBlocking { @@ -51,10 +50,7 @@ class WooPosSearchByIdentifierProcessVariationResultTest { @Test fun `given product with invalid parent id, when invoke called, then return product not found failure`() = runTest { // GIVEN - val product: Product = mock { - on { parentId }.thenReturn(0L) - on { remoteId }.thenReturn(456L) - } + val product: WooPosProductModelVersion2 = generateWooPosProduct(productId = 456L, parentId = 0L) // WHEN val result = sut(product) @@ -71,10 +67,7 @@ class WooPosSearchByIdentifierProcessVariationResultTest { // GIVEN val parentId = 123L val variationId = 456L - val product: Product = mock { - on { this.parentId }.thenReturn(parentId) - on { remoteId }.thenReturn(variationId) - } + val product: WooPosProductModelVersion2 = generateWooPosProduct(productId = variationId, parentId = parentId) runBlocking { whenever( @@ -104,10 +97,8 @@ class WooPosSearchByIdentifierProcessVariationResultTest { // GIVEN val parentId = 123L val variationId = 456L - val product: Product = mock { - on { this.parentId }.thenReturn(parentId) - on { remoteId }.thenReturn(variationId) - } + val product: WooPosProductModelVersion2 = generateWooPosProduct(productId = variationId, parentId = parentId) + val variation: ProductVariation = mock() val expectedFailure = WooPosSearchByIdentifierResult.Failure(WooPosSearchByIdentifierResult.Error.NetworkError) @@ -130,10 +121,7 @@ class WooPosSearchByIdentifierProcessVariationResultTest { // GIVEN val parentId = 123L val variationId = 456L - val product: Product = mock { - on { this.parentId }.thenReturn(parentId) - on { remoteId }.thenReturn(variationId) - } + val product: WooPosProductModelVersion2 = generateWooPosProduct(productId = variationId, parentId = parentId) val variationResult = WooPosSearchByIdentifierVariationFetch.VariationFetchResult.Failure( WooPosSearchByIdentifierResult.Error.UnknownError("Variation not found for ID: $variationId") diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/util/WooPosProductUtils.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/util/WooPosProductUtils.kt index 96fbd43cacac..ffe870f0f347 100644 --- a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/util/WooPosProductUtils.kt +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/util/WooPosProductUtils.kt @@ -8,6 +8,7 @@ import com.woocommerce.android.ui.woopos.common.data.models.WooPosProductModelVe fun generateWooPosProduct( productId: Long = 1, + parentId: Long? = null, productName: String = "Product 1", status: WooPosProductStatus = WooPosProductStatus.PUBLISH, amount: String = "10.0", @@ -20,7 +21,7 @@ fun generateWooPosProduct( pricing = WooPosPricing.RegularPricing(amount.toBigDecimal()), type = productType, isDownloadable = isDownloadable, - parentId = null, + parentId = parentId, sku = "", globalUniqueId = "", status = status, From f41b001f5d9e2436a67df0a111ede678a7766546 Mon Sep 17 00:00:00 2001 From: malinajirka Date: Mon, 8 Sep 2025 08:54:21 +0200 Subject: [PATCH 3/4] Migrate WooPosSearchByIdentifierResultConverter --- .../WooPosSearchByIdentifierResultConverter.kt | 2 +- .../WooPosSearchByIdentifierResultConverterTest.kt | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierResultConverter.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierResultConverter.kt index 9b2e2e6dce44..deb274a191bf 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierResultConverter.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierResultConverter.kt @@ -15,7 +15,7 @@ class WooPosSearchByIdentifierResultConverter @Inject constructor( return when (result) { is WooPosSearchByIdentifierResult.Success -> { val product = result.product - if (product.type.equals("variation", ignoreCase = true)) { + if (product.type.value.equals("variation", ignoreCase = true)) { variationProcess(product) } else { productsCache.addAll(listOf(product)) diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierResultConverterTest.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierResultConverterTest.kt index d0b14fc4ac46..449beb6dffb8 100644 --- a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierResultConverterTest.kt +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierResultConverterTest.kt @@ -2,7 +2,9 @@ package com.woocommerce.android.ui.woopos.common.data.searchbyidentifier import com.woocommerce.android.ui.products.ProductTestUtils import com.woocommerce.android.ui.woopos.common.data.WooPosProductsCache +import com.woocommerce.android.ui.woopos.common.data.models.WooPosProductModelVersion2 import com.woocommerce.android.ui.woopos.util.WooPosCoroutineTestRule +import com.woocommerce.android.ui.woopos.util.generateWooPosProduct import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.UnconfinedTestDispatcher import kotlinx.coroutines.test.runTest @@ -25,7 +27,7 @@ class WooPosSearchByIdentifierResultConverterTest { private val productsCache: WooPosProductsCache = mock() private val variationProcess: WooPosSearchByIdentifierProcessVariationResult = mock() - private val testProduct = ProductTestUtils.generateProduct() + private val testProduct = generateWooPosProduct() private val testVariation = ProductTestUtils.generateProductVariation() @Before @@ -51,7 +53,7 @@ class WooPosSearchByIdentifierResultConverterTest { @Test fun `given variation product success result, when converting, should process variation`() = runTest { // GIVEN - val variationProduct = testProduct.copy(type = "variation") + val variationProduct = testProduct.copy(type = WooPosProductModelVersion2.WooPosProductType.VARIATION) val successResult = WooPosSearchByIdentifierResult.Success(variationProduct) val variationSuccessResult = WooPosSearchByIdentifierResult.VariationSuccess(testVariation, testProduct) val searchFunction: suspend () -> WooPosSearchByIdentifierResult = { successResult } From f5884ec487f2902f352ebac3ea40f1fa5b7b85d7 Mon Sep 17 00:00:00 2001 From: malinajirka Date: Wed, 10 Sep 2025 14:47:26 +0200 Subject: [PATCH 4/4] Fix variation comparison --- .../WooPosSearchByIdentifierResultConverter.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierResultConverter.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierResultConverter.kt index deb274a191bf..a25aaefdd438 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierResultConverter.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/common/data/searchbyidentifier/WooPosSearchByIdentifierResultConverter.kt @@ -1,6 +1,7 @@ package com.woocommerce.android.ui.woopos.common.data.searchbyidentifier import com.woocommerce.android.ui.woopos.common.data.WooPosProductsCache +import com.woocommerce.android.ui.woopos.common.data.models.WooPosProductModelVersion2 import javax.inject.Inject class WooPosSearchByIdentifierResultConverter @Inject constructor( @@ -15,7 +16,7 @@ class WooPosSearchByIdentifierResultConverter @Inject constructor( return when (result) { is WooPosSearchByIdentifierResult.Success -> { val product = result.product - if (product.type.value.equals("variation", ignoreCase = true)) { + if (product.type == WooPosProductModelVersion2.WooPosProductType.VARIATION) { variationProcess(product) } else { productsCache.addAll(listOf(product))