From 79578adf41f9b75c313a3f3b9ab8e46c36d63a93 Mon Sep 17 00:00:00 2001 From: alessio-acitelli <97529051+alessio-acitelli@users.noreply.github.com> Date: Fri, 25 Oct 2024 14:17:49 +0200 Subject: [PATCH] BUG: [PAGOPA-2260] - issue cart (#98) * [PAGOPA-2260] issue cart: managed recovery via event-id * [PAGOPA-2260] issue cart: disable via event-id --------- Co-authored-by: aacitelli --- .../controller/impl/PaidNoticeController.java | 4 +- .../bizeventsservice/exception/AppError.java | 4 +- .../BizEventsViewUserRepository.java | 3 + .../service/ITransactionService.java | 2 + .../service/impl/TransactionService.java | 33 ++++++--- .../controller/PaidNoticeControllerTest.java | 4 +- .../service/TransactionServiceTest.java | 70 ++++++++++++++----- 7 files changed, 88 insertions(+), 32 deletions(-) diff --git a/src/main/java/it/gov/pagopa/bizeventsservice/controller/impl/PaidNoticeController.java b/src/main/java/it/gov/pagopa/bizeventsservice/controller/impl/PaidNoticeController.java index c5548058..309d6e1d 100644 --- a/src/main/java/it/gov/pagopa/bizeventsservice/controller/impl/PaidNoticeController.java +++ b/src/main/java/it/gov/pagopa/bizeventsservice/controller/impl/PaidNoticeController.java @@ -60,8 +60,8 @@ public ResponseEntity getPaidNotices(String fiscalCode, } @Override - public ResponseEntity disablePaidNotice(String fiscalCode, String transactionId) { - transactionService.disableTransaction(fiscalCode, transactionId); + public ResponseEntity disablePaidNotice(String fiscalCode, String eventId) { + transactionService.disablePaidNotice(fiscalCode, eventId); return new ResponseEntity<>(HttpStatus.OK); } diff --git a/src/main/java/it/gov/pagopa/bizeventsservice/exception/AppError.java b/src/main/java/it/gov/pagopa/bizeventsservice/exception/AppError.java index 2a4d6128..1f5b91b7 100644 --- a/src/main/java/it/gov/pagopa/bizeventsservice/exception/AppError.java +++ b/src/main/java/it/gov/pagopa/bizeventsservice/exception/AppError.java @@ -20,8 +20,8 @@ public enum AppError { VIEW_USER_NOT_FOUND_WITH_TAX_CODE(HttpStatus.NOT_FOUND, VIEW_USER_NOT_FOUND, "Not found a biz-events-view-user with tax code %s"), VIEW_USER_NOT_FOUND_WITH_TAX_CODE_AND_FILTER(HttpStatus.NOT_FOUND, VIEW_USER_NOT_FOUND, "Not found a biz-events-view-user with tax code %s and is_payer filter set to %s and is_debtor filter set to %s"), - VIEW_USER_NOT_FOUND_WITH_TRANSACTION_ID(HttpStatus.NOT_FOUND, VIEW_USER_NOT_FOUND, "Not found a biz-events-view-user with tax code %s and id %s"), - VIEW_GENERAL_NOT_FOUND_WITH_TRANSACTION_ID(HttpStatus.NOT_FOUND, VIEW_GENERAL_NOT_FOUND, "Not found a biz-events-view-general with id %s"), + VIEW_USER_NOT_FOUND_WITH_ID(HttpStatus.NOT_FOUND, VIEW_USER_NOT_FOUND, "Not found a biz-events-view-user with tax code %s and id %s"), + VIEW_GENERAL_NOT_FOUND_WITH_ID(HttpStatus.NOT_FOUND, VIEW_GENERAL_NOT_FOUND, "Not found a biz-events-view-general with id %s"), VIEW_CART_NOT_FOUND_WITH_TRANSACTION_ID_AND_TAX_CODE(HttpStatus.NOT_FOUND, VIEW_CART_NOT_FOUND, "Not found a biz-events-view-cart with id %s for the given tax code"), ERROR_MAPPING_BIZ_EVENT_TO_TRANSACTION_DETAIL(HttpStatus.INTERNAL_SERVER_ERROR, INVALID_DATA, "Error mapping bizEvent data to transaction details, missing property %s for bizEvent with id %s"), diff --git a/src/main/java/it/gov/pagopa/bizeventsservice/repository/BizEventsViewUserRepository.java b/src/main/java/it/gov/pagopa/bizeventsservice/repository/BizEventsViewUserRepository.java index 22ffc09b..ea7fa4be 100644 --- a/src/main/java/it/gov/pagopa/bizeventsservice/repository/BizEventsViewUserRepository.java +++ b/src/main/java/it/gov/pagopa/bizeventsservice/repository/BizEventsViewUserRepository.java @@ -24,4 +24,7 @@ public interface BizEventsViewUserRepository extends CosmosRepository getBizEventsViewUserByTaxCodeAndTransactionId(String fiscalCode, String transactionId); + + @Query("select * from c where c.taxCode = @fiscalCode and c.hidden = false and STARTSWITH(c.id, @eventId)") + List getBizEventsViewUserByTaxCodeAndId(String fiscalCode, String eventId); } diff --git a/src/main/java/it/gov/pagopa/bizeventsservice/service/ITransactionService.java b/src/main/java/it/gov/pagopa/bizeventsservice/service/ITransactionService.java index 4733bd57..d687c89a 100644 --- a/src/main/java/it/gov/pagopa/bizeventsservice/service/ITransactionService.java +++ b/src/main/java/it/gov/pagopa/bizeventsservice/service/ITransactionService.java @@ -29,4 +29,6 @@ public interface ITransactionService { ResponseEntity getPDFReceiptResponse(String fiscalCode, String eventId); void disableTransaction(String fiscalCode, String transactionId); + + void disablePaidNotice(String fiscalCode, String eventId); } diff --git a/src/main/java/it/gov/pagopa/bizeventsservice/service/impl/TransactionService.java b/src/main/java/it/gov/pagopa/bizeventsservice/service/impl/TransactionService.java index 00953282..be2df9bc 100644 --- a/src/main/java/it/gov/pagopa/bizeventsservice/service/impl/TransactionService.java +++ b/src/main/java/it/gov/pagopa/bizeventsservice/service/impl/TransactionService.java @@ -99,7 +99,7 @@ public TransactionListResponse getTransactionList(String taxCode, Boolean isPaye public TransactionDetailResponse getTransactionDetails(String taxCode, String eventReference) { List bizEventsViewGeneral = this.bizEventsViewGeneralRepository.findByTransactionId(eventReference); if (bizEventsViewGeneral.isEmpty()) { - throw new AppException(AppError.VIEW_GENERAL_NOT_FOUND_WITH_TRANSACTION_ID, eventReference); + throw new AppException(AppError.VIEW_GENERAL_NOT_FOUND_WITH_ID, eventReference); } List listOfCartViews; @@ -117,22 +117,22 @@ public TransactionDetailResponse getTransactionDetails(String taxCode, String ev @Override public NoticeDetailResponse getPaidNoticeDetail(String taxCode, String eventId) { - List bizEventsViewGeneral = this.bizEventsViewGeneralRepository.findByTransactionId(eventId); + Optional bizEventsViewGeneral = this.bizEventsViewGeneralRepository.findById(eventId); if (bizEventsViewGeneral.isEmpty()) { - throw new AppException(AppError.VIEW_GENERAL_NOT_FOUND_WITH_TRANSACTION_ID, eventId); + throw new AppException(AppError.VIEW_GENERAL_NOT_FOUND_WITH_ID, eventId); } List listOfCartViews; - if (bizEventsViewGeneral.get(0).getPayer() != null && bizEventsViewGeneral.get(0).getPayer().getTaxCode().equals(taxCode)) { - listOfCartViews = this.bizEventsViewCartRepository.getBizEventsViewCartByTransactionId(eventId); + if (bizEventsViewGeneral.get().getPayer() != null && bizEventsViewGeneral.get().getPayer().getTaxCode().equals(taxCode)) { + listOfCartViews = this.bizEventsViewCartRepository.getBizEventsViewCartByTransactionId(bizEventsViewGeneral.get().getTransactionId()); } else { - listOfCartViews = this.bizEventsViewCartRepository.getBizEventsViewCartByTransactionIdAndFilteredByTaxCode(eventId, taxCode); + listOfCartViews = this.bizEventsViewCartRepository.getBizEventsViewCartByTransactionIdAndFilteredByTaxCode(bizEventsViewGeneral.get().getTransactionId(), taxCode); } if (listOfCartViews.isEmpty()) { - throw new AppException(AppError.VIEW_CART_NOT_FOUND_WITH_TRANSACTION_ID_AND_TAX_CODE, eventId); + throw new AppException(AppError.VIEW_CART_NOT_FOUND_WITH_TRANSACTION_ID_AND_TAX_CODE, bizEventsViewGeneral.get().getTransactionId()); } - return ConvertViewsToTransactionDetailResponse.convertPaidNoticeDetails(taxCode, bizEventsViewGeneral.get(0), listOfCartViews); + return ConvertViewsToTransactionDetailResponse.convertPaidNoticeDetails(taxCode, bizEventsViewGeneral.get(), listOfCartViews); } @@ -143,13 +143,28 @@ public void disableTransaction(String fiscalCode, String transactionId) { .getBizEventsViewUserByTaxCodeAndTransactionId(fiscalCode, transactionId); if (CollectionUtils.isEmpty(listOfViewUser)) { - throw new AppException(AppError.VIEW_USER_NOT_FOUND_WITH_TRANSACTION_ID, fiscalCode, transactionId); + throw new AppException(AppError.VIEW_USER_NOT_FOUND_WITH_ID, fiscalCode, transactionId); } // PAGOPA-1831: set hidden to true for all transactions with the same transactionId for the given fiscalCode listOfViewUser.forEach(u -> u.setHidden(true)); bizEventsViewUserRepository.saveAll(listOfViewUser); } + + @Override + public void disablePaidNotice(String fiscalCode, String eventId) { + + List listOfViewUser = this.bizEventsViewUserRepository + .getBizEventsViewUserByTaxCodeAndId(fiscalCode, eventId); + + if (CollectionUtils.isEmpty(listOfViewUser)) { + throw new AppException(AppError.VIEW_USER_NOT_FOUND_WITH_ID, fiscalCode, eventId); + } + + // set hidden to true for all paid notices with the same eventId for the given fiscalCode + listOfViewUser.forEach(u -> u.setHidden(true)); + bizEventsViewUserRepository.saveAll(listOfViewUser); + } @Override public byte[] getPDFReceipt(String fiscalCode, String eventId) { diff --git a/src/test/java/it/gov/pagopa/bizeventsservice/controller/PaidNoticeControllerTest.java b/src/test/java/it/gov/pagopa/bizeventsservice/controller/PaidNoticeControllerTest.java index c31c548e..f7b33a7d 100644 --- a/src/test/java/it/gov/pagopa/bizeventsservice/controller/PaidNoticeControllerTest.java +++ b/src/test/java/it/gov/pagopa/bizeventsservice/controller/PaidNoticeControllerTest.java @@ -134,7 +134,7 @@ void getPaidNoticeDisableShouldReturnOK() throws Exception { .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andReturn(); - verify(transactionService).disableTransaction(any(), any()); + verify(transactionService).disablePaidNotice(any(), any()); } @Test @@ -149,7 +149,7 @@ void getPaidNoticeDisableWithMissingFiscalCodeShouldReturnError() throws Excepti void getPaidNoticeDisableWithInvalidFiscalCodeShouldReturnError() throws Exception { doAnswer(x -> { throw new AppException(AppError.INVALID_FISCAL_CODE, INVALID_FISCAL_CODE); - }).when(transactionService).disableTransaction(anyString(), anyString()); + }).when(transactionService).disablePaidNotice(anyString(), anyString()); mvc.perform(post(PAIDS_EVENT_ID_DISABLE_PATH) .header(FISCAL_CODE_HEADER_KEY, INVALID_FISCAL_CODE) .contentType(MediaType.APPLICATION_JSON)) diff --git a/src/test/java/it/gov/pagopa/bizeventsservice/service/TransactionServiceTest.java b/src/test/java/it/gov/pagopa/bizeventsservice/service/TransactionServiceTest.java index 69c074eb..0da1cdf0 100644 --- a/src/test/java/it/gov/pagopa/bizeventsservice/service/TransactionServiceTest.java +++ b/src/test/java/it/gov/pagopa/bizeventsservice/service/TransactionServiceTest.java @@ -163,23 +163,22 @@ void taxCodeWithEventsAndMultipleCartItemsShouldReturnTransactionList() { @Test void idAndTaxCodeWithOneEventShouldReturnNoticeDetails() { - List generalViewList = Collections.singletonList(ViewGenerator.generateBizEventsViewGeneral()); - when(bizEventsViewGeneralRepository.findByTransactionId(ViewGenerator.TRANSACTION_ID)) - .thenReturn(generalViewList); + BizEventsViewGeneral viewGeneral = ViewGenerator.generateBizEventsViewGeneral(); + when(bizEventsViewGeneralRepository.findById(ViewGenerator.EVENT_ID)) + .thenReturn(Optional.ofNullable(viewGeneral)); List listOfCartView = Collections.singletonList(ViewGenerator.generateBizEventsViewCart()); when(bizEventsViewCartRepository.getBizEventsViewCartByTransactionId(ViewGenerator.TRANSACTION_ID)) .thenReturn(listOfCartView); NoticeDetailResponse noticeDetailResponse = Assertions.assertDoesNotThrow(() -> transactionService.getPaidNoticeDetail( - ViewGenerator.USER_TAX_CODE_WITH_TX, ViewGenerator.TRANSACTION_ID)); + ViewGenerator.USER_TAX_CODE_WITH_TX, ViewGenerator.EVENT_ID)); Assertions.assertNotNull(noticeDetailResponse); - verify(bizEventsViewGeneralRepository).findByTransactionId(ViewGenerator.TRANSACTION_ID); + verify(bizEventsViewGeneralRepository).findById(ViewGenerator.EVENT_ID); verify(bizEventsViewCartRepository).getBizEventsViewCartByTransactionId(ViewGenerator.TRANSACTION_ID); verifyNoMoreInteractions(bizEventsViewGeneralRepository); verifyNoMoreInteractions(bizEventsViewCartRepository); - BizEventsViewGeneral viewGeneral = generalViewList.get(0); Assertions.assertNotNull(noticeDetailResponse); InfoNotice infoNotice = noticeDetailResponse.getInfoNotice(); Assertions.assertEquals(viewGeneral.getTransactionId(), infoNotice.getEventId()); @@ -209,6 +208,32 @@ void idAndTaxCodeWithOneEventShouldReturnNoticeDetails() { Assertions.assertEquals(viewCart.getRefNumberType(), cartItem.getRefNumberType()); Assertions.assertEquals(viewCart.getRefNumberValue(), cartItem.getRefNumberValue()); } + + @Test + void idAndTaxCodeWithNoEventShouldReturnException() { + when(bizEventsViewGeneralRepository.findById(ViewGenerator.EVENT_ID)) + .thenReturn(Optional.ofNullable(null)); + + Assertions.assertThrows(AppException.class, () -> + transactionService.getPaidNoticeDetail( + ViewGenerator.USER_TAX_CODE_WITH_TX, ViewGenerator.EVENT_ID)); + + verify(bizEventsViewGeneralRepository).findById(ViewGenerator.EVENT_ID); + + BizEventsViewGeneral viewGeneral = ViewGenerator.generateBizEventsViewGeneral(); + when(bizEventsViewGeneralRepository.findById(ViewGenerator.EVENT_ID)) + .thenReturn(Optional.ofNullable(viewGeneral)); + List listOfCartView = new ArrayList<>(); + when(bizEventsViewCartRepository.getBizEventsViewCartByTransactionId(ViewGenerator.TRANSACTION_ID)) + .thenReturn(listOfCartView); + + Assertions.assertThrows(AppException.class, () -> + transactionService.getPaidNoticeDetail( + ViewGenerator.USER_TAX_CODE_WITH_TX, ViewGenerator.EVENT_ID)); + + verify(bizEventsViewGeneralRepository, times(2)).findById(ViewGenerator.EVENT_ID); + verify(bizEventsViewCartRepository).getBizEventsViewCartByTransactionId(ViewGenerator.TRANSACTION_ID); + } @Test void idAndTaxCodeWithOneEventShouldReturnTransactionDetails() { @@ -295,18 +320,18 @@ void idAndTaxCodeWithCartEventShouldReturnTransactionDetails() { @Test void idAndTaxCodeWithCartEventShouldReturnNoticeDetails() { - List generalViewList = Collections.singletonList(ViewGenerator.generateBizEventsViewGeneral()); - when(bizEventsViewGeneralRepository.findByTransactionId(ViewGenerator.TRANSACTION_ID)) - .thenReturn(generalViewList); + BizEventsViewGeneral viewGeneral = ViewGenerator.generateBizEventsViewGeneral(); + when(bizEventsViewGeneralRepository.findById(ViewGenerator.EVENT_ID)) + .thenReturn(Optional.ofNullable(viewGeneral)); List listOfCartView = ViewGenerator.generateListOfFiveViewCart(); when(bizEventsViewCartRepository.getBizEventsViewCartByTransactionId(ViewGenerator.TRANSACTION_ID)) .thenReturn(listOfCartView); NoticeDetailResponse noticeDetailResponse = Assertions.assertDoesNotThrow(() -> transactionService.getPaidNoticeDetail( - ViewGenerator.USER_TAX_CODE_WITH_TX, ViewGenerator.TRANSACTION_ID)); + ViewGenerator.USER_TAX_CODE_WITH_TX, ViewGenerator.EVENT_ID)); Assertions.assertNotNull(noticeDetailResponse); - verify(bizEventsViewGeneralRepository).findByTransactionId(ViewGenerator.TRANSACTION_ID); + verify(bizEventsViewGeneralRepository).findById(ViewGenerator.EVENT_ID); verify(bizEventsViewCartRepository).getBizEventsViewCartByTransactionId(ViewGenerator.TRANSACTION_ID); verifyNoMoreInteractions(bizEventsViewGeneralRepository); verifyNoMoreInteractions(bizEventsViewCartRepository); @@ -365,16 +390,27 @@ void transactionViewCartNotFoundThrowError() { @Test void transactionViewUserDisabled() { List viewUserList = Collections.singletonList(generateBizEventsViewUser()); - when(bizEventsViewUserRepository.getBizEventsViewUserByTaxCodeAndTransactionId(anyString(), anyString())) + when(bizEventsViewUserRepository.getBizEventsViewUserByTaxCodeAndId(anyString(), anyString())) .thenReturn(viewUserList); - Assertions.assertDoesNotThrow(() -> transactionService.disableTransaction( - ViewGenerator.USER_TAX_CODE_WITH_TX, ViewGenerator.TRANSACTION_ID)); + Assertions.assertDoesNotThrow(() -> transactionService.disablePaidNotice( + ViewGenerator.USER_TAX_CODE_WITH_TX, ViewGenerator.EVENT_ID)); ArgumentCaptor> argument = ArgumentCaptor.forClass(List.class); verify(bizEventsViewUserRepository).saveAll(argument.capture()); assertEquals(Boolean.TRUE, argument.getValue().get(0).getHidden()); } + + @Test + void transactionViewUserDisabledEmptyList() { + List viewUserList = new ArrayList<>(); + when(bizEventsViewUserRepository.getBizEventsViewUserByTaxCodeAndId(anyString(), anyString())) + .thenReturn(viewUserList); + Assertions.assertThrows(AppException.class, () -> transactionService.disablePaidNotice( + ViewGenerator.USER_TAX_CODE_WITH_TX, ViewGenerator.EVENT_ID)); + verify(bizEventsViewUserRepository, times(0)).saveAll(viewUserList); + } + @Test void transactionViewUserCartDisabled() { @@ -386,11 +422,11 @@ void transactionViewUserCartDisabled() { viewUserList.add(u); } - when(bizEventsViewUserRepository.getBizEventsViewUserByTaxCodeAndTransactionId(anyString(), anyString())) + when(bizEventsViewUserRepository.getBizEventsViewUserByTaxCodeAndId(anyString(), anyString())) .thenReturn(viewUserList); - Assertions.assertDoesNotThrow(() -> transactionService.disableTransaction( - ViewGenerator.USER_TAX_CODE_WITH_TX, ViewGenerator.TRANSACTION_ID)); + Assertions.assertDoesNotThrow(() -> transactionService.disablePaidNotice( + ViewGenerator.USER_TAX_CODE_WITH_TX, ViewGenerator.EVENT_ID)); ArgumentCaptor> argument = ArgumentCaptor.forClass(List.class); verify(bizEventsViewUserRepository).saveAll(argument.capture());