From 74f9d7a09edde118336edc8490ff040c062f378b Mon Sep 17 00:00:00 2001 From: svariant Date: Fri, 17 Nov 2023 15:40:09 +0100 Subject: [PATCH] [PRDP-220] Fixed Cosmos Client insert by generating custom id for receipt --- .../client/impl/ReceiptCosmosClientImpl.java | 4 +--- .../impl/BizEventToReceiptServiceImpl.java | 19 ++++++++----------- .../utils/BizEventToReceiptUtils.java | 8 +++++--- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/datastore/client/impl/ReceiptCosmosClientImpl.java b/src/main/java/it/gov/pagopa/receipt/pdf/datastore/client/impl/ReceiptCosmosClientImpl.java index 22acf60d..2d0026df 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/datastore/client/impl/ReceiptCosmosClientImpl.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/datastore/client/impl/ReceiptCosmosClientImpl.java @@ -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; diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/datastore/service/impl/BizEventToReceiptServiceImpl.java b/src/main/java/it/gov/pagopa/receipt/pdf/datastore/service/impl/BizEventToReceiptServiceImpl.java index 5739daa1..5a31e6c7 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/datastore/service/impl/BizEventToReceiptServiceImpl.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/datastore/service/impl/BizEventToReceiptServiceImpl.java @@ -87,22 +87,19 @@ public void handleSaveReceipt(Receipt receipt) { CosmosItemResponse 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); + } } /** diff --git a/src/main/java/it/gov/pagopa/receipt/pdf/datastore/utils/BizEventToReceiptUtils.java b/src/main/java/it/gov/pagopa/receipt/pdf/datastore/utils/BizEventToReceiptUtils.java index edf5bb8a..43f8798f 100644 --- a/src/main/java/it/gov/pagopa/receipt/pdf/datastore/utils/BizEventToReceiptUtils.java +++ b/src/main/java/it/gov/pagopa/receipt/pdf/datastore/utils/BizEventToReceiptUtils.java @@ -14,6 +14,7 @@ import java.util.Collections; import java.util.List; +import java.util.UUID; public class BizEventToReceiptUtils { @@ -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(); @@ -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) {