diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosLocalCatalogSyncRepository.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosLocalCatalogSyncRepository.kt index d1033608466d..ca1450899f26 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosLocalCatalogSyncRepository.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosLocalCatalogSyncRepository.kt @@ -1,8 +1,6 @@ package com.woocommerce.android.ui.woopos.localcatalog import com.woocommerce.android.ui.woopos.common.util.WooPosLogWrapper -import com.woocommerce.android.ui.woopos.localcatalog.WooPosSyncProductsAction.WooPosSyncProductsResult -import com.woocommerce.android.ui.woopos.localcatalog.WooPosSyncVariationsAction.WooPosSyncVariationsResult import com.woocommerce.android.ui.woopos.util.datastore.WooPosPreferencesRepository import com.woocommerce.android.ui.woopos.util.datastore.WooPosSyncTimestampManager import com.woocommerce.android.util.CoroutineDispatchers @@ -95,20 +93,20 @@ class WooPosLocalCatalogSyncRepository @Inject constructor( } val productSyncResult = syncProducts(site, modifiedAfterGmt, pageSize, maxPages) - if (productSyncResult is WooPosSyncProductsResult.Failed) { + if (productSyncResult is WooPosSyncResult.Failed) { return productSyncResult.toPosLocalCatalogSyncFailure() } val variationSyncResult = syncVariations(site, modifiedAfterGmt, pageSize, maxPages) - if (variationSyncResult is WooPosSyncVariationsResult.Failed) { + if (variationSyncResult is WooPosSyncResult.Failed) { return variationSyncResult.toPosLocalCatalogSyncFailure() } val syncDuration = System.currentTimeMillis() - startTime return PosLocalCatalogSyncResult.Success( - productsSynced = (productSyncResult as WooPosSyncProductsResult.Success).productsSynced, - variationsSynced = (variationSyncResult as WooPosSyncVariationsResult.Success).variationsSynced, + productsSynced = (productSyncResult as WooPosSyncResult.Success).syncedCount, + variationsSynced = (variationSyncResult as WooPosSyncResult.Success).syncedCount, syncDurationMs = syncDuration ) } @@ -118,10 +116,10 @@ class WooPosLocalCatalogSyncRepository @Inject constructor( modifiedAfterGmt: String?, pageSize: Int, maxPages: Int - ): WooPosSyncProductsResult { + ): WooPosSyncResult { val result = posSyncAction.syncProducts(site, modifiedAfterGmt, pageSize, maxPages) - if (result is WooPosSyncProductsResult.Success) { + if (result is WooPosSyncResult.Success) { result.serverDate?.let { serverDate -> syncTimestampManager.parseTimestampFromApi(serverDate)?.let { timestamp -> syncTimestampManager.storeProductsLastSyncTimestamp(timestamp) @@ -138,10 +136,10 @@ class WooPosLocalCatalogSyncRepository @Inject constructor( modifiedAfterGmt: String?, pageSize: Int, maxPages: Int - ): WooPosSyncVariationsResult { + ): WooPosSyncResult { val result = posSyncAction.syncVariations(site, modifiedAfterGmt, pageSize, maxPages) - if (result is WooPosSyncVariationsResult.Success) { + if (result is WooPosSyncResult.Success) { result.serverDate?.let { serverDate -> syncTimestampManager.parseTimestampFromApi(serverDate)?.let { timestamp -> syncTimestampManager.storeVariationsLastSyncTimestamp(timestamp) @@ -160,34 +158,18 @@ class WooPosLocalCatalogSyncRepository @Inject constructor( posLocalCatalogStore.getVariationCount(LocalId(site.id)).getOrElse { 0 } } -private fun WooPosSyncProductsResult.Failed.toPosLocalCatalogSyncFailure(): PosLocalCatalogSyncResult.Failure { +private fun WooPosSyncResult.Failed.toPosLocalCatalogSyncFailure(): PosLocalCatalogSyncResult.Failure { return when (this) { - is WooPosSyncProductsResult.Failed.CatalogTooLarge -> { + is WooPosSyncResult.Failed.CatalogTooLarge -> { PosLocalCatalogSyncResult.Failure.CatalogTooLarge( - error = "Product catalog too large: $totalPages pages exceed maximum of $maxPages pages", + error = "Catalog too large: $totalPages pages exceed maximum of $maxPages pages", totalPages = totalPages, maxPages = maxPages ) } - else -> { - PosLocalCatalogSyncResult.Failure.UnexpectedError(error) - } - } -} - -private fun WooPosSyncVariationsResult.Failed.toPosLocalCatalogSyncFailure(): PosLocalCatalogSyncResult.Failure { - return when (this) { - is WooPosSyncVariationsResult.Failed.CatalogTooLarge -> { - PosLocalCatalogSyncResult.Failure.CatalogTooLarge( - error = "Variations catalog too large: $totalPages pages exceed maximum of $maxPages pages", - totalPages = totalPages, - maxPages = maxPages - ) - } - - else -> { - PosLocalCatalogSyncResult.Failure.UnexpectedError(error) + is WooPosSyncResult.Failed.UnexpectedError -> { + PosLocalCatalogSyncResult.Failure.UnexpectedError(errorMessage) } } } diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosSyncAction.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosSyncAction.kt index de5e0b7e333b..a30d85590abb 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosSyncAction.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosSyncAction.kt @@ -7,9 +7,8 @@ import org.wordpress.android.fluxc.model.SiteModel import org.wordpress.android.fluxc.network.rest.wpcom.wc.product.CoreProductStatus import org.wordpress.android.fluxc.persistence.entity.pos.WooPosProductEntity import org.wordpress.android.fluxc.persistence.entity.pos.WooPosVariationEntity -import org.wordpress.android.fluxc.store.pos.localcatalog.WooPosLocalCatalogFetchProductsResult import org.wordpress.android.fluxc.store.pos.localcatalog.WooPosLocalCatalogStore -import org.wordpress.android.fluxc.store.pos.localcatalog.WooPosVariationsFetchResult +import org.wordpress.android.fluxc.store.pos.localcatalog.WooPosPaginatedFetchResult import javax.inject.Inject class WooPosSyncAction @Inject constructor( @@ -43,27 +42,42 @@ class WooPosSyncAction @Inject constructor( private typealias ServerDate = String +sealed interface WooPosSyncResult { + val syncedCount: Int + val serverDate: String? + + data class Success( + override val syncedCount: Int, + override val serverDate: String? + ) : WooPosSyncResult + + sealed class Failed( + val error: String + ) : WooPosSyncResult { + override val syncedCount: Int = 0 + override val serverDate: String? = null + + data class CatalogTooLarge( + val totalPages: Int, + val maxPages: Int + ) : Failed("Local Catalog too large: $totalPages pages exceed maximum of $maxPages pages") + + data class UnexpectedError( + val errorMessage: String + ) : Failed(errorMessage) + } +} + class WooPosSyncProductsAction @Inject constructor( private val posLocalCatalogStore: WooPosLocalCatalogStore, private val logger: WooPosLogWrapper, ) { - sealed class WooPosSyncProductsResult { - data class Success(val productsSynced: Int, val serverDate: String?) : WooPosSyncProductsResult() - - sealed class Failed(val error: String) : WooPosSyncProductsResult() { - class CatalogTooLarge(val totalPages: Int, val maxPages: Int) : - Failed("Local Catalog too large: $totalPages pages exceed maximum of $maxPages pages") - - class UnexpectedError(error: String) : Failed(error) - } - } - suspend fun execute( site: SiteModel, modifiedAfterGmt: String? = null, pageSize: Int, maxPages: Int - ): WooPosSyncProductsResult { + ): WooPosSyncResult { return runCatching { val isFullSync = modifiedAfterGmt == null @@ -98,7 +112,7 @@ class WooPosSyncProductsAction @Inject constructor( }.fold( onSuccess = { logger.d("Local Catalog transaction committed successfully") - WooPosSyncProductsResult.Success(allProducts.size, serverDate) + WooPosSyncResult.Success(allProducts.size, serverDate) }, onFailure = { error -> handleTransactionError(error) @@ -109,8 +123,12 @@ class WooPosSyncProductsAction @Inject constructor( onFailure = { error -> logger.e("Failed to sync products: ${error.message}") when (error) { - is CatalogTooLargeException -> error.toSyncResult() - else -> WooPosSyncProductsResult.Failed.UnexpectedError( + is CatalogTooLargeException -> WooPosSyncResult.Failed.CatalogTooLarge( + error.totalPages, + error.maxPages + ) + + else -> WooPosSyncResult.Failed.UnexpectedError( error.message ?: "Failed to sync products" ) } @@ -124,77 +142,32 @@ class WooPosSyncProductsAction @Inject constructor( pageSize: Int, maxPages: Int ): Pair, ServerDate> { - var currentPage = 1 - var pagesSynced = 0 - var totalPages = maxPages - var firstPageServerDate: String? = null - - val products = mutableListOf() - - while (pagesSynced < totalPages) { - val result = posLocalCatalogStore.fetchRecentlyModifiedProducts( - site = site, - pageSize = pageSize, - modifiedAfterGmt = modifiedAfterGmt, - page = currentPage, - ) - - result.fold( - onSuccess = { syncResult -> - processPageResult(syncResult, pagesSynced, maxPages) - if (pagesSynced == 0) { - firstPageServerDate = syncResult.serverDate - } - products.addAll(syncResult.products) - totalPages = syncResult.totalPages - pagesSynced++ - - if (!syncResult.hasMore || syncResult.syncedCount == 0) { - logger.d("Local Catalog: No more products to sync") - } else { - currentPage = syncResult.nextPage - } - }, - onFailure = { error -> - logger.e("Local Catalog Sync failed on page ${pagesSynced + 1}: ${error.message}") - throw error - } - ) - } - - logger.d("Local catalog sync completed, products synced across $pagesSynced pages") - return Pair( - products.toList(), - requireNotNull(firstPageServerDate) { "Can't be null since we throw an exception in the store layer." } - ) - } - - private fun processPageResult( - syncResult: WooPosLocalCatalogFetchProductsResult, - pagesSynced: Int, - maxPages: Int - ) { - if (pagesSynced == 0) { - if (syncResult.totalPages > maxPages) { - logger.e( - "Local Catalog too large: ${syncResult.totalPages} pages exceed maximum of $maxPages pages" + val helper = PaginatedSyncHelper( + logger = logger, + entityType = "products", + fetchPage = { page -> + posLocalCatalogStore.fetchRecentlyModifiedProducts( + site = site, + pageSize = pageSize, + modifiedAfterGmt = modifiedAfterGmt, + page = page, ) - throw CatalogTooLargeException(syncResult.totalPages, maxPages) } - } - logger.d("Local Catalog page ${pagesSynced + 1} synced, ${syncResult.syncedCount} products") + ) + + return helper.fetchAllPages(maxPages) } - private fun handleTransactionError(error: Throwable): WooPosSyncProductsResult { + private fun handleTransactionError(error: Throwable): WooPosSyncResult { return when (error) { is CatalogTooLargeException -> { logger.e("Local Catalog too large, transaction rolled back") - error.toSyncResult() + WooPosSyncResult.Failed.CatalogTooLarge(error.totalPages, error.maxPages) } else -> { logger.e("Local Catalog Transaction failed and was rolled back: ${error.message}") - WooPosSyncProductsResult.Failed.UnexpectedError( + WooPosSyncResult.Failed.UnexpectedError( error.message ?: "Local Catalog Transaction failed and was rolled back" ) } @@ -205,52 +178,24 @@ class WooPosSyncProductsAction @Inject constructor( site: SiteModel, pageSize: Int ): List { - var currentPage = 1 - var pagesSynced = 0 - val trashProducts = mutableListOf() - logger.d("Fetching all trash products for incremental sync") - while (true) { - val result = posLocalCatalogStore.fetchRecentlyModifiedProducts( - site = site, - pageSize = pageSize, - modifiedAfterGmt = null, - page = currentPage, - includeStatus = listOf(CoreProductStatus.TRASH) - ) - - result.fold( - onSuccess = { syncResult -> - trashProducts.addAll(syncResult.products) - pagesSynced++ - - if (!syncResult.hasMore || syncResult.syncedCount == 0) { - logger.d( - "Finished fetching trash products: ${trashProducts.size} total across $pagesSynced pages" - ) - break - } else { - currentPage = syncResult.nextPage - } - }, - onFailure = { error -> - logger.e("Failed to fetch trash products on page ${pagesSynced + 1}: ${error.message}") - throw error - } - ) - } - - return trashProducts.toList() - } + val helper = PaginatedSyncHelper( + logger = logger, + entityType = "trash products", + fetchPage = { page -> + posLocalCatalogStore.fetchRecentlyModifiedProducts( + site = site, + pageSize = pageSize, + modifiedAfterGmt = null, + page = page, + includeStatus = listOf(CoreProductStatus.TRASH) + ) + } + ) - internal class CatalogTooLargeException( - val totalPages: Int, - val maxPages: Int - ) : Exception("Local Catalog too large: $totalPages pages exceed maximum of $maxPages pages") { - fun toSyncResult(): WooPosSyncProductsResult.Failed.CatalogTooLarge { - return WooPosSyncProductsResult.Failed.CatalogTooLarge(totalPages, maxPages) - } + val (trashProducts, _) = helper.fetchAllPages(Int.MAX_VALUE) + return trashProducts } } @@ -258,23 +203,12 @@ class WooPosSyncVariationsAction @Inject constructor( private val posLocalCatalogStore: WooPosLocalCatalogStore, private val logger: WooPosLogWrapper, ) { - sealed class WooPosSyncVariationsResult { - data class Success(val variationsSynced: Int, val serverDate: String?) : WooPosSyncVariationsResult() - - sealed class Failed(val error: String) : WooPosSyncVariationsResult() { - class CatalogTooLarge(val totalPages: Int, val maxPages: Int) : - Failed("Catalog too large: $totalPages pages exceed maximum of $maxPages pages") - - class UnexpectedError(error: String) : Failed(error) - } - } - suspend fun execute( site: SiteModel, modifiedAfterGmt: String? = null, pageSize: Int, maxPages: Int - ): WooPosSyncVariationsResult { + ): WooPosSyncResult { return runCatching { val (variations, serverDate) = fetchAllPages(site, modifiedAfterGmt, pageSize, maxPages) @@ -290,7 +224,7 @@ class WooPosSyncVariationsAction @Inject constructor( }.fold( onSuccess = { logger.d("Local Catalog variations transaction committed successfully") - WooPosSyncVariationsResult.Success(variations.size, serverDate) + WooPosSyncResult.Success(variations.size, serverDate) }, onFailure = { error -> handleTransactionError(error) @@ -301,8 +235,12 @@ class WooPosSyncVariationsAction @Inject constructor( onFailure = { error -> logger.e("Failed to sync variations: ${error.message}") when (error) { - is CatalogTooLargeException -> error.toSyncResult() - else -> WooPosSyncVariationsResult.Failed.UnexpectedError( + is CatalogTooLargeException -> WooPosSyncResult.Failed.CatalogTooLarge( + error.totalPages, + error.maxPages + ) + + else -> WooPosSyncResult.Failed.UnexpectedError( error.message ?: "Failed to sync variations" ) } @@ -316,90 +254,101 @@ class WooPosSyncVariationsAction @Inject constructor( pageSize: Int, maxPages: Int ): Pair, String> { + val helper = PaginatedSyncHelper( + logger = logger, + entityType = "variations", + fetchPage = { page -> + posLocalCatalogStore.fetchRecentlyModifiedVariations( + site = site, + modifiedAfterGmt = modifiedAfterGmt, + page = page, + pageSize = pageSize, + ) + } + ) + + return helper.fetchAllPages(maxPages) + } + + private fun handleTransactionError(error: Throwable): WooPosSyncResult { + return when (error) { + is CatalogTooLargeException -> { + logger.e("Local Catalog variations too large, transaction rolled back") + WooPosSyncResult.Failed.CatalogTooLarge(error.totalPages, error.maxPages) + } + + else -> { + logger.e("Local Catalog Variations Transaction failed and was rolled back: ${error.message}") + WooPosSyncResult.Failed.UnexpectedError( + error.message ?: "Local Catalog Variations Transaction failed and was rolled back" + ) + } + } + } +} + +private class CatalogTooLargeException( + val totalPages: Int, + val maxPages: Int +) : Exception("Catalog too large: $totalPages pages exceed maximum of $maxPages pages") + +private class PaginatedSyncHelper( + private val logger: WooPosLogWrapper, + private val entityType: String, + private val fetchPage: suspend (page: Int) -> Result> +) { + suspend fun fetchAllPages( + maxPages: Int + ): Pair, ServerDate> { var currentPage = 1 var pagesSynced = 0 var totalPages = maxPages var firstPageServerDate: String? = null - val variations = mutableListOf() + val items = mutableListOf() while (pagesSynced < totalPages) { - val result = posLocalCatalogStore.fetchRecentlyModifiedVariations( - site = site, - modifiedAfterGmt = modifiedAfterGmt, - page = currentPage, - pageSize = pageSize, - ) + val result = fetchPage(currentPage) result.fold( onSuccess = { syncResult -> - processPageResult(syncResult, pagesSynced, maxPages) if (pagesSynced == 0) { + if (syncResult.totalPages > maxPages) { + logger.e( + "Local Catalog $entityType too large: ${syncResult.totalPages} " + + "pages exceed maximum of $maxPages pages" + ) + throw CatalogTooLargeException(syncResult.totalPages, maxPages) + } firstPageServerDate = syncResult.serverDate + totalPages = syncResult.totalPages } - variations.addAll(syncResult.variations) - totalPages = syncResult.totalPages + + items.addAll(syncResult.items) pagesSynced++ + logger.d( + "Local Catalog $entityType page $pagesSynced synced, " + + "${syncResult.syncedCount} $entityType" + ) if (!syncResult.hasMore || syncResult.syncedCount == 0) { - logger.d("Local Catalog: No more variations to sync") + logger.d("Local Catalog: No more $entityType to sync") + break } else { currentPage = syncResult.nextPage } }, onFailure = { error -> - logger.e("Local Catalog Variations sync failed on page ${pagesSynced + 1}: ${error.message}") + logger.e("Local Catalog $entityType sync failed on page ${pagesSynced + 1}: ${error.message}") throw error } ) } - logger.d("Local catalog variations sync completed, variations synced across $pagesSynced pages") + logger.d("Local catalog $entityType sync completed, $entityType synced across $pagesSynced pages") return Pair( - variations.toList(), + items.toList(), requireNotNull(firstPageServerDate) { "Can't be null since we throw an exception in the store layer." } ) } - - private fun processPageResult( - syncResult: WooPosVariationsFetchResult, - pagesSynced: Int, - maxPages: Int - ) { - if (pagesSynced == 0) { - if (syncResult.totalPages > maxPages) { - logger.e( - "Local Catalog variations too large: ${syncResult.totalPages} pages exceed maximum " + - "of $maxPages pages" - ) - throw CatalogTooLargeException(syncResult.totalPages, maxPages) - } - } - logger.d("Local Catalog variations page ${pagesSynced + 1} synced, ${syncResult.syncedCount} variations") - } - - private fun handleTransactionError(error: Throwable): WooPosSyncVariationsResult { - return when (error) { - is CatalogTooLargeException -> { - logger.e("Local Catalog variations too large, transaction rolled back") - error.toSyncResult() - } - - else -> { - logger.e("Local Catalog Variations Transaction failed and was rolled back: ${error.message}") - WooPosSyncVariationsResult.Failed.UnexpectedError( - error.message ?: "Local Catalog Variations Transaction failed and was rolled back" - ) - } - } - } - - internal class CatalogTooLargeException( - val totalPages: Int, - val maxPages: Int - ) : Exception("Local Catalog variations too large: $totalPages pages exceed maximum of $maxPages pages") { - fun toSyncResult(): WooPosSyncVariationsResult.Failed.CatalogTooLarge { - return WooPosSyncVariationsResult.Failed.CatalogTooLarge(totalPages, maxPages) - } - } } diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosLocalCatalogSyncRepositoryTest.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosLocalCatalogSyncRepositoryTest.kt index a41a93355599..b3bc0ff5bc86 100644 --- a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosLocalCatalogSyncRepositoryTest.kt +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosLocalCatalogSyncRepositoryTest.kt @@ -65,10 +65,10 @@ class WooPosLocalCatalogSyncRepositoryTest : BaseUnitTest() { val productsSynced = 150 whenever(posSyncAction.syncProducts(any(), anyOrNull(), any(), any())) .thenReturn( - WooPosSyncProductsAction.WooPosSyncProductsResult.Success(productsSynced, "2024-01-01T12:00:00Z") + WooPosSyncResult.Success(productsSynced, "2024-01-01T12:00:00Z") ) whenever(posSyncAction.syncVariations(any(), anyOrNull(), any(), any())) - .thenReturn(WooPosSyncVariationsAction.WooPosSyncVariationsResult.Success(50, "2024-01-01T12:00:00Z")) + .thenReturn(WooPosSyncResult.Success(50, "2024-01-01T12:00:00Z")) // WHEN val result = sut.syncLocalCatalogFull(site) @@ -83,10 +83,10 @@ class WooPosLocalCatalogSyncRepositoryTest : BaseUnitTest() { val productsSynced = 150 whenever(posSyncAction.syncProducts(any(), anyOrNull(), any(), any())) .thenReturn( - WooPosSyncProductsAction.WooPosSyncProductsResult.Success(productsSynced, "2024-01-01T12:00:00Z") + WooPosSyncResult.Success(productsSynced, "2024-01-01T12:00:00Z") ) whenever(posSyncAction.syncVariations(any(), anyOrNull(), any(), any())) - .thenReturn(WooPosSyncVariationsAction.WooPosSyncVariationsResult.Success(50, "2024-01-01T12:00:00Z")) + .thenReturn(WooPosSyncResult.Success(50, "2024-01-01T12:00:00Z")) // WHEN sut.syncLocalCatalogFull(site) @@ -102,10 +102,10 @@ class WooPosLocalCatalogSyncRepositoryTest : BaseUnitTest() { val productsSynced = 150 whenever(posSyncAction.syncProducts(any(), anyOrNull(), any(), any())) .thenReturn( - WooPosSyncProductsAction.WooPosSyncProductsResult.Success(productsSynced, "2024-01-01T12:00:00Z") + WooPosSyncResult.Success(productsSynced, "2024-01-01T12:00:00Z") ) whenever(posSyncAction.syncVariations(any(), anyOrNull(), any(), any())) - .thenReturn(WooPosSyncVariationsAction.WooPosSyncVariationsResult.Success(50, "2024-01-01T12:00:00Z")) + .thenReturn(WooPosSyncResult.Success(50, "2024-01-01T12:00:00Z")) // WHEN sut.syncLocalCatalogFull(site) @@ -120,7 +120,7 @@ class WooPosLocalCatalogSyncRepositoryTest : BaseUnitTest() { val totalPages = 15 val maxPages = WooPosLocalCatalogSyncRepository.MAX_PAGES_PER_FULL_SYNC whenever(posSyncAction.syncProducts(any(), anyOrNull(), any(), any())) - .thenReturn(WooPosSyncProductsAction.WooPosSyncProductsResult.Failed.CatalogTooLarge(totalPages, maxPages)) + .thenReturn(WooPosSyncResult.Failed.CatalogTooLarge(totalPages, maxPages)) // WHEN sut.syncLocalCatalogFull(site) @@ -135,7 +135,7 @@ class WooPosLocalCatalogSyncRepositoryTest : BaseUnitTest() { val totalPages = 15 val maxPages = WooPosLocalCatalogSyncRepository.MAX_PAGES_PER_FULL_SYNC whenever(posSyncAction.syncProducts(any(), anyOrNull(), any(), any())) - .thenReturn(WooPosSyncProductsAction.WooPosSyncProductsResult.Failed.CatalogTooLarge(totalPages, maxPages)) + .thenReturn(WooPosSyncResult.Failed.CatalogTooLarge(totalPages, maxPages)) // WHEN val result = sut.syncLocalCatalogFull(site) @@ -149,7 +149,7 @@ class WooPosLocalCatalogSyncRepositoryTest : BaseUnitTest() { // GIVEN val errorMessage = "Network timeout" whenever(posSyncAction.syncProducts(any(), anyOrNull(), any(), any())) - .thenReturn(WooPosSyncProductsAction.WooPosSyncProductsResult.Failed.UnexpectedError(errorMessage)) + .thenReturn(WooPosSyncResult.Failed.UnexpectedError(errorMessage)) // WHEN val result = sut.syncLocalCatalogFull(site) @@ -164,10 +164,10 @@ class WooPosLocalCatalogSyncRepositoryTest : BaseUnitTest() { val productsSynced = 150 whenever(posSyncAction.syncProducts(any(), anyOrNull(), any(), any())) .thenReturn( - WooPosSyncProductsAction.WooPosSyncProductsResult.Success(productsSynced, "2024-01-01T12:00:00Z") + WooPosSyncResult.Success(productsSynced, "2024-01-01T12:00:00Z") ) whenever(posSyncAction.syncVariations(any(), anyOrNull(), any(), any())) - .thenReturn(WooPosSyncVariationsAction.WooPosSyncVariationsResult.Success(50, "2024-01-01T12:00:00Z")) + .thenReturn(WooPosSyncResult.Success(50, "2024-01-01T12:00:00Z")) // WHEN val result = sut.syncLocalCatalogIncremental(site) @@ -182,10 +182,10 @@ class WooPosLocalCatalogSyncRepositoryTest : BaseUnitTest() { val productsSynced = 150 whenever(posSyncAction.syncProducts(any(), anyOrNull(), any(), any())) .thenReturn( - WooPosSyncProductsAction.WooPosSyncProductsResult.Success(productsSynced, "2024-01-01T12:00:00Z") + WooPosSyncResult.Success(productsSynced, "2024-01-01T12:00:00Z") ) whenever(posSyncAction.syncVariations(any(), anyOrNull(), any(), any())) - .thenReturn(WooPosSyncVariationsAction.WooPosSyncVariationsResult.Success(50, "2024-01-01T12:00:00Z")) + .thenReturn(WooPosSyncResult.Success(50, "2024-01-01T12:00:00Z")) // WHEN sut.syncLocalCatalogIncremental(site) @@ -200,7 +200,7 @@ class WooPosLocalCatalogSyncRepositoryTest : BaseUnitTest() { val totalPages = 15 val maxPages = WooPosLocalCatalogSyncRepository.MAX_PAGES_PER_FULL_SYNC whenever(posSyncAction.syncProducts(any(), anyOrNull(), any(), any())) - .thenReturn(WooPosSyncProductsAction.WooPosSyncProductsResult.Failed.CatalogTooLarge(totalPages, maxPages)) + .thenReturn(WooPosSyncResult.Failed.CatalogTooLarge(totalPages, maxPages)) // WHEN val result = sut.syncLocalCatalogIncremental(site) @@ -214,7 +214,7 @@ class WooPosLocalCatalogSyncRepositoryTest : BaseUnitTest() { // GIVEN val errorMessage = "Network timeout" whenever(posSyncAction.syncProducts(any(), anyOrNull(), any(), any())) - .thenReturn(WooPosSyncProductsAction.WooPosSyncProductsResult.Failed.UnexpectedError(errorMessage)) + .thenReturn(WooPosSyncResult.Failed.UnexpectedError(errorMessage)) // WHEN val result = sut.syncLocalCatalogIncremental(site) @@ -241,9 +241,9 @@ class WooPosLocalCatalogSyncRepositoryTest : BaseUnitTest() { fun `given catalog size is exactly at limit, when sync starts, then proceeds with sync`() = testBlocking { // GIVEN whenever(posSyncAction.syncProducts(any(), anyOrNull(), any(), any())) - .thenReturn(WooPosSyncProductsAction.WooPosSyncProductsResult.Success(600, "2024-01-01T12:00:00Z")) + .thenReturn(WooPosSyncResult.Success(600, "2024-01-01T12:00:00Z")) whenever(posSyncAction.syncVariations(any(), anyOrNull(), any(), any())) - .thenReturn(WooPosSyncVariationsAction.WooPosSyncVariationsResult.Success(400, "2024-01-01T12:00:00Z")) + .thenReturn(WooPosSyncResult.Success(400, "2024-01-01T12:00:00Z")) // WHEN val result = sut.syncLocalCatalogFull(site) @@ -257,9 +257,9 @@ class WooPosLocalCatalogSyncRepositoryTest : BaseUnitTest() { // GIVEN givenCatalogSizeUnknown() whenever(posSyncAction.syncProducts(any(), anyOrNull(), any(), any())) - .thenReturn(WooPosSyncProductsAction.WooPosSyncProductsResult.Success(100, "2024-01-01T12:00:00Z")) + .thenReturn(WooPosSyncResult.Success(100, "2024-01-01T12:00:00Z")) whenever(posSyncAction.syncVariations(any(), anyOrNull(), any(), any())) - .thenReturn(WooPosSyncVariationsAction.WooPosSyncVariationsResult.Success(50, "2024-01-01T12:00:00Z")) + .thenReturn(WooPosSyncResult.Success(50, "2024-01-01T12:00:00Z")) // WHEN val result = sut.syncLocalCatalogFull(site) @@ -273,9 +273,9 @@ class WooPosLocalCatalogSyncRepositoryTest : BaseUnitTest() { // GIVEN givenCatalogSizeUnknown() whenever(posSyncAction.syncProducts(any(), anyOrNull(), any(), any())) - .thenReturn(WooPosSyncProductsAction.WooPosSyncProductsResult.Success(500, "2024-01-01T12:00:00Z")) + .thenReturn(WooPosSyncResult.Success(500, "2024-01-01T12:00:00Z")) whenever(posSyncAction.syncVariations(any(), anyOrNull(), any(), any())) - .thenReturn(WooPosSyncVariationsAction.WooPosSyncVariationsResult.Success(200, "2024-01-01T12:00:00Z")) + .thenReturn(WooPosSyncResult.Success(200, "2024-01-01T12:00:00Z")) // WHEN val result = sut.syncLocalCatalogFull(site) diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosSyncProductsActionTest.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosSyncProductsActionTest.kt index 496ad845175c..242b658765d8 100644 --- a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosSyncProductsActionTest.kt +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosSyncProductsActionTest.kt @@ -2,7 +2,6 @@ package com.woocommerce.android.ui.woopos.localcatalog import com.woocommerce.android.ui.woopos.common.util.WooPosLogWrapper import com.woocommerce.android.ui.woopos.localcatalog.WooPosLocalCatalogSyncRepository.Companion.PAGE_SIZE -import com.woocommerce.android.ui.woopos.localcatalog.WooPosSyncProductsAction.WooPosSyncProductsResult import kotlinx.coroutines.runBlocking import kotlinx.coroutines.test.runTest import org.assertj.core.api.Assertions.assertThat @@ -21,8 +20,8 @@ import org.wordpress.android.fluxc.model.LocalOrRemoteId import org.wordpress.android.fluxc.model.SiteModel import org.wordpress.android.fluxc.network.rest.wpcom.wc.product.CoreProductStatus import org.wordpress.android.fluxc.persistence.entity.pos.WooPosProductEntity -import org.wordpress.android.fluxc.store.pos.localcatalog.WooPosLocalCatalogFetchProductsResult import org.wordpress.android.fluxc.store.pos.localcatalog.WooPosLocalCatalogStore +import org.wordpress.android.fluxc.store.pos.localcatalog.WooPosPaginatedFetchResult import kotlin.Result as KotlinResult class WooPosSyncProductsActionTest { @@ -52,8 +51,8 @@ class WooPosSyncProductsActionTest { val result = sut.execute(site, pageSize = 100, maxPages = 2) // THEN - assertThat(result).isInstanceOf(WooPosSyncProductsResult.Success::class.java) - assertThat((result as WooPosSyncProductsResult.Success).productsSynced).isEqualTo(50) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) + assertThat((result as WooPosSyncResult.Success).syncedCount).isEqualTo(50) } @Test @@ -70,8 +69,8 @@ class WooPosSyncProductsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = maxPages) // THEN - assertThat(result).isInstanceOf(WooPosSyncProductsResult.Success::class.java) - assertThat((result as WooPosSyncProductsResult.Success).productsSynced).isEqualTo(250) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) + assertThat((result as WooPosSyncResult.Success).syncedCount).isEqualTo(250) } @Test @@ -84,8 +83,8 @@ class WooPosSyncProductsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = maxPages) // THEN - assertThat(result).isInstanceOf(WooPosSyncProductsResult.Success::class.java) - assertThat((result as WooPosSyncProductsResult.Success).productsSynced).isEqualTo(0) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) + assertThat((result as WooPosSyncResult.Success).syncedCount).isEqualTo(0) } @Test @@ -100,7 +99,7 @@ class WooPosSyncProductsActionTest { // THEN verify(posLocalCatalogStore).executeInTransaction(any()) - assertThat(result).isInstanceOf(WooPosSyncProductsResult.Success::class.java) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) } @Test @@ -117,8 +116,8 @@ class WooPosSyncProductsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = maxPages) // THEN - assertThat(result).isInstanceOf(WooPosSyncProductsResult.Success::class.java) - assertThat((result as WooPosSyncProductsResult.Success).productsSynced).isEqualTo(300) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) + assertThat((result as WooPosSyncResult.Success).syncedCount).isEqualTo(300) } @Test @@ -131,7 +130,7 @@ class WooPosSyncProductsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = maxPages) // THEN - assertThat(result).isInstanceOf(WooPosSyncProductsResult.Failed.CatalogTooLarge::class.java) + assertThat(result).isInstanceOf(WooPosSyncResult.Failed.CatalogTooLarge::class.java) } @Test @@ -144,8 +143,8 @@ class WooPosSyncProductsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = 10) // THEN - assertThat(result).isInstanceOf(WooPosSyncProductsResult.Failed.UnexpectedError::class.java) - assertThat((result as WooPosSyncProductsResult.Failed).error).contains(errorMessage) + assertThat(result).isInstanceOf(WooPosSyncResult.Failed.UnexpectedError::class.java) + assertThat((result as WooPosSyncResult.Failed).error).contains(errorMessage) } @Test @@ -159,8 +158,8 @@ class WooPosSyncProductsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = maxPages) // THEN - assertThat(result).isInstanceOf(WooPosSyncProductsResult.Failed.UnexpectedError::class.java) - assertThat((result as WooPosSyncProductsResult.Failed).error).contains(errorMessage) + assertThat(result).isInstanceOf(WooPosSyncResult.Failed.UnexpectedError::class.java) + assertThat((result as WooPosSyncResult.Failed).error).contains(errorMessage) } @Test @@ -173,7 +172,7 @@ class WooPosSyncProductsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = maxPages) // THEN - assertThat(result).isInstanceOf(WooPosSyncProductsResult.Failed.UnexpectedError::class.java) + assertThat(result).isInstanceOf(WooPosSyncResult.Failed.UnexpectedError::class.java) } @Test @@ -186,7 +185,7 @@ class WooPosSyncProductsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = maxPages) // THEN - assertThat(result).isInstanceOf(WooPosSyncProductsResult.Success::class.java) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) verify(posLocalCatalogStore, times(1)) .executeInTransaction(any()) } @@ -201,7 +200,7 @@ class WooPosSyncProductsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = maxPages) // THEN - assertThat(result).isInstanceOf(WooPosSyncProductsResult.Success::class.java) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) verify(posLocalCatalogStore, times(1)) .executeInTransaction(any()) } @@ -219,8 +218,8 @@ class WooPosSyncProductsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = 10) // THEN - assertThat(result).isInstanceOf(WooPosSyncProductsResult.Success::class.java) - assertThat((result as WooPosSyncProductsResult.Success).productsSynced).isEqualTo(250) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) + assertThat((result as WooPosSyncResult.Success).syncedCount).isEqualTo(250) verify(posLocalCatalogStore, times(1)) .executeInTransaction(any()) @@ -235,7 +234,7 @@ class WooPosSyncProductsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = 10) // THEN - assertThat(result).isInstanceOf(WooPosSyncProductsResult.Success::class.java) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) verify(posLocalCatalogStore).deleteAllProducts(eq(site.localId())) verify(posLocalCatalogStore).upsertProducts(any()) } @@ -250,7 +249,7 @@ class WooPosSyncProductsActionTest { val result = sut.execute(site, modifiedAfterGmt = modifiedAfter, pageSize = 100, maxPages = 10) // THEN - assertThat(result).isInstanceOf(WooPosSyncProductsResult.Success::class.java) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) verify(posLocalCatalogStore, times(0)).deleteAllProducts(any()) verify(posLocalCatalogStore).upsertProducts(any()) } @@ -269,8 +268,8 @@ class WooPosSyncProductsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = 10) // THEN - assertThat(result).isInstanceOf(WooPosSyncProductsResult.Failed.UnexpectedError::class.java) - assertThat((result as WooPosSyncProductsResult.Failed).error).contains(errorMessage) + assertThat(result).isInstanceOf(WooPosSyncResult.Failed.UnexpectedError::class.java) + assertThat((result as WooPosSyncResult.Failed).error).contains(errorMessage) } @Test @@ -344,8 +343,8 @@ class WooPosSyncProductsActionTest { ) ).thenReturn( KotlinResult.success( - WooPosLocalCatalogFetchProductsResult( - products = trashProducts, + WooPosPaginatedFetchResult( + items = trashProducts, syncedCount = productsCount, hasMore = false, nextPage = 1, @@ -372,8 +371,8 @@ class WooPosSyncProductsActionTest { ) ).thenReturn( KotlinResult.success( - WooPosLocalCatalogFetchProductsResult( - products = trashPage1, + WooPosPaginatedFetchResult( + items = trashPage1, syncedCount = 10, hasMore = true, nextPage = 2, @@ -393,8 +392,8 @@ class WooPosSyncProductsActionTest { ) ).thenReturn( KotlinResult.success( - WooPosLocalCatalogFetchProductsResult( - products = trashPage2, + WooPosPaginatedFetchResult( + items = trashPage2, syncedCount = 10, hasMore = true, nextPage = 3, @@ -414,8 +413,8 @@ class WooPosSyncProductsActionTest { ) ).thenReturn( KotlinResult.success( - WooPosLocalCatalogFetchProductsResult( - products = trashPage3, + WooPosPaginatedFetchResult( + items = trashPage3, syncedCount = 5, hasMore = false, nextPage = 3, @@ -557,8 +556,8 @@ class WooPosSyncProductsActionTest { ) .thenReturn( KotlinResult.success( - WooPosLocalCatalogFetchProductsResult( - products = mockProducts, + WooPosPaginatedFetchResult( + items = mockProducts, syncedCount = syncedCount, hasMore = hasMore, nextPage = if (hasMore) page + 1 else page, @@ -578,8 +577,8 @@ class WooPosSyncProductsActionTest { ) ).thenReturn( KotlinResult.success( - WooPosLocalCatalogFetchProductsResult( - products = mockProducts, + WooPosPaginatedFetchResult( + items = mockProducts, syncedCount = syncedCount, hasMore = hasMore, nextPage = if (hasMore) page + 1 else page, diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosSyncVariationsActionTest.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosSyncVariationsActionTest.kt index 446c359f8cc5..55407ba4232f 100644 --- a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosSyncVariationsActionTest.kt +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/localcatalog/WooPosSyncVariationsActionTest.kt @@ -2,7 +2,6 @@ package com.woocommerce.android.ui.woopos.localcatalog import com.woocommerce.android.ui.woopos.common.util.WooPosLogWrapper import com.woocommerce.android.ui.woopos.localcatalog.WooPosLocalCatalogSyncRepository.Companion.PAGE_SIZE -import com.woocommerce.android.ui.woopos.localcatalog.WooPosSyncVariationsAction.WooPosSyncVariationsResult import com.woocommerce.android.util.InlineClassesAnswer import kotlinx.coroutines.runBlocking import kotlinx.coroutines.test.runTest @@ -22,7 +21,7 @@ import org.wordpress.android.fluxc.model.LocalOrRemoteId.RemoteId import org.wordpress.android.fluxc.model.SiteModel import org.wordpress.android.fluxc.persistence.entity.pos.WooPosVariationEntity import org.wordpress.android.fluxc.store.pos.localcatalog.WooPosLocalCatalogStore -import org.wordpress.android.fluxc.store.pos.localcatalog.WooPosVariationsFetchResult +import org.wordpress.android.fluxc.store.pos.localcatalog.WooPosPaginatedFetchResult import kotlin.Result as KotlinResult class WooPosSyncVariationsActionTest { @@ -51,8 +50,8 @@ class WooPosSyncVariationsActionTest { val result = sut.execute(site, pageSize = 100, maxPages = 2) // THEN - assertThat(result).isInstanceOf(WooPosSyncVariationsResult.Success::class.java) - assertThat((result as WooPosSyncVariationsResult.Success).variationsSynced).isEqualTo(50) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) + assertThat((result as WooPosSyncResult.Success).syncedCount).isEqualTo(50) } @Test @@ -65,8 +64,8 @@ class WooPosSyncVariationsActionTest { val result = sut.execute(site, pageSize = 100, maxPages = 2) // THEN - assertThat(result).isInstanceOf(WooPosSyncVariationsResult.Success::class.java) - assertThat((result as WooPosSyncVariationsResult.Success).serverDate).isEqualTo(expectedServerDate) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) + assertThat((result as WooPosSyncResult.Success).serverDate).isEqualTo(expectedServerDate) } @Test @@ -84,8 +83,8 @@ class WooPosSyncVariationsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = maxPages) // THEN - assertThat(result).isInstanceOf(WooPosSyncVariationsResult.Success::class.java) - assertThat((result as WooPosSyncVariationsResult.Success).variationsSynced).isEqualTo(250) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) + assertThat((result as WooPosSyncResult.Success).syncedCount).isEqualTo(250) } @Test @@ -98,8 +97,8 @@ class WooPosSyncVariationsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = maxPages) // THEN - assertThat(result).isInstanceOf(WooPosSyncVariationsResult.Success::class.java) - assertThat((result as WooPosSyncVariationsResult.Success).variationsSynced).isEqualTo(0) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) + assertThat((result as WooPosSyncResult.Success).syncedCount).isEqualTo(0) } @Test @@ -120,7 +119,7 @@ class WooPosSyncVariationsActionTest { page = eq(1), pageSize = eq(100) ) - assertThat(result).isInstanceOf(WooPosSyncVariationsResult.Success::class.java) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) } @Test @@ -139,7 +138,7 @@ class WooPosSyncVariationsActionTest { page = eq(1), pageSize = eq(100) ) - assertThat(result).isInstanceOf(WooPosSyncVariationsResult.Success::class.java) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) } @Test @@ -156,8 +155,8 @@ class WooPosSyncVariationsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = maxPages) // THEN - assertThat(result).isInstanceOf(WooPosSyncVariationsResult.Success::class.java) - assertThat((result as WooPosSyncVariationsResult.Success).variationsSynced).isEqualTo(300) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) + assertThat((result as WooPosSyncResult.Success).syncedCount).isEqualTo(300) } @Test @@ -176,8 +175,8 @@ class WooPosSyncVariationsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = maxPages) // THEN - assertThat(result).isInstanceOf(WooPosSyncVariationsResult.Failed.CatalogTooLarge::class.java) - val failureResult = result as WooPosSyncVariationsResult.Failed.CatalogTooLarge + assertThat(result).isInstanceOf(WooPosSyncResult.Failed.CatalogTooLarge::class.java) + val failureResult = result as WooPosSyncResult.Failed.CatalogTooLarge assertThat(failureResult.totalPages).isEqualTo(3) assertThat(failureResult.maxPages).isEqualTo(maxPages) } @@ -192,8 +191,8 @@ class WooPosSyncVariationsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = 10) // THEN - assertThat(result).isInstanceOf(WooPosSyncVariationsResult.Failed.UnexpectedError::class.java) - assertThat((result as WooPosSyncVariationsResult.Failed).error).isEqualTo(errorMessage) + assertThat(result).isInstanceOf(WooPosSyncResult.Failed.UnexpectedError::class.java) + assertThat((result as WooPosSyncResult.Failed).error).isEqualTo(errorMessage) } @Test @@ -207,8 +206,8 @@ class WooPosSyncVariationsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = maxPages) // THEN - assertThat(result).isInstanceOf(WooPosSyncVariationsResult.Failed.UnexpectedError::class.java) - assertThat((result as WooPosSyncVariationsResult.Failed).error).isEqualTo(errorMessage) + assertThat(result).isInstanceOf(WooPosSyncResult.Failed.UnexpectedError::class.java) + assertThat((result as WooPosSyncResult.Failed).error).isEqualTo(errorMessage) } @Test @@ -221,8 +220,8 @@ class WooPosSyncVariationsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = maxPages) // THEN - assertThat(result).isInstanceOf(WooPosSyncVariationsResult.Failed.UnexpectedError::class.java) - assertThat((result as WooPosSyncVariationsResult.Failed).error).isEqualTo("Failed to sync variations") + assertThat(result).isInstanceOf(WooPosSyncResult.Failed.UnexpectedError::class.java) + assertThat((result as WooPosSyncResult.Failed).error).isEqualTo("Failed to sync variations") } @Test @@ -236,7 +235,7 @@ class WooPosSyncVariationsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = maxPages) // THEN - assertThat(result).isInstanceOf(WooPosSyncVariationsResult.Success::class.java) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) verify(posLocalCatalogStore, times(1)) .fetchRecentlyModifiedVariations(any(), anyOrNull(), any(), any()) } @@ -251,7 +250,7 @@ class WooPosSyncVariationsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = maxPages) // THEN - assertThat(result).isInstanceOf(WooPosSyncVariationsResult.Success::class.java) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) verify(posLocalCatalogStore, times(1)) .fetchRecentlyModifiedVariations(any(), anyOrNull(), any(), any()) } @@ -272,8 +271,8 @@ class WooPosSyncVariationsActionTest { val result = sut.execute(site, modifiedAfterGmt = null, pageSize = 100, maxPages = maxPages) // THEN - assertThat(result).isInstanceOf(WooPosSyncVariationsResult.Success::class.java) - assertThat((result as WooPosSyncVariationsResult.Success).serverDate).isEqualTo(firstServerDate) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) + assertThat((result as WooPosSyncResult.Success).serverDate).isEqualTo(firstServerDate) } @Test @@ -289,7 +288,7 @@ class WooPosSyncVariationsActionTest { verify(posLocalCatalogStore).fetchRecentlyModifiedVariations(any(), anyOrNull(), eq(1), any()) verify(posLocalCatalogStore).fetchRecentlyModifiedVariations(any(), anyOrNull(), eq(2), any()) verify(posLocalCatalogStore).fetchRecentlyModifiedVariations(any(), anyOrNull(), eq(3), any()) - assertThat(result).isInstanceOf(WooPosSyncVariationsResult.Success::class.java) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) } @Test @@ -303,7 +302,7 @@ class WooPosSyncVariationsActionTest { // THEN verify(posLocalCatalogStore).deleteAllVariations(siteId = eq(site.localId())) - assertThat(result).isInstanceOf(WooPosSyncVariationsResult.Success::class.java) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) } @Test @@ -317,7 +316,7 @@ class WooPosSyncVariationsActionTest { // THEN verify(posLocalCatalogStore, times(0)).deleteAllVariations(any()) - assertThat(result).isInstanceOf(WooPosSyncVariationsResult.Success::class.java) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) } @Test @@ -332,7 +331,7 @@ class WooPosSyncVariationsActionTest { verify(posLocalCatalogStore).deleteAllVariations( siteId = eq(site.localId()) ) - assertThat(result).isInstanceOf(WooPosSyncVariationsResult.Success::class.java) + assertThat(result).isInstanceOf(WooPosSyncResult.Success::class.java) } // Helper functions @@ -345,8 +344,8 @@ class WooPosSyncVariationsActionTest { .doAnswer( InlineClassesAnswer { KotlinResult.success( - WooPosVariationsFetchResult( - variations = variations, + WooPosPaginatedFetchResult( + items = variations, syncedCount = variationsCount, hasMore = false, nextPage = 2, @@ -373,8 +372,8 @@ class WooPosSyncVariationsActionTest { .doAnswer( InlineClassesAnswer { KotlinResult.success( - WooPosVariationsFetchResult( - variations = variations1, + WooPosPaginatedFetchResult( + items = variations1, syncedCount = page1Count, hasMore = true, nextPage = 2, @@ -389,8 +388,8 @@ class WooPosSyncVariationsActionTest { .doAnswer( InlineClassesAnswer { KotlinResult.success( - WooPosVariationsFetchResult( - variations = variations2, + WooPosPaginatedFetchResult( + items = variations2, syncedCount = page2Count, hasMore = page3Count > 0, nextPage = 3, @@ -412,8 +411,8 @@ class WooPosSyncVariationsActionTest { .doAnswer( InlineClassesAnswer { KotlinResult.success( - WooPosVariationsFetchResult( - variations = variations3, + WooPosPaginatedFetchResult( + items = variations3, syncedCount = page3Count, hasMore = false, nextPage = 4, @@ -430,8 +429,8 @@ class WooPosSyncVariationsActionTest { .doAnswer( InlineClassesAnswer { KotlinResult.success( - WooPosVariationsFetchResult( - variations = emptyList(), + WooPosPaginatedFetchResult( + items = emptyList(), syncedCount = 0, hasMore = false, nextPage = 1, @@ -459,8 +458,8 @@ class WooPosSyncVariationsActionTest { .doAnswer( InlineClassesAnswer { KotlinResult.success( - WooPosVariationsFetchResult( - variations = variations1, + WooPosPaginatedFetchResult( + items = variations1, syncedCount = 100, hasMore = true, nextPage = 2, @@ -480,8 +479,8 @@ class WooPosSyncVariationsActionTest { .doAnswer( InlineClassesAnswer { KotlinResult.success( - WooPosVariationsFetchResult( - variations = emptyList(), + WooPosPaginatedFetchResult( + items = emptyList(), syncedCount = 0, hasMore = false, // Changed to false so it stops fetching after zero items nextPage = 1, @@ -502,8 +501,8 @@ class WooPosSyncVariationsActionTest { .doAnswer( InlineClassesAnswer { KotlinResult.success( - WooPosVariationsFetchResult( - variations = variations1, + WooPosPaginatedFetchResult( + items = variations1, syncedCount = 100, hasMore = true, nextPage = 2, @@ -518,8 +517,8 @@ class WooPosSyncVariationsActionTest { .doAnswer( InlineClassesAnswer { KotlinResult.success( - WooPosVariationsFetchResult( - variations = variations2, + WooPosPaginatedFetchResult( + items = variations2, syncedCount = 100, hasMore = true, nextPage = 3, @@ -534,8 +533,8 @@ class WooPosSyncVariationsActionTest { .doAnswer( InlineClassesAnswer { KotlinResult.success( - WooPosVariationsFetchResult( - variations = variations3, + WooPosPaginatedFetchResult( + items = variations3, syncedCount = 50, hasMore = false, nextPage = 4, diff --git a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/store/pos/localcatalog/WooPosLocalCatalogFetchProductsResult.kt b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/store/pos/localcatalog/WooPosLocalCatalogFetchProductsResult.kt deleted file mode 100644 index b886d82843d3..000000000000 --- a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/store/pos/localcatalog/WooPosLocalCatalogFetchProductsResult.kt +++ /dev/null @@ -1,12 +0,0 @@ -package org.wordpress.android.fluxc.store.pos.localcatalog - -import org.wordpress.android.fluxc.persistence.entity.pos.WooPosProductEntity - -data class WooPosLocalCatalogFetchProductsResult( - val products: List, - val syncedCount: Int, - val hasMore: Boolean, - val nextPage: Int, - val totalPages: Int, - val serverDate: String, -) diff --git a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/store/pos/localcatalog/WooPosLocalCatalogStore.kt b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/store/pos/localcatalog/WooPosLocalCatalogStore.kt index 29fa08f603c5..e187a43bb138 100644 --- a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/store/pos/localcatalog/WooPosLocalCatalogStore.kt +++ b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/store/pos/localcatalog/WooPosLocalCatalogStore.kt @@ -163,7 +163,7 @@ class WooPosLocalCatalogStore @Inject constructor( page: Int = 1, pageSize: Int = DEFAULT_PAGE_SIZE, includeStatus: List? = null, - ): Result = + ): Result> = coroutineEngine.withDefaultContext(API, this, "fetchRecentlyModifiedProducts") { val validPageSize = pageSize.coerceIn(1, MAX_PAGE_SIZE) @@ -185,8 +185,8 @@ class WooPosLocalCatalogStore @Inject constructor( ) response.model.isNullOrEmpty() -> Result.success( - WooPosLocalCatalogFetchProductsResult( - products = emptyList(), + WooPosPaginatedFetchResult( + items = emptyList(), syncedCount = 0, hasMore = false, nextPage = page, @@ -211,8 +211,8 @@ class WooPosLocalCatalogStore @Inject constructor( } Result.success( - WooPosLocalCatalogFetchProductsResult( - products = products, + WooPosPaginatedFetchResult( + items = products, syncedCount = products.size, hasMore = hasMore, nextPage = if (hasMore) page + 1 else page, @@ -319,7 +319,7 @@ class WooPosLocalCatalogStore @Inject constructor( * @param modifiedAfterGmt ISO 8601 formatted date string (GMT) * @param page Starting page for pagination (1-based) * @param pageSize Number of variations to fetch per page (default: 100, max: 100) - * @return [Result] containing [WooPosVariationsFetchResult] with pagination info or error + * @return [Result] containing [WooPosPaginatedFetchResult] with pagination info or error */ @Suppress("LongMethod") suspend fun fetchRecentlyModifiedVariations( @@ -327,7 +327,7 @@ class WooPosLocalCatalogStore @Inject constructor( modifiedAfterGmt: String?, page: Int = 1, pageSize: Int = DEFAULT_PAGE_SIZE, - ): Result = + ): Result> = coroutineEngine.withDefaultContext(API, this, "fetchRecentlyModifiedVariations") { require(page > 0) { "Page number must be 1 or greater" } val validPageSize = pageSize.coerceIn(1, MAX_PAGE_SIZE) @@ -354,8 +354,8 @@ class WooPosLocalCatalogStore @Inject constructor( response.model.isNullOrEmpty() -> { Result.success( - WooPosVariationsFetchResult( - variations = emptyList(), + WooPosPaginatedFetchResult( + items = emptyList(), syncedCount = 0, hasMore = false, nextPage = page, @@ -385,8 +385,8 @@ class WooPosLocalCatalogStore @Inject constructor( } Result.success( - WooPosVariationsFetchResult( - variations = variations, + WooPosPaginatedFetchResult( + items = variations, syncedCount = variations.size, hasMore = hasMore, nextPage = if (hasMore) { diff --git a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/store/pos/localcatalog/WooPosVariationsFetchResult.kt b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/store/pos/localcatalog/WooPosPaginatedFetchResult.kt similarity index 52% rename from libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/store/pos/localcatalog/WooPosVariationsFetchResult.kt rename to libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/store/pos/localcatalog/WooPosPaginatedFetchResult.kt index 22673df11cfa..3eee2a65cb9c 100644 --- a/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/store/pos/localcatalog/WooPosVariationsFetchResult.kt +++ b/libs/fluxc-plugin/src/main/kotlin/org/wordpress/android/fluxc/store/pos/localcatalog/WooPosPaginatedFetchResult.kt @@ -1,9 +1,7 @@ package org.wordpress.android.fluxc.store.pos.localcatalog -import org.wordpress.android.fluxc.persistence.entity.pos.WooPosVariationEntity - -data class WooPosVariationsFetchResult( - val variations: List, +data class WooPosPaginatedFetchResult( + val items: List, val syncedCount: Int, val hasMore: Boolean, val nextPage: Int, diff --git a/libs/fluxc-plugin/src/test/java/org/wordpress/android/fluxc/store/pos/WooPosLocalCatalogStoreTest.kt b/libs/fluxc-plugin/src/test/java/org/wordpress/android/fluxc/store/pos/WooPosLocalCatalogStoreTest.kt index 5a8c3b6aae1c..7c1b92d74461 100644 --- a/libs/fluxc-plugin/src/test/java/org/wordpress/android/fluxc/store/pos/WooPosLocalCatalogStoreTest.kt +++ b/libs/fluxc-plugin/src/test/java/org/wordpress/android/fluxc/store/pos/WooPosLocalCatalogStoreTest.kt @@ -152,7 +152,7 @@ class WooPosLocalCatalogStoreTest { val syncResult = store.fetchRecentlyModifiedProducts(testSite, validDateString, 1, 100).getOrThrow() // THEN - assertThat(syncResult.products.size).isEqualTo(remoteProducts.size) + assertThat(syncResult.items.size).isEqualTo(remoteProducts.size) assertThat(syncResult.syncedCount).isEqualTo(2) assertThat(syncResult.hasMore).isFalse() assertThat(syncResult.nextPage).isEqualTo(1)