diff --git a/openapi/openapi.json b/openapi/openapi.json index 091c8aa..0a65d90 100644 --- a/openapi/openapi.json +++ b/openapi/openapi.json @@ -535,6 +535,15 @@ "type": "string" }, "required": true + }, + { + "in": "query", + "name": "isCart", + "description": "Boolean to determine if the id refers to a cart", + "schema": { + "type": "string" + }, + "required": false } ], "responses": { @@ -1381,6 +1390,15 @@ "type": "string" }, "required": true + }, + { + "in": "query", + "name": "isCart", + "description": "Boolean to determine if the id refers to a cart", + "schema": { + "type": "string" + }, + "required": false } ], "requestBody": { diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/ReceiptToReviewed.java b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/ReceiptToReviewed.java index 6280ac5..9c44bf8 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/ReceiptToReviewed.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/ReceiptToReviewed.java @@ -6,6 +6,7 @@ import it.gov.pagopa.receipt.pdf.helpdesk.client.impl.ReceiptCosmosClientImpl; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.ReceiptError; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.enumeration.ReceiptErrorStatusType; +import it.gov.pagopa.receipt.pdf.helpdesk.exception.CartNotFoundException; import it.gov.pagopa.receipt.pdf.helpdesk.exception.ReceiptNotFoundException; import it.gov.pagopa.receipt.pdf.helpdesk.model.ProblemJson; import org.slf4j.Logger; @@ -68,8 +69,18 @@ public HttpResponseMessage run( ReceiptError receiptError; try { - receiptError = receiptCosmosClient.getReceiptError(eventId); - } catch (NoSuchElementException | ReceiptNotFoundException e) { + + boolean isCart = Boolean.parseBoolean(request.getQueryParameters().getOrDefault( + "isCart", "false")); + + if (isCart) { + receiptError = this.receiptCosmosClient.getReceiptError( + (String) receiptCosmosClient.getCartDocument(eventId).getCartPaymentId().toArray()[0]); + } else { + receiptError = receiptCosmosClient.getReceiptError(eventId); + } + + } catch (NoSuchElementException | ReceiptNotFoundException | CartNotFoundException e) { responseMsg = String.format("No receiptError has been found with bizEventId %s", eventId); logger.error("[{}] {}", context.getFunctionName(), responseMsg, e); return request diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverNotNotifiedReceipt.java b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverNotNotifiedReceipt.java index 70a49fb..1551c61 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverNotNotifiedReceipt.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverNotNotifiedReceipt.java @@ -11,8 +11,10 @@ import com.microsoft.azure.functions.annotation.CosmosDBOutput; import com.microsoft.azure.functions.annotation.FunctionName; import com.microsoft.azure.functions.annotation.HttpTrigger; +import it.gov.pagopa.receipt.pdf.helpdesk.entity.event.BizEvent; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.Receipt; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.enumeration.ReceiptStatusType; +import it.gov.pagopa.receipt.pdf.helpdesk.exception.CartNotFoundException; import it.gov.pagopa.receipt.pdf.helpdesk.exception.ReceiptNotFoundException; import it.gov.pagopa.receipt.pdf.helpdesk.model.ProblemJson; import it.gov.pagopa.receipt.pdf.helpdesk.service.ReceiptCosmosService; @@ -85,8 +87,18 @@ public HttpResponseMessage run( Receipt receipt; try { - receipt = this.receiptCosmosService.getReceipt(eventId); - } catch (ReceiptNotFoundException e) { + + boolean isCart = Boolean.parseBoolean(request.getQueryParameters().getOrDefault( + "isCart", "false")); + + if (isCart) { + receipt = this.receiptCosmosService.getReceipt( + (String) receiptCosmosService.getCart(eventId).getCartPaymentId().toArray()[0]); + } else { + receipt = this.receiptCosmosService.getReceipt(eventId); + } + + } catch (ReceiptNotFoundException | CartNotFoundException e) { String responseMsg = String.format("Unable to retrieve the receipt with eventId %s", eventId); logger.error("[{}] {}", context.getFunctionName(), responseMsg, e); return request 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 968ad4d..1a43daf 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 @@ -61,10 +61,21 @@ public static Receipt getEvent( bizEvent = bizEventCosmosClient.getBizEventDocument(eventId); } - if (isBizEventInvalid(bizEvent, context, logger)) { + if (isCart) { + Integer intTotalNotice = Integer.parseInt(bizEvent.getPaymentInfo().getTotalNotice()); + if (!intTotalNotice.equals(listCart.size())) { + return null; + } + for (BizEvent event : listCart) { + if (isBizEventInvalid(event, context, logger)) { + return null; + } + } + } else if (isBizEventInvalid(bizEvent, context, logger)) { return null; } + if (receipt == null) { try { receipt = receiptCosmosService.getReceipt(eventId); diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/ReceiptToReviewedTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/ReceiptToReviewedTest.java index f51e66c..57d646a 100644 --- a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/ReceiptToReviewedTest.java +++ b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/ReceiptToReviewedTest.java @@ -1,9 +1,14 @@ package it.gov.pagopa.receipt.pdf.helpdesk; +import com.azure.cosmos.models.FeedResponse; import com.microsoft.azure.functions.*; import it.gov.pagopa.receipt.pdf.helpdesk.client.ReceiptCosmosClient; +import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartForReceipt; +import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartStatusType; +import it.gov.pagopa.receipt.pdf.helpdesk.entity.event.BizEvent; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.ReceiptError; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.enumeration.ReceiptErrorStatusType; +import it.gov.pagopa.receipt.pdf.helpdesk.exception.CartNotFoundException; import it.gov.pagopa.receipt.pdf.helpdesk.exception.ReceiptNotFoundException; import it.gov.pagopa.receipt.pdf.helpdesk.model.ReceiptToReviewedRequest; import it.gov.pagopa.receipt.pdf.helpdesk.util.HttpResponseMessageMock; @@ -12,10 +17,11 @@ import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.stubbing.Answer; -import java.util.Optional; +import java.util.*; import java.util.concurrent.atomic.AtomicReference; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; @@ -63,6 +69,36 @@ void requestWithValidBizEventSaveReceiptErrorInReviewed() throws ReceiptNotFound assertEquals(ReceiptErrorStatusType.REVIEWED, captured.getStatus()); } + @Test + void requestWithValidCartSaveReceiptErrorInReviewed() throws ReceiptNotFoundException, CartNotFoundException { + doAnswer((Answer) invocation -> { + HttpStatus status = (HttpStatus) invocation.getArguments()[0]; + return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status); + }).when(request).createResponseBuilder(any(HttpStatus.class)); + + ReceiptError receiptError = ReceiptError.builder() + .bizEventId(BIZ_EVENT_ID) + .status(ReceiptErrorStatusType.TO_REVIEW) + .build(); + when(receiptCosmosClient.getReceiptError(BIZ_EVENT_ID)).thenReturn(receiptError); + + when(request.getQueryParameters()).thenReturn(Collections.singletonMap("isCart","true")); + + when(receiptCosmosClient.getCartDocument(any())).thenReturn(generateCart()); + + function = spy(new ReceiptToReviewed(receiptCosmosClient)); + + // test execution + AtomicReference responseMessage = new AtomicReference<>(); + assertDoesNotThrow(() -> responseMessage.set(function.run(request, BIZ_EVENT_ID, documentdb,executionContextMock ))); + assertEquals(HttpStatus.OK, responseMessage.get().getStatus()); + + verify(documentdb).setValue(receiptErrorCaptor.capture()); + ReceiptError captured = receiptErrorCaptor.getValue(); + assertEquals(BIZ_EVENT_ID, captured.getBizEventId()); + assertEquals(ReceiptErrorStatusType.REVIEWED, captured.getStatus()); + } + @Test void requestWithValidBizEventIdButReceiptNotFound() throws ReceiptNotFoundException { doAnswer((Answer) invocation -> { @@ -120,4 +156,14 @@ void requestWithoutEventIdReturnsBadRequest() { verifyNoInteractions(documentdb); } + + private CartForReceipt generateCart() { + CartForReceipt cart = new CartForReceipt(); + cart.setId("1"); + cart.setStatus(CartStatusType.FAILED); + cart.setTotalNotice(1); + cart.setCartPaymentId(new HashSet<>(new ArrayList<>( + List.of(new String[]{"valid_biz_event_id"})))); + return cart; + } } \ No newline at end of file diff --git a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverNotNotifiedReceiptTest.java b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverNotNotifiedReceiptTest.java index a87be3d..dbb4cbe 100644 --- a/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverNotNotifiedReceiptTest.java +++ b/src/test/java/it/gov/pagopa/receipt/pdf/helpdesk/RecoverNotNotifiedReceiptTest.java @@ -5,9 +5,12 @@ import com.microsoft.azure.functions.HttpResponseMessage; import com.microsoft.azure.functions.HttpStatus; import com.microsoft.azure.functions.OutputBinding; +import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartForReceipt; +import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartStatusType; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.ReasonError; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.Receipt; import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.enumeration.ReceiptStatusType; +import it.gov.pagopa.receipt.pdf.helpdesk.exception.CartNotFoundException; import it.gov.pagopa.receipt.pdf.helpdesk.exception.ReceiptNotFoundException; import it.gov.pagopa.receipt.pdf.helpdesk.model.ProblemJson; import it.gov.pagopa.receipt.pdf.helpdesk.service.ReceiptCosmosService; @@ -24,8 +27,7 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.stubbing.Answer; -import java.util.List; -import java.util.Optional; +import java.util.*; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -101,6 +103,39 @@ void recoverNotNotifiedReceiptSuccess() throws ReceiptNotFoundException { assertNull(captured.getReasonErrPayer()); } + @Test + void recoverNotNotifiedCartReceiptSuccess() throws ReceiptNotFoundException, CartNotFoundException { + Receipt receipt = buildReceipt(); + when(receiptCosmosServiceMock.getReceipt(EVENT_ID)).thenReturn(receipt); + + doAnswer((Answer) invocation -> { + HttpStatus status = (HttpStatus) invocation.getArguments()[0]; + return new HttpResponseMessageMock.HttpResponseMessageBuilderMock().status(status); + }).when(requestMock).createResponseBuilder(any(HttpStatus.class)); + + when(requestMock.getQueryParameters()).thenReturn(Collections.singletonMap("isCart","true")); + + when(receiptCosmosServiceMock.getCart(any())).thenReturn(generateCart()); + + // test execution + HttpResponseMessage response = sut.run(requestMock, EVENT_ID, documentReceipts, executionContextMock); + + // test assertion + assertNotNull(response); + assertEquals(HttpStatus.OK, response.getStatus()); + assertNotNull(response.getBody()); + + verify(documentReceipts).setValue(receiptCaptor.capture()); + + assertEquals(1, receiptCaptor.getValue().size()); + Receipt captured = receiptCaptor.getValue().get(0); + assertEquals(ReceiptStatusType.GENERATED, captured.getStatus()); + assertEquals(EVENT_ID, captured.getEventId()); + assertEquals(0, captured.getNotificationNumRetry()); + assertNull(captured.getReasonErr()); + assertNull(captured.getReasonErrPayer()); + } + @Test void recoverReceiptFailForMissingEventId() { doAnswer((Answer) invocation -> { @@ -217,4 +252,14 @@ private Receipt buildReceipt() { .notifiedAt(0) .build(); } + + private CartForReceipt generateCart() { + CartForReceipt cart = new CartForReceipt(); + cart.setId("1"); + cart.setStatus(CartStatusType.FAILED); + cart.setTotalNotice(1); + cart.setCartPaymentId(new HashSet<>(new ArrayList<>( + List.of(new String[]{"eventId"})))); + return cart; + } } \ No newline at end of file