Skip to content

Commit

Permalink
Merge pull request #12158 from woocommerce/12144-woo-pos-do-not-send-…
Browse files Browse the repository at this point in the history
…price-when-create-an-order-only-product-ids

[Woo POS] Do not send price when create an order, only product ids
  • Loading branch information
backwardstruck authored Jul 30, 2024
2 parents 9de189a + baff7e5 commit 4da69c1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ package com.woocommerce.android.ui.woopos.home.totals

import com.woocommerce.android.model.Order
import com.woocommerce.android.ui.orders.creation.OrderCreateEditRepository
import com.woocommerce.android.ui.woopos.common.data.WooPosGetProductById
import com.woocommerce.android.util.DateUtils
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.async
import kotlinx.coroutines.withContext
import java.math.BigDecimal
import java.util.Date
import javax.inject.Inject

class WooPosTotalsRepository @Inject constructor(
private val orderCreateEditRepository: OrderCreateEditRepository,
private val dateUtils: DateUtils,
private val getProductById: WooPosGetProductById,
) {
private var orderCreationJob: Deferred<Result<Order>>? = null

Expand All @@ -25,6 +22,10 @@ class WooPosTotalsRepository @Inject constructor(
orderCreationJob?.cancel()

return withContext(IO) {
productIds.forEach { productId ->
require(productId >= 0) { "Invalid product ID: $productId" }
}

orderCreationJob = async {
val order = Order.getEmptyOrder(
dateCreated = dateUtils.getCurrentDateInSiteTimeZone() ?: Date(),
Expand All @@ -35,16 +36,14 @@ class WooPosTotalsRepository @Inject constructor(
.groupingBy { it }
.eachCount()
.map { (productId, quantity) ->
val product = getProductById(productId)
Order.Item.EMPTY.copy(
itemId = 0L,
productId = productId,
variationId = 0L,
quantity = quantity.toFloat(),
total = EMPTY_TOTALS_SUBTOTAL_VALUE,
subtotal = EMPTY_TOTALS_SUBTOTAL_VALUE,
price = product?.price ?: BigDecimal.ZERO,
sku = product?.sku.orEmpty(),
price = EMPTY_TOTALS_SUBTOTAL_VALUE,
attributesList = emptyList(),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,31 @@ package com.woocommerce.android.ui.woopos.home.totals

import com.woocommerce.android.model.Order
import com.woocommerce.android.ui.orders.creation.OrderCreateEditRepository
import com.woocommerce.android.ui.woopos.common.data.WooPosGetProductById
import com.woocommerce.android.util.DateUtils
import kotlinx.coroutines.test.runTest
import org.assertj.core.api.Assertions.assertThat
import org.junit.Before
import org.junit.Test
import org.mockito.kotlin.any
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever

class WooPosTotalsRepositoryTest {
private val orderCreateEditRepository: OrderCreateEditRepository = mock()
private val dateUtils: DateUtils = mock()
private val getProductById: WooPosGetProductById = mock()

private lateinit var repository: WooPosTotalsRepository

@Before
fun setUp() {
repository = WooPosTotalsRepository(
orderCreateEditRepository,
dateUtils,
getProductById
)
}

@Test
fun `given empty product list, when createOrderWithProducts called, then return error`() = runTest {
// GIVEN
repository = WooPosTotalsRepository(
orderCreateEditRepository,
dateUtils
)
val productIds = emptyList<Long>()

// WHEN
Expand All @@ -44,6 +39,10 @@ class WooPosTotalsRepositoryTest {
@Test
fun `given product ids without duplicates, when createOrderWithProducts, then items all quantity one`() = runTest {
// GIVEN
repository = WooPosTotalsRepository(
orderCreateEditRepository,
dateUtils
)
val productIds = listOf(1L, 2L, 3L)

// WHEN
Expand All @@ -63,6 +62,10 @@ class WooPosTotalsRepositoryTest {
@Test
fun `given product ids with duplicates, when createOrderWithProducts, then items quantity is correct`() = runTest {
// GIVEN
repository = WooPosTotalsRepository(
orderCreateEditRepository,
dateUtils
)
val productIds = listOf(1L, 1L, 2L, 3L, 3L, 3L)

// WHEN
Expand All @@ -78,4 +81,25 @@ class WooPosTotalsRepositoryTest {
assertThat(orderCapture.lastValue.items.size).isEqualTo(3)
assertThat(orderCapture.lastValue.items.map { it.quantity }).containsExactly(2f, 1f, 3f)
}

@Test
fun `given product ids, when createOrder with some invalid ids, then return failure`() = runTest {
// GIVEN
repository = WooPosTotalsRepository(
orderCreateEditRepository,
dateUtils
)
val productIds = listOf(1L, -1L, 3L)
val mockOrder: Order = mock()
whenever(orderCreateEditRepository.createOrUpdateOrder(any(), eq(""))).thenReturn(Result.success(mockOrder))

// WHEN
val result = runCatching { repository.createOrderWithProducts(productIds) }

// THEN
assertThat(result.isFailure).isTrue()
assertThat(result.exceptionOrNull()).isInstanceOf(IllegalArgumentException::class.java)
assertThat(result.exceptionOrNull()?.message).isEqualTo("Invalid product ID: -1")
verify(orderCreateEditRepository, never()).createOrUpdateOrder(any(), eq(""))
}
}

0 comments on commit 4da69c1

Please sign in to comment.