Skip to content

Commit

Permalink
Merge branch 'trunk' into 2757-parse-invalid-coupon-error
Browse files Browse the repository at this point in the history
  • Loading branch information
samiuelson committed Jun 21, 2023
2 parents 5950d6d + d0a1aa0 commit ad9bb74
Show file tree
Hide file tree
Showing 8 changed files with 326 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.wordpress.android.fluxc.model

import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import org.wordpress.android.fluxc.model.WCProductModel.AddOnsMetadataKeys
import org.wordpress.android.fluxc.model.WCProductModel.QuantityRulesMetadataKeys
import org.wordpress.android.fluxc.utils.EMPTY_JSON_ARRAY
import org.wordpress.android.fluxc.utils.isElementNullOrEmpty
import javax.inject.Inject

class StripProductMetaData @Inject internal constructor(private val gson: Gson) {
operator fun invoke(metadata: String?): String {
if (metadata.isNullOrEmpty()) return EMPTY_JSON_ARRAY

return gson.fromJson(metadata, JsonArray::class.java)
.mapNotNull { it as? JsonObject }
.asSequence()
.filter { jsonObject ->
val isNullOrEmpty = jsonObject[WCMetaData.VALUE].isElementNullOrEmpty()
jsonObject[WCMetaData.KEY]?.asString.orEmpty() in SUPPORTED_KEYS && isNullOrEmpty.not()
}.toList()
.takeIf { it.isNotEmpty() }
?.let { gson.toJson(it) } ?: EMPTY_JSON_ARRAY
}

companion object {
val SUPPORTED_KEYS: Set<String> = buildSet {
add(AddOnsMetadataKeys.ADDONS_METADATA_KEY)
addAll(QuantityRulesMetadataKeys.ALL_KEYS)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ package org.wordpress.android.fluxc.model
import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import org.wordpress.android.fluxc.utils.isElementNullOrEmpty
import javax.inject.Inject

class StripProductVariationMetaData @Inject internal constructor(private val gson: Gson) {
operator fun invoke(metadata: String?): String? {
if (metadata == null) return null
if (metadata.isNullOrEmpty()) return null

return gson.fromJson(metadata, JsonArray::class.java)
.mapNotNull { it as? JsonObject }
.asSequence()
.filter { jsonObject ->
jsonObject[WCMetaData.KEY]?.asString.orEmpty() in SUPPORTED_KEYS &&
jsonObject[WCMetaData.VALUE]?.asString.orEmpty().isNotBlank()
val isNullOrEmpty = jsonObject[WCMetaData.VALUE].isElementNullOrEmpty()
jsonObject[WCMetaData.KEY]?.asString.orEmpty() in SUPPORTED_KEYS && isNullOrEmpty.not()
}.toList()
.takeIf { it.isNotEmpty() }
?.let { gson.toJson(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.yarolegovich.wellsql.core.annotation.Column
import com.yarolegovich.wellsql.core.annotation.PrimaryKey
import com.yarolegovich.wellsql.core.annotation.Table
import org.wordpress.android.fluxc.model.LocalOrRemoteId.RemoteId
import org.wordpress.android.fluxc.model.WCProductModel.AddOnsMetadataKeys.ADDONS_METADATA_KEY
import org.wordpress.android.fluxc.model.WCProductVariationModel.ProductVariantOption
import org.wordpress.android.fluxc.model.addons.RemoteAddonDto
import org.wordpress.android.fluxc.network.utils.getBoolean
Expand All @@ -26,10 +27,6 @@ import org.wordpress.android.util.AppLog.T
*/
@Table(addOn = WellSqlConfig.ADDON_WOOCOMMERCE)
data class WCProductModel(@PrimaryKey @Column private var id: Int = 0) : Identifiable {
companion object {
private const val ADDONS_METADATA_KEY = "_product_addons"
}

@Column var localSiteId = 0
@Column var remoteProductId = 0L // The unique identifier for this product on the server
val remoteId
Expand Down Expand Up @@ -557,6 +554,10 @@ data class WCProductModel(@PrimaryKey @Column private var id: Int = 0) : Identif
return storedFiles == updatedFiles
}

object AddOnsMetadataKeys {
const val ADDONS_METADATA_KEY = "_product_addons"
}

object QuantityRulesMetadataKeys {
const val MINIMUM_ALLOWED_QUANTITY = "minimum_allowed_quantity"
const val MAXIMUM_ALLOWED_QUANTITY = "maximum_allowed_quantity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.wordpress.android.fluxc.generated.endpoint.WPAPI
import org.wordpress.android.fluxc.generated.endpoint.WPCOMREST
import org.wordpress.android.fluxc.model.LocalOrRemoteId.RemoteId
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.model.StripProductMetaData
import org.wordpress.android.fluxc.model.StripProductVariationMetaData
import org.wordpress.android.fluxc.model.WCProductCategoryModel
import org.wordpress.android.fluxc.model.WCProductImageModel
Expand Down Expand Up @@ -82,6 +83,7 @@ class ProductRestClient @Inject constructor(
private val wooNetwork: WooNetwork,
private val wpComNetwork: WPComNetwork,
private val coroutineEngine: CoroutineEngine,
private val stripProductMetaData: StripProductMetaData,
private val stripProductVariationMetaData: StripProductVariationMetaData
) {
/**
Expand Down Expand Up @@ -291,6 +293,7 @@ class ProductRestClient @Inject constructor(
response.data?.let {
val newModel = it.asProductModel().apply {
localSiteId = site.id
metadata = stripProductMetaData(metadata)
}
RemoteProductPayload(newModel, site)
} ?: RemoteProductPayload(
Expand Down Expand Up @@ -410,7 +413,10 @@ class ProductRestClient @Inject constructor(
when (response) {
is WPAPIResponse.Success -> {
val productModels = response.data?.map {
it.asProductModel().apply { localSiteId = site.id }
it.asProductModel().apply {
localSiteId = site.id
metadata = stripProductMetaData(metadata)
}
}.orEmpty()

val loadedMore = offset > 0
Expand Down Expand Up @@ -520,7 +526,10 @@ class ProductRestClient @Inject constructor(
return response.toWooPayload { products ->
products.map {
it.asProductModel()
.apply { localSiteId = site.id }
.apply {
localSiteId = site.id
metadata = stripProductMetaData(metadata)
}
}
}
}
Expand Down Expand Up @@ -876,6 +885,7 @@ class ProductRestClient @Inject constructor(
response.data?.let {
val newModel = it.asProductModel().apply {
localSiteId = site.id
metadata = stripProductMetaData(metadata)
}
val payload = RemoteUpdateProductPayload(site, newModel)
dispatcher.dispatch(WCProductActionBuilder.newUpdatedProductAction(payload))
Expand Down Expand Up @@ -1189,6 +1199,7 @@ class ProductRestClient @Inject constructor(
response.data?.let {
val newModel = it.asProductModel().apply {
localSiteId = site.id
metadata = stripProductMetaData(metadata)
}
val payload = RemoteUpdateProductImagesPayload(site, newModel)
dispatcher.dispatch(WCProductActionBuilder.newUpdatedProductImagesAction(payload))
Expand Down Expand Up @@ -1486,6 +1497,7 @@ class ProductRestClient @Inject constructor(
val newModel = product.asProductModel().apply {
id = product.id?.toInt() ?: 0
localSiteId = site.id
metadata = stripProductMetaData(metadata)
}
val payload = RemoteAddProductPayload(site, newModel)
dispatcher.dispatch(WCProductActionBuilder.newAddedProductAction(payload))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.wordpress.android.fluxc.utils

import com.google.gson.JsonElement

const val EMPTY_JSON_ARRAY = "[]"

fun JsonElement?.isElementNullOrEmpty(): Boolean {
return this?.let {
when{
this.isJsonObject -> this.asJsonObject.size() == 0
this.isJsonArray -> this.asJsonArray.size()== 0
this.isJsonPrimitive && this.asJsonPrimitive.isString -> this.asString.isEmpty()
this.isJsonNull -> true
else -> false
}
} ?: true
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ProductRestClientTest {
private val wpComNetwork: WPComNetwork = mock()

@Before fun setUp() {
sut = ProductRestClient(mock(), wooNetwork, wpComNetwork, initCoroutineEngine(), mock())
sut = ProductRestClient(mock(), wooNetwork, wpComNetwork, initCoroutineEngine(), mock(), mock())
}

@Test
Expand Down
Loading

0 comments on commit ad9bb74

Please sign in to comment.