Skip to content

Commit

Permalink
FINERACT-1760: savings account externalId support
Browse files Browse the repository at this point in the history
  • Loading branch information
abraham-menyhart authored and adamsaghy committed Sep 13, 2023
1 parent 6d3357e commit 781f3ae
Show file tree
Hide file tree
Showing 15 changed files with 508 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ public static InteropAccountData build(SavingsAccount account) {
SavingsProduct product = account.savingsProduct();
SavingsAccountSubStatusEnum subStatus = SavingsAccountSubStatusEnum.fromInt(account.getSubStatus());

return new InteropAccountData(account.getExternalId(), product.getId().toString(), product.getName(), product.getShortName(),
account.getCurrency().getCode(), account.getAccountBalance(), account.getWithdrawableBalance(), account.getStatus(),
subStatus, account.getAccountType(), account.depositAccountType(), account.getActivationLocalDate(),
return new InteropAccountData(account.getExternalId().getValue(), product.getId().toString(), product.getName(),
product.getShortName(), account.getCurrency().getCode(), account.getAccountBalance(), account.getWithdrawableBalance(),
account.getStatus(), subStatus, account.getAccountType(), account.depositAccountType(), account.getActivationLocalDate(),
calcStatusUpdateOn(account), account.getWithdrawnOnDate(), account.retrieveLastTransactionDate(), ids,
account.getClient().getId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static InteropTransactionData build(SavingsAccountTransaction transaction
sb.append(SavingsEnumerations.transactionType(transactionType).getValue());
}

return new InteropTransactionData(savingsAccount.getId(), savingsAccount.getExternalId(), transactionId, transactionType, amount,
chargeAmount, currency, runningBalance, bookingDateTime, valueDateTime, sb.toString());
return new InteropTransactionData(savingsAccount.getId(), savingsAccount.getExternalId().getValue(), transactionId, transactionType,
amount, chargeAmount, currency, runningBalance, bookingDateTime, valueDateTime, sb.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.apache.fineract.infrastructure.core.exception.PlatformDataIntegrityException;
import org.apache.fineract.infrastructure.core.serialization.DefaultToApiJsonSerializer;
import org.apache.fineract.infrastructure.core.service.DateUtils;
import org.apache.fineract.infrastructure.core.service.ExternalIdFactory;
import org.apache.fineract.infrastructure.core.service.MathUtil;
import org.apache.fineract.infrastructure.core.service.database.DatabaseSpecificSQLGenerator;
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
Expand Down Expand Up @@ -267,7 +268,7 @@ public InteropIdentifierAccountResponseData getAccountByIdentifier(@NotNull Inte
throw new InteropAccountNotFoundException(idType, idValue, subIdOrType);
}

return InteropIdentifierAccountResponseData.build(identifier.getId(), identifier.getAccount().getExternalId());
return InteropIdentifierAccountResponseData.build(identifier.getId(), identifier.getAccount().getExternalId().getValue());
}

@NotNull
Expand All @@ -288,7 +289,7 @@ public InteropIdentifierAccountResponseData registerAccountIdentifier(@NotNull I

identifierRepository.saveAndFlush(identifier);

return InteropIdentifierAccountResponseData.build(identifier.getId(), savingsAccount.getExternalId());
return InteropIdentifierAccountResponseData.build(identifier.getId(), savingsAccount.getExternalId().getValue());
} catch (final JpaSystemException | DataIntegrityViolationException dve) {
handleInteropDataIntegrityIssues(idType, request.getAccountId(), dve.getMostSpecificCause(), dve);
return InteropIdentifierAccountResponseData.empty();
Expand All @@ -309,7 +310,7 @@ public InteropIdentifierAccountResponseData deleteAccountIdentifier(@NotNull Int
throw new InteropAccountNotFoundException(idType, idValue, subIdOrType);
}

String accountId = identifier.getAccount().getExternalId();
String accountId = identifier.getAccount().getExternalId().getValue();
Long id = identifier.getId();

identifierRepository.delete(identifier);
Expand Down Expand Up @@ -356,8 +357,8 @@ public InteropQuoteResponseData createQuote(@NotNull JsonCommand command) {
if (transactionType.isDebit()) {
fee = savingsAccount.calculateWithdrawalFee(request.getAmount().getAmount());
if (MathUtil.isLessThan(savingsAccount.getWithdrawableBalance(), request.getAmount().getAmount().add(fee))) {
throw new InsufficientAccountBalanceException(savingsAccount.getExternalId(), savingsAccount.getWithdrawableBalance(), fee,
request.getAmount().getAmount());
throw new InsufficientAccountBalanceException(savingsAccount.getExternalId().getValue(),
savingsAccount.getWithdrawableBalance(), fee, request.getAmount().getAmount());
}
} else {
fee = BigDecimal.ZERO;
Expand Down Expand Up @@ -390,15 +391,15 @@ public InteropTransferResponseData prepareTransfer(@NotNull JsonCommand command)
BigDecimal total = calculateTotalTransferAmount(request, savingsAccount);

if (MathUtil.isLessThan(savingsAccount.getWithdrawableBalance(), total)) {
throw new InsufficientAccountBalanceException(savingsAccount.getExternalId(), savingsAccount.getWithdrawableBalance(), null,
total);
throw new InsufficientAccountBalanceException(savingsAccount.getExternalId().getValue(),
savingsAccount.getWithdrawableBalance(), null, total);
}
if (findTransaction(savingsAccount, transferCode, AMOUNT_HOLD.getValue()) != null) {
throw new InteropTransferAlreadyOnHoldException(savingsAccount.getExternalId(), transferCode);
throw new InteropTransferAlreadyOnHoldException(savingsAccount.getExternalId().getValue(), transferCode);
}

PaymentDetail paymentDetail = instance(findPaymentType(), savingsAccount.getExternalId(), null, getRoutingCode(), transferCode,
null);
PaymentDetail paymentDetail = instance(findPaymentType(), savingsAccount.getExternalId().getValue(), null, getRoutingCode(),
transferCode, null);
SavingsAccountTransaction holdTransaction = SavingsAccountTransaction.holdAmount(savingsAccount, savingsAccount.office(),
paymentDetail, transactionDate, Money.of(savingsAccount.getCurrency(), total), DateUtils.getLocalDateTimeOfTenant(),
getLoginUser(), false);
Expand Down Expand Up @@ -427,7 +428,7 @@ public InteropTransferResponseData commitTransfer(@NotNull JsonCommand command)
String transferCode = request.getTransferCode();

if (findTransaction(savingsAccount, transferCode, (isDebit ? WITHDRAWAL : DEPOSIT).getValue()) != null) {
throw new InteropTransferAlreadyCommittedException(savingsAccount.getExternalId(), transferCode);
throw new InteropTransferAlreadyCommittedException(savingsAccount.getExternalId().getValue(), transferCode);
}

LocalDateTime transactionDateTime = DateUtils.getLocalDateTimeOfTenant();
Expand All @@ -439,17 +440,17 @@ public InteropTransferResponseData commitTransfer(@NotNull JsonCommand command)
if (isDebit) {
SavingsAccountTransaction holdTransaction = findTransaction(savingsAccount, transferCode, AMOUNT_HOLD.getValue());
if (holdTransaction == null) {
throw new InteropTransferMissingException(savingsAccount.getExternalId(), transferCode);
throw new InteropTransferMissingException(savingsAccount.getExternalId().getValue(), transferCode);
}

BigDecimal totalTransferAmount = calculateTotalTransferAmount(request, savingsAccount);
if (holdTransaction.getAmount().compareTo(totalTransferAmount) != 0) {
throw new InteropTransferMissingException(savingsAccount.getExternalId(), transferCode);
throw new InteropTransferMissingException(savingsAccount.getExternalId().getValue(), transferCode);
}

if (MathUtil.isLessThan(savingsAccount.getWithdrawableBalance().add(holdTransaction.getAmount()), totalTransferAmount)) {
throw new InsufficientAccountBalanceException(savingsAccount.getExternalId(), savingsAccount.getWithdrawableBalance(), null,
totalTransferAmount);
throw new InsufficientAccountBalanceException(savingsAccount.getExternalId().getValue(),
savingsAccount.getWithdrawableBalance(), null, totalTransferAmount);
}

if (holdTransaction.getReleaseIdOfHoldAmountTransaction() == null) {
Expand All @@ -464,12 +465,12 @@ public InteropTransferResponseData commitTransfer(@NotNull JsonCommand command)

SavingsTransactionBooleanValues transactionValues = new SavingsTransactionBooleanValues(false, true, true, false, false);
transaction = savingsAccountService.handleWithdrawal(savingsAccount, fmt, transactionDate, request.getAmount().getAmount(),
instance(findPaymentType(), savingsAccount.getExternalId(), null, getRoutingCode(), transferCode, null),
instance(findPaymentType(), savingsAccount.getExternalId().getValue(), null, getRoutingCode(), transferCode, null),
transactionValues, backdatedTxnsAllowedTill);
} else {
transaction = savingsAccountService.handleDeposit(savingsAccount, fmt, transactionDate, request.getAmount().getAmount(),
instance(findPaymentType(), savingsAccount.getExternalId(), null, getRoutingCode(), transferCode, null), false, true,
backdatedTxnsAllowedTill);
instance(findPaymentType(), savingsAccount.getExternalId().getValue(), null, getRoutingCode(), transferCode, null),
false, true, backdatedTxnsAllowedTill);
}

String note = request.getNote();
Expand Down Expand Up @@ -506,7 +507,7 @@ public InteropTransferResponseData commitTransfer(@NotNull JsonCommand command)

savingsAccountRepository.save(savingsAccount);
} else {
throw new InteropTransferMissingException(savingsAccount.getExternalId(), request.getTransferCode());
throw new InteropTransferMissingException(savingsAccount.getExternalId().getValue(), request.getTransferCode());
}

return InteropTransferResponseData.build(command.commandId(), request.getTransactionCode(), InteropActionState.ACCEPTED,
Expand Down Expand Up @@ -558,7 +559,7 @@ public InteropTransferResponseData commitTransfer(@NotNull JsonCommand command)
}

private SavingsAccount validateAndGetSavingAccount(String accountId) {
SavingsAccount savingsAccount = savingsAccountRepository.findByExternalId(accountId);
SavingsAccount savingsAccount = savingsAccountRepository.findByExternalId(ExternalIdFactory.produce(accountId));
if (savingsAccount == null) {
throw new SavingsAccountNotFoundException(accountId);
}
Expand Down
Loading

0 comments on commit 781f3ae

Please sign in to comment.