diff --git a/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/OrderEventsDaoImpl.java b/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/OrderEventsDaoImpl.java index 3b868a5..7360e01 100644 --- a/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/OrderEventsDaoImpl.java +++ b/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/OrderEventsDaoImpl.java @@ -1,5 +1,22 @@ package org.folio.dao.acquisition.impl; +import static java.lang.String.format; +import static org.folio.util.AuditEventDBConstants.ACTION_DATE_FIELD; +import static org.folio.util.AuditEventDBConstants.ACTION_FIELD; +import static org.folio.util.AuditEventDBConstants.EVENT_DATE_FIELD; +import static org.folio.util.AuditEventDBConstants.ID_FIELD; +import static org.folio.util.AuditEventDBConstants.MODIFIED_CONTENT_FIELD; +import static org.folio.util.AuditEventDBConstants.ORDER_BY_PATTERN; +import static org.folio.util.AuditEventDBConstants.ORDER_ID_FIELD; +import static org.folio.util.AuditEventDBConstants.TOTAL_RECORDS_FIELD; +import static org.folio.util.AuditEventDBConstants.USER_ID_FIELD; +import static org.folio.util.DbUtils.formatDBTableName; + +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.util.Date; +import java.util.UUID; + import io.vertx.core.Future; import io.vertx.core.Promise; import io.vertx.core.json.JsonObject; @@ -14,15 +31,6 @@ import org.folio.util.PostgresClientFactory; import org.springframework.stereotype.Repository; -import java.time.LocalDateTime; -import java.time.ZoneOffset; -import java.util.Date; -import java.util.UUID; - -import static java.lang.String.format; -import static org.folio.rest.persist.PostgresClient.convertToPsqlStandard; -import static org.folio.util.AuditEventDBConstants.*; - @Repository public class OrderEventsDaoImpl implements OrderEventsDao { @@ -46,10 +54,9 @@ public OrderEventsDaoImpl(PostgresClientFactory pgClientFactory) { public Future> save(OrderAuditEvent orderAuditEvent, String tenantId) { LOGGER.debug("save:: Saving Order AuditEvent with tenant id : {}", tenantId); Promise> promise = Promise.promise(); + LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId); String logTable = formatDBTableName(tenantId, TABLE_NAME); - String query = format(INSERT_SQL, logTable); - makeSaveCall(promise, query, orderAuditEvent, tenantId); LOGGER.info("save:: Saved Order AuditEvent with tenant id : {}", tenantId); return promise.future(); @@ -60,7 +67,7 @@ public Future getAuditEventsByOrderId(String orderId, LOGGER.debug("getAuditEventsByOrderId:: Retrieving AuditEvent with order id : {}", orderId); Promise> promise = Promise.promise(); try { - LOGGER.info("getAuditEventsByOrderId:: Trying to Retrieve AuditEvent with order id : {}", orderId); + LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId); String logTable = formatDBTableName(tenantId, TABLE_NAME); String query = format(GET_BY_ORDER_ID_SQL, logTable, logTable, format(ORDER_BY_PATTERN, sortBy, sortOrder)); Tuple queryParams = Tuple.of(UUID.fromString(orderId), limit, offset); @@ -116,8 +123,4 @@ private OrderAuditEvent mapRowToOrderEvent(Row row) { .withOrderSnapshot(JsonObject.mapFrom(row.getValue(MODIFIED_CONTENT_FIELD))); } - private String formatDBTableName(String tenantId, String table) { - LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId); - return format("%s.%s", convertToPsqlStandard(tenantId), table); - } } diff --git a/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/OrderLineEventsDaoImpl.java b/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/OrderLineEventsDaoImpl.java index 4add4a2..47a4555 100644 --- a/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/OrderLineEventsDaoImpl.java +++ b/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/OrderLineEventsDaoImpl.java @@ -1,5 +1,23 @@ package org.folio.dao.acquisition.impl; +import static java.lang.String.format; +import static org.folio.util.AuditEventDBConstants.ACTION_DATE_FIELD; +import static org.folio.util.AuditEventDBConstants.ACTION_FIELD; +import static org.folio.util.AuditEventDBConstants.EVENT_DATE_FIELD; +import static org.folio.util.AuditEventDBConstants.ID_FIELD; +import static org.folio.util.AuditEventDBConstants.MODIFIED_CONTENT_FIELD; +import static org.folio.util.AuditEventDBConstants.ORDER_BY_PATTERN; +import static org.folio.util.AuditEventDBConstants.ORDER_ID_FIELD; +import static org.folio.util.AuditEventDBConstants.ORDER_LINE_ID_FIELD; +import static org.folio.util.AuditEventDBConstants.TOTAL_RECORDS_FIELD; +import static org.folio.util.AuditEventDBConstants.USER_ID_FIELD; +import static org.folio.util.DbUtils.formatDBTableName; + +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.util.Date; +import java.util.UUID; + import io.vertx.core.Future; import io.vertx.core.Promise; import io.vertx.core.json.JsonObject; @@ -14,15 +32,6 @@ import org.folio.util.PostgresClientFactory; import org.springframework.stereotype.Repository; -import java.time.LocalDateTime; -import java.time.ZoneOffset; -import java.util.Date; -import java.util.UUID; - -import static java.lang.String.format; -import static org.folio.rest.persist.PostgresClient.convertToPsqlStandard; -import static org.folio.util.AuditEventDBConstants.*; - @Repository public class OrderLineEventsDaoImpl implements OrderLineEventsDao { @@ -39,7 +48,6 @@ public class OrderLineEventsDaoImpl implements OrderLineEventsDao { private final PostgresClientFactory pgClientFactory; - public OrderLineEventsDaoImpl(PostgresClientFactory pgClientFactory) { this.pgClientFactory = pgClientFactory; } @@ -48,12 +56,10 @@ public OrderLineEventsDaoImpl(PostgresClientFactory pgClientFactory) { public Future> save(OrderLineAuditEvent orderLineAuditEvent, String tenantId) { LOGGER.debug("save:: Saving OrderLine AuditEvent with tenant id : {}", tenantId); Promise> promise = Promise.promise(); + LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId); String logTable = formatDBTableName(tenantId, TABLE_NAME); - String query = format(INSERT_SQL, logTable); - makeSaveCall(promise, query, orderLineAuditEvent, tenantId); - LOGGER.info("save:: Saved OrderLine AuditEvent with tenant id : {}", tenantId); return promise.future(); } @@ -63,7 +69,7 @@ public Future getAuditEventsByOrderLineId(String LOGGER.debug("getAuditEventsByOrderLineId:: Retrieving AuditEvent with order line id : {}", orderLineId); Promise> promise = Promise.promise(); try { - LOGGER.info("getAuditEventsByOrderLineId:: Trying to Retrieve AuditEvent with order line id : {}", orderLineId); + LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId); String logTable = formatDBTableName(tenantId, TABLE_NAME); String query = format(GET_BY_ORDER_LINE_ID_SQL, logTable, logTable, format(ORDER_BY_PATTERN, sortBy, sortOrder)); Tuple queryParams = Tuple.of(UUID.fromString(orderLineId), limit, offset); @@ -120,9 +126,4 @@ private OrderLineAuditEvent mapRowToOrderLineEvent(Row row) { .withActionDate(Date.from(row.getLocalDateTime(ACTION_DATE_FIELD).toInstant(ZoneOffset.UTC))) .withOrderLineSnapshot(JsonObject.mapFrom(row.getValue(MODIFIED_CONTENT_FIELD))); } - - private String formatDBTableName(String tenantId, String table) { - LOGGER.debug("formatDBTableName:: Formatting DB Table Name with tenant id : {}", tenantId); - return format("%s.%s", convertToPsqlStandard(tenantId), table); - } } diff --git a/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/PieceEventsDaoImpl.java b/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/PieceEventsDaoImpl.java index 41f081b..6595eef 100644 --- a/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/PieceEventsDaoImpl.java +++ b/mod-audit-server/src/main/java/org/folio/dao/acquisition/impl/PieceEventsDaoImpl.java @@ -1,7 +1,6 @@ package org.folio.dao.acquisition.impl; import static java.lang.String.format; -import static org.folio.rest.persist.PostgresClient.convertToPsqlStandard; import static org.folio.util.AuditEventDBConstants.ACTION_DATE_FIELD; import static org.folio.util.AuditEventDBConstants.ACTION_FIELD; import static org.folio.util.AuditEventDBConstants.EVENT_DATE_FIELD; @@ -41,7 +40,8 @@ public class PieceEventsDaoImpl implements PieceEventsDao { private static final String GET_STATUS_CHANGE_HISTORY_BY_PIECE_ID_SQL = """ WITH StatusChanges AS (SELECT id, action, piece_id, user_id, event_date, action_date, modified_content_snapshot, - LAG(modified_content_snapshot ->> 'receivingStatus') OVER (PARTITION BY piece_id ORDER BY action_date) AS previous_status FROM %s + LAG(modified_content_snapshot ->> 'receivingStatus') OVER (PARTITION BY piece_id ORDER BY action_date) AS previous_status + FROM %s WHERE piece_id=$1 ) SELECT id, action, piece_id, user_id, event_date, action_date, modified_content_snapshot, (SELECT COUNT(*) AS total_records FROM StatusChanges