Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PRDP-304] feat: Updated checks for anonym debtor management #45

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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())
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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();
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpResponseMessage.Builder>) invocation -> {
HttpStatus status = (HttpStatus) invocation.getArguments()[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
Loading