Skip to content

Commit

Permalink
[PRDP-220] Fixed Cosmos Client insert by generating custom id for rec…
Browse files Browse the repository at this point in the history
…eipt
  • Loading branch information
svariant committed Nov 17, 2023
1 parent 7e80c33 commit 74f9d7a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.cosmos.CosmosContainer;
import com.azure.cosmos.CosmosDatabase;
import com.azure.cosmos.models.CosmosItemResponse;
import com.azure.cosmos.models.CosmosQueryRequestOptions;
import com.azure.cosmos.models.FeedResponse;
import com.azure.cosmos.models.*;
import com.azure.cosmos.util.CosmosPagedIterable;
import it.gov.pagopa.receipt.pdf.datastore.client.ReceiptCosmosClient;
import it.gov.pagopa.receipt.pdf.datastore.entity.receipt.Receipt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,19 @@ public void handleSaveReceipt(Receipt receipt) {
CosmosItemResponse<Receipt> response = receiptCosmosClient.saveReceipts(receipt);

statusCode = response.getStatusCode();

if (statusCode == (HttpStatus.CREATED.value())) {
receipt.setId(response.getItem().getId());
return;
}
} catch (Exception e) {
statusCode = ReasonErrorCode.ERROR_COSMOS.getCode();
logger.error(String.format("Save receipt with eventId %s on cosmos failed", receipt.getEventId()), e);
}

String errorString = String.format(
"[BizEventToReceiptService] Error saving receipt to cosmos for receipt with eventId %s, cosmos client responded with status %s",
receipt.getEventId(), statusCode);
handleError(receipt, ReceiptStatusType.FAILED, errorString, statusCode);
//Error info
logger.error(errorString);
if (statusCode != (HttpStatus.CREATED.value())) {
String errorString = String.format(
"[BizEventToReceiptService] Error saving receipt to cosmos for receipt with eventId %s, cosmos client responded with status %s",
receipt.getEventId(), statusCode);
handleError(receipt, ReceiptStatusType.FAILED, errorString, statusCode);
//Error info
logger.error(errorString);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.util.Collections;
import java.util.List;
import java.util.UUID;

public class BizEventToReceiptUtils {

Expand All @@ -28,6 +29,7 @@ public static Receipt createReceipt(BizEvent bizEvent, BizEventToReceiptService
Receipt receipt = new Receipt();

// Insert biz-event data into receipt
receipt.setId(bizEvent.getId()+UUID.randomUUID());
receipt.setEventId(bizEvent.getId());

EventData eventData = new EventData();
Expand Down Expand Up @@ -58,9 +60,9 @@ public static Receipt createReceipt(BizEvent bizEvent, BizEventToReceiptService
/**
* Checks if the instance of Biz Event is in status DONE and contsains all required informations to process
* in the receipt generation
* @param bizEvent
* @param context
* @param logger
* @param bizEvent BizEvent to validate
* @param context Function context
* @param logger Function logger
* @return boolean to determine if the proposed event is invalid
*/
public static boolean isBizEventInvalid(BizEvent bizEvent, ExecutionContext context, Logger logger) {
Expand Down

0 comments on commit 74f9d7a

Please sign in to comment.