Skip to content
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

[Woo POS][Cash & Receipts] Update customer with email and use action to send it #13034

Open
wants to merge 10 commits into
base: trunk
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private fun WooPosTotalsScreen(
if (state is WooPosTotalsViewState.ReceiptSending) {
WooPosTotalsPaymentReceiptScreen(
state,
onEmailAddressChanged = {},
onEmailAddressChanged = { onUIEvent(WooPosTotalsUIEvent.OnEmailChanged(it)) },
onSendReceiptClicked = { onUIEvent(WooPosTotalsUIEvent.OnSendReceiptClicked) }
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ sealed class WooPosTotalsUIEvent {
data object RetryOrderCreationClicked : WooPosTotalsUIEvent()
data object OnStartReceiptFlowClicked : WooPosTotalsUIEvent()
data object OnSendReceiptClicked : WooPosTotalsUIEvent()
data class OnEmailChanged(val email: String) : WooPosTotalsUIEvent()
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import com.woocommerce.android.ui.woopos.home.ParentToChildrenEvent
import com.woocommerce.android.ui.woopos.home.WooPosChildrenToParentEventSender
import com.woocommerce.android.ui.woopos.home.WooPosParentToChildrenEventReceiver
import com.woocommerce.android.ui.woopos.home.items.WooPosItemsViewModel
import com.woocommerce.android.ui.woopos.home.totals.payment.receipt.WooPosIsReceiptSendingAvailable
import com.woocommerce.android.ui.woopos.home.totals.payment.receipt.WooPosTotalsPaymentReceiptIsSendingAvailable
import com.woocommerce.android.ui.woopos.home.totals.payment.receipt.WooPosTotalsPaymentReceiptRepository
import com.woocommerce.android.ui.woopos.util.WooPosNetworkStatus
import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsEvent
import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsTracker
Expand All @@ -36,10 +37,11 @@ class WooPosTotalsViewModel @Inject constructor(
private val childrenToParentEventSender: WooPosChildrenToParentEventSender,
private val cardReaderFacade: WooPosCardReaderFacade,
private val totalsRepository: WooPosTotalsRepository,
private val receiptRepository: WooPosTotalsPaymentReceiptRepository,
private val priceFormat: WooPosFormatPrice,
private val analyticsTracker: WooPosAnalyticsTracker,
private val networkStatus: WooPosNetworkStatus,
private val isReceiptSendingAvailable: WooPosIsReceiptSendingAvailable,
private val isReceiptSendingAvailable: WooPosTotalsPaymentReceiptIsSendingAvailable,
savedState: SavedStateHandle,
) : ViewModel() {

Expand Down Expand Up @@ -81,10 +83,13 @@ class WooPosTotalsViewModel @Inject constructor(
is WooPosTotalsUIEvent.RetryOrderCreationClicked -> {
createOrderDraft(dataState.value.itemClickedDataList)
}
WooPosTotalsUIEvent.OnSendReceiptClicked -> TODO()
WooPosTotalsUIEvent.OnSendReceiptClicked -> sendReceiptByEmail()
WooPosTotalsUIEvent.OnStartReceiptFlowClicked -> {
uiState.value = WooPosTotalsViewState.ReceiptSending(email = "")
}
is WooPosTotalsUIEvent.OnEmailChanged -> {
uiState.value = WooPosTotalsViewState.ReceiptSending(email = event.email)
}
}
}

Expand All @@ -100,6 +105,16 @@ class WooPosTotalsViewModel @Inject constructor(
}
}

private fun sendReceiptByEmail() {
val viewState = uiState.value as WooPosTotalsViewState.ReceiptSending
val email = viewState.email
val orderId = dataState.value.orderId
check(orderId != EMPTY_ORDER_ID)
viewModelScope.launch {
receiptRepository.sendReceiptByEmail(orderId, email)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have no designs, so no handling of the error or success in this PR

}
}

private fun listenUpEvents() {
viewModelScope.launch {
parentToChildrenEventReceiver.events.collect { event ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.inject.Inject

class WooPosIsReceiptSendingAvailable @Inject constructor(
class WooPosTotalsPaymentReceiptIsSendingAvailable @Inject constructor(
private val isReceiptsEnabled: WooPosIsReceiptsEnabled,
private val getWooCoreVersion: GetWooCorePluginCachedVersion,
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.woocommerce.android.ui.woopos.home.totals.payment.receipt

import com.woocommerce.android.model.Order
import com.woocommerce.android.model.OrderMapper
import com.woocommerce.android.tools.SelectedSite
import com.woocommerce.android.ui.orders.creation.OrderCreateEditRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.wordpress.android.fluxc.store.WCOrderStore
import javax.inject.Inject

class WooPosTotalsPaymentReceiptRepository @Inject constructor(
private val selectedSite: SelectedSite,
private val orderStore: WCOrderStore,
private val orderCreateEditRepository: OrderCreateEditRepository,
private val orderMapper: OrderMapper,
) {
suspend fun sendReceiptByEmail(orderId: Long, email: String): Result<Unit> = withContext(Dispatchers.IO) {
val order = getOrderById(orderId)
if (order == null) {
return@withContext Result.failure(Exception("Failed to get order"))
}

if (updateOrderWithEmail(order, email).isFailure) {
return@withContext Result.failure(Exception("Failed to update order with email"))
}

return@withContext triggerOrderReceiptSending(orderId)
}

private suspend fun triggerOrderReceiptSending(orderId: Long): Result<Unit> {
val sendOrderResult = orderStore.sendOrderReceipt(selectedSite.get(), orderId)
return if (sendOrderResult.isError) {
Result.failure(Exception("Failed to send order receipt"))
} else {
Result.success(Unit)
}
}

private suspend fun updateOrderWithEmail(order: Order, email: String): Result<Order> {
val updatedBillingAddress = order.billingAddress.copy(email = email)
val updatedCustomer = order.customer?.copy(billingAddress = updatedBillingAddress)
return orderCreateEditRepository.createOrUpdateOrder(
order = order.copy(customer = updatedCustomer)
)
}

private suspend fun getOrderById(orderId: Long) =
orderStore.getOrderByIdAndSite(orderId, selectedSite.get())?.let {
orderMapper.toAppModel(it)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import com.woocommerce.android.ui.woopos.home.ParentToChildrenEvent
import com.woocommerce.android.ui.woopos.home.WooPosChildrenToParentEventSender
import com.woocommerce.android.ui.woopos.home.WooPosParentToChildrenEventReceiver
import com.woocommerce.android.ui.woopos.home.items.WooPosItemsViewModel
import com.woocommerce.android.ui.woopos.home.totals.payment.receipt.WooPosIsReceiptSendingAvailable
import com.woocommerce.android.ui.woopos.home.totals.payment.receipt.WooPosTotalsPaymentReceiptIsSendingAvailable
import com.woocommerce.android.ui.woopos.util.WooPosCoroutineTestRule
import com.woocommerce.android.ui.woopos.util.WooPosNetworkStatus
import com.woocommerce.android.ui.woopos.util.analytics.WooPosAnalyticsEvent
Expand Down Expand Up @@ -62,7 +62,7 @@ class WooPosTotalsViewModelTest {
on { paymentStatus }.thenReturn(MutableStateFlow(WooPosCardReaderPaymentStatus.Unknown))
}
private val analyticsTracker: WooPosAnalyticsTracker = mock()
private val isReceiptSendingAvailable: WooPosIsReceiptSendingAvailable = mock {
private val isReceiptSendingAvailable: WooPosTotalsPaymentReceiptIsSendingAvailable = mock {
onBlocking { invoke() }.thenReturn(false)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class WooPosIsReceiptSendingAvailableTest {
private val isReceiptsEnabled: WooPosIsReceiptsEnabled = mock()
private val getWooCoreVersion: GetWooCorePluginCachedVersion = mock()

private val receiptSendingAvailable = WooPosIsReceiptSendingAvailable(
private val receiptSendingAvailable = WooPosTotalsPaymentReceiptIsSendingAvailable(
isReceiptsEnabled = isReceiptsEnabled,
getWooCoreVersion = getWooCoreVersion
)
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ stripe-terminal = '3.7.1'
tinder-statemachine = '0.2.0'
wiremock = '2.26.3'
wordpress-aztec = 'v2.1.4'
wordpress-fluxc = 'trunk-3d095f6f0a41e50faab2038a9f195f536b6e4520'
wordpress-fluxc = '3115-79440d50f3b8dd18a4bf98862c3be1861b82a320'
wordpress-login = '1.19.0'
wordpress-libaddressinput = '0.0.2'
wordpress-mediapicker = '0.3.1'
Expand Down