Skip to content

Commit

Permalink
Add error for voucher code looks like an account number
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa authored and Pururun committed Aug 21, 2024
1 parent 5d38db0 commit d1e3348
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,23 @@ private fun EnterVoucherBody(
)
}
}
if (
state.voucherState is VoucherDialogState.Error &&
state.voucherState.error is RedeemVoucherError.EnteredAccountNumber
) {
Text(
modifier = Modifier.padding(top = Dimens.smallPadding),
text = stringResource(id = R.string.voucher_is_account_number),
color = MaterialTheme.colorScheme.onPrimary,
style = MaterialTheme.typography.bodySmall
)
}
}

private fun RedeemVoucherError.message(): Int =
when (this) {
RedeemVoucherError.TooShortVoucher,
RedeemVoucherError.EnteredAccountNumber,
RedeemVoucherError.InvalidVoucher -> R.string.invalid_voucher
RedeemVoucherError.VoucherAlreadyUsed -> R.string.voucher_already_used
RedeemVoucherError.RpcError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package net.mullvad.mullvadvpn.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import arrow.core.raise.either
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
Expand All @@ -11,7 +12,9 @@ import kotlinx.coroutines.launch
import net.mullvad.mullvadvpn.compose.state.VoucherDialogState
import net.mullvad.mullvadvpn.compose.state.VoucherDialogUiState
import net.mullvad.mullvadvpn.constant.VOUCHER_LENGTH
import net.mullvad.mullvadvpn.lib.model.ParseVoucherCodeError
import net.mullvad.mullvadvpn.lib.model.RedeemVoucherError
import net.mullvad.mullvadvpn.lib.model.VoucherCode
import net.mullvad.mullvadvpn.lib.shared.VoucherRepository
import net.mullvad.mullvadvpn.util.VoucherRegexHelper

Expand All @@ -26,11 +29,23 @@ class VoucherDialogViewModel(private val voucherRepository: VoucherRepository) :
}
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), VoucherDialogUiState.INITIAL)

fun onRedeem(voucherCode: String) {
fun onRedeem(voucherInput: String) {
vmState.update { VoucherDialogState.Verifying }
viewModelScope.launch {
voucherRepository
.submitVoucher(voucherCode)
either {
val voucherCode =
VoucherCode.fromString(voucherInput)
.mapLeft {
when (it) {
is ParseVoucherCodeError.AllDigit ->
RedeemVoucherError.EnteredAccountNumber
is ParseVoucherCodeError.TooShort ->
RedeemVoucherError.TooShortVoucher
}
}
.bind()
voucherRepository.submitVoucher(voucherCode).bind()
}
.fold(
{ error -> setError(error) },
{ success -> handleAddedTime(success.timeAdded) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ import net.mullvad.mullvadvpn.lib.model.UnknownApiAccessMethodError
import net.mullvad.mullvadvpn.lib.model.UnknownCustomListError
import net.mullvad.mullvadvpn.lib.model.UpdateApiAccessMethodError
import net.mullvad.mullvadvpn.lib.model.UpdateCustomListError
import net.mullvad.mullvadvpn.lib.model.VoucherCode
import net.mullvad.mullvadvpn.lib.model.WebsiteAuthToken
import net.mullvad.mullvadvpn.lib.model.WireguardConstraints as ModelWireguardConstraints
import net.mullvad.mullvadvpn.lib.model.WireguardEndpointData as ModelWireguardEndpointData
Expand Down Expand Up @@ -575,8 +576,10 @@ class ManagementService(
.mapLeft(SetWireguardConstraintsError::Unknown)
.mapEmpty()

suspend fun submitVoucher(voucher: String): Either<RedeemVoucherError, RedeemVoucherSuccess> =
Either.catch { grpc.submitVoucher(StringValue.of(voucher)).toDomain() }
suspend fun submitVoucher(
voucher: VoucherCode
): Either<RedeemVoucherError, RedeemVoucherSuccess> =
Either.catch { grpc.submitVoucher(StringValue.of(voucher.value)).toDomain() }
.mapLeftStatus {
when (it.status.code) {
Status.Code.INVALID_ARGUMENT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ sealed class RedeemVoucherError {

data object VoucherAlreadyUsed : RedeemVoucherError()

data object TooShortVoucher : RedeemVoucherError()

data object EnteredAccountNumber : RedeemVoucherError()

data object RpcError : RedeemVoucherError()

data class Unknown(val error: Throwable) : RedeemVoucherError()
Expand Down
1 change: 1 addition & 0 deletions android/lib/resource/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<string name="redeem">Redeem</string>
<string name="invalid_voucher">Voucher code is invalid.</string>
<string name="voucher_already_used">Voucher code has already been used.</string>
<string name="voucher_is_account_number">It looks like you’ve entered an account number instead of a voucher code. If you would like to change the active account, please log out first.</string>
<string name="error_occurred">An error occurred.</string>
<string name="settings">Settings</string>
<string name="no_internet_connection">No internet connection</string>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package net.mullvad.mullvadvpn.lib.shared

import net.mullvad.mullvadvpn.lib.daemon.grpc.ManagementService
import net.mullvad.mullvadvpn.lib.model.VoucherCode

class VoucherRepository(
private val managementService: ManagementService,
private val accountRepository: AccountRepository
) {
suspend fun submitVoucher(voucher: String) =
suspend fun submitVoucher(voucher: VoucherCode) =
managementService.submitVoucher(voucher).onRight {
accountRepository.onVoucherRedeemed(it.newExpiryDate)
}
Expand Down
3 changes: 3 additions & 0 deletions gui/locales/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -2318,6 +2318,9 @@ msgstr ""
msgid "Invalid or missing value \"%s\""
msgstr ""

msgid "It looks like you’ve entered an account number instead of a voucher code. If you would like to change the active account, please log out first."
msgstr ""

msgid "Lets you select apps that should access the Internet directly without going through the VPN tunnel."
msgstr ""

Expand Down

0 comments on commit d1e3348

Please sign in to comment.