From 5725058183bdaedf164aeae60001c56a5b3d3863 Mon Sep 17 00:00:00 2001 From: Alessio Cialini <63233981+alessio-cialini@users.noreply.github.com> Date: Wed, 10 Jul 2024 15:56:21 +0200 Subject: [PATCH] [VAS-1167] feat: add checks on amount for unamanaged cart (#132) * [VAS-1167] feat: introducing checks on amount to discard legacy cart content * [VAS-1167] feat: Updated BizEventToReceiptUtils * [VAS-1167] feat: removed variable blocking int. test * [VAS-1167] feat: updated method name for cartMod1 validation * [VAS-1167] feat: updated method name for cartMod1 validation * [VAS-1167] feat: updated method name for cartMod1 validation --- helm/values-dev.yaml | 1 - .../src/step_definitions/common.js | 2 +- performance-test/src/modules/common.js | 2 +- .../utils/BizEventToReceiptUtils.java | 23 ++++++++++++++++++ .../pdf/datastore/BizEventToReceiptTest.java | 24 +++++++++++++++++++ 5 files changed, 49 insertions(+), 3 deletions(-) diff --git a/helm/values-dev.yaml b/helm/values-dev.yaml index e21c7232..628473fb 100644 --- a/helm/values-dev.yaml +++ b/helm/values-dev.yaml @@ -106,7 +106,6 @@ microservice-chart: ECOMMERCE_FILTER_ENABLED: "true" ENABLE_CART: "true" AUTHENTICATED_CHANNELS: "IO" - UNWANTED_REMITTANCE_INFO: "pagamento multibeneficiario,pagamento bpay" envFieldRef: APP_NAME: "metadata.labels['app.kubernetes.io/instance']" APP_VERSION: "metadata.labels['app.kubernetes.io/version']" diff --git a/integration-test/src/step_definitions/common.js b/integration-test/src/step_definitions/common.js index d5600fca..15a16448 100644 --- a/integration-test/src/step_definitions/common.js +++ b/integration-test/src/step_definitions/common.js @@ -112,7 +112,7 @@ function createEvent(id, transactionId, totalNotice) { "idTransaction": "123456", "transactionId": transactionId ? transactionId : "123456", "grandTotal": 0, - "amount": 0, + "amount": 1000, "fee": 0 } }, diff --git a/performance-test/src/modules/common.js b/performance-test/src/modules/common.js index 515ec06c..5b4890aa 100644 --- a/performance-test/src/modules/common.js +++ b/performance-test/src/modules/common.js @@ -108,7 +108,7 @@ export function createEvent(id, customCF) { "idTransaction": "123456", "transactionId": "123456", "grandTotal": 0, - "amount": 0, + "amount": 1000, "fee": 0 } }, diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/datastore/utils/BizEventToReceiptUtils.java b/src/main/java/it/gov/pagopa/receipt/pdf/datastore/utils/BizEventToReceiptUtils.java index 890aa1cc..45459719 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/datastore/utils/BizEventToReceiptUtils.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/datastore/utils/BizEventToReceiptUtils.java @@ -111,6 +111,13 @@ public static boolean isBizEventInvalid(BizEvent bizEvent, ExecutionContext cont return true; } + if (!isCartMod1(bizEvent)) { + logger.debug("[{}] event with id {} contain either an invalid amount value," + + " or it is a legacy cart element", + context.getFunctionName(), bizEvent.getId()); + return true; + } + try { Receipt receipt = service.getReceipt(bizEvent.getId()); logger.debug("[{}] event with id {} discarded because already processed, receipt already exist with id {}", @@ -298,6 +305,22 @@ public static boolean isValidFiscalCode(String fiscalCode) { return false; } + /** + * Method to check if the content comes from a legacy cart model (see https://pagopa.atlassian.net/browse/VAS-1167) + * @param bizEvent bizEvent to validate + * @return flag to determine if it is a manageable cart, or otherwise, will return false if + * it is considered a legacy cart content (not having a totalNotice field and having amount values != 0) + */ + public static boolean isCartMod1(BizEvent bizEvent) { + if (bizEvent.getPaymentInfo() != null && bizEvent.getPaymentInfo().getTotalNotice() == null) { + return bizEvent.getTransactionDetails() != null && + new BigDecimal(bizEvent.getPaymentInfo().getAmount()).subtract( + formatEuroCentAmount(bizEvent.getTransactionDetails().getTransaction().getAmount())) + .intValue() == 0; + } + return true; + } + public static boolean isValidChannelOrigin(BizEvent bizEvent) { if (bizEvent.getTransactionDetails() != null) { diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/datastore/BizEventToReceiptTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/datastore/BizEventToReceiptTest.java index f5ed53d2..9f960d74 100644 --- a/src/test/java/it/gov/pagopa/receipt/pdf/datastore/BizEventToReceiptTest.java +++ b/src/test/java/it/gov/pagopa/receipt/pdf/datastore/BizEventToReceiptTest.java @@ -508,6 +508,24 @@ void bizEventNotProcessedCartNotEnabled() throws CartNotFoundException { verify(cartReceiptsCosmosClient, never()).saveCart(any()); } + @Test + void runDiscardedWithInvalidCartAmounts() { + List bizEventItems = new ArrayList<>(); + BizEvent bizEvent = generateValidBizEvent(null); + bizEvent.getTransactionDetails().getTransaction().setAmount(100); + bizEventItems.add(bizEvent); + + @SuppressWarnings("unchecked") + OutputBinding> documentdb = (OutputBinding>) spy(OutputBinding.class); + BizEventToReceiptServiceImpl receiptService = new BizEventToReceiptServiceImpl( + pdvTokenizerServiceMock, receiptCosmosClient, cartReceiptsCosmosClient, bizEventCosmosClientMock, queueClient); + function = new BizEventToReceipt(receiptService); + // test execution + assertDoesNotThrow(() -> function.processBizEventToReceipt(bizEventItems, documentdb, context)); + + verify(documentdb, never()).setValue(any()); + } + private BizEvent generateValidBizEvent(String totalNotice){ BizEvent item = new BizEvent(); @@ -520,10 +538,12 @@ private BizEvent generateValidBizEvent(String totalNotice){ Transaction transaction = new Transaction(); transaction.setCreationDate(String.valueOf(LocalDateTime.now())); transaction.setOrigin(VALID_IO_CHANNEL); + transaction.setAmount(10000); transactionDetails.setTransaction(transaction); PaymentInfo paymentInfo = new PaymentInfo(); paymentInfo.setTotalNotice(totalNotice); + paymentInfo.setAmount("100.0"); item.setEventStatus(BizEventStatusType.DONE); item.setId(EVENT_ID); @@ -545,9 +565,11 @@ private BizEvent generateValidBizEventWithTDetails(String totalNotice){ Transaction transaction = new Transaction(); transaction.setCreationDate(String.valueOf(LocalDateTime.now())); transaction.setOrigin(VALID_IO_CHANNEL); + transaction.setAmount(10000); transactionDetails.setTransaction(transaction); transactionDetails.setUser(User.builder().fiscalCode(PAYER_FISCAL_CODE).build()); + PaymentInfo paymentInfo = new PaymentInfo(); paymentInfo.setTotalNotice(totalNotice); @@ -572,10 +594,12 @@ private BizEvent generateAnonymDebtorBizEvent(String totalNotice){ Transaction transaction = new Transaction(); transaction.setCreationDate(String.valueOf(LocalDateTime.now())); transaction.setOrigin(VALID_IO_CHANNEL); + transaction.setAmount(10000); transactionDetails.setTransaction(transaction); PaymentInfo paymentInfo = new PaymentInfo(); paymentInfo.setTotalNotice(totalNotice); + paymentInfo.setAmount("100"); item.setEventStatus(BizEventStatusType.DONE); item.setId(EVENT_ID);