Skip to content

Commit

Permalink
Add store functionality to batch update orders status.
Browse files Browse the repository at this point in the history
  • Loading branch information
hafizrahman committed Dec 13, 2024
1 parent fe0b98e commit 37c74d0
Showing 1 changed file with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooError
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooErrorType.API_ERROR
import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooResult
import org.wordpress.android.fluxc.network.rest.wpcom.wc.order.BatchOrderApiResponse
import org.wordpress.android.fluxc.network.rest.wpcom.wc.order.BatchOrderApiResponse.ErrorResponse
import org.wordpress.android.fluxc.network.rest.wpcom.wc.order.OrderRestClient
import org.wordpress.android.fluxc.network.rest.wpcom.wc.order.OrderRestClient.OrderBy
import org.wordpress.android.fluxc.network.rest.wpcom.wc.order.OrderRestClient.SortOrder
Expand All @@ -44,6 +45,7 @@ import org.wordpress.android.fluxc.store.WCOrderStore.OrderErrorType.PARSE_ERROR
import org.wordpress.android.fluxc.store.WCOrderStore.OrderErrorType.TIMEOUT_ERROR
import org.wordpress.android.fluxc.store.WCOrderStore.UpdateOrderResult.OptimisticUpdateResult
import org.wordpress.android.fluxc.store.WCOrderStore.UpdateOrderResult.RemoteUpdateResult
import org.wordpress.android.fluxc.store.WCOrderStore.UpdateOrdersStatusResult.FailedOrder
import org.wordpress.android.fluxc.tools.CoroutineEngine
import org.wordpress.android.util.AppLog
import org.wordpress.android.util.AppLog.T.API
Expand Down Expand Up @@ -306,6 +308,18 @@ class WCOrderStore @Inject constructor(
}
}

data class UpdateOrdersStatusResult(
val updatedOrders: List<Long> = emptyList(),
val failedOrders: List<FailedOrder> = emptyList()
) {
data class FailedOrder(
val id: Long,
val errorCode: String,
val errorMessage: String,
val errorStatus: Int
)
}

data class OrderError(val type: OrderErrorType = GENERIC_ERROR, val message: String = "") : OnChangedError

enum class OrderErrorType {
Expand Down Expand Up @@ -1156,4 +1170,41 @@ class WCOrderStore @Inject constructor(
WooResult(orders)
}
}

suspend fun batchUpdateOrdersStatus(
site: SiteModel,
orderIds: List<Long>,
newStatus: String
): WooResult<UpdateOrdersStatusResult> {
val result = wcOrderRestClient.batchUpdateOrdersStatus(site, orderIds, newStatus)

return if (!result.isError) {
val orders = result.response
val updatedOrders = mutableListOf<Long>()
val failedOrders = mutableListOf<FailedOrder>()

orders.forEach { response ->
when (response) {
is BatchOrderApiResponse.OrderResponse.Success -> {
response.order.id?.let { updatedOrders.add(it) }
}

is BatchOrderApiResponse.OrderResponse.Error -> {
failedOrders.add(
FailedOrder(
id = response.id,
errorCode = response.error.code,
errorMessage = response.error.message,
errorStatus = response.error.data.status
)
)
}
}
}

WooResult(UpdateOrdersStatusResult(updatedOrders, failedOrders))
} else {
WooResult(WooError(API_ERROR, SERVER_ERROR, result.error.message))
}
}
}

0 comments on commit 37c74d0

Please sign in to comment.