Skip to content

Commit

Permalink
[MODINVOSTO-187] Update invoice and invoice line API
Browse files Browse the repository at this point in the history
  • Loading branch information
Saba-Zedginidze-EPAM committed Nov 6, 2024
1 parent 61cfb60 commit b9294dd
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
12 changes: 12 additions & 0 deletions src/main/java/org/folio/dao/DbUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package org.folio.dao;

import static javax.ws.rs.core.Response.Status.NOT_FOUND;
import static org.folio.rest.persist.PostgresClient.convertToPsqlStandard;

import io.vertx.core.Future;
import io.vertx.ext.web.handler.HttpException;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;

public class DbUtils {

private static final String TABLE_NAME_TEMPLATE = "%s.%s";
Expand All @@ -10,4 +16,10 @@ public static String getTenantTableName(String tenantId, String tableName) {
return TABLE_NAME_TEMPLATE.formatted(convertToPsqlStandard(tenantId), tableName);
}

public static Future<Void> verifyEntityUpdate(RowSet<Row> updated) {
return updated.rowCount() == 1
? Future.succeededFuture()
: Future.failedFuture(new HttpException(NOT_FOUND.getStatusCode(), NOT_FOUND.getReasonPhrase()));
}

}
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 @@ -11,7 +11,7 @@ public interface InvoiceDAO {

Future<Invoice> getInvoiceByIdForUpdate(String invoiceId, Conn conn);
Future<String> createInvoice(Invoice invoice, Conn conn);
Future<Void> updateInvoice(Invoice invoice, Conn conn);
Future<Void> updateInvoice(String id, Invoice invoice, Conn conn);
Future<DBClient> deleteInvoice(String id, DBClient client);
Future<DBClient> deleteInvoiceLinesByInvoiceId(String id, DBClient client);
Future<DBClient> deleteInvoiceDocumentsByInvoiceId(String id, DBClient client);
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/org/folio/dao/invoice/InvoicePostgresDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.dao.DbUtils;
import org.folio.dbschema.ObjectMapperTool;
import org.folio.rest.jaxrs.model.Contents;
import org.folio.rest.jaxrs.model.DocumentMetadata;
Expand Down Expand Up @@ -65,9 +66,11 @@ public Future<String> createInvoice(Invoice invoice, Conn conn) {
}

@Override
public Future<Void> updateInvoice(Invoice invoice, Conn conn) {
return conn.update(INVOICE_TABLE, invoice, invoice.getId())
.onFailure(t -> log.error("updateInvoice failed for invoice with id {}", invoice.getId(), t))
public Future<Void> updateInvoice(String id, Invoice invoice, Conn conn) {
return conn.update(INVOICE_TABLE, invoice, id)
.compose(DbUtils::verifyEntityUpdate)
.onSuccess(v -> log.info("updateInvoice:: Invoice with id: '{}' successfully updated", invoice.getId()))
.onFailure(t -> log.error("Update failed for invoice with id {}", invoice.getId(), t))
.mapEmpty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.vertx.core.Future;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.dao.DbUtils;
import org.folio.rest.jaxrs.model.InvoiceLine;
import org.folio.rest.persist.Conn;
import org.folio.rest.persist.Criteria.Criterion;
Expand All @@ -11,6 +12,7 @@
import java.util.List;

import static org.folio.rest.impl.InvoiceStorageImpl.INVOICE_LINE_TABLE;
import static org.folio.rest.utils.ResponseUtils.convertPgExceptionIfNeeded;

public class InvoiceLinesPostgresDAO implements InvoiceLinesDAO {
private final Logger log = LogManager.getLogger();
Expand All @@ -27,7 +29,8 @@ public Future<List<InvoiceLine>> getInvoiceLines(Criterion criterion, Conn conn)
@Override
public Future<String> createInvoiceLine(InvoiceLine invoiceLine, Conn conn) {
log.trace("createInvoiceLine:: Creating invoice line: {}", invoiceLine);
return conn.save(INVOICE_LINE_TABLE, invoiceLine)
return conn.save(INVOICE_LINE_TABLE, invoiceLine.getId(), invoiceLine, true)
.recover(t -> Future.failedFuture(convertPgExceptionIfNeeded(t)))
.onSuccess(invoiceLineId -> log.info("createInvoiceLine:: Created invoice line with id: {}", invoiceLineId))
.onFailure(t -> log.error("Failed to create invoice line: {}", invoiceLine, t));
}
Expand All @@ -36,6 +39,7 @@ public Future<String> createInvoiceLine(InvoiceLine invoiceLine, Conn conn) {
public Future<Void> updateInvoiceLine(String id, InvoiceLine invoiceLine, Conn conn) {
log.trace("updateInvoiceLine:: Updating invoice line: {}", invoiceLine);
return conn.update(INVOICE_LINE_TABLE, invoiceLine, id)
.compose(DbUtils::verifyEntityUpdate)
.onSuccess(v -> log.info("updateInvoiceLine:: Updated invoice line with id: {}", id))
.onFailure(t -> log.error("Failed to update invoice line with id: {}", id, t))
.mapEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Future<InvoiceLineNumber> retrieveNewLineNumber(String invoiceId, DBClien
log.debug("Updating invoice {} with new nextInvoiceLineNumber", invoiceId);
int nextNumber = invoice.getNextInvoiceLineNumber();
invoice.setNextInvoiceLineNumber(nextNumber + 1);
return invoiceDAO.updateInvoice(invoice, conn)
return invoiceDAO.updateInvoice(invoiceId, invoice, conn)
.map(v -> nextNumber);
})
.map(n -> {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/folio/service/InvoiceStorageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void putInvoiceStorageInvoicesById(String id, Invoice invoice, Map<String
vertxContext.runOnContext(v -> {
log.info("putInvoiceStorageInvoicesById:: Updating invoice with id: {}", id);
new DBClient(vertxContext, headers).getPgClient()
.withTrans(conn -> invoiceDAO.updateInvoice(invoice, conn)
.withTrans(conn -> invoiceDAO.updateInvoice(id, invoice, conn)
.compose(invoiceId -> auditOutboxService.saveInvoiceOutboxLog(conn, invoice, InvoiceAuditEvent.Action.EDIT, headers)))
.onSuccess(s -> {
log.info("putInvoiceStorageInvoicesById:: Successfully updated invoice with id: {}", id);
Expand Down

0 comments on commit b9294dd

Please sign in to comment.