From a2b606bd165cb70dbd04267af72b70d08148caa9 Mon Sep 17 00:00:00 2001 From: Kapil Verma Date: Fri, 6 Sep 2024 17:01:00 +0530 Subject: [PATCH 1/9] CIRC-2138: added sync for request table post PrintEvent insertions --- ramls/request.json | 23 +++++++ .../org/folio/rest/impl/PrintEventsApi.java | 4 +- .../org/folio/service/PrintEventsService.java | 60 +++++++++++++++++-- 3 files changed, 80 insertions(+), 7 deletions(-) diff --git a/ramls/request.json b/ramls/request.json index aa582e033..b43bca206 100644 --- a/ramls/request.json +++ b/ramls/request.json @@ -221,6 +221,29 @@ "description": "Tags", "$ref": "raml-util/schemas/tags.schema" }, + "printDetails": { + "type": "object", + "description": "PrintDetails", + "properties": { + "printCount": { + "type": "integer", + "description": "Number of times print slip generated." + }, + "requesterId": { + "type": "string", + "description": "UUID of print slip requester." + }, + "isPrinted": { + "type": "boolean", + "description": "Whether print slip was printed in past." + }, + "printEventDate": { + "type": "string", + "format": "date-time", + "description": "Date and time when print slip was generated last time." + } + } + }, "awaitingPickupRequestClosedDate": { "description": "A date when the request with awaiting pickup status was closed", "type": "string", diff --git a/src/main/java/org/folio/rest/impl/PrintEventsApi.java b/src/main/java/org/folio/rest/impl/PrintEventsApi.java index 023de7d00..d544782c6 100644 --- a/src/main/java/org/folio/rest/impl/PrintEventsApi.java +++ b/src/main/java/org/folio/rest/impl/PrintEventsApi.java @@ -22,9 +22,7 @@ public class PrintEventsApi implements PrintEventsStorage { public void postPrintEventsStoragePrintEventsEntry(PrintEventsRequest printEventsRequest, Map okapiHeaders, Handler> asyncResultHandler, Context vertxContext) { LOG.info("postPrintEventsStoragePrintEvents:: save print events {}", printEventsRequest); new PrintEventsService(vertxContext, okapiHeaders) - .create(printEventsRequest) - .onSuccess(response -> asyncResultHandler.handle(succeededFuture(response))) - .onFailure(throwable -> asyncResultHandler.handle(succeededFuture(PostPrintEventsStoragePrintEventsEntryResponse.respond500WithTextPlain(throwable.getMessage())))); + .create(printEventsRequest, asyncResultHandler); } @Override diff --git a/src/main/java/org/folio/service/PrintEventsService.java b/src/main/java/org/folio/service/PrintEventsService.java index be0838237..8bd1866a1 100644 --- a/src/main/java/org/folio/service/PrintEventsService.java +++ b/src/main/java/org/folio/service/PrintEventsService.java @@ -2,10 +2,10 @@ import io.vertx.core.AsyncResult; import io.vertx.core.Context; -import io.vertx.core.Future; import io.vertx.core.Handler; import io.vertx.sqlclient.Row; import io.vertx.sqlclient.RowSet; +import io.vertx.sqlclient.Tuple; import org.folio.rest.RestVerticle; import org.folio.rest.jaxrs.model.PrintEventsRequest; import org.folio.rest.jaxrs.model.PrintEventsStatusResponse; @@ -14,6 +14,7 @@ import org.folio.rest.model.PrintEvent; import org.folio.rest.persist.PgUtil; import org.folio.rest.persist.PostgresClient; +import org.folio.rest.tools.utils.MetadataUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,12 +24,14 @@ import java.util.Date; import java.util.List; import java.util.Map; +import java.util.function.BiFunction; import java.util.stream.Collectors; import static io.vertx.core.Future.succeededFuture; import static org.folio.rest.persist.PgUtil.postgresClient; import static org.folio.rest.persist.PostgresClient.convertToPsqlStandard; import static org.folio.support.ModuleConstants.PRINT_EVENTS_TABLE; +import static org.folio.support.ModuleConstants.REQUEST_TABLE; public class PrintEventsService { @@ -36,6 +39,7 @@ public class PrintEventsService { private static final int MAX_ENTITIES = 10000; private final Context vertxContext; private final Map okapiHeaders; + private final PostgresClient postgresClient; private static final String PRINT_EVENT_FETCH_QUERY = """ WITH cte AS ( @@ -53,14 +57,43 @@ ORDER BY (jsonb->>'printEventDate')::timestamptz DESC) AS rank rank = 1; """; + private static BiFunction requestPrintSyncQueryFun = + (String schemaAndTableName, String ddd) -> String.format(""" + WITH print_counts AS ( + SELECT + jsonb->>'requestId' AS request_id, + COUNT(*) AS print_count + FROM %s + WHERE jsonb->>'requestId' IN ($1) + GROUP BY jsonb->>'requestId' + ) + UPDATE %s + SET jsonb = + (jsonb + || jsonb_build_object( + 'printDetails', + jsonb_build_object( + 'printCount', print_counts.print_count, + 'requesterId', $2, + 'printed', true, + 'printEventDate', $3 + ) + ) + ) + FROM print_counts + WHERE id = print_counts.request_id::uuid; + """, schemaAndTableName); + public PrintEventsService(Context vertxContext, Map okapiHeaders) { this.vertxContext = vertxContext; this.okapiHeaders = okapiHeaders; + this.postgresClient = PgUtil.postgresClient(vertxContext, okapiHeaders); } - public Future create(PrintEventsRequest printEventRequest) { + public void create(PrintEventsRequest printEventRequest, + Handler> asyncResultHandler) { LOG.info("create:: save print events {}", printEventRequest); List printEvents = printEventRequest.getRequestIds().stream().map(requestId -> { PrintEvent event = new PrintEvent(); @@ -70,8 +103,27 @@ public Future create(PrintEventsRequest printEventRequest) { event.setPrintEventDate(printEventRequest.getPrintEventDate()); return event; }).toList(); - return PgUtil.postSync(PRINT_EVENTS_TABLE, printEvents, MAX_ENTITIES, false, okapiHeaders, vertxContext, - PrintEventsStorage.PostPrintEventsStoragePrintEventsEntryResponse.class); + try { + MetadataUtil.populateMetadata(printEvents, okapiHeaders); + } catch (Throwable e) { + String msg = "Cannot populate metadata of printEvents list elements: " + e.getMessage(); + LOG.error(msg, e); + asyncResultHandler.handle(succeededFuture(PrintEventsStorage.PostPrintEventsStoragePrintEventsEntryResponse.respond500WithTextPlain(msg))); + return; + } + postgresClient.withTrans(conn -> conn.saveBatch(PRINT_EVENTS_TABLE, printEvents) + .onSuccess(handler -> { + String tenantId = okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT); + conn.execute(requestPrintSyncQueryFun.apply(convertToPsqlStandard(tenantId) + PRINT_EVENTS_TABLE, + convertToPsqlStandard(tenantId) + REQUEST_TABLE), + Tuple.of(printEventRequest.getRequestIds(), + printEventRequest.getRequesterId(), + printEventRequest.getPrintEventDate())) + .onSuccess(res -> asyncResultHandler.handle(succeededFuture(PrintEventsStorage.PostPrintEventsStoragePrintEventsEntryResponse.respond201()))) + .onFailure(throwable -> asyncResultHandler.handle(succeededFuture(PrintEventsStorage.PostPrintEventsStoragePrintEventsEntryResponse.respond500WithTextPlain(throwable.getMessage())))); + + }) + .onFailure(throwable -> asyncResultHandler.handle(succeededFuture(PrintEventsStorage.PostPrintEventsStoragePrintEventsEntryResponse.respond500WithTextPlain(throwable.getMessage()))))); } public void getPrintEventRequestDetails(List requestIds, Handler> asyncResultHandler) { From 5b904bfbc4443d7f1cbf8c55b096e980a8d9103d Mon Sep 17 00:00:00 2001 From: Kapil Verma Date: Mon, 9 Sep 2024 10:54:56 +0530 Subject: [PATCH 2/9] CIRC-2138: added sync for request table post PrintEvent insertions --- src/main/java/org/folio/service/PrintEventsService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/folio/service/PrintEventsService.java b/src/main/java/org/folio/service/PrintEventsService.java index 8bd1866a1..e80459f48 100644 --- a/src/main/java/org/folio/service/PrintEventsService.java +++ b/src/main/java/org/folio/service/PrintEventsService.java @@ -58,7 +58,7 @@ ORDER BY (jsonb->>'printEventDate')::timestamptz DESC) AS rank """; private static BiFunction requestPrintSyncQueryFun = - (String schemaAndTableName, String ddd) -> String.format(""" + (String printEventTableName, String requestTableName) -> String.format(""" WITH print_counts AS ( SELECT jsonb->>'requestId' AS request_id, @@ -82,7 +82,7 @@ WITH print_counts AS ( ) FROM print_counts WHERE id = print_counts.request_id::uuid; - """, schemaAndTableName); + """, printEventTableName, requestTableName); From f9d30305d85b88d5697d588fce5179daf6840b3e Mon Sep 17 00:00:00 2001 From: Kapil Verma Date: Mon, 9 Sep 2024 14:59:59 +0530 Subject: [PATCH 3/9] CIRC-2138: added sync for request table post PrintEvent insertions --- .../org/folio/service/PrintEventsService.java | 62 +++++++++++++++---- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/folio/service/PrintEventsService.java b/src/main/java/org/folio/service/PrintEventsService.java index e80459f48..f103bac39 100644 --- a/src/main/java/org/folio/service/PrintEventsService.java +++ b/src/main/java/org/folio/service/PrintEventsService.java @@ -19,11 +19,13 @@ import org.slf4j.LoggerFactory; import javax.ws.rs.core.Response; +import java.text.SimpleDateFormat; import java.time.ZoneOffset; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; +import java.util.TimeZone; import java.util.function.BiFunction; import java.util.stream.Collectors; @@ -111,19 +113,53 @@ public void create(PrintEventsRequest printEventRequest, asyncResultHandler.handle(succeededFuture(PrintEventsStorage.PostPrintEventsStoragePrintEventsEntryResponse.respond500WithTextPlain(msg))); return; } - postgresClient.withTrans(conn -> conn.saveBatch(PRINT_EVENTS_TABLE, printEvents) - .onSuccess(handler -> { - String tenantId = okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT); - conn.execute(requestPrintSyncQueryFun.apply(convertToPsqlStandard(tenantId) + PRINT_EVENTS_TABLE, - convertToPsqlStandard(tenantId) + REQUEST_TABLE), - Tuple.of(printEventRequest.getRequestIds(), - printEventRequest.getRequesterId(), - printEventRequest.getPrintEventDate())) - .onSuccess(res -> asyncResultHandler.handle(succeededFuture(PrintEventsStorage.PostPrintEventsStoragePrintEventsEntryResponse.respond201()))) - .onFailure(throwable -> asyncResultHandler.handle(succeededFuture(PrintEventsStorage.PostPrintEventsStoragePrintEventsEntryResponse.respond500WithTextPlain(throwable.getMessage())))); - - }) - .onFailure(throwable -> asyncResultHandler.handle(succeededFuture(PrintEventsStorage.PostPrintEventsStoragePrintEventsEntryResponse.respond500WithTextPlain(throwable.getMessage()))))); + + String tenantId = okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT); + String printEventTableName = + convertToPsqlStandard(tenantId) + "." + PRINT_EVENTS_TABLE; + String requestTableName = + convertToPsqlStandard(tenantId) + "." + REQUEST_TABLE; + +// postgresClient +// .execute( +// requestPrintSyncQueryFun.apply(printEventTableName, +// requestTableName), +// buildRequestPrintSyncQueryParams(printEventRequest) +// ).onSuccess(res -> asyncResultHandler.handle(succeededFuture(PrintEventsStorage.PostPrintEventsStoragePrintEventsEntryResponse.respond201()))) +// .onFailure(throwable -> asyncResultHandler.handle(succeededFuture(PrintEventsStorage.PostPrintEventsStoragePrintEventsEntryResponse.respond500WithTextPlain(throwable.getMessage())))); + postgresClient.withTrans(conn -> { + // First saveBatch operationx + return conn.saveBatch(PRINT_EVENTS_TABLE, printEvents) + .compose(printEventsResult -> { + // Now proceed to the second operation after the first succeeds + return conn.execute( + requestPrintSyncQueryFun.apply(printEventTableName, + requestTableName), + buildRequestPrintSyncQueryParams(printEventRequest) + ); + }); + }).onFailure(handler -> + asyncResultHandler.handle( + succeededFuture(PrintEventsStorage.PostPrintEventsStoragePrintEventsEntryResponse.respond500WithTextPlain(handler.getMessage())) + ) + ) // Handle failure + .onSuccess(handler -> + asyncResultHandler.handle( + succeededFuture(PrintEventsStorage.PostPrintEventsStoragePrintEventsEntryResponse.respond201()) + ) + ); + } + + private static Tuple buildRequestPrintSyncQueryParams(PrintEventsRequest printEventRequest) { + SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); + df.setTimeZone(TimeZone.getTimeZone(ZoneOffset.UTC)); + return Tuple.of( + printEventRequest.getRequestIds().stream() + .map(requestId -> "'" + requestId + "'") + .collect(Collectors.joining(", ")), + printEventRequest.getRequesterId(), + df.format(printEventRequest.getPrintEventDate()) + ); } public void getPrintEventRequestDetails(List requestIds, Handler> asyncResultHandler) { From eee5608c94358be716d52fe090348f00199aab7c Mon Sep 17 00:00:00 2001 From: Kapil Verma Date: Mon, 9 Sep 2024 16:11:18 +0530 Subject: [PATCH 4/9] CIRC-2138: added sync for request table post PrintEvent insertions --- .../org/folio/service/PrintEventsService.java | 74 +++++++++---------- 1 file changed, 33 insertions(+), 41 deletions(-) diff --git a/src/main/java/org/folio/service/PrintEventsService.java b/src/main/java/org/folio/service/PrintEventsService.java index f103bac39..dfe4ec6d2 100644 --- a/src/main/java/org/folio/service/PrintEventsService.java +++ b/src/main/java/org/folio/service/PrintEventsService.java @@ -5,7 +5,6 @@ import io.vertx.core.Handler; import io.vertx.sqlclient.Row; import io.vertx.sqlclient.RowSet; -import io.vertx.sqlclient.Tuple; import org.folio.rest.RestVerticle; import org.folio.rest.jaxrs.model.PrintEventsRequest; import org.folio.rest.jaxrs.model.PrintEventsStatusResponse; @@ -26,7 +25,6 @@ import java.util.List; import java.util.Map; import java.util.TimeZone; -import java.util.function.BiFunction; import java.util.stream.Collectors; import static io.vertx.core.Future.succeededFuture; @@ -59,14 +57,13 @@ ORDER BY (jsonb->>'printEventDate')::timestamptz DESC) AS rank rank = 1; """; - private static BiFunction requestPrintSyncQueryFun = - (String printEventTableName, String requestTableName) -> String.format(""" + private static String requestPrintSyncQueryString = """ WITH print_counts AS ( SELECT jsonb->>'requestId' AS request_id, COUNT(*) AS print_count FROM %s - WHERE jsonb->>'requestId' IN ($1) + WHERE jsonb->>'requestId' IN (%s) GROUP BY jsonb->>'requestId' ) UPDATE %s @@ -76,15 +73,15 @@ WITH print_counts AS ( 'printDetails', jsonb_build_object( 'printCount', print_counts.print_count, - 'requesterId', $2, + 'requesterId', %s, 'printed', true, - 'printEventDate', $3 + 'printEventDate', %s ) ) ) FROM print_counts WHERE id = print_counts.request_id::uuid; - """, printEventTableName, requestTableName); + """; @@ -114,35 +111,20 @@ public void create(PrintEventsRequest printEventRequest, return; } - String tenantId = okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT); - String printEventTableName = - convertToPsqlStandard(tenantId) + "." + PRINT_EVENTS_TABLE; - String requestTableName = - convertToPsqlStandard(tenantId) + "." + REQUEST_TABLE; - -// postgresClient -// .execute( -// requestPrintSyncQueryFun.apply(printEventTableName, -// requestTableName), -// buildRequestPrintSyncQueryParams(printEventRequest) -// ).onSuccess(res -> asyncResultHandler.handle(succeededFuture(PrintEventsStorage.PostPrintEventsStoragePrintEventsEntryResponse.respond201()))) -// .onFailure(throwable -> asyncResultHandler.handle(succeededFuture(PrintEventsStorage.PostPrintEventsStoragePrintEventsEntryResponse.respond500WithTextPlain(throwable.getMessage())))); postgresClient.withTrans(conn -> { - // First saveBatch operationx - return conn.saveBatch(PRINT_EVENTS_TABLE, printEvents) - .compose(printEventsResult -> { - // Now proceed to the second operation after the first succeeds - return conn.execute( - requestPrintSyncQueryFun.apply(printEventTableName, - requestTableName), - buildRequestPrintSyncQueryParams(printEventRequest) - ); - }); - }).onFailure(handler -> + // First saveBatch operationx + return conn.saveBatch(PRINT_EVENTS_TABLE, printEvents) + .compose(printEventsResult -> { + // Now proceed to the second operation after the first succeeds + return conn.execute( + buildRequestSyncQuery(printEventRequest, okapiHeaders) + ); + }); + }).onFailure(handler -> asyncResultHandler.handle( succeededFuture(PrintEventsStorage.PostPrintEventsStoragePrintEventsEntryResponse.respond500WithTextPlain(handler.getMessage())) ) - ) // Handle failure + ) .onSuccess(handler -> asyncResultHandler.handle( succeededFuture(PrintEventsStorage.PostPrintEventsStoragePrintEventsEntryResponse.respond201()) @@ -150,16 +132,26 @@ public void create(PrintEventsRequest printEventRequest, ); } - private static Tuple buildRequestPrintSyncQueryParams(PrintEventsRequest printEventRequest) { + private String buildRequestSyncQuery(PrintEventsRequest printEventRequest, + Map okapiHeaders) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); df.setTimeZone(TimeZone.getTimeZone(ZoneOffset.UTC)); - return Tuple.of( - printEventRequest.getRequestIds().stream() - .map(requestId -> "'" + requestId + "'") - .collect(Collectors.joining(", ")), - printEventRequest.getRequesterId(), - df.format(printEventRequest.getPrintEventDate()) - ); + + String tenantId = okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT); + String printEventTableName = + convertToPsqlStandard(tenantId) + "." + PRINT_EVENTS_TABLE; + String requestTableName = + convertToPsqlStandard(tenantId) + "." + REQUEST_TABLE; + String requestIds = printEventRequest.getRequestIds().stream() + .map(requestId -> "'" + requestId + "'") + .collect(Collectors.joining(", ")); + String requesterId = "'" + printEventRequest.getRequesterId() + "'"; + String printEventDate = + "'" + df.format(printEventRequest.getPrintEventDate()) + "'"; + + return requestPrintSyncQueryString.formatted(printEventTableName, + requestIds, requestTableName, requesterId, + printEventDate); } public void getPrintEventRequestDetails(List requestIds, Handler> asyncResultHandler) { From a26146e9333d21ff4b0183a5bfe19fc212c1fab7 Mon Sep 17 00:00:00 2001 From: Kapil Verma Date: Fri, 13 Sep 2024 15:41:33 +0530 Subject: [PATCH 5/9] CIRC-2138: set additionalProperties false for request.printDetails --- ramls/request.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ramls/request.json b/ramls/request.json index b43bca206..17b591a42 100644 --- a/ramls/request.json +++ b/ramls/request.json @@ -242,7 +242,8 @@ "format": "date-time", "description": "Date and time when print slip was generated last time." } - } + }, + "additionalProperties": false }, "awaitingPickupRequestClosedDate": { "description": "A date when the request with awaiting pickup status was closed", From d9bb7ff191d03510e262229eddeea488b67370ef Mon Sep 17 00:00:00 2001 From: Kapil Verma Date: Fri, 13 Sep 2024 16:30:01 +0530 Subject: [PATCH 6/9] CIRC-2138: fixed printed to isPrinted in printDetails in request sync query --- src/main/java/org/folio/service/PrintEventsService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/folio/service/PrintEventsService.java b/src/main/java/org/folio/service/PrintEventsService.java index dfe4ec6d2..efdaa3822 100644 --- a/src/main/java/org/folio/service/PrintEventsService.java +++ b/src/main/java/org/folio/service/PrintEventsService.java @@ -74,7 +74,7 @@ WITH print_counts AS ( jsonb_build_object( 'printCount', print_counts.print_count, 'requesterId', %s, - 'printed', true, + 'isPrinted', true, 'printEventDate', %s ) ) From 6cdb47dd0807ab40ac47a8d5f26ba7ce4fe3acaf Mon Sep 17 00:00:00 2001 From: Kapil Verma Date: Mon, 16 Sep 2024 15:27:03 +0530 Subject: [PATCH 7/9] CIRC-2138: fixed sonar issues --- .../org/folio/rest/impl/PrintEventsApi.java | 2 -- .../org/folio/service/PrintEventsService.java | 33 ++++++++----------- 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/folio/rest/impl/PrintEventsApi.java b/src/main/java/org/folio/rest/impl/PrintEventsApi.java index d544782c6..fc707b131 100644 --- a/src/main/java/org/folio/rest/impl/PrintEventsApi.java +++ b/src/main/java/org/folio/rest/impl/PrintEventsApi.java @@ -13,8 +13,6 @@ import javax.ws.rs.core.Response; import java.util.Map; -import static io.vertx.core.Future.succeededFuture; - public class PrintEventsApi implements PrintEventsStorage { private static final Logger LOG = LoggerFactory.getLogger(PrintEventsApi.class); diff --git a/src/main/java/org/folio/service/PrintEventsService.java b/src/main/java/org/folio/service/PrintEventsService.java index efdaa3822..5a3e361c0 100644 --- a/src/main/java/org/folio/service/PrintEventsService.java +++ b/src/main/java/org/folio/service/PrintEventsService.java @@ -36,7 +36,6 @@ public class PrintEventsService { private static final Logger LOG = LoggerFactory.getLogger(PrintEventsService.class); - private static final int MAX_ENTITIES = 10000; private final Context vertxContext; private final Map okapiHeaders; private final PostgresClient postgresClient; @@ -92,9 +91,10 @@ public PrintEventsService(Context vertxContext, Map okapiHeaders } public void create(PrintEventsRequest printEventRequest, - Handler> asyncResultHandler) { + Handler> asyncResultHandler) { LOG.info("create:: save print events {}", printEventRequest); - List printEvents = printEventRequest.getRequestIds().stream().map(requestId -> { + List printEvents = + printEventRequest.getRequestIds().stream().map(requestId -> { PrintEvent event = new PrintEvent(); event.setRequestId(requestId); event.setRequesterId(printEventRequest.getRequesterId()); @@ -104,32 +104,26 @@ public void create(PrintEventsRequest printEventRequest, }).toList(); try { MetadataUtil.populateMetadata(printEvents, okapiHeaders); - } catch (Throwable e) { - String msg = "Cannot populate metadata of printEvents list elements: " + e.getMessage(); + } catch (Exception e) { + String msg = + "Cannot populate metadata of printEvents list elements: " + e.getMessage(); LOG.error(msg, e); asyncResultHandler.handle(succeededFuture(PrintEventsStorage.PostPrintEventsStoragePrintEventsEntryResponse.respond500WithTextPlain(msg))); return; } - postgresClient.withTrans(conn -> { - // First saveBatch operationx - return conn.saveBatch(PRINT_EVENTS_TABLE, printEvents) - .compose(printEventsResult -> { - // Now proceed to the second operation after the first succeeds - return conn.execute( - buildRequestSyncQuery(printEventRequest, okapiHeaders) - ); - }); - }).onFailure(handler -> + postgresClient.withTrans(conn -> conn.saveBatch(PRINT_EVENTS_TABLE, + printEvents) + .compose(printEventsResult -> conn.execute( + buildRequestSyncQuery(printEventRequest, okapiHeaders) + ))).onFailure(handler -> asyncResultHandler.handle( succeededFuture(PrintEventsStorage.PostPrintEventsStoragePrintEventsEntryResponse.respond500WithTextPlain(handler.getMessage())) ) - ) - .onSuccess(handler -> + ).onSuccess(handler -> asyncResultHandler.handle( succeededFuture(PrintEventsStorage.PostPrintEventsStoragePrintEventsEntryResponse.respond201()) - ) - ); + )); } private String buildRequestSyncQuery(PrintEventsRequest printEventRequest, @@ -157,7 +151,6 @@ private String buildRequestSyncQuery(PrintEventsRequest printEventRequest, public void getPrintEventRequestDetails(List requestIds, Handler> asyncResultHandler) { LOG.debug("getPrintEventRequestDetails:: Fetching print event details for requestIds {}", requestIds); String tenantId = okapiHeaders.get(RestVerticle.OKAPI_HEADER_TENANT); - PostgresClient postgresClient = postgresClient(vertxContext, okapiHeaders); postgresClient.execute(formatQuery(tenantId, requestIds), handler -> { try { if (handler.succeeded()) { From 2a7598cd44a792fa1005f7418cd057de517c8b80 Mon Sep 17 00:00:00 2001 From: Kapil Verma Date: Mon, 23 Sep 2024 16:28:51 +0530 Subject: [PATCH 8/9] CIRC-2138: addded test case for 5XX case --- .../java/org/folio/service/PrintEventsService.java | 2 -- .../java/org/folio/rest/api/PrintEventsAPITest.java | 13 +++++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/folio/service/PrintEventsService.java b/src/main/java/org/folio/service/PrintEventsService.java index 5a3e361c0..d1e6b634d 100644 --- a/src/main/java/org/folio/service/PrintEventsService.java +++ b/src/main/java/org/folio/service/PrintEventsService.java @@ -36,7 +36,6 @@ public class PrintEventsService { private static final Logger LOG = LoggerFactory.getLogger(PrintEventsService.class); - private final Context vertxContext; private final Map okapiHeaders; private final PostgresClient postgresClient; @@ -85,7 +84,6 @@ WITH print_counts AS ( public PrintEventsService(Context vertxContext, Map okapiHeaders) { - this.vertxContext = vertxContext; this.okapiHeaders = okapiHeaders; this.postgresClient = PgUtil.postgresClient(vertxContext, okapiHeaders); } diff --git a/src/test/java/org/folio/rest/api/PrintEventsAPITest.java b/src/test/java/org/folio/rest/api/PrintEventsAPITest.java index c7f932482..2ea8445bb 100644 --- a/src/test/java/org/folio/rest/api/PrintEventsAPITest.java +++ b/src/test/java/org/folio/rest/api/PrintEventsAPITest.java @@ -15,6 +15,7 @@ import static org.folio.rest.support.http.InterfaceUrls.printEventsUrl; import static org.folio.rest.support.matchers.HttpResponseStatusCodeMatchers.isCreated; +import static org.folio.rest.support.matchers.HttpResponseStatusCodeMatchers.isInternalServerError; import static org.folio.rest.support.matchers.HttpResponseStatusCodeMatchers.isOk; import static org.folio.rest.support.matchers.HttpResponseStatusCodeMatchers.isUnprocessableEntity; import static org.hamcrest.core.Is.is; @@ -180,6 +181,18 @@ public void getPrintEventStatusWithInvalidRequestIds() throws MalformedURLExcept assertThat(jsonObject.getJsonArray("printEventsStatusResponses").size(), is(0)); } + @Test + public void createPrintEventLogAndValidate5XX() throws MalformedURLException, + ExecutionException, InterruptedException { + JsonObject printEventsJson = getPrintEvent(); + final CompletableFuture postCompleted = new CompletableFuture<>(); + client.post(printEventsUrl("/print-events-entry"), printEventsJson, + "INVALID_TENANT_ID", + ResponseHandler.json(postCompleted)); + final JsonResponse postResponse = postCompleted.get(); + assertThat(postResponse, isInternalServerError()); + } + private JsonObject getPrintEvent() { List requestIds = List.of("5f5751b4-e352-4121-adca-204b0c2aec43", "5f5751b4-e352-4121-adca-204b0c2aec44"); return new JsonObject() From a0e8d4b79d11d141dc34c837f1df0a36a225f6c8 Mon Sep 17 00:00:00 2001 From: Kapil Verma Date: Tue, 24 Sep 2024 17:15:55 +0530 Subject: [PATCH 9/9] CIRC-2138: Bump up interface version --- descriptors/ModuleDescriptor-template.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/descriptors/ModuleDescriptor-template.json b/descriptors/ModuleDescriptor-template.json index f4f17887d..f3cf1ef27 100644 --- a/descriptors/ModuleDescriptor-template.json +++ b/descriptors/ModuleDescriptor-template.json @@ -179,7 +179,7 @@ }, { "id": "request-storage", - "version": "6.0", + "version": "6.1", "handlers": [ { "methods": ["GET"],