Skip to content

Commit

Permalink
BUG: [PAGOPA-2260] - issue cart (#98)
Browse files Browse the repository at this point in the history
* [PAGOPA-2260] issue cart: managed recovery via event-id

* [PAGOPA-2260] issue cart: disable via event-id

---------

Co-authored-by: aacitelli <[email protected]>
  • Loading branch information
alessio-acitelli and aacitelli authored Oct 25, 2024
1 parent 1a5364a commit 79578ad
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public ResponseEntity<NoticeListWrapResponse> getPaidNotices(String fiscalCode,
}

@Override
public ResponseEntity<Void> disablePaidNotice(String fiscalCode, String transactionId) {
transactionService.disableTransaction(fiscalCode, transactionId);
public ResponseEntity<Void> disablePaidNotice(String fiscalCode, String eventId) {
transactionService.disablePaidNotice(fiscalCode, eventId);
return new ResponseEntity<>(HttpStatus.OK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ public interface BizEventsViewUserRepository extends CosmosRepository<BizEventsV

@Query("select * from c where c.transactionId=@transactionId and c.taxCode = @fiscalCode and c.hidden = false")
List<BizEventsViewUser> getBizEventsViewUserByTaxCodeAndTransactionId(String fiscalCode, String transactionId);

@Query("select * from c where c.taxCode = @fiscalCode and c.hidden = false and STARTSWITH(c.id, @eventId)")
List<BizEventsViewUser> getBizEventsViewUserByTaxCodeAndId(String fiscalCode, String eventId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ public interface ITransactionService {
ResponseEntity<Resource> getPDFReceiptResponse(String fiscalCode, String eventId);

void disableTransaction(String fiscalCode, String transactionId);

void disablePaidNotice(String fiscalCode, String eventId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public TransactionListResponse getTransactionList(String taxCode, Boolean isPaye
public TransactionDetailResponse getTransactionDetails(String taxCode, String eventReference) {
List<BizEventsViewGeneral> 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<BizEventsViewCart> listOfCartViews;
Expand All @@ -117,22 +117,22 @@ public TransactionDetailResponse getTransactionDetails(String taxCode, String ev

@Override
public NoticeDetailResponse getPaidNoticeDetail(String taxCode, String eventId) {
List<BizEventsViewGeneral> bizEventsViewGeneral = this.bizEventsViewGeneralRepository.findByTransactionId(eventId);
Optional<BizEventsViewGeneral> 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<BizEventsViewCart> 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);
}


Expand All @@ -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<BizEventsViewUser> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,23 +163,22 @@ void taxCodeWithEventsAndMultipleCartItemsShouldReturnTransactionList() {

@Test
void idAndTaxCodeWithOneEventShouldReturnNoticeDetails() {
List<BizEventsViewGeneral> 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<BizEventsViewCart> 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());
Expand Down Expand Up @@ -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<BizEventsViewCart> 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() {
Expand Down Expand Up @@ -295,18 +320,18 @@ void idAndTaxCodeWithCartEventShouldReturnTransactionDetails() {

@Test
void idAndTaxCodeWithCartEventShouldReturnNoticeDetails() {
List<BizEventsViewGeneral> 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<BizEventsViewCart> 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);
Expand Down Expand Up @@ -365,16 +390,27 @@ void transactionViewCartNotFoundThrowError() {
@Test
void transactionViewUserDisabled() {
List<BizEventsViewUser> 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<List<BizEventsViewUser>> argument = ArgumentCaptor.forClass(List.class);
verify(bizEventsViewUserRepository).saveAll(argument.capture());
assertEquals(Boolean.TRUE, argument.getValue().get(0).getHidden());
}

@Test
void transactionViewUserDisabledEmptyList() {
List<BizEventsViewUser> 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() {

Expand All @@ -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<List<BizEventsViewUser>> argument = ArgumentCaptor.forClass(List.class);
verify(bizEventsViewUserRepository).saveAll(argument.capture());
Expand Down

0 comments on commit 79578ad

Please sign in to comment.