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 f752030..a489661 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 @@ -107,7 +107,8 @@ public String getTransactionCreationDate(BizEvent bizEvent) { public void tokenizeFiscalCodes(BizEvent bizEvent, Receipt receipt, EventData eventData) throws JsonProcessingException, PDVTokenizerException { try { if (bizEvent.getDebtor() != null && bizEvent.getDebtor().getEntityUniqueIdentifierValue() != null) { - eventData.setDebtorFiscalCode( + eventData.setDebtorFiscalCode("ANONIMO".equals(bizEvent.getDebtor().getEntityUniqueIdentifierValue()) ? + bizEvent.getDebtor().getEntityUniqueIdentifierValue() : pdvTokenizerService.generateTokenForFiscalCodeWithRetry(bizEvent.getDebtor().getEntityUniqueIdentifierValue()) ); } diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/BuildTemplateServiceImpl.java b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/BuildTemplateServiceImpl.java index f800471..119631e 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/BuildTemplateServiceImpl.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/BuildTemplateServiceImpl.java @@ -104,10 +104,12 @@ public ReceiptPDFTemplate buildTemplate(BizEvent bizEvent, boolean isGeneratingD .type(getRefNumberType(bizEvent)) .value(getRefNumberValue(bizEvent)) .build()) - .debtor(Debtor.builder() - .fullName(getDebtorFullName(bizEvent)) - .taxCode(getDebtorTaxCode(bizEvent)) - .build()) + .debtor("ANONIMO".equals(receipt.getEventData().getDebtorFiscalCode()) ? + null : + Debtor.builder() + .fullName(getDebtorFullName(bizEvent)) + .taxCode(getDebtorTaxCode(bizEvent)) + .build()) .payee(Payee.builder() .name(getPayeeName(bizEvent)) .taxCode(getPayeeTaxCode(bizEvent)) diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/GenerateReceiptPdfServiceImpl.java b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/GenerateReceiptPdfServiceImpl.java index a5fc083..9ce925e 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/GenerateReceiptPdfServiceImpl.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/GenerateReceiptPdfServiceImpl.java @@ -83,11 +83,12 @@ public PdfGeneration generateReceipts(Receipt receipt, BizEvent bizEvent, Path w } //Generate debtor's partial PDF - PdfMetadata generationResult = generateAndSavePDFReceipt(bizEvent, receipt, receipt.getMdAttach().getName(), true, workingDirPath); - pdfGeneration.setDebtorMetadata(generationResult); - + if (!"ANONIMO".equals(debtorCF)) { + PdfMetadata generationResult = generateAndSavePDFReceipt(bizEvent, receipt, receipt.getMdAttach().getName(), true, workingDirPath); + pdfGeneration.setDebtorMetadata(generationResult); + } - return pdfGeneration; + return pdfGeneration; } /** @@ -97,17 +98,23 @@ public PdfGeneration generateReceipts(Receipt receipt, BizEvent bizEvent, Path w public boolean verifyAndUpdateReceipt(Receipt receipt, PdfGeneration pdfGeneration) throws ReceiptGenerationNotToRetryException { PdfMetadata debtorMetadata = pdfGeneration.getDebtorMetadata(); boolean result = true; - if (debtorMetadata == null) { - logger.error("Unexpected result for debtor pdf receipt generation. Receipt id {}", receipt.getId()); - return false; - } - if (pdfGeneration.isGenerateOnlyDebtor()) { - if (debtorMetadata.getStatusCode() != SC_OK) { - String errMsg = String.format("Debtor receipt generation fail with status %s", debtorMetadata.getStatusCode()); - throw new ReceiptGenerationNotToRetryException(errMsg); + if (receipt.getEventData() != null && !"ANONIMO".equals(receipt.getEventData().getDebtorFiscalCode())) { + + + if (debtorMetadata == null) { + logger.error("Unexpected result for debtor pdf receipt generation. Receipt id {}", receipt.getId()); + return false; + } + + if (pdfGeneration.isGenerateOnlyDebtor()) { + if (debtorMetadata.getStatusCode() != SC_OK) { + String errMsg = String.format("Debtor receipt generation fail with status %s", debtorMetadata.getStatusCode()); + throw new ReceiptGenerationNotToRetryException(errMsg); + } + return result; } - return result; + } PdfMetadata payerMetadata = pdfGeneration.getPayerMetadata(); @@ -119,7 +126,7 @@ public boolean verifyAndUpdateReceipt(Receipt receipt, PdfGeneration pdfGenerati if (debtorMetadata.getStatusCode() != SC_OK || payerMetadata.getStatusCode() != SC_OK) { String errMsg = String.format("Receipt generation fail for debtor (status: %s) and/or payer (status: %s)", - debtorMetadata.getStatusCode(), payerMetadata.getStatusCode()); + debtorMetadata != null ? debtorMetadata.getStatusCode() : "N/A", payerMetadata.getStatusCode()); throw new ReceiptGenerationNotToRetryException(errMsg); } return result; 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 9f3734e..8343ad8 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 @@ -176,7 +176,8 @@ public static boolean isBizEventInvalid(BizEvent bizEvent, ExecutionContext cont } if (bizEvent.getDebtor().getEntityUniqueIdentifierValue() == null || - bizEvent.getDebtor().getEntityUniqueIdentifierValue().equals("ANONIMO")) { + (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", context.getFunctionName(), bizEvent.getId()); return true; 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 fa5ba14..f85bb94 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 @@ -318,7 +318,9 @@ void runDiscardedWithEventNotDONE() throws BizEventNotFoundException { @Test void generateAnonymousDebtorBizEvent() throws BizEventNotFoundException { - when(bizEventCosmosClientMock.getBizEventDocument(EVENT_ID)).thenReturn(generateAnonymDebtorBizEvent()); + BizEvent bizEvent = generateAnonymDebtorBizEvent(); + bizEvent.setPayer(null); + when(bizEventCosmosClientMock.getBizEventDocument(EVENT_ID)).thenReturn(bizEvent); doAnswer((Answer) invocation -> { HttpStatus status = (HttpStatus) invocation.getArguments()[0]; 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 10b4546..0e485b9 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 @@ -2028,7 +2028,8 @@ void mapTemplateNoCartItemDebtorTaxCodeValueError() { .iuv(IUV) .build()) .build(); - TemplateDataMappingException e = assertThrows(TemplateDataMappingException.class, () -> buildTemplateService.buildTemplate(event, GENERATED_BY_PAYER, Receipt.builder().build())); + TemplateDataMappingException e = assertThrows(TemplateDataMappingException.class, () -> buildTemplateService.buildTemplate(event, GENERATED_BY_PAYER, + Receipt.builder().eventData(EventData.builder().debtorFiscalCode("DEBTOR_FC").build()).build())); assertEquals(ReasonErrorCode.ERROR_TEMPLATE_PDF.getCode(), e.getStatusCode()); assertEquals(String.format(TemplateDataField.ERROR_MAPPING_MESSAGE, TemplateDataField.CART_ITEM_DEBTOR_TAX_CODE), e.getMessage()); @@ -2059,7 +2060,8 @@ void mapTemplateNoCartItemPayeeTaxCodeValueError() { .entityUniqueIdentifierValue(DEBTOR_VALID_CF) .build()) .build(); - TemplateDataMappingException e = assertThrows(TemplateDataMappingException.class, () -> buildTemplateService.buildTemplate(event, GENERATED_BY_PAYER, Receipt.builder().build())); + TemplateDataMappingException e = assertThrows(TemplateDataMappingException.class, () -> buildTemplateService.buildTemplate(event, GENERATED_BY_PAYER, + Receipt.builder().eventData(EventData.builder().debtorFiscalCode("DEBTOR_FC").build()).build())); assertEquals(ReasonErrorCode.ERROR_TEMPLATE_PDF.getCode(), e.getStatusCode()); assertEquals(String.format(TemplateDataField.ERROR_MAPPING_MESSAGE, TemplateDataField.CART_ITEM_PAYEE_TAX_CODE), e.getMessage()); diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/GenerateReceiptPdfServiceImplTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/GenerateReceiptPdfServiceImplTest.java index 8e9ba4c..8a0b514 100644 --- a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/GenerateReceiptPdfServiceImplTest.java +++ b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/service/impl/GenerateReceiptPdfServiceImplTest.java @@ -285,6 +285,7 @@ void generateReceiptsPayerNullFailSaveToBlobStorageReturn500() throws Exception @Test void verifyPayerNullOrSameDebtorPayerWithSuccess() throws ReceiptGenerationNotToRetryException { Receipt receipt = buildReceiptForVerify(true, true); + receipt.setEventData(EventData.builder().debtorFiscalCode("DEBTOR_FC").build()); PdfGeneration pdfGeneration = PdfGeneration.builder() .debtorMetadata(PdfMetadata.builder() @@ -360,6 +361,7 @@ void verifyPayerNullOrSameDebtorPayerFailMetadataNull() throws ReceiptGeneration @Test void verifyPayerNullOrSameDebtorPayerAlreadyCreatedSuccess() throws ReceiptGenerationNotToRetryException { Receipt receipt = buildReceiptForVerify(true, false); + receipt.setEventData(EventData.builder().debtorFiscalCode("DEBTOR_FC").build()); PdfGeneration pdfGeneration = PdfGeneration.builder() .debtorMetadata(PdfMetadata.builder() @@ -384,6 +386,7 @@ void verifyPayerNullOrSameDebtorPayerAlreadyCreatedSuccess() throws ReceiptGener @Test void verifyPayerNullOrSameDebtorPayerFailReceiptGenerationInError() throws ReceiptGenerationNotToRetryException { Receipt receipt = buildReceiptForVerify(false, false); + receipt.setEventData(EventData.builder().debtorFiscalCode("DEBTOR_FC").build()); PdfGeneration pdfGeneration = PdfGeneration.builder() .debtorMetadata(PdfMetadata.builder() @@ -400,6 +403,7 @@ void verifyPayerNullOrSameDebtorPayerFailReceiptGenerationInError() throws Recei @Test void verifyPayerNullOrSameDebtorPayerFailThrowsReceiptGenerationNotToRetryException() { Receipt receipt = buildReceiptForVerify(false, false); + receipt.setEventData(EventData.builder().debtorFiscalCode("DEBTOR_FC").build()); PdfGeneration pdfGeneration = PdfGeneration.builder() .debtorMetadata(PdfMetadata.builder()