Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit bd10033

Browse files
authored
Merge pull request #1384 from wordpress-mobile/wc/product-review-tweaks
Woo: Simplify process to update Product Review status
2 parents ed99202 + d1a0ef5 commit bd10033

File tree

5 files changed

+79
-26
lines changed

5 files changed

+79
-26
lines changed

example/src/androidTest/java/org/wordpress/android/fluxc/mocked/MockedStack_WCProductsTest.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import org.wordpress.android.fluxc.TestUtils
1212
import org.wordpress.android.fluxc.action.WCProductAction
1313
import org.wordpress.android.fluxc.annotations.action.Action
1414
import org.wordpress.android.fluxc.model.SiteModel
15-
import org.wordpress.android.fluxc.model.WCProductReviewModel
1615
import org.wordpress.android.fluxc.module.ResponseMockingInterceptor
1716
import org.wordpress.android.fluxc.network.rest.wpcom.wc.product.ProductRestClient
1817
import org.wordpress.android.fluxc.persistence.ProductSqlUtils
@@ -339,7 +338,7 @@ class MockedStack_WCProductsTest : MockedStack_Base() {
339338
@Test
340339
fun testUpdateProductReviewStatusSuccess() {
341340
interceptor.respondWith("wc-update-product-review-response-success.json")
342-
productRestClient.updateProductReviewStatus(siteModel, WCProductReviewModel(), "spam")
341+
productRestClient.updateProductReviewStatus(siteModel, 0, "spam")
343342

344343
countDownLatch = CountDownLatch(1)
345344
assertTrue(countDownLatch.await(TestUtils.DEFAULT_TIMEOUT_MS.toLong(), TimeUnit.MILLISECONDS))
@@ -369,7 +368,7 @@ class MockedStack_WCProductsTest : MockedStack_Base() {
369368
@Test
370369
fun testUpdateProductReviewStatusFailed() {
371370
interceptor.respondWithError("wc-response-failure-invalid-param.json")
372-
productRestClient.updateProductReviewStatus(siteModel, WCProductReviewModel(), "spam")
371+
productRestClient.updateProductReviewStatus(siteModel, 0, "spam")
373372

374373
countDownLatch = CountDownLatch(1)
375374
assertTrue(countDownLatch.await(TestUtils.DEFAULT_TIMEOUT_MS.toLong(), TimeUnit.MILLISECONDS))

example/src/androidTest/java/org/wordpress/android/fluxc/release/ReleaseStack_WCProductTest.kt

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.greenrobot.eventbus.ThreadMode
55
import org.junit.Assert.assertEquals
66
import org.junit.Assert.assertNotEquals
77
import org.junit.Assert.assertNotNull
8+
import org.junit.Assert.assertNull
89
import org.junit.Assert.assertTrue
910
import org.junit.Test
1011
import org.wordpress.android.fluxc.TestUtils
@@ -216,17 +217,63 @@ class ReleaseStack_WCProductTest : ReleaseStack_WCBase() {
216217
.getProductReviewByRemoteId(sSite.id, remoteProductReviewId)
217218
assertNotNull(review)
218219

219-
// Update review status
220+
// Update review status to spam - should get deleted from db
220221
review?.let {
221-
val newStatus = when (it.status) {
222-
"hold", "approved", "unapproved", "trash", "unspam", "untrash" -> "spam"
223-
else -> "approved"
224-
}
222+
val newStatus = "spam"
223+
nextEvent = TestEvent.UPDATED_PRODUCT_REVIEW_STATUS
224+
mCountDownLatch = CountDownLatch(1)
225+
mDispatcher.dispatch(
226+
WCProductActionBuilder.newUpdateProductReviewStatusAction(
227+
UpdateProductReviewStatusPayload(sSite, review.remoteProductReviewId, newStatus)))
228+
assertTrue(mCountDownLatch.await(TestUtils.DEFAULT_TIMEOUT_MS.toLong(), MILLISECONDS))
229+
230+
// Verify results - review should be deleted from db
231+
val savedReview = productStore
232+
.getProductReviewByRemoteId(sSite.id, remoteProductReviewId)
233+
assertNull(savedReview)
234+
}
235+
236+
// Update review status to approved - should get added to db
237+
review?.let {
238+
val newStatus = "approved"
239+
nextEvent = TestEvent.UPDATED_PRODUCT_REVIEW_STATUS
240+
mCountDownLatch = CountDownLatch(1)
241+
mDispatcher.dispatch(
242+
WCProductActionBuilder.newUpdateProductReviewStatusAction(
243+
UpdateProductReviewStatusPayload(sSite, review.remoteProductReviewId, newStatus)))
244+
assertTrue(mCountDownLatch.await(TestUtils.DEFAULT_TIMEOUT_MS.toLong(), MILLISECONDS))
245+
246+
// Verify results
247+
val savedReview = productStore
248+
.getProductReviewByRemoteId(sSite.id, remoteProductReviewId)
249+
assertNotNull(savedReview)
250+
assertEquals(newStatus, savedReview!!.status)
251+
}
252+
253+
// Update review status to trash - should get deleted from db
254+
review?.let {
255+
val newStatus = "trash"
256+
nextEvent = TestEvent.UPDATED_PRODUCT_REVIEW_STATUS
257+
mCountDownLatch = CountDownLatch(1)
258+
mDispatcher.dispatch(
259+
WCProductActionBuilder.newUpdateProductReviewStatusAction(
260+
UpdateProductReviewStatusPayload(sSite, review.remoteProductReviewId, newStatus)))
261+
assertTrue(mCountDownLatch.await(TestUtils.DEFAULT_TIMEOUT_MS.toLong(), MILLISECONDS))
262+
263+
// Verify results - review should be deleted from db
264+
val savedReview = productStore
265+
.getProductReviewByRemoteId(sSite.id, remoteProductReviewId)
266+
assertNull(savedReview)
267+
}
268+
269+
// Update review status to hold - should get added to db
270+
review?.let {
271+
val newStatus = "hold"
225272
nextEvent = TestEvent.UPDATED_PRODUCT_REVIEW_STATUS
226273
mCountDownLatch = CountDownLatch(1)
227274
mDispatcher.dispatch(
228275
WCProductActionBuilder.newUpdateProductReviewStatusAction(
229-
UpdateProductReviewStatusPayload(sSite, review, newStatus)))
276+
UpdateProductReviewStatusPayload(sSite, review.remoteProductReviewId, newStatus)))
230277
assertTrue(mCountDownLatch.await(TestUtils.DEFAULT_TIMEOUT_MS.toLong(), MILLISECONDS))
231278

232279
// Verify results

plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/product/ProductRestClient.kt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,7 @@ class ProductRestClient(
286286
},
287287
WPComErrorListener { networkError ->
288288
val productReviewError = networkErrorToProductError(networkError)
289-
val payload = RemoteProductReviewPayload(
290-
error = productReviewError,
291-
site = site,
292-
productReview = WCProductReviewModel()
293-
.apply { remoteProductReviewId = remoteReviewId })
289+
val payload = RemoteProductReviewPayload(error = productReviewError, site = site)
294290
dispatcher.dispatch(WCProductActionBuilder.newFetchedSingleProductReviewAction(payload))
295291
},
296292
{ request: WPComGsonRequest<*> -> add(request) })
@@ -299,16 +295,16 @@ class ProductRestClient(
299295

300296
/**
301297
* Makes a PUT call to `/wc/v3/products/reviews/<id>` via the Jetpack tunnel (see [JetpackTunnelGsonRequest]),
302-
* updating the status for the given [productReview] to [newStatus].
298+
* updating the status for the given product review to [newStatus].
303299
*
304300
* Dispatches a [WCProductAction.UPDATED_PRODUCT_REVIEW_STATUS]
305301
*
306302
* @param [site] The site to fetch product reviews for
307-
* @param [productReview] The [WCProductReviewModel] to be updated
303+
* @param [remoteReviewId] The remote ID of the product review to be updated
308304
* @param [newStatus] The new status to update the product review to
309305
*/
310-
fun updateProductReviewStatus(site: SiteModel, productReview: WCProductReviewModel, newStatus: String) {
311-
val url = WOOCOMMERCE.products.reviews.id(productReview.remoteProductReviewId).pathV3
306+
fun updateProductReviewStatus(site: SiteModel, remoteReviewId: Long, newStatus: String) {
307+
val url = WOOCOMMERCE.products.reviews.id(remoteReviewId).pathV3
312308
val responseType = object : TypeToken<ProductReviewApiResponse>() {}.type
313309
val params = mapOf("status" to newStatus)
314310
val request = JetpackTunnelGsonRequest.buildPutRequest(url, site.siteId, params, responseType,
@@ -323,7 +319,7 @@ class ProductRestClient(
323319
},
324320
WPComErrorListener { networkError ->
325321
val productReviewError = networkErrorToProductError(networkError)
326-
val payload = RemoteProductReviewPayload(productReviewError, site, productReview)
322+
val payload = RemoteProductReviewPayload(productReviewError, site)
327323
dispatcher.dispatch(WCProductActionBuilder.newUpdatedProductReviewStatusAction(payload))
328324
})
329325
add(request)

plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/persistence/ProductSqlUtils.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@ object ProductSqlUtils {
199199
}
200200
}
201201

202+
fun deleteProductReview(productReview: WCProductReviewModel) =
203+
WellSql.delete(WCProductReviewModel::class.java)
204+
.where()
205+
.equals(WCProductReviewModelTable.REMOTE_PRODUCT_REVIEW_ID, productReview.remoteProductReviewId)
206+
.endWhere().execute()
207+
202208
fun getProductReviewByRemoteId(
203209
localSiteId: Int,
204210
remoteReviewId: Long

plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/store/WCProductStore.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class WCProductStore @Inject constructor(dispatcher: Dispatcher, private val wcP
7171

7272
class UpdateProductReviewStatusPayload(
7373
var site: SiteModel,
74-
var productReview: WCProductReviewModel,
74+
var remoteReviewId: Long,
7575
var newStatus: String
7676
) : Payload<BaseNetworkError>()
7777

@@ -154,9 +154,8 @@ class WCProductStore @Inject constructor(dispatcher: Dispatcher, private val wcP
154154
) : Payload<ProductError>() {
155155
constructor(
156156
error: ProductError,
157-
site: SiteModel,
158-
productReview: WCProductReviewModel
159-
) : this(site, productReview) {
157+
site: SiteModel
158+
) : this(site) {
160159
this.error = error
161160
}
162161
}
@@ -306,7 +305,7 @@ class WCProductStore @Inject constructor(dispatcher: Dispatcher, private val wcP
306305
}
307306

308307
private fun updateProductReviewStatus(payload: UpdateProductReviewStatusPayload) {
309-
with(payload) { wcProductRestClient.updateProductReviewStatus(site, productReview, newStatus) }
308+
with(payload) { wcProductRestClient.updateProductReviewStatus(site, remoteReviewId, newStatus) }
310309
}
311310

312311
private fun handleFetchSingleProductCompleted(payload: RemoteProductPayload) {
@@ -402,8 +401,14 @@ class WCProductStore @Inject constructor(dispatcher: Dispatcher, private val wcP
402401
if (payload.isError) {
403402
onProductReviewChanged = OnProductReviewChanged(0).also { it.error = payload.error }
404403
} else {
405-
val rowsAffected = payload.productReview?.let {
406-
ProductSqlUtils.insertOrUpdateProductReview(it)
404+
val rowsAffected = payload.productReview?.let { review ->
405+
if (review.status == "spam" || review.status == "trash") {
406+
// Delete this review from the database
407+
ProductSqlUtils.deleteProductReview(review)
408+
} else {
409+
// Insert or update in the database
410+
ProductSqlUtils.insertOrUpdateProductReview(review)
411+
}
407412
} ?: 0
408413
onProductReviewChanged = OnProductReviewChanged(rowsAffected)
409414
}

0 commit comments

Comments
 (0)