Skip to content

Commit

Permalink
temp change to RegenerateReceiptPdf
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-cialini committed Dec 7, 2023
1 parent f9c6222 commit f1550ed
Show file tree
Hide file tree
Showing 2 changed files with 257 additions and 253 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package it.gov.pagopa.receipt.pdf.helpdesk;

import com.microsoft.azure.functions.*;
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 com.microsoft.azure.functions.annotation.*;
import it.gov.pagopa.receipt.pdf.helpdesk.client.BizEventCosmosClient;
import it.gov.pagopa.receipt.pdf.helpdesk.client.ReceiptCosmosClient;
import it.gov.pagopa.receipt.pdf.helpdesk.client.impl.BizEventCosmosClientImpl;
Expand All @@ -23,6 +20,7 @@
import java.io.IOException;
import java.nio.file.Path;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import static it.gov.pagopa.receipt.pdf.helpdesk.utils.GenerateReceiptUtils.*;
Expand Down Expand Up @@ -69,91 +67,97 @@ public HttpResponseMessage run (
authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
@BindingName("bizevent-id") String eventId,
@CosmosDBOutput(
name = "ReceiptDatastore",
databaseName = "db",
collectionName = "receipts",
connectionStringSetting = "COSMOS_RECEIPTS_CONN_STRING")
OutputBinding<List<Receipt>> documentReceipts,
final ExecutionContext context) {

logger.info("[{}] function called at {}", context.getFunctionName(), LocalDateTime.now());

if (eventId != null) {

try {
BizEvent bizEvent = bizEventCosmosClient.getBizEventDocument(eventId);

//Retrieve receipt's data from CosmosDB
Receipt receipt = getReceipt(context, bizEvent, receiptCosmosClient, logger);


//Verify receipt status
if (receipt.getEventData() != null && isHasAllAttachments(receipt)) {

logger.info("[{}] Generating pdf for Receipt with id {} and bizEvent with id {}",
context.getFunctionName(),
receipt.getId(),
bizEvent.getId());
//Generate and save PDF
PdfGeneration pdfGeneration;
Path workingDirPath = createWorkingDirectory();
try {
pdfGeneration = generateReceiptPdfService.generateReceipts(receipt, bizEvent, workingDirPath);

//Verify PDF generation success
boolean success;
success = generateReceiptPdfService.verifyAndUpdateReceipt(receipt, pdfGeneration);

return success ?
request.createResponseBuilder(HttpStatus.OK)
.body("OK")
.build() :
request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ProblemJson.builder()
.title(HttpStatus.INTERNAL_SERVER_ERROR.name())
.detail("Receipt could not be updated with the new attachments")
.status(HttpStatus.INTERNAL_SERVER_ERROR.value())
.build())
.build();

} catch (Exception e) {
logger.error(e.getMessage(), e);
request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ProblemJson.builder()
.title(HttpStatus.INTERNAL_SERVER_ERROR.name())
.detail("Error during receipt generation: " + e.getMessage())
.status(HttpStatus.INTERNAL_SERVER_ERROR.value())
.build())
.build();
} finally {
deleteTempFolder(workingDirPath, logger);
}

}

} catch (ReceiptNotFoundException | BizEventNotFoundException exception) {
logger.error(exception.getMessage(), exception);
String message = "Missing required informations";
if (exception.getClass().equals(ReceiptNotFoundException.class)) {
message = "Receipt not found with event-id " + eventId;
} else if (exception.getClass().equals(BizEventNotFoundException.class)) {
message = "BizEvent not found with event-id " + eventId;
}
return request
.createResponseBuilder(HttpStatus.BAD_REQUEST)
.body(ProblemJson.builder()
.title(HttpStatus.BAD_REQUEST.name())
.detail(message)
.status(HttpStatus.BAD_REQUEST.value())
.build())
.build();
} catch (IOException e) {
logger.error(e.getMessage(), e);
return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ProblemJson.builder()
.title(HttpStatus.INTERNAL_SERVER_ERROR.name())
.detail("Unexpected error while managing the receipt file")
.status(HttpStatus.INTERNAL_SERVER_ERROR.value())
.build())
.build();
}

}
// if (eventId != null) {
//
// try {
// BizEvent bizEvent = bizEventCosmosClient.getBizEventDocument(eventId);
//
// //Retrieve receipt's data from CosmosDB
// Receipt receipt = getReceipt(context, bizEvent, receiptCosmosClient, logger);
//
//
// //Verify receipt status
// if (receipt.getEventData() != null && isHasAllAttachments(receipt)) {
//
// logger.info("[{}] Generating pdf for Receipt with id {} and bizEvent with id {}",
// context.getFunctionName(),
// receipt.getId(),
// bizEvent.getId());
// //Generate and save PDF
// PdfGeneration pdfGeneration;
// Path workingDirPath = createWorkingDirectory();
// try {
// pdfGeneration = generateReceiptPdfService.generateReceipts(receipt, bizEvent, workingDirPath);
//
// //Verify PDF generation success
// boolean success;
// success = generateReceiptPdfService.verifyAndUpdateReceipt(receipt, pdfGeneration);
//
// return success ?
// request.createResponseBuilder(HttpStatus.OK)
// .body("OK")
// .build() :
// request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
// .body(ProblemJson.builder()
// .title(HttpStatus.INTERNAL_SERVER_ERROR.name())
// .detail("Receipt could not be updated with the new attachments")
// .status(HttpStatus.INTERNAL_SERVER_ERROR.value())
// .build())
// .build();
//
// } catch (Exception e) {
// logger.error(e.getMessage(), e);
// request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
// .body(ProblemJson.builder()
// .title(HttpStatus.INTERNAL_SERVER_ERROR.name())
// .detail("Error during receipt generation: " + e.getMessage())
// .status(HttpStatus.INTERNAL_SERVER_ERROR.value())
// .build())
// .build();
// } finally {
// deleteTempFolder(workingDirPath, logger);
// }
//
// }
//
// } catch (ReceiptNotFoundException | BizEventNotFoundException exception) {
// logger.error(exception.getMessage(), exception);
// String message = "Missing required informations";
// if (exception.getClass().equals(ReceiptNotFoundException.class)) {
// message = "Receipt not found with event-id " + eventId;
// } else if (exception.getClass().equals(BizEventNotFoundException.class)) {
// message = "BizEvent not found with event-id " + eventId;
// }
// return request
// .createResponseBuilder(HttpStatus.BAD_REQUEST)
// .body(ProblemJson.builder()
// .title(HttpStatus.BAD_REQUEST.name())
// .detail(message)
// .status(HttpStatus.BAD_REQUEST.value())
// .build())
// .build();
// } catch (IOException e) {
// logger.error(e.getMessage(), e);
// return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR)
// .body(ProblemJson.builder()
// .title(HttpStatus.INTERNAL_SERVER_ERROR.name())
// .detail("Unexpected error while managing the receipt file")
// .status(HttpStatus.INTERNAL_SERVER_ERROR.value())
// .build())
// .build();
// }
//
// }

return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
.body(ProblemJson.builder()
Expand Down
Loading

0 comments on commit f1550ed

Please sign in to comment.