Refactor didReadCountryField to set country on MoneyGram acct

MoneyGram is not a CountryBasedPaymentAccount, but it does
have a country field.
This commit is contained in:
ghubstan 2020-11-21 15:22:15 -03:00
parent 636fac9170
commit 23bfa2e7fc
No known key found for this signature in database
GPG Key ID: E35592D6800A861E

View File

@ -21,6 +21,7 @@ package bisq.core.api.model;
import bisq.core.locale.Country; import bisq.core.locale.Country;
import bisq.core.locale.FiatCurrency; import bisq.core.locale.FiatCurrency;
import bisq.core.payment.CountryBasedPaymentAccount; import bisq.core.payment.CountryBasedPaymentAccount;
import bisq.core.payment.MoneyGramAccount;
import bisq.core.payment.PaymentAccount; import bisq.core.payment.PaymentAccount;
import bisq.core.payment.payload.PaymentAccountPayload; import bisq.core.payment.payload.PaymentAccountPayload;
@ -285,22 +286,31 @@ class PaymentAccountTypeAdapter extends TypeAdapter<PaymentAccount> {
} }
private boolean didReadCountryField(JsonReader in, PaymentAccount account, String fieldName) { private boolean didReadCountryField(JsonReader in, PaymentAccount account, String fieldName) {
if (account.isCountryBasedPaymentAccount() && fieldName.equals("country")) { if (!fieldName.equals("country"))
// Read the country code, and use it to set the account's country and single return false;
// trade currency fields.
String countryCode = nextStringOrNull(in); String countryCode = nextStringOrNull(in);
Optional<Country> country = findCountryByCode(countryCode); Optional<Country> country = findCountryByCode(countryCode);
if (country.isPresent()) { if (country.isPresent()) {
if (account.isCountryBasedPaymentAccount()) {
((CountryBasedPaymentAccount) account).setCountry(country.get()); ((CountryBasedPaymentAccount) account).setCountry(country.get());
FiatCurrency fiatCurrency = getCurrencyByCountryCode(checkNotNull(countryCode)); FiatCurrency fiatCurrency = getCurrencyByCountryCode(checkNotNull(countryCode));
account.setSingleTradeCurrency(fiatCurrency); account.setSingleTradeCurrency(fiatCurrency);
return true; } else if (account.isMoneyGramAccount()) {
((MoneyGramAccount) account).setCountry(country.get());
} else { } else {
throw new IllegalArgumentException( String errMsg = format("cannot set the country on a %s",
format("'%s' is an invalid country code.", countryCode)); paymentAccountType.getSimpleName());
log.error(StringUtils.capitalize(errMsg) + ".");
throw new IllegalStateException("programmer error: " + errMsg);
} }
return true;
} else { } else {
return false; throw new IllegalArgumentException(
format("'%s' is an invalid country code.", countryCode));
} }
} }
@ -315,7 +325,6 @@ class PaymentAccountTypeAdapter extends TypeAdapter<PaymentAccount> {
paymentAccountType.getSimpleName()); paymentAccountType.getSimpleName());
log.error(StringUtils.capitalize(errMsg) + ".", ex); log.error(StringUtils.capitalize(errMsg) + ".", ex);
throw new IllegalStateException("programmer error: " + errMsg); throw new IllegalStateException("programmer error: " + errMsg);
} }
} }