Skip to content

[Shipping labels] Update shipment status after refunding #14199

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

Open
wants to merge 3 commits into
base: issue/WOOMOB-609-fix-inconsistent-currency-in-shipping-labels-flow
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ShippingLabelRefundViewModel @Inject constructor(
) : ScopedViewModel(savedState) {
private var refundJob: Job? = null
val isRefundInProgress: Boolean
get() = refundJob?.isActive ?: false
get() = refundJob?.isActive == true

private val arguments: ShippingLabelRefundFragmentArgs by savedState.navArgs()

Expand Down Expand Up @@ -79,6 +79,6 @@ class ShippingLabelRefundViewModel @Inject constructor(
@IgnoredOnParcel
val isRefundExpired: Boolean
get() = shippingLabel?.isAnonymized == true ||
shippingLabel?.refundExpiryDate?.let { Date().after(it) } ?: false
shippingLabel?.refundExpiryDate?.let { Date().after(it) } == true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import com.woocommerce.android.ui.orders.wooshippinglabels.models.DestinationShi
import com.woocommerce.android.ui.orders.wooshippinglabels.models.ShipmentUIModel
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.WooShippingLabelPackageCreationFragment.Companion.PACKAGE_SELECTION_RESULT
import com.woocommerce.android.ui.orders.wooshippinglabels.packages.ui.PackageData
import com.woocommerce.android.ui.orders.wooshippinglabels.refund.WooShippingLabelRefundFragment
import com.woocommerce.android.ui.orders.wooshippinglabels.split.WooShippingSplitShipmentFragment
import com.woocommerce.android.util.ActivityUtils
import com.woocommerce.android.util.ChromeCustomTabUtils
Expand Down Expand Up @@ -156,6 +157,10 @@ class WooShippingLabelCreationFragment : BaseFragment(), BackPressListener {
handleResult<List<ShipmentUIModel>>(WooShippingSplitShipmentFragment.SPLIT_SHIPMENT_RESULT) {
viewModel.onShipmentSplit(it)
}

handleResult<Long>(WooShippingLabelRefundFragment.KEY_REFUND_SHIPPING_LABEL_RESULT) {
viewModel.onShippingLabelRefunded(it)
}
}

override fun onRequestAllowBackPress(): Boolean = viewModel.allowBackNavigation()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class WooShippingLabelCreationViewModel @Inject constructor(
private val shouldRequireCustoms: ShouldRequireCustomsForm,
private val shouldRequireITN: ShouldRequireITN,
private val fetchShippingLabelFile: FetchShippingLabelFile,
private val observeShippingLabelStatus: ObserveShippingLabelStatus
private val observeShippingLabelStatus: ObserveShippingLabelStatus,
) : ScopedViewModel(savedState) {
private val navArgs: WooShippingLabelCreationFragmentArgs by savedState.navArgs()

Expand Down Expand Up @@ -567,6 +567,26 @@ class WooShippingLabelCreationViewModel @Inject constructor(
shipments.value = newShipments
}

fun onShippingLabelRefunded(labelId: Long) {
// Find the shipment with the given labelId
val shipmentIndex = shipments.value.indexOfFirst { it.labelId == labelId }

// If the shipment is found, reset its purchased state
if (shipmentIndex != -1) {
updateShipment(
shipmentIndex,
shipments.value[shipmentIndex].copy(
purchased = false,
labelId = null,
carrierId = null,
trackingNumber = null,
purchaseState = PurchaseState.NoStarted,
status = ShippingLabelStatus.UNKNOWN
Comment on lines +580 to +584
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don’t focus too much on this part, as it has been changed in the following PR: 1fcf8de

)
)
}
}

private fun getSelectedOriginAddress(originAddresses: List<OriginShippingAddress>): OriginShippingAddress {
return shippingAddresses.value?.shipFrom?.takeIf {
it != OriginShippingAddress.EMPTY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import androidx.compose.material.Surface
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.woocommerce.android.extensions.navigateBackWithResult
import com.woocommerce.android.ui.base.BaseFragment
import com.woocommerce.android.ui.base.UIMessageResolver
import com.woocommerce.android.ui.compose.theme.WooThemeWithBackground
import com.woocommerce.android.ui.main.MainActivity.Companion.BackPressListener
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.Exit
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ExitWithResult
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ShowSnackbar
import dagger.hilt.android.AndroidEntryPoint
import javax.inject.Inject
Expand Down Expand Up @@ -62,7 +64,8 @@ class WooShippingLabelRefundFragment : BaseFragment(), BackPressListener {
uiMessageResolver.getSnack(event.message, *event.args).show()
}

is Exit -> navigateBackWithResult(KEY_REFUND_SHIPPING_LABEL_RESULT, true)
is Exit -> findNavController().popBackStack()
is ExitWithResult<*> -> navigateBackWithResult(KEY_REFUND_SHIPPING_LABEL_RESULT, event.data)
else -> event.isHandled = false
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.woocommerce.android.ui.orders.wooshippinglabels.datasource.WooShippin
import com.woocommerce.android.ui.orders.wooshippinglabels.networking.WooShippingLabelRepository
import com.woocommerce.android.util.CurrencyFormatter
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.Exit
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ExitWithResult
import com.woocommerce.android.viewmodel.MultiLiveEvent.Event.ShowSnackbar
import com.woocommerce.android.viewmodel.ScopedViewModel
import com.woocommerce.android.viewmodel.navArgs
Expand Down Expand Up @@ -63,7 +64,7 @@ class WooShippingLabelRefundViewModel @Inject constructor(
repository.refundLabel(selectedSite.get(), arguments.orderId, arguments.labelId)
.takeIf { it.isError.not() }?.let {
triggerEvent(ShowSnackbar(R.string.shipping_label_refund_success))
triggerEvent(Exit)
triggerEvent(ExitWithResult(arguments.labelId))
} ?: run {
triggerEvent(ShowSnackbar(R.string.order_refunds_amount_refund_error))
isLoadingFlow.value = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class WooShippingLabelRefundViewModelTest : BaseUnitTest() {
assertThat(capturedEvents.first()).isInstanceOf(Event.ShowSnackbar::class.java)
assertThat((capturedEvents.first() as Event.ShowSnackbar).message)
.isEqualTo(R.string.shipping_label_refund_success)
assertThat(capturedEvents.last()).isEqualTo(Event.Exit)
assertThat(capturedEvents.last()).isEqualTo(Event.ExitWithResult(mockLabelId))
}

@Test
Expand Down
Loading