Skip to content
Permalink
Browse files
added validation to prevent negative account balance if charges excee…
…d deposited amount
  • Loading branch information
mgeiss committed Oct 19, 2017
1 parent fa44fe0 commit 3b862591f3c9056432e104af36f62c9869a00400
Showing 2 changed files with 27 additions and 7 deletions.
@@ -530,6 +530,7 @@ public void shouldNotReopenAccountClosed() throws Exception {

final Account account = new Account();
account.setState(Account.State.OPEN.name());
account.setBalance(2000.00D);
Mockito.doAnswer(invocation -> Optional.of(account))
.when(super.accountingServiceSpy).findAccount(openAccountTransaction.getCustomerAccountIdentifier());
Mockito.doAnswer(invocation -> Collections.emptyList())
@@ -220,7 +220,8 @@ ResponseEntity<Void> confirm(@PathVariable("tellerCode") final String tellerCode
.orElseThrow(() -> ServiceException.notFound("Transaction {0} not found.", tellerTransactionIdentifier));

this.verifyAccounts(tellerTransaction);
this.verifyWithdrawalTransaction(tellerTransactionIdentifier, confirmTellerTransactionCommand);
this.verifyDepositTransaction(tellerTransaction, confirmTellerTransactionCommand);
this.verifyWithdrawalTransaction(tellerTransaction, confirmTellerTransactionCommand);

this.commandGateway.process(confirmTellerTransactionCommand);
break;
@@ -288,13 +289,8 @@ private void verifyAccount(final String accountIdentifier) {
}
}

private void verifyWithdrawalTransaction(final String tellerTransactionIdentifier,
private void verifyWithdrawalTransaction(final TellerTransaction tellerTransaction,
final ConfirmTellerTransactionCommand confirmTellerTransactionCommand) {

final TellerTransaction tellerTransaction =
this.tellerOperationService.getTellerTransaction(tellerTransactionIdentifier)
.orElseThrow(() -> ServiceException.notFound("Transaction {0} not found.", tellerTransactionIdentifier));

final String transactionType = tellerTransaction.getTransactionType();

if (transactionType.equals(ServiceConstants.TX_ACCOUNT_TRANSFER)
@@ -325,6 +321,29 @@ private void verifyWithdrawalTransaction(final String tellerTransactionIdentifie
}
}

private void verifyDepositTransaction(final TellerTransaction tellerTransaction,
final ConfirmTellerTransactionCommand confirmTellerTransactionCommand) {
final String transactionType = tellerTransaction.getTransactionType();

if (transactionType.equals(ServiceConstants.TX_CASH_DEPOSIT)
|| transactionType.equals(ServiceConstants.TX_OPEN_ACCOUNT)) {

final Account account = this.accountingService.findAccount(tellerTransaction.getCustomerAccountIdentifier())
.orElseThrow(() -> ServiceException.notFound("Customer account {0} not found.",
tellerTransaction.getCustomerAccountIdentifier()));

final BigDecimal newBalance = BigDecimal.valueOf(account.getBalance()).add(tellerTransaction.getAmount());

final TellerTransactionCosts tellerTransactionCosts =
this.tellerTransactionProcessor.getCosts(tellerTransaction);

if (!confirmTellerTransactionCommand.chargesIncluded() &&
tellerTransactionCosts.getTotalAmount().compareTo(newBalance) > 0) {
throw ServiceException.conflict("Account has not enough balance.");
}
}
}

private void verifyTellerTransaction(final Teller teller, final TellerTransaction tellerTransaction) {
final String transactionType = tellerTransaction.getTransactionType();
final BigDecimal transactionAmount = tellerTransaction.getAmount();

0 comments on commit 3b86259

Please sign in to comment.