From 29fa7bfb88d5a8de4ebb703c926dcf33de37a23e Mon Sep 17 00:00:00 2001 From: saba_zedginidze Date: Tue, 5 Nov 2024 02:39:29 +0400 Subject: [PATCH] [MODINVOSTO-187] Send outbox events on invoice/line updates --- .../service/InvoiceLineStorageService.java | 13 +++-- .../folio/service/InvoiceStorageService.java | 57 ++++++++++--------- 2 files changed, 40 insertions(+), 30 deletions(-) diff --git a/src/main/java/org/folio/service/InvoiceLineStorageService.java b/src/main/java/org/folio/service/InvoiceLineStorageService.java index c5831fe9..8e7da797 100644 --- a/src/main/java/org/folio/service/InvoiceLineStorageService.java +++ b/src/main/java/org/folio/service/InvoiceLineStorageService.java @@ -10,8 +10,10 @@ import java.util.Map; import org.folio.dao.lines.InvoiceLinesDAO; +import org.folio.rest.jaxrs.model.EventAction; import org.folio.rest.jaxrs.model.InvoiceLine; import org.folio.rest.persist.DBClient; +import org.folio.service.audit.AuditOutboxService; import io.vertx.core.AsyncResult; import io.vertx.core.Context; @@ -24,6 +26,7 @@ @RequiredArgsConstructor public class InvoiceLineStorageService { + private final AuditOutboxService auditOutboxService; private final InvoiceLinesDAO invoiceLinesDAO; public void createInvoiceLine(InvoiceLine invoiceLine, Handler> asyncResultHandler, @@ -32,7 +35,8 @@ public void createInvoiceLine(InvoiceLine invoiceLine, Handler { log.info("createInvoiceLine:: Creating a new invoiceLine by id: {}", invoiceLine.getId()); new DBClient(vertxContext, headers).getPgClient() - .withTrans(conn -> invoiceLinesDAO.createInvoiceLine(invoiceLine, conn)) + .withTrans(conn -> invoiceLinesDAO.createInvoiceLine(invoiceLine, conn) + .compose(invoiceLineId -> auditOutboxService.saveInvoiceLineOutboxLog(conn, invoiceLine, EventAction.CREATE, headers))) .onSuccess(s -> { log.info("createInvoiceLine:: Successfully created a new invoiceLine by id: {}", invoiceLine.getId()); asyncResultHandler.handle(buildResponseWithLocation(headers.get(OKAPI_URL), INVOICE_LINES_PREFIX + invoiceLine.getId(), invoiceLine)); @@ -51,13 +55,14 @@ public void createInvoiceLine(InvoiceLine invoiceLine, Handler okapiHeaders, + public void updateInvoiceLine(String id, InvoiceLine invoiceLine, Map headers, Handler> asyncResultHandler, Context vertxContext) { try { vertxContext.runOnContext(v -> { log.info("updateInvoiceLine:: Updating invoice line with id: {}", id); - new DBClient(vertxContext, okapiHeaders).getPgClient() - .withTrans(conn -> invoiceLinesDAO.updateInvoiceLine(id, invoiceLine, conn)) + new DBClient(vertxContext, headers).getPgClient() + .withTrans(conn -> invoiceLinesDAO.updateInvoiceLine(id, invoiceLine, conn) + .compose(invoiceLineId -> auditOutboxService.saveInvoiceLineOutboxLog(conn, invoiceLine, EventAction.EDIT, headers))) .onSuccess(s -> { log.info("updateInvoiceLine:: Successfully updated invoice line with id: {}", id); asyncResultHandler.handle(buildNoContentResponse()); diff --git a/src/main/java/org/folio/service/InvoiceStorageService.java b/src/main/java/org/folio/service/InvoiceStorageService.java index 791f46b8..b8b23e99 100644 --- a/src/main/java/org/folio/service/InvoiceStorageService.java +++ b/src/main/java/org/folio/service/InvoiceStorageService.java @@ -19,11 +19,13 @@ import org.folio.dao.invoice.InvoiceDAO; import org.folio.rest.jaxrs.model.Document; import org.folio.rest.jaxrs.model.DocumentCollection; +import org.folio.rest.jaxrs.model.EventAction; import org.folio.rest.jaxrs.model.Invoice; import org.folio.rest.jaxrs.model.InvoiceDocument; import org.folio.rest.jaxrs.resource.InvoiceStorage.GetInvoiceStorageInvoicesDocumentsByIdResponse; import org.folio.rest.persist.DBClient; import org.folio.rest.persist.PgUtil; +import org.folio.service.audit.AuditOutboxService; import io.vertx.core.AsyncResult; import io.vertx.core.Context; @@ -39,6 +41,7 @@ public class InvoiceStorageService { private static final String INVOICE_ID_MISMATCH_ERROR_MESSAGE = "Invoice id mismatch"; private final InvoiceDAO invoiceDAO; + private final AuditOutboxService auditOutboxService; public void postInvoiceStorageInvoices(Invoice invoice, Handler> asyncResultHandler, Context vertxContext, Map headers) { @@ -46,7 +49,8 @@ public void postInvoiceStorageInvoices(Invoice invoice, Handler { log.info("postInvoiceStorageInvoices:: Creating a new invoice by id: {}", invoice.getId()); new DBClient(vertxContext, headers).getPgClient() - .withTrans(conn -> invoiceDAO.createInvoice(invoice, conn)) + .withTrans(conn -> invoiceDAO.createInvoice(invoice, conn) + .compose(invoiceId -> auditOutboxService.saveInvoiceOutboxLog(conn, invoice, EventAction.CREATE, headers))) .onSuccess(s -> { log.info("postInvoiceStorageInvoices:: Successfully created a new invoice by id: {}", invoice.getId()); asyncResultHandler.handle(buildResponseWithLocation(headers.get(OKAPI_URL), INVOICE_PREFIX + invoice.getId(), invoice)); @@ -65,6 +69,32 @@ public void postInvoiceStorageInvoices(Invoice invoice, Handler headers, + Handler> asyncResultHandler, Context vertxContext) { + try { + vertxContext.runOnContext(v -> { + log.info("putInvoiceStorageInvoicesById:: Updating invoice with id: {}", id); + new DBClient(vertxContext, headers).getPgClient() + .withTrans(conn -> invoiceDAO.updateInvoice(invoice, conn) + .compose(invoiceId -> auditOutboxService.saveInvoiceOutboxLog(conn, invoice, EventAction.EDIT, headers))) + .onSuccess(s -> { + log.info("putInvoiceStorageInvoicesById:: Successfully updated invoice with id: {}", id); + asyncResultHandler.handle(buildNoContentResponse()); + }) + .onFailure(f -> { + log.error("Error occurred while updating invoice with id: {}", id, f); + asyncResultHandler.handle(buildErrorResponse(f)); + }); + }); + } catch (Exception e) { + log.error("Error occurred while updating invoice with id: {}", invoice.getId(), e); + asyncResultHandler.handle(buildErrorResponse( + new HttpException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), + Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase()) + )); + } + } + public void deleteInvoiceStorageInvoicesById(String id, Handler> asyncResultHandler, Context vertxContext, Map headers) { try { @@ -96,31 +126,6 @@ public void deleteInvoiceStorageInvoicesById(String id, Handler okapiHeaders, - Handler> asyncResultHandler, Context vertxContext) { - try { - vertxContext.runOnContext(v -> { - log.info("putInvoiceStorageInvoicesById:: Updating invoice with id: {}", id); - new DBClient(vertxContext, okapiHeaders).getPgClient() - .withTrans(conn -> invoiceDAO.updateInvoice(invoice, conn)) - .onSuccess(s -> { - log.info("putInvoiceStorageInvoicesById:: Successfully updated invoice with id: {}", id); - asyncResultHandler.handle(buildNoContentResponse()); - }) - .onFailure(f -> { - log.error("Error occurred while updating invoice with id: {}", id, f); - asyncResultHandler.handle(buildErrorResponse(f)); - }); - }); - } catch (Exception e) { - log.error("Error occurred while updating invoice with id: {}", invoice.getId(), e); - asyncResultHandler.handle(buildErrorResponse( - new HttpException(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), - Response.Status.INTERNAL_SERVER_ERROR.getReasonPhrase()) - )); - } - } - public void getInvoiceStorageInvoicesDocumentsById(String id, int offset, int limit, String query, Map okapiHeaders, Handler> asyncResultHandler, Context vertxContext) { log.debug("getInvoiceStorageInvoicesDocumentsById:: Getting invoice documents by invoice id: {}", id);