All Downloads are FREE. Search and download functionalities are using the official Maven repository.

au.org.consumerdatastandards.holder.api.banking.BankingScheduledPaymentsApiController Maven / Gradle / Ivy

The newest version!
package au.org.consumerdatastandards.holder.api.banking;

import au.org.consumerdatastandards.holder.api.ApiControllerBase;
import au.org.consumerdatastandards.holder.model.banking.BankingProductCategory;
import au.org.consumerdatastandards.holder.model.banking.BankingScheduledPayment;
import au.org.consumerdatastandards.holder.model.banking.ParamAccountOpenStatus;
import au.org.consumerdatastandards.holder.model.banking.ParamProductCategory;
import au.org.consumerdatastandards.holder.model.banking.RequestAccountIds;
import au.org.consumerdatastandards.holder.model.banking.ResponseBankingScheduledPaymentsList;
import au.org.consumerdatastandards.holder.model.banking.ResponseBankingScheduledPaymentsListData;
import au.org.consumerdatastandards.holder.service.banking.BankingScheduledPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.NativeWebRequest;

import java.util.Date;
import java.util.Optional;
import java.util.UUID;

@Validated
@Controller
@CrossOrigin(allowedHeaders = "*")
@RequestMapping("${openapi.consumerDataStandards.base-path:/cds-au/v1}")
public class BankingScheduledPaymentsApiController extends ApiControllerBase implements BankingScheduledPaymentsApi {

    private final BankingScheduledPaymentService scheduledPaymentService;
    private final NativeWebRequest request;

    @Autowired
    public BankingScheduledPaymentsApiController(NativeWebRequest request, BankingScheduledPaymentService scheduledPaymentService) {
        this.request = request;
        this.scheduledPaymentService = scheduledPaymentService;
    }

    @Override
    public Optional getRequest() {
        return Optional.ofNullable(request);
    }

    public ResponseEntity listScheduledPayments(String accountId,
                                                                                      Integer page,
                                                                                      Integer pageSize,
                                                                                      String xCdsClientHeaders,
                                                                                      Date xFapiAuthDate,
                                                                                      String xFapiCustomerIpAddress,
                                                                                      UUID xFapiInteractionId,
                                                                                      Integer xMinV,
                                                                                      Integer xV) {
        int supportedVersion = validateHeaders(xCdsClientHeaders, xFapiCustomerIpAddress, xFapiInteractionId, xMinV, xV, 2);
        validatePageSize(pageSize, xFapiInteractionId);
        HttpHeaders headers = generateResponseHeaders(xFapiInteractionId, supportedVersion);
        Integer actualPage = getPagingValue(page, 1);
        Integer actualPageSize = getPagingValue(pageSize, 25);
        ResponseBankingScheduledPaymentsListData listData = new ResponseBankingScheduledPaymentsListData();
        Page scheduledPaymentPage
            = scheduledPaymentService.getBankingScheduledPayments(accountId, PageRequest.of(actualPage - 1, actualPageSize), supportedVersion);
        validatePageRange(actualPage, scheduledPaymentPage.getTotalPages(), xFapiInteractionId);
        return getResponse(headers, actualPage, actualPageSize, listData, scheduledPaymentPage);
    }

    @Override
    public ResponseEntity listScheduledPaymentsBulk(ParamProductCategory productCategory,
                                                                                          ParamAccountOpenStatus openStatus,
                                                                                          Boolean isOwned,
                                                                                          Integer page,
                                                                                          Integer pageSize,
                                                                                          String xCdsClientHeaders,
                                                                                          Date xFapiAuthDate,
                                                                                          String xFapiCustomerIpAddress,
                                                                                          UUID xFapiInteractionId,
                                                                                          Integer xMinV,
                                                                                          Integer xV) {
        int supportedVersion = validateHeaders(xCdsClientHeaders, xFapiCustomerIpAddress, xFapiInteractionId, xMinV, xV, 2);
        validatePageSize(pageSize, xFapiInteractionId);
        HttpHeaders headers = generateResponseHeaders(xFapiInteractionId, supportedVersion);
        Integer actualPage = getPagingValue(page, 1);
        Integer actualPageSize = getPagingValue(pageSize, 25);
        ResponseBankingScheduledPaymentsListData listData = new ResponseBankingScheduledPaymentsListData();
        Page scheduledPaymentPage
            = scheduledPaymentService.getBankingScheduledPayments(
                    productCategory == null ? null : BankingProductCategory.valueOf(productCategory.name()),
                    openStatus, isOwned, PageRequest.of(actualPage - 1, actualPageSize), supportedVersion);
        validatePageRange(actualPage, scheduledPaymentPage.getTotalPages(), xFapiInteractionId);
        return getResponse(headers, actualPage, actualPageSize, listData, scheduledPaymentPage);
    }

    public ResponseEntity listScheduledPaymentsSpecificAccounts(RequestAccountIds accountIds,
                                                                                                      Integer page,
                                                                                                      Integer pageSize,
                                                                                                      String xCdsClientHeaders,
                                                                                                      Date xFapiAuthDate,
                                                                                                      String xFapiCustomerIpAddress,
                                                                                                      UUID xFapiInteractionId,
                                                                                                      Integer xMinV,
                                                                                                      Integer xV) {
        int supportedVersion = validateHeaders(xCdsClientHeaders, xFapiCustomerIpAddress, xFapiInteractionId, xMinV, xV, 2);
        validatePageSize(pageSize, xFapiInteractionId);
        HttpHeaders headers = generateResponseHeaders(xFapiInteractionId, supportedVersion);
        Integer actualPage = getPagingValue(page, 1);
        Integer actualPageSize = getPagingValue(pageSize, 25);
        ResponseBankingScheduledPaymentsListData listData = new ResponseBankingScheduledPaymentsListData();
        Page scheduledPaymentPage
            = scheduledPaymentService.getBankingScheduledPayments(
                accountIds.getData().getAccountIds(), PageRequest.of(actualPage - 1, actualPageSize), supportedVersion);
        validatePageRange(actualPage, scheduledPaymentPage.getTotalPages(), xFapiInteractionId);
        return getResponse(headers, actualPage, actualPageSize, listData, scheduledPaymentPage);
    }

    private ResponseEntity getResponse(HttpHeaders headers, Integer actualPage, Integer actualPageSize, ResponseBankingScheduledPaymentsListData listData, Page scheduledPaymentPage) {
        listData.setScheduledPayments(scheduledPaymentPage.getContent());
        ResponseBankingScheduledPaymentsList responseBankingScheduledPaymentsList = new ResponseBankingScheduledPaymentsList();
        responseBankingScheduledPaymentsList.setData(listData);
        responseBankingScheduledPaymentsList.setLinks(getLinkData(request, scheduledPaymentPage, actualPage, actualPageSize));
        responseBankingScheduledPaymentsList.setMeta(getMetaData(scheduledPaymentPage));
        return new ResponseEntity<>(responseBankingScheduledPaymentsList, headers, HttpStatus.OK);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy