From 61ba6df7f34f7ac3512a4d86382252a4347d9ffd Mon Sep 17 00:00:00 2001 From: saba_zedginidze Date: Mon, 4 Nov 2024 16:38:14 +0400 Subject: [PATCH] [MODINVOSTO-187] Refactor invoice creation to use conn --- .../org/folio/dao/invoice/InvoiceDAO.java | 2 +- .../folio/dao/invoice/InvoicePostgresDAO.java | 21 +++++++------------ .../org/folio/rest/utils/ResponseUtils.java | 11 ++++++---- .../folio/service/InvoiceStorageService.java | 19 ++++++----------- 4 files changed, 21 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/folio/dao/invoice/InvoiceDAO.java b/src/main/java/org/folio/dao/invoice/InvoiceDAO.java index 8849753e..4e34af84 100644 --- a/src/main/java/org/folio/dao/invoice/InvoiceDAO.java +++ b/src/main/java/org/folio/dao/invoice/InvoiceDAO.java @@ -10,7 +10,7 @@ public interface InvoiceDAO { Future getInvoiceByIdForUpdate(String invoiceId, Conn conn); - Future createInvoice(Invoice invoice, DBClient client); + Future createInvoice(Invoice invoice, Conn conn); Future updateInvoice(Invoice invoice, Conn conn); Future deleteInvoice(String id, DBClient client); Future deleteInvoiceLinesByInvoiceId(String id, DBClient client); diff --git a/src/main/java/org/folio/dao/invoice/InvoicePostgresDAO.java b/src/main/java/org/folio/dao/invoice/InvoicePostgresDAO.java index 683c6502..debc457b 100644 --- a/src/main/java/org/folio/dao/invoice/InvoicePostgresDAO.java +++ b/src/main/java/org/folio/dao/invoice/InvoicePostgresDAO.java @@ -5,6 +5,7 @@ import static org.folio.rest.impl.InvoiceStorageImpl.INVOICE_ID_FIELD_NAME; import static org.folio.rest.impl.InvoiceStorageImpl.INVOICE_LINE_TABLE; import static org.folio.rest.impl.InvoiceStorageImpl.INVOICE_TABLE; +import static org.folio.rest.utils.ResponseUtils.convertPgExceptionIfNeeded; import static org.folio.rest.utils.ResponseUtils.handleFailure; import java.util.UUID; @@ -49,26 +50,18 @@ public Future getInvoiceByIdForUpdate(String invoiceId, Conn conn) { } @Override - public Future createInvoice(Invoice invoice, DBClient client) { + public Future createInvoice(Invoice invoice, Conn conn) { log.info("Creating new invoice with id={}", invoice.getId()); - Promise promise = Promise.promise(); if (invoice.getId() == null) { invoice.setId(UUID.randomUUID().toString()); } if (invoice.getNextInvoiceLineNumber() == null) { invoice.setNextInvoiceLineNumber(1); } - client.getPgClient().save(client.getConnection(), INVOICE_TABLE, invoice.getId(), invoice, reply -> { - if (reply.failed()) { - String errorMessage = String.format("Invoice creation with id=%s failed", invoice.getId()); - log.error(errorMessage, reply.cause()); - handleFailure(promise, reply); - } else { - log.info("New invoice with id={} successfully created", invoice.getId()); - promise.complete(client); - } - }); - return promise.future(); + return conn.save(INVOICE_TABLE, invoice.getId(), invoice, true) + .recover(t -> Future.failedFuture(convertPgExceptionIfNeeded(t))) + .onFailure(t -> log.error("createInvoice failed for invoice with id {}", invoice.getId(), t)) + .onSuccess(s -> log.info("createInvoice:: New invoice with id: '{}' successfully created", invoice.getId())); } @Override @@ -185,7 +178,7 @@ public Future getInvoiceDocument(String invoiceId, String docum invoiceDocument.setMetadata(documentMetadata.getMetadata()); String base64Content = reply.result().iterator().next().getString(1); - if (StringUtils.isNotEmpty(base64Content)){ + if (StringUtils.isNotEmpty(base64Content)) { invoiceDocument.setContents(new Contents().withData(base64Content)); } log.info("Invoice document with invoiceId={} and documentId={} successfully retrieved", invoiceId, documentId); diff --git a/src/main/java/org/folio/rest/utils/ResponseUtils.java b/src/main/java/org/folio/rest/utils/ResponseUtils.java index 2217d327..fdedf4b1 100644 --- a/src/main/java/org/folio/rest/utils/ResponseUtils.java +++ b/src/main/java/org/folio/rest/utils/ResponseUtils.java @@ -46,12 +46,15 @@ public static Handler>> handleNoContentResponse(Handler promise, AsyncResult reply) { - Throwable cause = reply.cause(); - String badRequestMessage = PgExceptionUtil.badRequestMessage(cause); + promise.fail(convertPgExceptionIfNeeded(reply.cause())); + } + + public static Throwable convertPgExceptionIfNeeded(Throwable cause) { + var badRequestMessage = PgExceptionUtil.badRequestMessage(cause); if (badRequestMessage != null) { - promise.fail(new HttpException(Response.Status.BAD_REQUEST.getStatusCode(), badRequestMessage)); + return new HttpException(Response.Status.BAD_REQUEST.getStatusCode(), badRequestMessage); } else { - promise.fail(new HttpException(INTERNAL_SERVER_ERROR.getStatusCode(), cause.getMessage())); + return new HttpException(INTERNAL_SERVER_ERROR.getStatusCode(), cause.getMessage()); } } diff --git a/src/main/java/org/folio/service/InvoiceStorageService.java b/src/main/java/org/folio/service/InvoiceStorageService.java index 4daae9c6..cb767057 100644 --- a/src/main/java/org/folio/service/InvoiceStorageService.java +++ b/src/main/java/org/folio/service/InvoiceStorageService.java @@ -50,22 +50,15 @@ public void postInvoiceStorageInvoices(Invoice invoice, Handler { log.info("postInvoiceStorageInvoices:: Creating a new invoice by id: {}", invoice.getId()); - - DBClient client = new DBClient(vertxContext, headers); - client.startTx() - .compose(t -> invoiceDAO.createInvoice(invoice, client)) - .compose(t -> client.endTx()) + new DBClient(vertxContext, headers).getPgClient() + .withTrans(conn -> invoiceDAO.createInvoice(invoice, conn)) .onComplete(reply -> { if (reply.failed()) { - // The result of rollback operation is not so important, main failure cause is used to build the response - client.rollbackTransaction().onComplete(res -> asyncResultHandler.handle(buildErrorResponse( - reply.cause()))); - } else { - log.info("postInvoiceStorageInvoices:: Preparing response to client"); - asyncResultHandler.handle( - buildResponseWithLocation(headers.get(OKAPI_URL), INVOICE_PREFIX + invoice.getId(), invoice) - ); + asyncResultHandler.handle(buildErrorResponse(reply.cause())); + return; } + log.info("postInvoiceStorageInvoices:: Preparing response to client"); + asyncResultHandler.handle(buildResponseWithLocation(headers.get(OKAPI_URL), INVOICE_PREFIX + invoice.getId(), invoice)); }); }); } catch (Exception e) {