-
Notifications
You must be signed in to change notification settings - Fork 136
[POS Orders] Fetch refunds remotely if not stored locally #14915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
toupper
merged 8 commits into
fix/pos-historical-orders-fixes-for-beta
from
fix/pos-historical-orders-show-refunds-net-payment
Nov 12, 2025
Merged
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
358764a
Fetch order refunds if necessary
toupper 3f4a43f
Cleaner way to set refund total
toupper 77cc5e1
Revert changes as it is done in trunk
toupper 6fb5585
Merge branch 'fix/pos-historical-orders-fixes-for-beta' into fix/pos-…
kidinov 59e0806
Fetch refunds asynchronously
toupper d6b20cd
Remove not so useful comment
toupper b98b563
Attempt to fix test in CI
toupper 76683f1
Remove comment
toupper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
21 changes: 0 additions & 21 deletions
21
...in/kotlin/com/woocommerce/android/ui/woopos/common/data/WooPosGetOrderRefundsByOrderId.kt
This file was deleted.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
...c/main/kotlin/com/woocommerce/android/ui/woopos/common/data/WooPosRetrieveOrderRefunds.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package com.woocommerce.android.ui.woopos.common.data | ||
|
|
||
| import com.woocommerce.android.model.Order | ||
| import com.woocommerce.android.model.Refund | ||
| import com.woocommerce.android.model.toAppModel | ||
| import com.woocommerce.android.tools.SelectedSite | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.withContext | ||
| import org.wordpress.android.fluxc.store.WCRefundStore | ||
| import java.math.BigDecimal | ||
| import javax.inject.Inject | ||
|
|
||
| /** | ||
| * Use case that retrieves all refunds for the given [Order]. | ||
| * | ||
| * - First attempts to load refunds from the local store. | ||
| * - If none are found, fetches them from the remote source. | ||
| * - If the order's [Order.refundTotal] is zero, returns an empty list without querying any source. | ||
| * | ||
| * This optimization prevents unnecessary store or network access for non-refunded orders. | ||
| */ | ||
| class WooPosRetrieveOrderRefunds @Inject constructor( | ||
| private val refundStore: WCRefundStore, | ||
| private val selectedSite: SelectedSite | ||
| ) { | ||
| suspend operator fun invoke(order: Order): List<Refund> = | ||
| withContext(Dispatchers.IO) { | ||
| if (order.refundTotal.compareTo(BigDecimal.ZERO) == 0) { | ||
| return@withContext emptyList() | ||
| } | ||
|
|
||
| val site = selectedSite.get() | ||
|
|
||
| var refundModels = refundStore.getAllRefunds(site, order.id) | ||
| if (refundModels.isEmpty()) { | ||
| refundModels = refundStore.fetchAllRefunds(site, order.id).model ?: emptyList() | ||
| } | ||
|
|
||
| refundModels.map { it.toAppModel() } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 0 additions & 102 deletions
102
...otlin/com/woocommerce/android/ui/woopos/common/data/WooPosGetOrderRefundsByOrderIdTest.kt
This file was deleted.
Oops, something went wrong.
142 changes: 142 additions & 0 deletions
142
...st/kotlin/com/woocommerce/android/ui/woopos/common/data/WooPosRetrieveOrderRefundsTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| package com.woocommerce.android.ui.woopos.common.data | ||
|
|
||
| import com.woocommerce.android.tools.SelectedSite | ||
| import com.woocommerce.android.ui.orders.OrderTestUtils | ||
| 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.mock | ||
| import org.mockito.kotlin.verify | ||
| import org.mockito.kotlin.whenever | ||
| import org.wordpress.android.fluxc.model.SiteModel | ||
| import org.wordpress.android.fluxc.model.refunds.WCRefundModel | ||
| import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooResult | ||
| import org.wordpress.android.fluxc.store.WCRefundStore | ||
| import java.math.BigDecimal | ||
| import java.util.Date | ||
|
|
||
| class WooPosRetrieveOrderRefundsTest { | ||
| private lateinit var refundStore: WCRefundStore | ||
| private lateinit var selectedSite: SelectedSite | ||
| private lateinit var sut: WooPosRetrieveOrderRefunds | ||
| private lateinit var site: SiteModel | ||
|
|
||
| @Before | ||
| fun setup() { | ||
| refundStore = mock() | ||
| selectedSite = mock() | ||
| site = mock() | ||
|
|
||
| whenever(selectedSite.get()).thenReturn(site) | ||
|
|
||
| sut = WooPosRetrieveOrderRefunds( | ||
| refundStore = refundStore, | ||
| selectedSite = selectedSite | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `given refunds exist locally, when invoke called, then returns mapped refunds`() = runTest { | ||
| // GIVEN | ||
| val order = OrderTestUtils.generateTestOrder(orderId = 123L) | ||
|
|
||
| val fluxCRefunds = listOf( | ||
| WCRefundModel( | ||
| id = 1L, | ||
| dateCreated = Date(), | ||
| amount = BigDecimal.TEN, | ||
| reason = "Test refund", | ||
| automaticGatewayRefund = true, | ||
| items = emptyList(), | ||
| shippingLineItems = emptyList(), | ||
| feeLineItems = emptyList() | ||
| ), | ||
| WCRefundModel( | ||
| id = 2L, | ||
| dateCreated = Date(), | ||
| amount = BigDecimal.valueOf(5), | ||
| reason = "Another refund", | ||
| automaticGatewayRefund = false, | ||
| items = emptyList(), | ||
| shippingLineItems = emptyList(), | ||
| feeLineItems = emptyList() | ||
| ) | ||
| ) | ||
| whenever(refundStore.getAllRefunds(site, order.id)).thenReturn(fluxCRefunds) | ||
|
|
||
| // WHEN | ||
| val result = sut.invoke(order) | ||
|
|
||
| // THEN | ||
| assertThat(result).hasSize(2) | ||
| assertThat(result[0].id).isEqualTo(1L) | ||
| assertThat(result[0].amount).isEqualTo(BigDecimal.TEN) | ||
| assertThat(result[1].id).isEqualTo(2L) | ||
| assertThat(result[1].amount).isEqualTo(BigDecimal.valueOf(5)) | ||
| } | ||
|
|
||
| @Test | ||
| fun `given no refunds exist locally, when invoke called, then fetches and returns mapped refunds`() = runTest { | ||
| // GIVEN | ||
| val order = OrderTestUtils.generateTestOrder(orderId = 123L) | ||
|
|
||
| whenever(refundStore.getAllRefunds(site, order.id)).thenReturn(emptyList()) | ||
|
|
||
| val remoteRefunds = listOf( | ||
| WCRefundModel( | ||
| id = 10L, | ||
| dateCreated = Date(), | ||
| amount = BigDecimal.ONE, | ||
| reason = "Remote refund", | ||
| automaticGatewayRefund = false, | ||
| items = emptyList(), | ||
| shippingLineItems = emptyList(), | ||
| feeLineItems = emptyList() | ||
| ) | ||
| ) | ||
| whenever(refundStore.fetchAllRefunds(site, order.id)).thenReturn( | ||
| WooResult(model = remoteRefunds) | ||
| ) | ||
|
|
||
| // WHEN | ||
| val result = sut.invoke(order) | ||
|
|
||
| // THEN | ||
| assertThat(result).hasSize(1) | ||
| assertThat(result[0].id).isEqualTo(10L) | ||
| assertThat(result[0].amount).isEqualTo(BigDecimal.ONE) | ||
| verify(refundStore).getAllRefunds(site, order.id) | ||
| verify(refundStore).fetchAllRefunds(site, order.id) | ||
| } | ||
|
|
||
| @Test | ||
| fun `given order refundTotal is zero, when invoke called, then returns empty list and does not hit store`() = runTest { | ||
| // GIVEN | ||
| val order = OrderTestUtils.generateTestOrder(orderId = 999L, refundTotal = BigDecimal.ZERO) | ||
|
|
||
| // WHEN | ||
| val result = sut.invoke(order) | ||
|
|
||
| // THEN | ||
| assertThat(result).isEmpty() | ||
| verify(refundStore, org.mockito.kotlin.never()).getAllRefunds(any(), any()) | ||
| } | ||
|
|
||
| @Test | ||
| fun `given order provided, when invoke called, then passes correct orderId to store`() = runTest { | ||
| // GIVEN | ||
| val order = OrderTestUtils.generateTestOrder(orderId = 456L, refundTotal = BigDecimal.ONE) | ||
| whenever(refundStore.getAllRefunds(any(), any())).thenReturn(emptyList()) | ||
| whenever(refundStore.fetchAllRefunds(any(), any(), any(), any())).thenReturn( | ||
| WooResult(emptyList()) | ||
| ) | ||
|
|
||
| // WHEN | ||
| sut.invoke(order) | ||
|
|
||
| // THEN | ||
| verify(refundStore).getAllRefunds(site, order.id) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
np: I’m not sure how much value this comment currently adds, as it simply translates to the English code below. However, if the implementation changes in the future, the comment is likely not to be updated and may actually confuse the code reader rather than clarify it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, let's remove it. I wanted to add information that it's not explicitly stated in the API, but the code is indeed sufficiently clear to obtain that info from there. Done in d6b20cd