Skip to content

Commit

Permalink
Merge pull request #47 from pagopa/PRDP-307-api-get-cart
Browse files Browse the repository at this point in the history
[PRDP-307] feat: API getCart
  • Loading branch information
pasqualespica authored Jan 3, 2024
2 parents 4892be9 + 14ad309 commit e55dbb5
Show file tree
Hide file tree
Showing 16 changed files with 585 additions and 37 deletions.
68 changes: 38 additions & 30 deletions README.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions helm/values-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ microservice-chart:
COSMOS_RECEIPT_CONTAINER_NAME: "receipts"
COSMOS_RECEIPT_ERROR_CONTAINER_NAME: "receipts-message-errors"
COSMOS_RECEIPT_MESSAGE_CONTAINER_NAME: "receipts-io-messages"
COSMOS_RECEIPT_CART_CONTAINER_NAME: "cart-for-receipts"
COSMOS_BIZ_EVENT_CONTAINER_NAME: "biz-events"
PDF_ENGINE_ENDPOINT: "https://api.dev.platform.pagopa.it/shared/pdf-engine/v1/generate-pdf"
BLOB_STORAGE_ACCOUNT_ENDPOINT: "https://pagopadweureceiptsfnsa.blob.core.windows.net/"
Expand Down
1 change: 1 addition & 0 deletions helm/values-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ microservice-chart:
COSMOS_RECEIPT_CONTAINER_NAME: "receipts"
COSMOS_RECEIPT_ERROR_CONTAINER_NAME: "receipts-message-errors"
COSMOS_RECEIPT_MESSAGE_CONTAINER_NAME: "receipts-io-messages"
COSMOS_RECEIPT_CART_CONTAINER_NAME: "cart-for-receipts"
COSMOS_BIZ_EVENT_CONTAINER_NAME: "biz-events"
PDF_ENGINE_ENDPOINT: "https://api.platform.pagopa.it/shared/pdf-engine/v1/generate-pdf"
BLOB_STORAGE_ACCOUNT_ENDPOINT: "https://pagopapweureceiptsfnsa.blob.core.windows.net"
Expand Down
1 change: 1 addition & 0 deletions helm/values-uat.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ microservice-chart:
COSMOS_RECEIPT_CONTAINER_NAME: "receipts"
COSMOS_RECEIPT_ERROR_CONTAINER_NAME: "receipts-message-errors"
COSMOS_RECEIPT_MESSAGE_CONTAINER_NAME: "receipts-io-messages"
COSMOS_RECEIPT_CART_CONTAINER_NAME: "cart-for-receipts"
COSMOS_BIZ_EVENT_CONTAINER_NAME: "biz-events"
PDF_ENGINE_ENDPOINT: "https://api.uat.platform.pagopa.it/shared/pdf-engine/v1/generate-pdf"
BLOB_STORAGE_ACCOUNT_ENDPOINT: "https://pagopauweureceiptsfnsa.blob.core.windows.net"
Expand Down
147 changes: 147 additions & 0 deletions openapi/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,126 @@
}
]
},
"/cart/{cart-id}": {
"get": {
"tags": [
"API-getCart"
],
"summary": "Retrieve from CosmosDB the cart with the given cart id",
"operationId": "GetCart",
"parameters": [
{
"in": "path",
"name": "cart-id",
"description": "Cart id.",
"schema": {
"type": "string"
},
"required": true
}
],
"responses": {
"200": {
"description": "Successful Calls.",
"headers": {
"X-Request-Id": {
"description": "This header identifies the call",
"schema": {
"type": "string"
}
}
},
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CartForReceipt"
}
}
}
},
"400": {
"description": "Bad request.",
"headers": {
"X-Request-Id": {
"description": "This header identifies the call",
"schema": {
"type": "string"
}
}
},
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProblemJson"
}
}
}
},
"404": {
"description": "Receipt error not found.",
"headers": {
"X-Request-Id": {
"description": "This header identifies the call",
"schema": {
"type": "string"
}
}
},
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProblemJson"
}
}
}
},
"500": {
"description": "Receipt could not be updated with the new attachments",
"headers": {
"X-Request-Id": {
"description": "This header identifies the call",
"schema": {
"type": "string"
}
}
},
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProblemJson"
}
}
}
},
"default": {
"description": "Unexpected error.",
"headers": {
"X-Request-Id": {
"description": "This header identifies the call",
"schema": {
"type": "string"
}
}
}
}
},
"security": [
{
"ApiKey": []
}
]
},
"parameters": [
{
"name": "X-Request-Id",
"in": "header",
"description": "This header identifies the call, if not passed it is self-generated. This ID is returned in the response.",
"schema": {
"type": "string"
}
}
]
},
"/receipts-error/{event-id}/reviewed": {
"post": {
"tags": [
Expand Down Expand Up @@ -1530,6 +1650,33 @@
}
}
},
"CartForReceipt": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"example": "712341"
},
"cartPaymentId": {
"type": "array",
"items": {
"type": "string",
"example": "76abb1f1-c9f9-4ead-9e66-12fec4d51042"
}
},
"totalNotice": {
"type": "integer",
"example": "3"
},
"status": {
"type": "string",
"enum": ["INSERTED", "FAILED", "SENT"]
},
"reasonError": {
"$ref": "#/components/schemas/ReasonErr"
}
}
},
"ProblemJson": {
"type": "object",
"properties": {
Expand Down
89 changes: 89 additions & 0 deletions src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/GetCart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package it.gov.pagopa.receipt.pdf.helpdesk;

import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.BindingName;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartForReceipt;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.CartNotFoundException;
import it.gov.pagopa.receipt.pdf.helpdesk.model.ProblemJson;
import it.gov.pagopa.receipt.pdf.helpdesk.service.ReceiptCosmosService;
import it.gov.pagopa.receipt.pdf.helpdesk.service.impl.ReceiptCosmosServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDateTime;
import java.util.Optional;

/**
* Azure Functions with HTTP Trigger.
*/
public class GetCart {

private final Logger logger = LoggerFactory.getLogger(GetCart.class);

private final ReceiptCosmosService receiptCosmosService;

public GetCart() {
this.receiptCosmosService = new ReceiptCosmosServiceImpl();
}

GetCart(ReceiptCosmosService receiptCosmosService) {
this.receiptCosmosService = receiptCosmosService;
}

/**
* This function will be invoked when a Http Trigger occurs.
* <p>
* It retrieves the receipt with the specified biz event id
* <p>
*
* @return response with {@link HttpStatus#OK} and the receipt if found
*/
@FunctionName("GetCart")
public HttpResponseMessage run(
@HttpTrigger(name = "GetCartTrigger",
methods = {HttpMethod.GET},
route = "cart/{cart-id}",
authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
@BindingName("cart-id") String cartId,
final ExecutionContext context) {
logger.info("[{}] function called at {}", context.getFunctionName(), LocalDateTime.now());

if (cartId == null || cartId.isBlank()) {
return request
.createResponseBuilder(HttpStatus.BAD_REQUEST)
.body(ProblemJson.builder()
.title(HttpStatus.BAD_REQUEST.name())
.detail("Please pass a valid cart id")
.status(HttpStatus.BAD_REQUEST.value())
.build())
.build();
}

try {
CartForReceipt cart = this.receiptCosmosService.getCart(cartId);
return request
.createResponseBuilder(HttpStatus.OK)
.body(cart)
.build();
} catch (CartNotFoundException e) {
String responseMsg = String.format("Unable to retrieve the cart with id %s", cartId);
logger.error("[{}] {}", context.getFunctionName(), responseMsg, e);
return request
.createResponseBuilder(HttpStatus.NOT_FOUND)
.body(ProblemJson.builder()
.title(HttpStatus.NOT_FOUND.name())
.detail(responseMsg)
.status(HttpStatus.NOT_FOUND.value())
.build())
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

import com.azure.cosmos.models.CosmosItemResponse;
import com.azure.cosmos.models.FeedResponse;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.cart.CartForReceipt;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.IOMessage;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.Receipt;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.ReceiptError;
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.IoMessageNotFoundException;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.ReceiptNotFoundException;

public interface ReceiptCosmosClient {

Receipt getReceiptDocument(String eventId) throws ReceiptNotFoundException;

/**
* Retrieve the failed receipt documents with {@link ReceiptStatusType#INSERTED} status
*
Expand Down Expand Up @@ -57,4 +58,13 @@ public interface ReceiptCosmosClient {
Iterable<FeedResponse<Receipt>> getIOErrorToNotifyReceiptDocuments(String continuationToken, Integer pageSize);

IOMessage getIoMessage(String messageId) throws IoMessageNotFoundException;

/**
* Retrieve the cart with the provided id from Cosmos
*
* @param cartId the cart id
* @return the cart
* @throws CartNotFoundException if no cart was found in the container
*/
CartForReceipt getCartDocument(String cartId) throws CartNotFoundException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import com.azure.cosmos.models.FeedResponse;
import com.azure.cosmos.util.CosmosPagedIterable;
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.receipt.IOMessage;
import it.gov.pagopa.receipt.pdf.helpdesk.entity.receipt.Receipt;
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.entity.receipt.enumeration.ReceiptStatusType;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.CartNotFoundException;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.IoMessageNotFoundException;
import it.gov.pagopa.receipt.pdf.helpdesk.exception.ReceiptNotFoundException;

Expand All @@ -29,9 +31,8 @@ public class ReceiptCosmosClientImpl implements ReceiptCosmosClient {

private final String databaseId = System.getenv().getOrDefault("COSMOS_RECEIPT_DB_NAME", "db");
private final String containerId = System.getenv().getOrDefault("COSMOS_RECEIPT_CONTAINER_NAME", "receipt");

private final String containerCartId = System.getenv().getOrDefault("COSMOS_RECEIPT_CART_CONTAINER_NAME", "cart-for-receipts");
private final String containerMessageId = System.getenv().getOrDefault("COSMOS_RECEIPT_MESSAGE_CONTAINER_NAME", "receipts-io-messages");

private final String containerReceiptErrorId = System.getenv().getOrDefault("COSMOS_RECEIPT_ERROR_CONTAINER_NAME", "receipts-message-errors");

private final String millisDiff = System.getenv("MAX_DATE_DIFF_MILLIS");
Expand All @@ -43,6 +44,7 @@ public class ReceiptCosmosClientImpl implements ReceiptCosmosClient {

private final int recordsLimitRecoverNotNotified = Integer.parseInt(System.getenv().getOrDefault("RECOVER_NOT_NOTIFIED_MASSIVE_MAX_RECORDS", "200"));

private static final String DOCUMENT_NOT_FOUND_ERR_MSG = "Document not found in the defined container";

private final CosmosClient cosmosClient;

Expand Down Expand Up @@ -88,7 +90,7 @@ public Receipt getReceiptDocument(String eventId) throws ReceiptNotFoundExceptio
if (queryResponse.iterator().hasNext()) {
return queryResponse.iterator().next();
}
throw new ReceiptNotFoundException("Document not found in the defined container");
throw new ReceiptNotFoundException(DOCUMENT_NOT_FOUND_ERR_MSG);
}

/**
Expand Down Expand Up @@ -169,9 +171,8 @@ public ReceiptError getReceiptError(String bizEventId) throws ReceiptNotFoundEx

if (queryResponse.iterator().hasNext()) {
return queryResponse.iterator().next();
} else {
throw new ReceiptNotFoundException("Document not found in the defined container");
}
throw new ReceiptNotFoundException(DOCUMENT_NOT_FOUND_ERR_MSG);
}

/**
Expand Down Expand Up @@ -260,6 +261,24 @@ public IOMessage getIoMessage(String messageId) throws IoMessageNotFoundExceptio
if (queryResponse.iterator().hasNext()) {
return queryResponse.iterator().next();
}
throw new IoMessageNotFoundException("Document not found in the defined container");
throw new IoMessageNotFoundException(DOCUMENT_NOT_FOUND_ERR_MSG);
}

@Override
public CartForReceipt getCartDocument(String cartId) throws CartNotFoundException {
CosmosDatabase cosmosDatabase = this.cosmosClient.getDatabase(databaseId);
CosmosContainer cosmosContainer = cosmosDatabase.getContainer(containerCartId);

//Build query
String query = String.format("SELECT * FROM c WHERE c.id = '%s'", cartId);

//Query the container
CosmosPagedIterable<CartForReceipt> queryResponse = cosmosContainer
.queryItems(query, new CosmosQueryRequestOptions(), CartForReceipt.class);

if (queryResponse.iterator().hasNext()) {
return queryResponse.iterator().next();
}
throw new CartNotFoundException(DOCUMENT_NOT_FOUND_ERR_MSG);
}
}
Loading

0 comments on commit e55dbb5

Please sign in to comment.