Skip to content

Commit

Permalink
[MODINVOSTO-187] Refactor invoice creation to use conn
Browse files Browse the repository at this point in the history
  • Loading branch information
Saba-Zedginidze-EPAM committed Nov 4, 2024
1 parent e142bc7 commit 61ba6df
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/folio/dao/invoice/InvoiceDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public interface InvoiceDAO {

Future<Invoice> getInvoiceByIdForUpdate(String invoiceId, Conn conn);
Future<DBClient> createInvoice(Invoice invoice, DBClient client);
Future<String> createInvoice(Invoice invoice, Conn conn);
Future<Void> updateInvoice(Invoice invoice, Conn conn);
Future<DBClient> deleteInvoice(String id, DBClient client);
Future<DBClient> deleteInvoiceLinesByInvoiceId(String id, DBClient client);
Expand Down
21 changes: 7 additions & 14 deletions src/main/java/org/folio/dao/invoice/InvoicePostgresDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,26 +50,18 @@ public Future<Invoice> getInvoiceByIdForUpdate(String invoiceId, Conn conn) {
}

@Override
public Future<DBClient> createInvoice(Invoice invoice, DBClient client) {
public Future<String> createInvoice(Invoice invoice, Conn conn) {
log.info("Creating new invoice with id={}", invoice.getId());
Promise<DBClient> 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
Expand Down Expand Up @@ -185,7 +178,7 @@ public Future<InvoiceDocument> 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);
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/org/folio/rest/utils/ResponseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ public static <T> Handler<AsyncResult<Tx<T>>> handleNoContentResponse(Handler<As
}

public static void handleFailure(Promise<?> 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());
}
}

Expand Down
19 changes: 6 additions & 13 deletions src/main/java/org/folio/service/InvoiceStorageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,15 @@ public void postInvoiceStorageInvoices(Invoice invoice, Handler<AsyncResult<Resp
try {
vertxContext.runOnContext(v -> {
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) {
Expand Down

0 comments on commit 61ba6df

Please sign in to comment.