Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.wordpress.android.fluxc.store.WCProductStore.RemoteUpdateProductImagesPayload;
import org.wordpress.android.fluxc.store.WCProductStore.RemoteUpdateProductPayload;
import org.wordpress.android.fluxc.store.WCProductStore.RemoteUpdatedProductPasswordPayload;
import org.wordpress.android.fluxc.store.WCProductStore.SearchProductsByGlobalUniqueIdPayload;
import org.wordpress.android.fluxc.store.WCProductStore.SearchProductsPayload;
import org.wordpress.android.fluxc.store.WCProductStore.UpdateProductImagesPayload;
import org.wordpress.android.fluxc.store.WCProductStore.UpdateProductPasswordPayload;
Expand All @@ -40,6 +41,8 @@ public enum WCProductAction implements IAction {
FETCH_PRODUCTS,
@Action(payloadType = SearchProductsPayload.class)
SEARCH_PRODUCTS,
@Action(payloadType = SearchProductsByGlobalUniqueIdPayload.class)
SEARCH_PRODUCTS_BY_GLOBAL_UNIQUE_ID,
@Action(payloadType = FetchProductShippingClassListPayload.class)
FETCH_PRODUCT_SHIPPING_CLASS_LIST,
@Action(payloadType = FetchSingleProductShippingClassPayload.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ class ProductRestClient @Inject constructor(
sortType: ProductSorting = DEFAULT_PRODUCT_SORTING,
searchQuery: String? = null,
skuSearchOptions: SkuSearchOptions = SkuSearchOptions.Disabled,
globalUniqueIdSearchQuery: String? = null,
includedProductIds: List<Long>? = null,
filterOptions: Map<ProductFilterOption, String>? = null,
excludedProductIds: List<Long>? = null
Expand All @@ -432,6 +433,7 @@ class ProductRestClient @Inject constructor(
offset = offset,
searchQuery = searchQuery,
skuSearchOptions = skuSearchOptions,
globalUniqueIdSearchQuery = globalUniqueIdSearchQuery,
includedProductIds = includedProductIds,
excludedProductIds = excludedProductIds,
filterOptions = filterOptions
Expand All @@ -452,7 +454,7 @@ class ProductRestClient @Inject constructor(

val loadedMore = offset > 0
val canLoadMore = productModels.size == pageSize
if (searchQuery == null) {
if (searchQuery == null && globalUniqueIdSearchQuery == null) {
val payload = RemoteProductListPayload(
site,
productModels,
Expand Down Expand Up @@ -497,6 +499,26 @@ class ProductRestClient @Inject constructor(
}
}

fun searchProductsByGlobalUniqueId(
site: SiteModel,
globalUniqueIdSearchQuery: String? = null,
pageSize: Int = DEFAULT_PRODUCT_PAGE_SIZE,
offset: Int = 0,
sorting: ProductSorting = DEFAULT_PRODUCT_SORTING,
excludedProductIds: List<Long>? = null,
filterOptions: Map<ProductFilterOption, String>? = null
) {
fetchProducts(
site = site,
pageSize = pageSize,
offset = offset,
sortType = sorting,
globalUniqueIdSearchQuery = globalUniqueIdSearchQuery,
excludedProductIds = excludedProductIds,
filterOptions = filterOptions
)
}

fun searchProducts(
site: SiteModel,
searchQuery: String,
Expand Down Expand Up @@ -583,6 +605,7 @@ class ProductRestClient @Inject constructor(
offset: Int,
searchQuery: String?,
skuSearchOptions: SkuSearchOptions,
globalUniqueIdSearchQuery: String? = null,
includedProductIds: List<Long>? = null,
excludedProductIds: List<Long>? = null,
filterOptions: Map<ProductFilterOption, String>? = null
Expand Down Expand Up @@ -631,9 +654,20 @@ class ProductRestClient @Inject constructor(
}
}

addGlobalUniqueIdSearchQuery(params, globalUniqueIdSearchQuery)

return params
}

private fun addGlobalUniqueIdSearchQuery(
params: MutableMap<String, String>,
globalUniqueIdSearchQuery: String?
) {
if (!globalUniqueIdSearchQuery.isNullOrEmpty()) {
params["global_unique_id"] = globalUniqueIdSearchQuery
}
}

/**
* Makes a GET request to `/wp-json/wc/v3/products/categories` retrieving a list of product
* categories for the given WooCommerce [SiteModel].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ class WCProductStore @Inject constructor(
var filterOptions: Map<ProductFilterOption, String>? = null,
) : Payload<BaseNetworkError>()

class SearchProductsByGlobalUniqueIdPayload(
var site: SiteModel,
var globalUniqueId: String,
var pageSize: Int = DEFAULT_PRODUCT_PAGE_SIZE,
var offset: Int = 0,
var sorting: ProductSorting = DEFAULT_PRODUCT_SORTING,
var excludedProductIds: List<Long>? = null,
var filterOptions: Map<ProductFilterOption, String>? = null,
) : Payload<BaseNetworkError>()

class FetchProductVariationsPayload(
var site: SiteModel,
var remoteProductId: Long,
Expand Down Expand Up @@ -905,6 +915,9 @@ class WCProductStore @Inject constructor(
WCProductAction.SEARCH_PRODUCTS ->
searchProducts(action.payload as SearchProductsPayload)

WCProductAction.SEARCH_PRODUCTS_BY_GLOBAL_UNIQUE_ID ->
searchProductsByGlobalUniqueId(action.payload as SearchProductsByGlobalUniqueIdPayload)

WCProductAction.UPDATE_PRODUCT_IMAGES ->
updateProductImages(action.payload as UpdateProductImagesPayload)

Expand Down Expand Up @@ -1211,6 +1224,20 @@ class WCProductStore @Inject constructor(
}
}

private fun searchProductsByGlobalUniqueId(payload: SearchProductsByGlobalUniqueIdPayload) {
with(payload) {
wcProductRestClient.searchProductsByGlobalUniqueId(
site = site,
globalUniqueIdSearchQuery = globalUniqueId,
pageSize = pageSize,
offset = offset,
sorting = sorting,
excludedProductIds = excludedProductIds,
filterOptions = filterOptions
)
}
}

suspend fun fetchProductVariations(payload: FetchProductVariationsPayload): OnProductChanged {
return coroutineEngine.withDefaultContext(API, this, "fetchProductVariations") {
val result = with(payload) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,33 @@ class ProductRestClientTest {
}
}

@Test
fun `when fetch products called with the global unique id, then correct params is used for network call`() {
runBlockingTest {
val globalUniqueIdSearchQuery = "test global unique id"
sut.fetchProducts(
site = site,
globalUniqueIdSearchQuery = globalUniqueIdSearchQuery
)
val argumentCaptor = argumentCaptor<MutableMap<String, String>>()
verify(wooNetwork).executeGetGsonRequest(
any(),
any(),
clazz = eq(Array<ProductApiResponse>::class.java),
params = argumentCaptor.capture(),
enableCaching = any(),
cacheTimeToLive = any(),
forced = any(),
requestTimeout = any(),
retries = any()
)

assertThat(argumentCaptor.firstValue.getOrDefault("global_unique_id", null)).isEqualTo(
globalUniqueIdSearchQuery
)
}
}

private fun WCProductModel.withRegularPrice(newRegularPrice: String): WCProductModel =
copy().apply {
remoteProductId = [email protected]
Expand Down
Loading