Skip to content

Commit

Permalink
Merge pull request #18 from pagopa/PRDP-251-get-receipt-api
Browse files Browse the repository at this point in the history
[PRDP-251] feat: Add get receipt API
  • Loading branch information
pasqualespica authored Dec 4, 2023
2 parents 9e40de4 + b005dd1 commit 55e797f
Show file tree
Hide file tree
Showing 7 changed files with 557 additions and 7 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=pagopa_pagopa-receipt-pdf-helpdesk&metric=alert_status)](https://sonarcloud.io/dashboard?id=pagopa_pagopa-receipt-pdf-helpdesk)

Java Azure Functions that exposed the following recover APIs:
- GetReceipt
- GetReceiptByOrganizationFiscalCodeAndIUV
- ReceiptToReviewed
- RecoverFailedReceipt
- RecoverFailedReceiptMassive
- RecoverNotNotifiedReceipt
- RecoverNotNotifiedReceiptMassive
- RegenerateReceiptPdf

---

Expand Down
82 changes: 82 additions & 0 deletions src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/GetReceipt.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
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.receipt.Receipt;
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;
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 GetReceipt {

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

private final ReceiptCosmosService receiptCosmosService;

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

GetReceipt(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("GetReceipt")
public HttpResponseMessage run(
@HttpTrigger(name = "GetReceiptTrigger",
methods = {HttpMethod.GET},
route = "receipts/{event-id}",
authLevel = AuthorizationLevel.FUNCTION)
HttpRequestMessage<Optional<String>> request,
@BindingName("event-id") String eventId,
final ExecutionContext context) {
logger.info("[{}] function called at {}", context.getFunctionName(), LocalDateTime.now());

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

try {
Receipt receipt = this.receiptCosmosService.getReceipt(eventId);
return request
.createResponseBuilder(HttpStatus.OK)
.body(receipt)
.build();
} catch (ReceiptNotFoundException e) {
String responseMsg = String.format("Unable to retrieve the receipt with eventId %s", eventId);
logger.error("[{}] {}", context.getFunctionName(), responseMsg, e);
return request.createResponseBuilder(HttpStatus.NOT_FOUND).body(responseMsg).build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
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.client.BizEventCosmosClient;
import it.gov.pagopa.receipt.pdf.helpdesk.client.impl.BizEventCosmosClientImpl;
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.exception.BizEventNotFoundException;
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;
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 GetReceiptByOrganizationFiscalCodeAndIUV {

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

private final ReceiptCosmosService receiptCosmosService;
private final BizEventCosmosClient bizEventCosmosClient;

public GetReceiptByOrganizationFiscalCodeAndIUV() {
this.receiptCosmosService = new ReceiptCosmosServiceImpl();
this.bizEventCosmosClient = BizEventCosmosClientImpl.getInstance();
}

GetReceiptByOrganizationFiscalCodeAndIUV(ReceiptCosmosService receiptCosmosService, BizEventCosmosClient bizEventCosmosClient) {
this.receiptCosmosService = receiptCosmosService;
this.bizEventCosmosClient = bizEventCosmosClient;
}

/**
* This function will be invoked when a Http Trigger occurs.
* <p>
* It retrieves the receipt with the specified organization fiscal code and iuv
* <p>
*
* @return response with {@link HttpStatus#OK} and the receipt if found
*/
@FunctionName("GetReceiptByOrganizationFiscalCodeAndIUV")
public HttpResponseMessage run(
@HttpTrigger(name = "GetReceiptByOrganizationFiscalCodeAndIUVTrigger",
methods = {HttpMethod.GET},
route = "receipts/organizations/{organization-fiscal-code}/iuvs/{iuv}",
authLevel = AuthorizationLevel.FUNCTION)
HttpRequestMessage<Optional<String>> request,
@BindingName("organization-fiscal-code") String organizationFiscalCode,
@BindingName("iuv") String iuv,
final ExecutionContext context) {
logger.info("[{}] function called at {}", context.getFunctionName(), LocalDateTime.now());

if (organizationFiscalCode == null
|| organizationFiscalCode.isBlank()
|| iuv == null
|| iuv.isBlank()
) {
return request
.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ProblemJson.builder()
.title(HttpStatus.BAD_REQUEST.name())
.detail("Please pass a valid organization fiscal code and iuv")
.status(HttpStatus.BAD_REQUEST.value())
.build())
.build();
}

BizEvent bizEvent;
try {
bizEvent = this.bizEventCosmosClient
.getBizEventDocumentByOrganizationFiscalCodeAndIUV(organizationFiscalCode, iuv);
} catch (BizEventNotFoundException e) {
String responseMsg = String.format("Unable to retrieve the biz-event with organization fiscal code %s and iuv %s",
organizationFiscalCode, iuv);
logger.error("[{}] {}", context.getFunctionName(), responseMsg, e);
return request.createResponseBuilder(HttpStatus.NOT_FOUND).body(responseMsg).build();
}

try {
Receipt receipt = this.receiptCosmosService.getReceipt(bizEvent.getId());
return request
.createResponseBuilder(HttpStatus.OK)
.body(receipt)
.build();
} catch (ReceiptNotFoundException e) {
String responseMsg = String.format("Unable to retrieve the receipt with eventId %s", bizEvent.getId());
logger.error("[{}] {}", context.getFunctionName(), responseMsg, e);
return request.createResponseBuilder(HttpStatus.NOT_FOUND).body(responseMsg).build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,23 @@
import it.gov.pagopa.receipt.pdf.helpdesk.exception.BizEventNotFoundException;

public interface BizEventCosmosClient {

/**
* Retrieve biz-even document from CosmosDB database
*
* @param eventId Biz-event id
* @return biz-event document
* @throws BizEventNotFoundException in case no biz-event has been found with the given idEvent
*/
BizEvent getBizEventDocument(String eventId) throws BizEventNotFoundException;

/**
* Retrieve biz-even document with the specified organization fiscal code and iuv from CosmosDB database
*
* @param organizationFiscalCode the organization fiscal code
* @param iuv the iuv
* @return biz-event document
* @throws BizEventNotFoundException in case no biz-event has been found with the given idEvent
*/
BizEvent getBizEventDocumentByOrganizationFiscalCodeAndIUV(String organizationFiscalCode, String iuv) throws BizEventNotFoundException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,15 @@ public static BizEventCosmosClientImpl getInstance() {
if (instance == null) {
instance = new BizEventCosmosClientImpl();
}

return instance;
}

/**
* Retrieve biz-event document from Cosmos database
*
* @param eventId Biz-event id
* @return biz-event document
* @throws BizEventNotFoundException in case no biz-event has been found with the given idEvent
* {@inheritDoc}
*/
@Override
public BizEvent getBizEventDocument(String eventId) throws BizEventNotFoundException {
CosmosDatabase cosmosDatabase = this.cosmosClient.getDatabase(databaseId);

CosmosContainer cosmosContainer = cosmosDatabase.getContainer(containerId);

//Build query
Expand All @@ -70,4 +64,23 @@ public BizEvent getBizEventDocument(String eventId) throws BizEventNotFoundExcep
throw new BizEventNotFoundException("Document not found in the defined container");
}


@Override
public BizEvent getBizEventDocumentByOrganizationFiscalCodeAndIUV(String organizationFiscalCode, String iuv) throws BizEventNotFoundException {
CosmosDatabase cosmosDatabase = this.cosmosClient.getDatabase(databaseId);
CosmosContainer cosmosContainer = cosmosDatabase.getContainer(containerId);

//Build query
String query = String.format("SELECT * FROM c WHERE c.creditor.idPA = '%s' AND c.debtorPosition.iuv = '%s'",
organizationFiscalCode, iuv);

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

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

0 comments on commit 55e797f

Please sign in to comment.