FINERACT-1157:UnsupportedOperationException at InteropServiceImpl

This commit is contained in:
Wandji Collins
2021-01-17 22:42:12 +01:00
committed by Michael Vorburger ⛑️
parent 8c52097094
commit e43be716ae
@@ -97,11 +97,13 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.HttpClientErrorException;
@Service @Service
public class InteropServiceImpl implements InteropService { public class InteropServiceImpl implements InteropService {
@@ -246,7 +248,7 @@ public class InteropServiceImpl implements InteropService {
String subIdOrType) { String subIdOrType) {
InteropIdentifier identifier = findIdentifier(idType, idValue, subIdOrType); InteropIdentifier identifier = findIdentifier(idType, idValue, subIdOrType);
if (identifier == null) { if (identifier == null) {
throw new UnsupportedOperationException( throw new HttpClientErrorException(HttpStatus.BAD_REQUEST,
"Account not found for identifier " + idType + "/" + idValue + (subIdOrType == null ? "" : ("/" + subIdOrType))); "Account not found for identifier " + idType + "/" + idValue + (subIdOrType == null ? "" : ("/" + subIdOrType)));
} }
@@ -279,7 +281,7 @@ public class InteropServiceImpl implements InteropService {
String subIdOrType) { String subIdOrType) {
InteropIdentifier identifier = findIdentifier(idType, idValue, subIdOrType); InteropIdentifier identifier = findIdentifier(idType, idValue, subIdOrType);
if (identifier == null) { if (identifier == null) {
throw new UnsupportedOperationException( throw new HttpClientErrorException(HttpStatus.BAD_REQUEST,
"Account not found for identifier " + idType + "/" + idValue + (subIdOrType == null ? "" : ("/" + subIdOrType))); "Account not found for identifier " + idType + "/" + idValue + (subIdOrType == null ? "" : ("/" + subIdOrType)));
} }
@@ -329,7 +331,7 @@ public class InteropServiceImpl implements InteropService {
if (transactionType.isDebit()) { if (transactionType.isDebit()) {
fee = savingsAccount.calculateWithdrawalFee(request.getAmount().getAmount()); fee = savingsAccount.calculateWithdrawalFee(request.getAmount().getAmount());
if (MathUtil.isLessThan(savingsAccount.getWithdrawableBalance(), request.getAmount().getAmount().add(fee))) { if (MathUtil.isLessThan(savingsAccount.getWithdrawableBalance(), request.getAmount().getAmount().add(fee))) {
throw new UnsupportedOperationException("Not enough balance amount for requested amount plus fees!"); throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Not enough balance amount for requested amount plus fees!");
} }
} else { } else {
fee = BigDecimal.ZERO; fee = BigDecimal.ZERO;
@@ -362,10 +364,10 @@ public class InteropServiceImpl implements InteropService {
BigDecimal total = calculateTotalTransferAmount(request, savingsAccount); BigDecimal total = calculateTotalTransferAmount(request, savingsAccount);
if (MathUtil.isLessThan(savingsAccount.getWithdrawableBalance(), total)) { if (MathUtil.isLessThan(savingsAccount.getWithdrawableBalance(), total)) {
throw new UnsupportedOperationException("Not enough balance amount for requested amount plus fees!"); throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Not enough balance amount for requested amount plus fees!");
} }
if (findTransaction(savingsAccount, transferCode, AMOUNT_HOLD.getValue()) != null) { if (findTransaction(savingsAccount, transferCode, AMOUNT_HOLD.getValue()) != null) {
throw new UnsupportedOperationException("Transfer amount was already put on hold " + transferCode); throw new HttpClientErrorException(HttpStatus.TOO_MANY_REQUESTS, "Transfer amount was already put on hold " + transferCode);
} }
PaymentDetail paymentDetail = instance(findPaymentType(), savingsAccount.getExternalId(), null, getRoutingCode(), transferCode, PaymentDetail paymentDetail = instance(findPaymentType(), savingsAccount.getExternalId(), null, getRoutingCode(), transferCode,
@@ -398,7 +400,7 @@ public class InteropServiceImpl implements InteropService {
String transferCode = request.getTransferCode(); String transferCode = request.getTransferCode();
if (findTransaction(savingsAccount, transferCode, (isDebit ? WITHDRAWAL : DEPOSIT).getValue()) != null) { if (findTransaction(savingsAccount, transferCode, (isDebit ? WITHDRAWAL : DEPOSIT).getValue()) != null) {
throw new UnsupportedOperationException("Transfer was already committed with code: " + transferCode); throw new HttpClientErrorException(HttpStatus.TOO_MANY_REQUESTS, "Transfer was already committed with code: " + transferCode);
} }
LocalDateTime transactionDateTime = DateUtils.getLocalDateTimeOfTenant(); LocalDateTime transactionDateTime = DateUtils.getLocalDateTimeOfTenant();
@@ -409,16 +411,17 @@ public class InteropServiceImpl implements InteropService {
if (isDebit) { if (isDebit) {
SavingsAccountTransaction holdTransaction = findTransaction(savingsAccount, transferCode, AMOUNT_HOLD.getValue()); SavingsAccountTransaction holdTransaction = findTransaction(savingsAccount, transferCode, AMOUNT_HOLD.getValue());
if (holdTransaction == null) { if (holdTransaction == null) {
throw new UnsupportedOperationException("Missing onhold transaction for transfer: " + transferCode); throw new HttpClientErrorException(HttpStatus.NOT_FOUND, "Missing onhold transaction for transfer: " + transferCode);
} }
BigDecimal totalTransferAmount = calculateTotalTransferAmount(request, savingsAccount); BigDecimal totalTransferAmount = calculateTotalTransferAmount(request, savingsAccount);
if (holdTransaction.getAmount().compareTo(totalTransferAmount) != 0) { if (holdTransaction.getAmount().compareTo(totalTransferAmount) != 0) {
throw new UnsupportedOperationException("Transfer request amount plus fees does not match with onhold amount!"); throw new HttpClientErrorException(HttpStatus.BAD_REQUEST,
"Transfer request amount plus fees does not match with onhold amount!");
} }
if (MathUtil.isLessThan(savingsAccount.getWithdrawableBalance().add(holdTransaction.getAmount()), totalTransferAmount)) { if (MathUtil.isLessThan(savingsAccount.getWithdrawableBalance().add(holdTransaction.getAmount()), totalTransferAmount)) {
throw new UnsupportedOperationException("Not enough balance amount!"); throw new HttpClientErrorException(HttpStatus.BAD_REQUEST, "Not enough balance amount!");
} }
if (holdTransaction.getReleaseIdOfHoldAmountTransaction() == null) { if (holdTransaction.getReleaseIdOfHoldAmountTransaction() == null) {
@@ -473,7 +476,8 @@ public class InteropServiceImpl implements InteropService {
savingsAccountRepository.save(savingsAccount); savingsAccountRepository.save(savingsAccount);
} else { } else {
throw new UnsupportedOperationException("No active onhold transaction exists with transferCode " + request.getTransferCode()); throw new HttpClientErrorException(HttpStatus.BAD_REQUEST,
"No active onhold transaction exists with transferCode " + request.getTransferCode());
} }
return InteropTransferResponseData.build(command.commandId(), request.getTransactionCode(), InteropActionState.ACCEPTED, return InteropTransferResponseData.build(command.commandId(), request.getTransactionCode(), InteropActionState.ACCEPTED,
@@ -494,7 +498,7 @@ public class InteropServiceImpl implements InteropService {
return InteropKycResponseData.build(accountKyc); return InteropKycResponseData.build(accountKyc);
} catch (final EmptyResultDataAccessException e) { } catch (final EmptyResultDataAccessException e) {
throw new UnsupportedOperationException("Error in retrieving KYC information: " + e); throw new HttpClientErrorException(HttpStatus.NO_CONTENT, "Error in retrieving KYC information: " + e);
} }
} }
@@ -524,7 +528,7 @@ public class InteropServiceImpl implements InteropService {
private Loan validateAndGetLoan(String accountId) { private Loan validateAndGetLoan(String accountId) {
Loan loan = loanRepository.findNonClosedLoanByAccountNumber(accountId); Loan loan = loanRepository.findNonClosedLoanByAccountNumber(accountId);
if (loan == null) { if (loan == null) {
throw new UnsupportedOperationException("Loan not found for the given account No: " + accountId); throw new HttpClientErrorException(HttpStatus.NOT_FOUND, "Loan not found for the given account No: " + accountId);
} }
return loan; return loan;
} }
@@ -536,12 +540,12 @@ public class InteropServiceImpl implements InteropService {
ApplicationCurrency requestCurrency = currencyRepository.findOneByCode(request.getAmount().getCurrency()); ApplicationCurrency requestCurrency = currencyRepository.findOneByCode(request.getAmount().getCurrency());
if (!savingsAccount.getCurrency().getCode().equals(requestCurrency.getCode())) { if (!savingsAccount.getCurrency().getCode().equals(requestCurrency.getCode())) {
throw new UnsupportedOperationException("Account and request has different currencies!"); throw new HttpClientErrorException(HttpStatus.CONFLICT, "Account and request has different currencies!");
} }
SavingsAccountTransactionType transactionType = request.getTransactionRole().getTransactionType(); SavingsAccountTransactionType transactionType = request.getTransactionRole().getTransactionType();
if (!savingsAccount.isTransactionAllowed(transactionType, request.getExpirationLocalDate())) { if (!savingsAccount.isTransactionAllowed(transactionType, request.getExpirationLocalDate())) {
throw new UnsupportedOperationException("Transaction not allowed on account!"); throw new HttpClientErrorException(HttpStatus.NOT_ACCEPTABLE, "Transaction not allowed on account!");
} }
request.normalizeAmounts(savingsAccount.getCurrency()); request.normalizeAmounts(savingsAccount.getCurrency());