From 00b377b2faa62e6746e901d1501dd6be63ad1a1e Mon Sep 17 00:00:00 2001 From: Alessio Cialini Date: Mon, 22 Jan 2024 16:10:40 +0100 Subject: [PATCH] feat: add cf checks to helpdesk --- .../impl/BizEventToReceiptServiceImpl.java | 20 +++++++++++------- .../utils/BizEventToReceiptUtils.java | 21 +++++++++++++++---- .../pdf/helpdesk/RecoverFailedCartTest.java | 4 ++-- .../RecoverFailedReceiptMassiveTest.java | 4 ++-- .../RecoverFailedReceiptScheduledTest.java | 4 ++-- .../helpdesk/RecoverFailedReceiptTest.java | 4 ++-- .../impl/BuildTemplateServiceImplTest.java | 4 ++-- .../utils/BizEventToReceiptUtilsTest.java | 4 ++-- 8 files changed, 41 insertions(+), 24 deletions(-) diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/BizEventToReceiptServiceImpl.java b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/BizEventToReceiptServiceImpl.java index 11b2e964..3c52dbbb 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/BizEventToReceiptServiceImpl.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/BizEventToReceiptServiceImpl.java @@ -23,6 +23,7 @@ import it.gov.pagopa.receipt.pdf.helpdesk.exception.PdfJsonMappingException; import it.gov.pagopa.receipt.pdf.helpdesk.service.BizEventToReceiptService; import it.gov.pagopa.receipt.pdf.helpdesk.service.PDVTokenizerServiceRetryWrapper; +import it.gov.pagopa.receipt.pdf.helpdesk.utils.BizEventToReceiptUtils; import it.gov.pagopa.receipt.pdf.helpdesk.utils.ObjectMapperUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -45,6 +46,9 @@ public class BizEventToReceiptServiceImpl implements BizEventToReceiptService { private final ReceiptCosmosClient receiptCosmosClient; + public static final String FISCAL_CODE_ANONYMOUS = "ANONIMO"; + + public BizEventToReceiptServiceImpl() { this.pdvTokenizerService = new PDVTokenizerServiceRetryWrapperImpl(); this.queueClient = ReceiptQueueClientImpl.getInstance(); @@ -127,14 +131,13 @@ public String getTransactionCreationDate(BizEvent bizEvent) { @Override public void tokenizeFiscalCodes(BizEvent bizEvent, Receipt receipt, EventData eventData) throws JsonProcessingException, PDVTokenizerException { try { - if (bizEvent.getDebtor() != null && bizEvent.getDebtor().getEntityUniqueIdentifierValue() != null) { - eventData.setDebtorFiscalCode("ANONIMO".equals(bizEvent.getDebtor().getEntityUniqueIdentifierValue()) ? - bizEvent.getDebtor().getEntityUniqueIdentifierValue() : - pdvTokenizerService.generateTokenForFiscalCodeWithRetry( - bizEvent.getDebtor().getEntityUniqueIdentifierValue()) - ); - } - if (bizEvent.getPayer() != null && bizEvent.getPayer().getEntityUniqueIdentifierValue() != null + eventData.setDebtorFiscalCode( + bizEvent.getDebtor() != null && BizEventToReceiptUtils.isValidFiscalCode(bizEvent.getDebtor().getEntityUniqueIdentifierValue()) ? + pdvTokenizerService.generateTokenForFiscalCodeWithRetry(bizEvent.getDebtor().getEntityUniqueIdentifierValue()) : + FISCAL_CODE_ANONYMOUS + ); + + if (bizEvent.getPayer() != null && BizEventToReceiptUtils.isValidFiscalCode(bizEvent.getPayer().getEntityUniqueIdentifierValue()) && isFromAuthenticatedOrigin(bizEvent)) { eventData.setPayerFiscalCode( pdvTokenizerService.generateTokenForFiscalCodeWithRetry( @@ -142,6 +145,7 @@ && isFromAuthenticatedOrigin(bizEvent)) { ); } else if (bizEvent.getTransactionDetails() != null && bizEvent.getTransactionDetails().getUser() != null && bizEvent.getTransactionDetails().getUser().getFiscalCode() != null + && BizEventToReceiptUtils.isValidFiscalCode(bizEvent.getTransactionDetails().getUser().getFiscalCode()) && isFromAuthenticatedOrigin(bizEvent)) { eventData.setPayerFiscalCode( pdvTokenizerService.generateTokenForFiscalCodeWithRetry( diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/utils/BizEventToReceiptUtils.java b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/utils/BizEventToReceiptUtils.java index 4fa21f72..a777b51d 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/utils/BizEventToReceiptUtils.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/utils/BizEventToReceiptUtils.java @@ -233,10 +233,12 @@ public static boolean isBizEventInvalid(BizEvent bizEvent, ExecutionContext cont return true; } - if (bizEvent.getDebtor().getEntityUniqueIdentifierValue() == null || - (bizEvent.getDebtor().getEntityUniqueIdentifierValue().equals("ANONIMO") && - (bizEvent.getPayer() == null || bizEvent.getPayer().getEntityUniqueIdentifierValue() == null))) { - logger.debug("[{}] event with id {} discarded because debtor identifier is missing or ANONIMO", + if ( + (bizEvent.getDebtor() == null || !isValidFiscalCode(bizEvent.getDebtor().getEntityUniqueIdentifierValue())) + && + (bizEvent.getPayer() == null || !isValidFiscalCode(bizEvent.getPayer().getEntityUniqueIdentifierValue())) + ) { + logger.debug("[{}] event with id {} discarded because debtor's and payer's identifiers are missing or not valid", context.getFunctionName(), bizEvent.getId()); return true; } @@ -434,5 +436,16 @@ public static boolean isFromAuthenticatedOrigin(BizEvent bizEvent) { )); } + public static boolean isValidFiscalCode(String fiscalCode) { + if (fiscalCode != null && !fiscalCode.isEmpty()) { + Pattern patternCF = Pattern.compile("^[A-Z]{6}[0-9LMNPQRSTUV]{2}[ABCDEHLMPRST][0-9LMNPQRSTUV]{2}[A-Z][0-9LMNPQRSTUV]{3}[A-Z]$"); + Pattern patternPIVA = Pattern.compile("/^[0-9]{11}$/"); + + return patternCF.matcher(fiscalCode).find() || patternPIVA.matcher(fiscalCode).find(); + } + + return false; + } + private BizEventToReceiptUtils() {} } diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedCartTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedCartTest.java index e3aa8aa0..1540bfbd 100644 --- a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedCartTest.java +++ b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedCartTest.java @@ -50,8 +50,8 @@ @MockitoSettings(strictness = Strictness.LENIENT) class RecoverFailedCartTest { - private final String PAYER_FISCAL_CODE = "a valid payer CF"; - private final String DEBTOR_FISCAL_CODE = "a valid debtor CF"; + private final String PAYER_FISCAL_CODE = "AAAAAA00A00A000D"; + private final String DEBTOR_FISCAL_CODE = "AAAAAA00A00A000P"; private final String TOKENIZED_DEBTOR_FISCAL_CODE = "tokenizedDebtorFiscalCode"; private final String TOKENIZED_PAYER_FISCAL_CODE = "tokenizedPayerFiscalCode"; private final String EVENT_ID = "a valid id"; diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptMassiveTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptMassiveTest.java index c26735a3..efba0bcb 100644 --- a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptMassiveTest.java +++ b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptMassiveTest.java @@ -276,9 +276,9 @@ private BizEvent generateValidBizEvent(String eventId){ BizEvent item = new BizEvent(); Payer payer = new Payer(); - payer.setEntityUniqueIdentifierValue("a valid payer CF"); + payer.setEntityUniqueIdentifierValue("AAAAAA00A00A000P"); Debtor debtor = new Debtor(); - debtor.setEntityUniqueIdentifierValue("a valid debtor CF"); + debtor.setEntityUniqueIdentifierValue("AAAAAA00A00A000D"); TransactionDetails transactionDetails = new TransactionDetails(); Transaction transaction = new Transaction(); diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptScheduledTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptScheduledTest.java index 3921b697..501242f5 100644 --- a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptScheduledTest.java +++ b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptScheduledTest.java @@ -161,9 +161,9 @@ private BizEvent generateValidBizEvent(String eventId) { BizEvent item = new BizEvent(); Payer payer = new Payer(); - payer.setEntityUniqueIdentifierValue("a valid payer CF"); + payer.setEntityUniqueIdentifierValue("AAAAAA00A00A000D"); Debtor debtor = new Debtor(); - debtor.setEntityUniqueIdentifierValue("a valid debtor CF"); + debtor.setEntityUniqueIdentifierValue("AAAAAA00A00A000D"); TransactionDetails transactionDetails = new TransactionDetails(); Transaction transaction = new Transaction(); diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptTest.java index 8df78604..9ef8941b 100644 --- a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptTest.java +++ b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverFailedReceiptTest.java @@ -48,8 +48,8 @@ @ExtendWith(MockitoExtension.class) class RecoverFailedReceiptTest { - private final String PAYER_FISCAL_CODE = "a valid payer CF"; - private final String DEBTOR_FISCAL_CODE = "a valid debtor CF"; + private final String PAYER_FISCAL_CODE = "AAAAAA00A00A000D"; + private final String DEBTOR_FISCAL_CODE = "AAAAAA00A00A000P"; private final String TOKENIZED_DEBTOR_FISCAL_CODE = "tokenizedDebtorFiscalCode"; private final String TOKENIZED_PAYER_FISCAL_CODE = "tokenizedPayerFiscalCode"; private final String EVENT_ID = "a valid id"; diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/BuildTemplateServiceImplTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/BuildTemplateServiceImplTest.java index 7d892b84..15e535e3 100644 --- a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/BuildTemplateServiceImplTest.java +++ b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/BuildTemplateServiceImplTest.java @@ -27,9 +27,9 @@ class BuildTemplateServiceImplTest { public static final String DEBTOR_FULL_NAME_INVALID = "-- --"; public static final String DEBTOR_FULL_NAME_SPECIAL_CHAR = "John,Doe:Megacorp;SRL::Avenue;Street/"; public static final String DEBTOR_FULL_NAME_SPECIAL_CHAR_FORMATTED = "John Doe Megacorp SRL Avenue Street "; - public static final String DEBTOR_VALID_CF = "CF_DEBTOR"; + public static final String DEBTOR_VALID_CF = "AAAAAA00A00A000D"; public static final String PAYER_FULL_NAME = "John Doe PAYER"; - public static final String PAYER_VALID_CF = "CF_PAYER"; + public static final String PAYER_VALID_CF = "AAAAAA00A00A000P"; public static final String HOLDER_FULL_NAME = "John Doe HOLDER"; public static final String PAYMENT_TOKEN = "9a9bad2caf604b86a339476373c659b0"; public static final String AMOUNT_WITHOUT_CENTS = "7000"; diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/utils/BizEventToReceiptUtilsTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/utils/BizEventToReceiptUtilsTest.java index b2ed05c1..94003d13 100644 --- a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/utils/BizEventToReceiptUtilsTest.java +++ b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/utils/BizEventToReceiptUtilsTest.java @@ -29,8 +29,8 @@ @ExtendWith(MockitoExtension.class) class BizEventToReceiptUtilsTest { private final String EVENT_ID = "a valid id"; - private final String PAYER_FISCAL_CODE = "a valid payer CF"; - private final String DEBTOR_FISCAL_CODE = "a valid debtor CF"; + private final String PAYER_FISCAL_CODE = "AAAAAA00A00A000P"; + private final String DEBTOR_FISCAL_CODE = "AAAAAA00A00A000D"; private final String TOKENIZED_DEBTOR_FISCAL_CODE = "tokenizedDebtorFiscalCode"; private final String TOKENIZED_PAYER_FISCAL_CODE = "tokenizedPayerFiscalCode"; public static final String REMITTANCE_INFORMATION_PAYMENT_INFO = "TARI 2021";