Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MODAUD-180 Modify SQL query for /status-change-history to return records if claimingInterval changed #159

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
""";

Expand Down Expand Up @@ -116,10 +118,8 @@ public Future<PieceAuditEventCollection> getAuditEventsWithStatusChangesByPieceI
private PieceAuditEventCollection mapRowToListOfPieceEvent(RowSet<Row> 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;
}
Expand Down