generated from pagopa/pagopa-functions-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from pagopa/PRDP-251-get-receipt-api
[PRDP-251] feat: Add get receipt API
- Loading branch information
Showing
7 changed files
with
557 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/main/java/it/gov/pagopa/receipt/pdf/helpdesk/GetReceipt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
...ain/java/it/gov/pagopa/receipt/pdf/helpdesk/GetReceiptByOrganizationFiscalCodeAndIUV.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.