From ee6f2597e77bf811e3d842739e89f52a5c1af6cf Mon Sep 17 00:00:00 2001 From: Jacopo Carlini Date: Fri, 13 Dec 2024 17:35:41 +0100 Subject: [PATCH] fix(checkBrokerAllowance): resolve a issue about segregationcode and applicationcode --- .../service/impl/BrokerServiceImpl.java | 168 ++++++++++++------ 1 file changed, 113 insertions(+), 55 deletions(-) diff --git a/src/main/java/it/gov/pagopa/payment/notices/service/service/impl/BrokerServiceImpl.java b/src/main/java/it/gov/pagopa/payment/notices/service/service/impl/BrokerServiceImpl.java index 11053d25..1018680f 100644 --- a/src/main/java/it/gov/pagopa/payment/notices/service/service/impl/BrokerServiceImpl.java +++ b/src/main/java/it/gov/pagopa/payment/notices/service/service/impl/BrokerServiceImpl.java @@ -1,73 +1,131 @@ package it.gov.pagopa.payment.notices.service.service.impl; import it.gov.pagopa.payment.notices.service.client.ApiConfigClient; -import it.gov.pagopa.payment.notices.service.exception.AppError; -import it.gov.pagopa.payment.notices.service.exception.AppException; +import it.gov.pagopa.payment.notices.service.model.CreditorInstitutionView; import it.gov.pagopa.payment.notices.service.model.CreditorInstitutionsView; import it.gov.pagopa.payment.notices.service.service.BrokerService; + +import java.util.ArrayList; +import java.util.List; + +import org.jetbrains.annotations.NotNull; import org.springframework.beans.factory.annotation.Value; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; -import java.util.Objects; - @Service public class BrokerServiceImpl implements BrokerService { - private final ApiConfigClient apiConfigClient; - private final Integer pageLimit; - - public BrokerServiceImpl(ApiConfigClient apiConfigClient, - @Value("${notice.check.page.limit}") Integer pageLimit) { - this.apiConfigClient = apiConfigClient; - this.pageLimit = pageLimit; + private final ApiConfigClient apiConfigClient; + private final Integer pageLimit; + + public BrokerServiceImpl( + ApiConfigClient apiConfigClient, @Value("${notice.check.page.limit}") Integer pageLimit) { + this.apiConfigClient = apiConfigClient; + this.pageLimit = pageLimit; + } + + @Override + @Cacheable(value = "checkBrokerAllowance") + public boolean checkBrokerAllowance( + String brokerTaxCode, String targetTaxCode, String noticeCode) { + + int pageNumber = 0; + boolean isValid; + + CreditorInstitutionsView creditorInstitutionsView; + + do { + // get paged of stations + creditorInstitutionsView = + apiConfigClient.getCreditorInstitutionsAssociatedToBrokerStations( + pageLimit, pageNumber, targetTaxCode, brokerTaxCode); + + // check every station in the page + isValid = checkNoticeNumberInPage(noticeCode, creditorInstitutionsView); + + pageNumber += 1; + + // check another page is we don't find any configured station + } while (!isValid && !isPagesFinished(creditorInstitutionsView)); + + return isValid; + } + + /** + * @param noticeCode the notice code is the NAV + * @param creditorInstitutionsView the list of configured stations for the EC + * @return true if the NAV starts with a valid configuration + *

if auxDigit = null the NAV must start with (0|3 + segregationCode|applicationCode) + *

if auxDigit = 1 the NAV must start with 1 + *

if auxDigit = 2 the NAV must start with 2 + */ + private static boolean checkNoticeNumberInPage( + String noticeCode, CreditorInstitutionsView creditorInstitutionsView) { + + // check input + if (creditorInstitutionsView == null + || creditorInstitutionsView.getCreditorInstitutionList() == null + || creditorInstitutionsView.getCreditorInstitutionList().isEmpty()) { + return false; } - @Override - @Cacheable(value = "checkBrokerAllowance") - public boolean checkBrokerAllowance(String brokerTaxCode, String targetTaxCode, String noticeCode) { - - try { - - int pageNumber = 0; - - while( true ) { - - CreditorInstitutionsView creditorInstitutionsView = - apiConfigClient.getCreditorInstitutionsAssociatedToBrokerStations( - pageLimit, pageNumber, targetTaxCode, brokerTaxCode); - - if(creditorInstitutionsView == null || - creditorInstitutionsView.getCreditorInstitutionList().size() == 0) { - return false; - } - - boolean checkResult = creditorInstitutionsView.getCreditorInstitutionList() - .stream().anyMatch(item -> { - String codeToCheck = - String.valueOf(Objects.requireNonNullElse( - item.getAuxDigit(), 3)) - .concat( - String.valueOf(Objects.requireNonNullElse( - item.getProgressivo(), 0)) - ) - .concat( - String.valueOf(Objects.requireNonNullElse( - item.getSegregazione(), 0)) - ); - return noticeCode.startsWith(codeToCheck); - }); - - if(checkResult || creditorInstitutionsView.getCreditorInstitutionList().size() != pageLimit) { - return checkResult; - } - - } - - } catch (Exception e) { - throw new AppException(AppError.ERROR_ON_PT_ALLOWANCE_CHECK); + // find a configured station + return creditorInstitutionsView.getCreditorInstitutionList().stream() + .anyMatch( + item -> { + + // Note: auxDigit = null is 0 or 3 + // if auxDigit is 0 or 3 NAV must start with 3|0 + segregationCode|applicationCode + if (item.getAuxDigit() == null) { + List prefixes = getPrefixes(item); + + return prefixes.stream().anyMatch(noticeCode::startsWith); + } + + // if auxDigit = 1 the NAV must start with 1 + if (item.getAuxDigit() == 1) { + return noticeCode.startsWith("1"); + } + + // if auxDigit = 2 the NAV must start with 2 + if (item.getAuxDigit() == 2) { + return noticeCode.startsWith("2"); + } + + return false; + }); + } + +/** +* + * @param stationConfiguration the item of the page + * @return a list of valid prefixes extracted from the configuration of the station for the EC +*/ + @NotNull + private static List getPrefixes(CreditorInstitutionView stationConfiguration) { + List prefixes = new ArrayList<>(); + + if (stationConfiguration.getSegregazione() != null) { + String segregationCode = String.format("%02d", stationConfiguration.getSegregazione()); + prefixes.add("3" + segregationCode); + prefixes.add("0" + segregationCode); } - + if (stationConfiguration.getProgressivo() != null) { + String applicationCode = String.format("%02d", stationConfiguration.getProgressivo()); + prefixes.add("3" + applicationCode); + prefixes.add("0" + applicationCode); + } + return prefixes; } + /** + * @param creditorInstitutionsView the page return from ApiConfig + * @return true if the page is the last + */ + private static boolean isPagesFinished(CreditorInstitutionsView creditorInstitutionsView) { + return creditorInstitutionsView == null + || creditorInstitutionsView.getCreditorInstitutionList() == null + || creditorInstitutionsView.getCreditorInstitutionList().isEmpty(); + } }