Search in sources :

Example 1 with FRBalanceType

use of com.forgerock.openbanking.common.model.openbanking.domain.account.common.FRBalanceType in project openbanking-aspsp by OpenBankingToolkit.

the class DataUpdater method updateBalances.

void updateBalances(FRAccountData accountDataDiff, Set<String> accountIds) {
    // Balance
    List<FRBalance> balancesToSave = new ArrayList<>();
    List<FRBalance> newBalancesToSave = new ArrayList<>();
    Set<FRBalanceType> balanceTypes = new HashSet<>();
    for (OBCashBalance1 obBalanceDiff : accountDataDiff.getBalances()) {
        String accountId = accountDataDiff.getAccount().getAccountId();
        if (obBalanceDiff.getAccountId() != null && !obBalanceDiff.getAccountId().equals(accountId)) {
            throw new ResponseStatusException(HttpStatus.UNAUTHORIZED, String.format("The account id '%s' refers in a balance doesn't match the main account id '%s'", obBalanceDiff.getAccountId(), accountId));
        }
        FRCashBalance frCashBalanceDiff = toFRCashBalance(obBalanceDiff);
        FRBalanceType balanceType = frCashBalanceDiff.getType();
        if (balanceTypes.contains(balanceType)) {
            throw new ResponseStatusException(HttpStatus.BAD_REQUEST, String.format("You can't add two balances of the same type '%s' for the same account ID '%s'", balanceType, accountId));
        }
        balanceTypes.add(balanceType);
        Optional<FRBalance> isBalance = balanceRepository.findByAccountIdAndBalanceType(accountId, balanceType);
        if (isBalance.isPresent()) {
            FRCashBalance balance = isBalance.get().getBalance();
            if (frCashBalanceDiff.getAmount() != null) {
                balance.setAmount(frCashBalanceDiff.getAmount());
            }
            if (balanceType != null) {
                balance.setType(balanceType);
            }
            if (frCashBalanceDiff.getCreditDebitIndicator() != null) {
                balance.setCreditDebitIndicator(frCashBalanceDiff.getCreditDebitIndicator());
            }
            if (frCashBalanceDiff.getCreditLines() != null) {
                balance.setCreditLines(frCashBalanceDiff.getCreditLines());
            }
            if (frCashBalanceDiff.getDateTime() != null) {
                balance.setDateTime(frCashBalanceDiff.getDateTime());
            }
            balancesToSave.add(isBalance.get());
        } else {
            FRBalance balance1 = new FRBalance();
            balance1.setBalance(frCashBalanceDiff);
            balance1.setAccountId(accountId);
            newBalancesToSave.add(balance1);
        }
    }
    if (balanceRepository.countByAccountIdIn(accountIds) + newBalancesToSave.size() > documentLimit) {
        throw new ResponseStatusException(HttpStatus.PAYLOAD_TOO_LARGE, String.format("Cannot add balances as it has exceeded maximum limit of %s", documentLimit));
    }
    List<FRBalance> allBalances = ImmutableList.<FRBalance>builder().addAll(balancesToSave).addAll(newBalancesToSave).build();
    balanceRepository.saveAll(allBalances);
}
Also used : FRBalanceType(com.forgerock.openbanking.common.model.openbanking.domain.account.common.FRBalanceType) ResponseStatusException(org.springframework.web.server.ResponseStatusException) FRCashBalanceConverter.toFRCashBalance(com.forgerock.openbanking.common.services.openbanking.converter.account.FRCashBalanceConverter.toFRCashBalance)

Aggregations

FRBalanceType (com.forgerock.openbanking.common.model.openbanking.domain.account.common.FRBalanceType)1 FRCashBalanceConverter.toFRCashBalance (com.forgerock.openbanking.common.services.openbanking.converter.account.FRCashBalanceConverter.toFRCashBalance)1 ResponseStatusException (org.springframework.web.server.ResponseStatusException)1