From 479f86e8ef888f270af1404255f1ff450c504b25 Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 29 Nov 2024 09:46:58 +0100 Subject: [PATCH 01/10] Fluxc with the action to send an email --- gradle/libs.versions.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 22aaadbf266..9a0f85309f4 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -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' From 421f9eeee8429ee2ebf464d93165c5b049b1c6e9 Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 29 Nov 2024 09:48:51 +0100 Subject: [PATCH 02/10] Renamed availability checker to follow the convention --- .../android/ui/woopos/home/totals/WooPosTotalsViewModel.kt | 4 ++-- ...gAvailable.kt => WooPosTotalsIsReceiptSendingAvailable.kt} | 2 +- .../ui/woopos/home/totals/WooPosTotalsViewModelTest.kt | 4 ++-- .../payment/receipt/WooPosIsReceiptSendingAvailableTest.kt | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) rename WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/{WooPosIsReceiptSendingAvailable.kt => WooPosTotalsIsReceiptSendingAvailable.kt} (94%) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModel.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModel.kt index ee93330f333..d17c833b247 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModel.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModel.kt @@ -13,7 +13,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.WooPosTotalsIsReceiptSendingAvailable 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 @@ -39,7 +39,7 @@ class WooPosTotalsViewModel @Inject constructor( private val priceFormat: WooPosFormatPrice, private val analyticsTracker: WooPosAnalyticsTracker, private val networkStatus: WooPosNetworkStatus, - private val isReceiptSendingAvailable: WooPosIsReceiptSendingAvailable, + private val isReceiptSendingAvailable: WooPosTotalsIsReceiptSendingAvailable, savedState: SavedStateHandle, ) : ViewModel() { diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosIsReceiptSendingAvailable.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsIsReceiptSendingAvailable.kt similarity index 94% rename from WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosIsReceiptSendingAvailable.kt rename to WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsIsReceiptSendingAvailable.kt index 6d0c709416c..55cff8fa6d3 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosIsReceiptSendingAvailable.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsIsReceiptSendingAvailable.kt @@ -7,7 +7,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import javax.inject.Inject -class WooPosIsReceiptSendingAvailable @Inject constructor( +class WooPosTotalsIsReceiptSendingAvailable @Inject constructor( private val isReceiptsEnabled: WooPosIsReceiptsEnabled, private val getWooCoreVersion: GetWooCorePluginCachedVersion, ) { diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModelTest.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModelTest.kt index 37ddcd86d60..d832b64f437 100644 --- a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModelTest.kt +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModelTest.kt @@ -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.WooPosTotalsIsReceiptSendingAvailable 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 @@ -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: WooPosTotalsIsReceiptSendingAvailable = mock { onBlocking { invoke() }.thenReturn(false) } diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosIsReceiptSendingAvailableTest.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosIsReceiptSendingAvailableTest.kt index 9679aa4c4cd..10af8f81a8b 100644 --- a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosIsReceiptSendingAvailableTest.kt +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosIsReceiptSendingAvailableTest.kt @@ -12,7 +12,7 @@ class WooPosIsReceiptSendingAvailableTest { private val isReceiptsEnabled: WooPosIsReceiptsEnabled = mock() private val getWooCoreVersion: GetWooCorePluginCachedVersion = mock() - private val receiptSendingAvailable = WooPosIsReceiptSendingAvailable( + private val receiptSendingAvailable = WooPosTotalsIsReceiptSendingAvailable( isReceiptsEnabled = isReceiptsEnabled, getWooCoreVersion = getWooCoreVersion ) From d471c83a44bce8d59915007c7cbc87925b789c2c Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 29 Nov 2024 10:14:22 +0100 Subject: [PATCH 03/10] Call repository when a button clicked --- .../ui/woopos/home/totals/WooPosTotalsViewModel.kt | 14 +++++++++++++- .../receipt/WooPosTotalsReceiptRepository.kt | 12 ++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsReceiptRepository.kt diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModel.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModel.kt index d17c833b247..3e80cd3f827 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModel.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModel.kt @@ -14,6 +14,7 @@ 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.WooPosTotalsIsReceiptSendingAvailable +import com.woocommerce.android.ui.woopos.home.totals.payment.receipt.WooPosTotalsReceiptRepository 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 @@ -36,6 +37,7 @@ class WooPosTotalsViewModel @Inject constructor( private val childrenToParentEventSender: WooPosChildrenToParentEventSender, private val cardReaderFacade: WooPosCardReaderFacade, private val totalsRepository: WooPosTotalsRepository, + private val receiptRepository: WooPosTotalsReceiptRepository, private val priceFormat: WooPosFormatPrice, private val analyticsTracker: WooPosAnalyticsTracker, private val networkStatus: WooPosNetworkStatus, @@ -81,7 +83,7 @@ class WooPosTotalsViewModel @Inject constructor( is WooPosTotalsUIEvent.RetryOrderCreationClicked -> { createOrderDraft(dataState.value.itemClickedDataList) } - WooPosTotalsUIEvent.OnSendReceiptClicked -> TODO() + WooPosTotalsUIEvent.OnSendReceiptClicked -> { sendReceiptByEmail() } WooPosTotalsUIEvent.OnStartReceiptFlowClicked -> { uiState.value = WooPosTotalsViewState.ReceiptSending(email = "") } @@ -100,6 +102,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) + } + } + private fun listenUpEvents() { viewModelScope.launch { parentToChildrenEventReceiver.events.collect { event -> diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsReceiptRepository.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsReceiptRepository.kt new file mode 100644 index 00000000000..d4a25f1906e --- /dev/null +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsReceiptRepository.kt @@ -0,0 +1,12 @@ +package com.woocommerce.android.ui.woopos.home.totals.payment.receipt + +import org.wordpress.android.fluxc.store.WCOrderStore +import javax.inject.Inject + +class WooPosTotalsReceiptRepository @Inject constructor( + orderStore: WCOrderStore, +) { + suspend fun sendReceiptByEmail(orderId: Long, email: String) { + + } +} From 62e345e74aa710d4621f7569f35d811e7899f068 Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 29 Nov 2024 10:15:33 +0100 Subject: [PATCH 04/10] Fixed naming --- .../ui/woopos/home/totals/WooPosTotalsViewModel.kt | 10 +++++----- ...=> WooPosTotalsPaymentReceiptIsSendingAvailable.kt} | 2 +- ...tory.kt => WooPosTotalsPaymentReceiptRepository.kt} | 2 +- .../ui/woopos/home/totals/WooPosTotalsViewModelTest.kt | 4 ++-- .../receipt/WooPosIsReceiptSendingAvailableTest.kt | 2 +- 5 files changed, 10 insertions(+), 10 deletions(-) rename WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/{WooPosTotalsIsReceiptSendingAvailable.kt => WooPosTotalsPaymentReceiptIsSendingAvailable.kt} (93%) rename WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/{WooPosTotalsReceiptRepository.kt => WooPosTotalsPaymentReceiptRepository.kt} (80%) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModel.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModel.kt index 3e80cd3f827..9feeb582981 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModel.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModel.kt @@ -13,8 +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.WooPosTotalsIsReceiptSendingAvailable -import com.woocommerce.android.ui.woopos.home.totals.payment.receipt.WooPosTotalsReceiptRepository +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 @@ -37,11 +37,11 @@ class WooPosTotalsViewModel @Inject constructor( private val childrenToParentEventSender: WooPosChildrenToParentEventSender, private val cardReaderFacade: WooPosCardReaderFacade, private val totalsRepository: WooPosTotalsRepository, - private val receiptRepository: WooPosTotalsReceiptRepository, + private val receiptRepository: WooPosTotalsPaymentReceiptRepository, private val priceFormat: WooPosFormatPrice, private val analyticsTracker: WooPosAnalyticsTracker, private val networkStatus: WooPosNetworkStatus, - private val isReceiptSendingAvailable: WooPosTotalsIsReceiptSendingAvailable, + private val isReceiptSendingAvailable: WooPosTotalsPaymentReceiptIsSendingAvailable, savedState: SavedStateHandle, ) : ViewModel() { @@ -83,7 +83,7 @@ class WooPosTotalsViewModel @Inject constructor( is WooPosTotalsUIEvent.RetryOrderCreationClicked -> { createOrderDraft(dataState.value.itemClickedDataList) } - WooPosTotalsUIEvent.OnSendReceiptClicked -> { sendReceiptByEmail() } + WooPosTotalsUIEvent.OnSendReceiptClicked -> sendReceiptByEmail() WooPosTotalsUIEvent.OnStartReceiptFlowClicked -> { uiState.value = WooPosTotalsViewState.ReceiptSending(email = "") } diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsIsReceiptSendingAvailable.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptIsSendingAvailable.kt similarity index 93% rename from WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsIsReceiptSendingAvailable.kt rename to WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptIsSendingAvailable.kt index 55cff8fa6d3..ac269e81514 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsIsReceiptSendingAvailable.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptIsSendingAvailable.kt @@ -7,7 +7,7 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext import javax.inject.Inject -class WooPosTotalsIsReceiptSendingAvailable @Inject constructor( +class WooPosTotalsPaymentReceiptIsSendingAvailable @Inject constructor( private val isReceiptsEnabled: WooPosIsReceiptsEnabled, private val getWooCoreVersion: GetWooCorePluginCachedVersion, ) { diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsReceiptRepository.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepository.kt similarity index 80% rename from WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsReceiptRepository.kt rename to WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepository.kt index d4a25f1906e..f4ede769bb4 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsReceiptRepository.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepository.kt @@ -3,7 +3,7 @@ package com.woocommerce.android.ui.woopos.home.totals.payment.receipt import org.wordpress.android.fluxc.store.WCOrderStore import javax.inject.Inject -class WooPosTotalsReceiptRepository @Inject constructor( +class WooPosTotalsPaymentReceiptRepository @Inject constructor( orderStore: WCOrderStore, ) { suspend fun sendReceiptByEmail(orderId: Long, email: String) { diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModelTest.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModelTest.kt index d832b64f437..1290117d06b 100644 --- a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModelTest.kt +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModelTest.kt @@ -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.WooPosTotalsIsReceiptSendingAvailable +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 @@ -62,7 +62,7 @@ class WooPosTotalsViewModelTest { on { paymentStatus }.thenReturn(MutableStateFlow(WooPosCardReaderPaymentStatus.Unknown)) } private val analyticsTracker: WooPosAnalyticsTracker = mock() - private val isReceiptSendingAvailable: WooPosTotalsIsReceiptSendingAvailable = mock { + private val isReceiptSendingAvailable: WooPosTotalsPaymentReceiptIsSendingAvailable = mock { onBlocking { invoke() }.thenReturn(false) } diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosIsReceiptSendingAvailableTest.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosIsReceiptSendingAvailableTest.kt index 10af8f81a8b..269b2b9f357 100644 --- a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosIsReceiptSendingAvailableTest.kt +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosIsReceiptSendingAvailableTest.kt @@ -12,7 +12,7 @@ class WooPosIsReceiptSendingAvailableTest { private val isReceiptsEnabled: WooPosIsReceiptsEnabled = mock() private val getWooCoreVersion: GetWooCorePluginCachedVersion = mock() - private val receiptSendingAvailable = WooPosTotalsIsReceiptSendingAvailable( + private val receiptSendingAvailable = WooPosTotalsPaymentReceiptIsSendingAvailable( isReceiptsEnabled = isReceiptsEnabled, getWooCoreVersion = getWooCoreVersion ) From 06789daaf53c5dba3f9794de2886cfc8c1646b1e Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 29 Nov 2024 10:19:08 +0100 Subject: [PATCH 05/10] Update email state --- .../android/ui/woopos/home/totals/WooPosTotalsScreen.kt | 2 +- .../android/ui/woopos/home/totals/WooPosTotalsUIEvent.kt | 1 + .../android/ui/woopos/home/totals/WooPosTotalsViewModel.kt | 3 +++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsScreen.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsScreen.kt index 6dd302722b5..a6e52b037ea 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsScreen.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsScreen.kt @@ -90,7 +90,7 @@ private fun WooPosTotalsScreen( if (state is WooPosTotalsViewState.ReceiptSending) { WooPosTotalsPaymentReceiptScreen( state, - onEmailAddressChanged = {}, + onEmailAddressChanged = { onUIEvent(WooPosTotalsUIEvent.OnEmailChanged(it)) }, onSendReceiptClicked = { onUIEvent(WooPosTotalsUIEvent.OnSendReceiptClicked) } ) } diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsUIEvent.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsUIEvent.kt index 26143344294..af295147e17 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsUIEvent.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsUIEvent.kt @@ -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() } diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModel.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModel.kt index 9feeb582981..b09aa73cc22 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModel.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModel.kt @@ -87,6 +87,9 @@ class WooPosTotalsViewModel @Inject constructor( WooPosTotalsUIEvent.OnStartReceiptFlowClicked -> { uiState.value = WooPosTotalsViewState.ReceiptSending(email = "") } + is WooPosTotalsUIEvent.OnEmailChanged -> { + uiState.value = WooPosTotalsViewState.ReceiptSending(email = event.email) + } } } From 275f1c75ed1c7b30b444cf98add1035de45df31b Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 29 Nov 2024 10:45:32 +0100 Subject: [PATCH 06/10] Code that updates email and sends order details there --- .../WooPosTotalsPaymentReceiptRepository.kt | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepository.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepository.kt index f4ede769bb4..a1393241fe8 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepository.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepository.kt @@ -1,12 +1,38 @@ 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( - orderStore: WCOrderStore, + private val selectedSite: SelectedSite, + private val orderStore: WCOrderStore, + private val orderCreateEditRepository: OrderCreateEditRepository, + private val orderMapper: OrderMapper, ) { - suspend fun sendReceiptByEmail(orderId: Long, email: String) { + suspend fun sendReceiptByEmail(orderId: Long, email: String): Result = withContext(Dispatchers.IO) { + val order = getOrderById(orderId) ?: return@withContext Result.failure(Exception("Order not found")) + val updatedCustomer = order.customer?.copy(email = email) ?: Order.Customer.EMPTY.copy(email = email) + val updatedOrder = order.copy(customer = updatedCustomer) + val updateOrderResult = orderCreateEditRepository.createOrUpdateOrder(updatedOrder) + if (updateOrderResult.isFailure) return@withContext Result.failure(Exception("Failed to update order")) + + val sendOrderResult = orderStore.sendOrderReceipt(selectedSite.get(), orderId) + return@withContext if (sendOrderResult.isError) { + Result.failure(Exception("Failed to send order receipt")) + } else { + Result.success(Unit) + } } + + private suspend fun getOrderById(orderId: Long) = + orderStore.getOrderByIdAndSite(orderId, selectedSite.get())?.let { + orderMapper.toAppModel(it) + } } From 5eff1cc59b207d51782108743b621237a35c8ea2 Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 29 Nov 2024 10:50:10 +0100 Subject: [PATCH 07/10] Simplified the code --- .../WooPosTotalsPaymentReceiptRepository.kt | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepository.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepository.kt index a1393241fe8..5f6967f592f 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepository.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepository.kt @@ -16,21 +16,34 @@ class WooPosTotalsPaymentReceiptRepository @Inject constructor( private val orderMapper: OrderMapper, ) { suspend fun sendReceiptByEmail(orderId: Long, email: String): Result = withContext(Dispatchers.IO) { - val order = getOrderById(orderId) ?: return@withContext Result.failure(Exception("Order not found")) - val updatedCustomer = order.customer?.copy(email = email) ?: Order.Customer.EMPTY.copy(email = email) - val updatedOrder = order.copy(customer = updatedCustomer) - val updateOrderResult = orderCreateEditRepository.createOrUpdateOrder(updatedOrder) + 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")) + } - if (updateOrderResult.isFailure) return@withContext Result.failure(Exception("Failed to update order")) + return@withContext triggerOrderReceiptSending(orderId) + } + private suspend fun triggerOrderReceiptSending(orderId: Long): Result { val sendOrderResult = orderStore.sendOrderReceipt(selectedSite.get(), orderId) - return@withContext if (sendOrderResult.isError) { + 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 { + val updatedCustomer = order.customer?.copy(email = email) ?: Order.Customer.EMPTY.copy(email = email) + return orderCreateEditRepository.createOrUpdateOrder( + order = order.copy(customer = updatedCustomer) + ) + } + private suspend fun getOrderById(orderId: Long) = orderStore.getOrderByIdAndSite(orderId, selectedSite.get())?.let { orderMapper.toAppModel(it) From 2435ddea4c86833ef5970ae90e27096dc0431d9c Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 29 Nov 2024 12:14:14 +0100 Subject: [PATCH 08/10] Update billing address --- .../payment/receipt/WooPosTotalsPaymentReceiptRepository.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepository.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepository.kt index 5f6967f592f..2bed30dfc4b 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepository.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepository.kt @@ -38,7 +38,8 @@ class WooPosTotalsPaymentReceiptRepository @Inject constructor( } private suspend fun updateOrderWithEmail(order: Order, email: String): Result { - val updatedCustomer = order.customer?.copy(email = email) ?: Order.Customer.EMPTY.copy(email = email) + val updatedBillingAddress = order.billingAddress.copy(email = email) + val updatedCustomer = order.customer?.copy(billingAddress = updatedBillingAddress) return orderCreateEditRepository.createOrUpdateOrder( order = order.copy(customer = updatedCustomer) ) From 18a93ac876a585529bef756258c088ec4156c6ff Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 29 Nov 2024 15:58:31 +0100 Subject: [PATCH 09/10] WooPosTotalsPaymentReceiptRepositoryTest --- .../home/totals/WooPosTotalsViewModelTest.kt | 23 ++-- ...ooPosTotalsPaymentReceiptRepositoryTest.kt | 126 ++++++++++++++++++ 2 files changed, 139 insertions(+), 10 deletions(-) create mode 100644 WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepositoryTest.kt diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModelTest.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModelTest.kt index 1290117d06b..9d2ae980132 100644 --- a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModelTest.kt +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/WooPosTotalsViewModelTest.kt @@ -12,6 +12,7 @@ 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.WooPosTotalsPaymentReceiptIsSendingAvailable +import com.woocommerce.android.ui.woopos.home.totals.payment.receipt.WooPosTotalsPaymentReceiptRepository 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 @@ -65,6 +66,7 @@ class WooPosTotalsViewModelTest { private val isReceiptSendingAvailable: WooPosTotalsPaymentReceiptIsSendingAvailable = mock { onBlocking { invoke() }.thenReturn(false) } + private val receiptRepository: WooPosTotalsPaymentReceiptRepository = mock() private companion object { private const val EMPTY_ORDER_ID = -1L @@ -706,15 +708,16 @@ class WooPosTotalsViewModelTest { priceFormat: WooPosFormatPrice = mock(), savedState: SavedStateHandle = SavedStateHandle(), ) = WooPosTotalsViewModel( - resourceProvider, - parentToChildrenEventReceiver, - childrenToParentEventSender, - cardReaderFacade, - totalsRepository, - priceFormat, - analyticsTracker, - networkStatus, - isReceiptSendingAvailable, - savedState + resourceProvider = resourceProvider, + parentToChildrenEventReceiver = parentToChildrenEventReceiver, + childrenToParentEventSender = childrenToParentEventSender, + cardReaderFacade = cardReaderFacade, + receiptRepository = receiptRepository, + totalsRepository = totalsRepository, + priceFormat = priceFormat, + analyticsTracker = analyticsTracker, + networkStatus = networkStatus, + isReceiptSendingAvailable = isReceiptSendingAvailable, + savedState = savedState, ) } diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepositoryTest.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepositoryTest.kt new file mode 100644 index 00000000000..63458bb66fd --- /dev/null +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepositoryTest.kt @@ -0,0 +1,126 @@ +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.test.runTest +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test +import org.mockito.kotlin.any +import org.mockito.kotlin.anyOrNull +import org.mockito.kotlin.eq +import org.mockito.kotlin.mock +import org.mockito.kotlin.whenever +import org.wordpress.android.fluxc.model.SiteModel +import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType +import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooError +import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooErrorType +import org.wordpress.android.fluxc.network.rest.wpcom.wc.WooPayload +import org.wordpress.android.fluxc.store.WCOrderStore + +class WooPosTotalsPaymentReceiptRepositoryTest { + private val siteModel: SiteModel = mock() + private val selectedSite: SelectedSite = mock { + on { get() }.thenReturn(siteModel) + } + private val orderStore: WCOrderStore = mock() + private val orderCreateEditRepository: OrderCreateEditRepository = mock() + private val orderMapper: OrderMapper = mock() + + private val repository = WooPosTotalsPaymentReceiptRepository( + selectedSite, orderStore, orderCreateEditRepository, orderMapper + ) + + @Test + fun `given valid order id and email, when sendReceiptByEmail, then return success`() = runTest { + // GIVEN + val orderId = 1L + val email = "test@example.com" + val mockOrder: Order = mock { + on { billingAddress }.thenReturn(mock()) + on { customer }.thenReturn(mock()) + } + whenever(orderStore.getOrderByIdAndSite(orderId, siteModel)).thenReturn(mock()) + whenever(orderMapper.toAppModel(any())).thenReturn(mockOrder) + whenever(orderCreateEditRepository.createOrUpdateOrder(any(), eq(""))).thenReturn(Result.success(mockOrder)) + val sendOrderReceiptResult = WooPayload(Unit) + whenever(orderStore.sendOrderReceipt(siteModel, orderId)).thenReturn(sendOrderReceiptResult) + + // WHEN + val result = repository.sendReceiptByEmail(orderId, email) + + // THEN + assertThat(result.isSuccess).isTrue() + } + + @Test + fun `given invalid order id, when sendReceiptByEmail, then return failure`() = runTest { + // GIVEN + val orderId = 999L + val email = "test@example.com" + whenever(selectedSite.get()).thenReturn(siteModel) + whenever(orderStore.getOrderByIdAndSite(orderId, siteModel)).thenReturn(null) + + // WHEN + val result = repository.sendReceiptByEmail(orderId, email) + + // THEN + assertThat(result.isFailure).isTrue() + } + + @Test + fun `given email update fails, when sendReceiptByEmail, then return failure`() = runTest { + // GIVEN + val email = "test@example.com" + val orderId = 1L + val mockOrder: Order = mock { + on { billingAddress }.thenReturn(mock()) + on { customer }.thenReturn(mock()) + } + + whenever(selectedSite.get()).thenReturn(siteModel) + whenever(orderStore.getOrderByIdAndSite(orderId, siteModel)).thenReturn(mock()) + whenever(orderMapper.toAppModel(any())).thenReturn(mockOrder) + whenever(orderCreateEditRepository.createOrUpdateOrder(anyOrNull(), eq(""))).thenReturn( + Result.failure(Exception("Update failed")) + ) + + // WHEN + val result = repository.sendReceiptByEmail(orderId, email) + + // THEN + assertThat(result.isFailure).isTrue() + } + + + @Test + fun `given receipt sending fails, when sendReceiptByEmail, then return failure`() = runTest { + // GIVEN + val orderId = 1L + val email = "test@example.com" + val mockOrder: Order = mock { + on { billingAddress }.thenReturn(mock()) + on { customer }.thenReturn(mock()) + } + whenever(selectedSite.get()).thenReturn(siteModel) + whenever(orderStore.getOrderByIdAndSite(orderId, siteModel)).thenReturn(mock()) + whenever(orderMapper.toAppModel(any())).thenReturn(mockOrder) + whenever(orderCreateEditRepository.createOrUpdateOrder(any(), eq(""))).thenReturn( + Result.success(mockOrder) + ) + val sendOrderReceiptResult = WooPayload( + WooError( + WooErrorType.GENERIC_ERROR, + GenericErrorType.TIMEOUT, + ) + ) + whenever(orderStore.sendOrderReceipt(siteModel, orderId)).thenReturn(sendOrderReceiptResult) + + // WHEN + val result = repository.sendReceiptByEmail(orderId, email) + + // THEN + assertThat(result.isFailure).isTrue() + } +} From 2f67f6f458f5536059d001fed38b6fc532a9562d Mon Sep 17 00:00:00 2001 From: Andrey Date: Fri, 29 Nov 2024 16:00:55 +0100 Subject: [PATCH 10/10] Fixed formatting --- .../receipt/WooPosTotalsPaymentReceiptRepositoryTest.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepositoryTest.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepositoryTest.kt index 63458bb66fd..e05e57942c9 100644 --- a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepositoryTest.kt +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/woopos/home/totals/payment/receipt/WooPosTotalsPaymentReceiptRepositoryTest.kt @@ -29,7 +29,10 @@ class WooPosTotalsPaymentReceiptRepositoryTest { private val orderMapper: OrderMapper = mock() private val repository = WooPosTotalsPaymentReceiptRepository( - selectedSite, orderStore, orderCreateEditRepository, orderMapper + selectedSite, + orderStore, + orderCreateEditRepository, + orderMapper, ) @Test @@ -93,7 +96,6 @@ class WooPosTotalsPaymentReceiptRepositoryTest { assertThat(result.isFailure).isTrue() } - @Test fun `given receipt sending fails, when sendReceiptByEmail, then return failure`() = runTest { // GIVEN