From 18dcc6d0c0d19bd65137547265d8402e78e6b75c Mon Sep 17 00:00:00 2001 From: Fabian Schedler Date: Thu, 5 Sep 2024 13:22:04 +0200 Subject: [PATCH] WR-378: Fix error message for already payed --- shared/localization.yml | 7 ++++ .../shared/core/data/api/ApiException.kt | 4 +++ .../billing/viewmodel/BillingViewModel.kt | 32 ++++++++++++------- .../shared/utils/ExceptionMessageMapper.kt | 1 + 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/shared/localization.yml b/shared/localization.yml index 6ed736d..ac6e11b 100644 --- a/shared/localization.yml +++ b/shared/localization.yml @@ -238,6 +238,13 @@ billing: noOpenBill: en: There is currently no open bill for table \"%1$s - %2$s\" de: Es gibt derzeit keine offene Rechnung für den Tisch \"%1$s - %2$s\" + productsAlreadyPayed: + title: + en: Already payed + de: Bereits bezahlt + desc: + en: Some of the selected products are already payed. + de: Einige der ausgewählten Produkte sind bereits bezahlt. amountToLow: en: The selected payment method requires a minimum amount of %1$s. de: Die ausgewählte Zahlungsmethode erfordert einen Mindestbetrag von %1$s. diff --git a/shared/src/commonMain/kotlin/org/datepollsystems/waiterrobot/shared/core/data/api/ApiException.kt b/shared/src/commonMain/kotlin/org/datepollsystems/waiterrobot/shared/core/data/api/ApiException.kt index deb05a3..57240de 100644 --- a/shared/src/commonMain/kotlin/org/datepollsystems/waiterrobot/shared/core/data/api/ApiException.kt +++ b/shared/src/commonMain/kotlin/org/datepollsystems/waiterrobot/shared/core/data/api/ApiException.kt @@ -78,6 +78,10 @@ internal sealed class ApiException : Exception() { @SerialName("BILL_AMOUNT_TOO_LOW") class BillAmountTooLow(val minAmount: Cents) : ApiException() + @Serializable + @SerialName("BILL_PRODUCTS_ALREADY_PAYED") + class BillProductsAlreadyPayed(val orderProductIds: Set) : ApiException() + @Serializable @SerialName("STRIPE_NOT_ACTIVATED") class StripeNotActivated(val eventId: Long) : ApiException() diff --git a/shared/src/commonMain/kotlin/org/datepollsystems/waiterrobot/shared/features/billing/viewmodel/BillingViewModel.kt b/shared/src/commonMain/kotlin/org/datepollsystems/waiterrobot/shared/features/billing/viewmodel/BillingViewModel.kt index 8159311..4784ba7 100644 --- a/shared/src/commonMain/kotlin/org/datepollsystems/waiterrobot/shared/features/billing/viewmodel/BillingViewModel.kt +++ b/shared/src/commonMain/kotlin/org/datepollsystems/waiterrobot/shared/features/billing/viewmodel/BillingViewModel.kt @@ -1,6 +1,7 @@ package org.datepollsystems.waiterrobot.shared.features.billing.viewmodel import org.datepollsystems.waiterrobot.shared.core.CommonApp +import org.datepollsystems.waiterrobot.shared.core.data.api.ApiException import org.datepollsystems.waiterrobot.shared.core.navigation.NavOrViewModelEffect import org.datepollsystems.waiterrobot.shared.core.viewmodel.AbstractViewModel import org.datepollsystems.waiterrobot.shared.core.viewmodel.DialogState @@ -57,18 +58,27 @@ class BillingViewModel internal constructor( reduce { state.withViewState(viewState = ViewState.Loading) } - val newBillItems = billingRepository.payBill( - table = table, - items = state.billItems.filter { it.selectedForBill > 0 }, - selectAll = CommonApp.settings.paymentSelectAllProductsByDefault - ) + try { + val newBillItems = billingRepository.payBill( + table = table, + items = state.billItems.filter { it.selectedForBill > 0 }, + selectAll = CommonApp.settings.paymentSelectAllProductsByDefault + ) - reduce { - state.copy( - viewState = ViewState.Idle, - _billItems = newBillItems.associateBy { it.virtualId }, - change = null, - moneyGivenText = "" + reduce { + state.copy( + viewState = ViewState.Idle, + _billItems = newBillItems.associateBy { it.virtualId }, + change = null, + moneyGivenText = "" + ) + } + } catch (_: ApiException.BillProductsAlreadyPayed) { + logger.i("Some products have already been payed.") + reduceError( + L.billing.productsAlreadyPayed.title(), + L.billing.productsAlreadyPayed.desc(), + dismiss = ::loadBill ) } } diff --git a/shared/src/commonMain/kotlin/org/datepollsystems/waiterrobot/shared/utils/ExceptionMessageMapper.kt b/shared/src/commonMain/kotlin/org/datepollsystems/waiterrobot/shared/utils/ExceptionMessageMapper.kt index 40d0df0..fc83ec6 100644 --- a/shared/src/commonMain/kotlin/org/datepollsystems/waiterrobot/shared/utils/ExceptionMessageMapper.kt +++ b/shared/src/commonMain/kotlin/org/datepollsystems/waiterrobot/shared/utils/ExceptionMessageMapper.kt @@ -40,6 +40,7 @@ internal fun ApiException.getLocalizedUserMessage(): String = when (this) { is ApiException.StripeNotActivated -> L.stripeInit.error.disabledForEvent() is ApiException.ProductStockToLow -> L.order.stockToLow.title() is ApiException.OrderAlreadySubmitted -> L.order.alreadyCreated() + is ApiException.BillProductsAlreadyPayed -> L.billing.productsAlreadyPayed.desc() // Unknown exceptions or exceptions that should normally not happen is ApiException.Generic,