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 09206956..cc1fd4ab 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 @@ -8,7 +8,6 @@ import static org.folio.util.AuditEventDBConstants.MODIFIED_CONTENT_FIELD; import static org.folio.util.AuditEventDBConstants.ORDER_BY_PATTERN; import static org.folio.util.AuditEventDBConstants.PIECE_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; @@ -41,13 +40,16 @@ 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 WHERE piece_id=$1 + LAG(modified_content_snapshot ->> 'receivingStatus') OVER w AS previous_status, + LAG(modified_content_snapshot ->> 'claimingInterval') OVER w AS previous_claiming_interval + FROM %s + WHERE piece_id=$1 + WINDOW w AS (PARTITION BY piece_id ORDER BY action_date) ) - SELECT id, action, piece_id, user_id, event_date, action_date, modified_content_snapshot, - (SELECT COUNT(*) AS total_records FROM StatusChanges - WHERE modified_content_snapshot ->> 'receivingStatus' <> COALESCE(previous_status, '')) - FROM StatusChanges WHERE modified_content_snapshot ->> 'receivingStatus' <> COALESCE(previous_status, '') + SELECT id, action, piece_id, user_id, event_date, action_date, modified_content_snapshot + FROM StatusChanges + WHERE modified_content_snapshot ->> 'receivingStatus' IS DISTINCT FROM previous_status + OR modified_content_snapshot ->> 'claimingInterval' IS DISTINCT FROM previous_claiming_interval %s LIMIT $2 OFFSET $3 """; @@ -116,10 +118,8 @@ public Future getAuditEventsWithStatusChangesByPieceI private PieceAuditEventCollection mapRowToListOfPieceEvent(RowSet rowSet) { LOGGER.debug("mapRowToListOfOrderEvent:: Mapping row to List of Piece Events"); PieceAuditEventCollection pieceAuditEventCollection = new PieceAuditEventCollection(); - rowSet.iterator().forEachRemaining(row -> { - pieceAuditEventCollection.getPieceAuditEvents().add(mapRowToPieceEvent(row)); - pieceAuditEventCollection.setTotalItems(row.getInteger(TOTAL_RECORDS_FIELD)); - }); + rowSet.iterator().forEachRemaining(row -> pieceAuditEventCollection.getPieceAuditEvents().add(mapRowToPieceEvent(row))); + pieceAuditEventCollection.setTotalItems(rowSet.rowCount()); LOGGER.debug("mapRowToListOfOrderEvent:: Mapped row to List of Piece Events"); return pieceAuditEventCollection; }