Skip to content

Commit

Permalink
[MODAUD-180] - Modify SQL query for /status-change-history to return …
Browse files Browse the repository at this point in the history
…records if claimingInterval changed (#161)

* [MODAUD-180] - Modify SQL query for /status-change-history to return records if claimingInterval changed

* [MODAUD-180] - Fixed Tests

* [MODAUD-180] - Fixed Tests

* [MODAUD-180] - Modify SQL query for /status-change-history to return records if claimingInterval changed

* [MODAUD-180] - Modify SQL query for /status-change-history to return records if claimingInterval changed
  • Loading branch information
azizbekxm authored Jan 31, 2024
1 parent 3580971 commit b442237
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,18 @@ 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 ->> 'claimingInterval') OVER (PARTITION BY piece_id ORDER BY action_date) AS previous_status
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 ->> 'claimingInterval' <> COALESCE(previous_status, ''))
FROM StatusChanges WHERE modified_content_snapshot ->> 'claimingInterval' <> COALESCE(previous_status, '')
WHERE modified_content_snapshot ->> 'receivingStatus' IS DISTINCT FROM previous_status
OR modified_content_snapshot ->> 'claimingInterval' IS DISTINCT FROM previous_claiming_interval)
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
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void setUp() {
@Test
void shouldReturnOrderEventsOnGetByOrderId() {
JsonObject jsonObject = new JsonObject();
jsonObject.put("name","Test Product2");
jsonObject.put("name", "Test Product2");

OrderAuditEvent orderAuditEvent = new OrderAuditEvent()
.withId(UUID.randomUUID().toString())
Expand All @@ -73,23 +73,23 @@ void shouldReturnOrderEventsOnGetByOrderId() {

orderEventDao.save(orderAuditEvent, TENANT_ID);

given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_ORDER_PATH+ INVALID_ID).then().log().all().statusCode(200)
given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_ORDER_PATH + INVALID_ID).then().log().all().statusCode(200)
.body(containsString("orderAuditEvents")).body(containsString("totalItems"));

given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_ORDER_PATH+ ORDER_ID).then().log().all().statusCode(200)
given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_ORDER_PATH + ORDER_ID).then().log().all().statusCode(200)
.body(containsString(ORDER_ID));

given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_ORDER_PATH+ ORDER_ID +"?limit=1").then().log().all().statusCode(200)
given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_ORDER_PATH + ORDER_ID + "?limit=1").then().log().all().statusCode(200)
.body(containsString(ORDER_ID));

given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_ORDER_PATH+ ORDER_ID + 123).then().log().all().statusCode(500)
given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_ORDER_PATH + ORDER_ID + 123).then().log().all().statusCode(500)
.body(containsString("UUID string too large"));
}

@Test
void shouldReturnOrderLineEventsOnGetByOrderLineId() {
JsonObject jsonObject = new JsonObject();
jsonObject.put("name","Test Product2");
jsonObject.put("name", "Test Product2");

OrderLineAuditEvent orderLineAuditEvent = new OrderLineAuditEvent()
.withId(UUID.randomUUID().toString())
Expand All @@ -103,28 +103,28 @@ void shouldReturnOrderLineEventsOnGetByOrderLineId() {

orderLineEventDao.save(orderLineAuditEvent, TENANT_ID);

given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_ORDER_LINE_PATH+ INVALID_ID)
given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_ORDER_LINE_PATH + INVALID_ID)
.then().log().all().statusCode(200)
.body(containsString("orderLineAuditEvents")).body(containsString("totalItems"));

given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_ORDER_LINE_PATH+ ORDER_LINE_ID)
given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_ORDER_LINE_PATH + ORDER_LINE_ID)
.then().log().all().statusCode(200)
.body(containsString(ORDER_LINE_ID));

given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_ORDER_LINE_PATH+ ORDER_LINE_ID +"?limit=1").then().log().all().statusCode(200)
given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_ORDER_LINE_PATH + ORDER_LINE_ID + "?limit=1").then().log().all().statusCode(200)
.body(containsString(ORDER_LINE_ID));

given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_ORDER_LINE_PATH+ ORDER_LINE_ID +"?sortBy=action_date").then().log().all().statusCode(200)
given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_ORDER_LINE_PATH + ORDER_LINE_ID + "?sortBy=action_date").then().log().all().statusCode(200)
.body(containsString(ORDER_LINE_ID));

given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_ORDER_PATH+ ORDER_LINE_ID + 123).then().log().all().statusCode(500)
given().header(CONTENT_TYPE).header(TENANT).header(PERMS).get(ACQ_AUDIT_ORDER_PATH + ORDER_LINE_ID + 123).then().log().all().statusCode(500)
.body(containsString("UUID string too large"));
}

@Test
void shouldReturnPieceEventsOnGetByPieceId() {
JsonObject jsonObject = new JsonObject();
jsonObject.put("name","Test Product2");
jsonObject.put("name", "Test Product2");

PieceAuditEvent pieceAuditEvent = new PieceAuditEvent()
.withId(UUID.randomUUID().toString())
Expand All @@ -148,7 +148,7 @@ void shouldReturnPieceEventsOnGetByPieceId() {
.body(containsString(PIECE_ID));

given().header(CONTENT_TYPE).header(TENANT).header(PERMS)
.get(ACQ_AUDIT_PIECE_PATH + PIECE_ID +"?limit=1")
.get(ACQ_AUDIT_PIECE_PATH + PIECE_ID + "?limit=1")
.then().log().all().statusCode(200)
.body(containsString(PIECE_ID));

Expand All @@ -171,38 +171,48 @@ void shouldReturnPieceEventsStatusChangesHistoryGetByPieceId() {
String id5 = UUID.randomUUID().toString();
String id6 = UUID.randomUUID().toString();
String id7 = UUID.randomUUID().toString();
String id8 = UUID.randomUUID().toString();
String id9 = UUID.randomUUID().toString();
var pieceAuditEvent1 = createPieceAuditEvent(id1);
var pieceAuditEvent2 = createPieceAuditEvent(id2, 3); // claiming interval has changed
var pieceAuditEvent3 = createPieceAuditEvent(id3, 44); // claiming interval has changed
var pieceAuditEvent4 = createPieceAuditEvent(id4, 44);
var pieceAuditEvent5 = createPieceAuditEvent(id5, 16); // claiming interval has changed
var pieceAuditEventWithDifferentPiece1 = createPieceAuditEvent(id6, 55);
var pieceAuditEventWithDifferentPiece2 = createPieceAuditEvent(id7, 9);
var pieceAuditEvent2 = createPieceAuditEvent(id2, 3, "Claim sent"); // claiming interval has changed
var pieceAuditEvent3 = createPieceAuditEvent(id3, 44, "Claim sent"); // claiming interval has changed
var pieceAuditEvent4 = createPieceAuditEvent(id4, 44, "Claim sent");
var pieceAuditEvent5 = createPieceAuditEvent(id5, 16, "Claim sent"); // claiming interval has changed
var pieceAuditEvent6 = createPieceAuditEvent(id6, 16, "Received"); // receivingStatus has changed
var pieceAuditEvent7 = createPieceAuditEvent(id7, 16, "Received");
var pieceAuditEventWithDifferentPiece1 = createPieceAuditEvent(id8, 55, "Claim sent");
var pieceAuditEventWithDifferentPiece2 = createPieceAuditEvent(id9, 9, "Claim sent");
pieceAuditEventWithDifferentPiece1.setPieceId(UUID.randomUUID().toString());
pieceAuditEventWithDifferentPiece2.setPieceId(UUID.randomUUID().toString());
var localDateTime1 = LocalDateTime.of(2023, 4, 20, 6, 9, 30);
var localDateTime2 = LocalDateTime.of(2023, 4, 20, 6, 10, 30);
var localDateTime3 = LocalDateTime.of(2023, 4, 20, 6, 11, 30);
var localDateTime4 = LocalDateTime.of(2023, 4, 20, 6, 12, 30);
var localDateTime5 = LocalDateTime.of(2023, 4, 20, 6, 13, 30);
var localDateTime6 = LocalDateTime.of(2023, 4, 20, 6, 9, 25);
var localDateTime7 = LocalDateTime.of(2023, 4, 20, 6, 9, 20);
var localDateTime6 = LocalDateTime.of(2023, 4, 20, 6, 14, 30);
var localDateTime7 = LocalDateTime.of(2023, 4, 20, 6, 15, 30);
var localDateTime8 = LocalDateTime.of(2023, 4, 20, 6, 9, 20);
var localDateTime9 = LocalDateTime.of(2023, 4, 20, 6, 10, 20);
pieceAuditEvent1.setActionDate(Date.from(localDateTime1.atZone(ZoneId.systemDefault()).toInstant()));
pieceAuditEvent2.setActionDate(Date.from(localDateTime2.atZone(ZoneId.systemDefault()).toInstant()));
pieceAuditEvent3.setActionDate(Date.from(localDateTime3.atZone(ZoneId.systemDefault()).toInstant()));
pieceAuditEvent4.setActionDate(Date.from(localDateTime4.atZone(ZoneId.systemDefault()).toInstant()));
pieceAuditEvent5.setActionDate(Date.from(localDateTime5.atZone(ZoneId.systemDefault()).toInstant()));
pieceAuditEventWithDifferentPiece1.setActionDate(Date.from(localDateTime6.atZone(ZoneId.systemDefault()).toInstant()));
pieceAuditEventWithDifferentPiece2.setActionDate(Date.from(localDateTime7.atZone(ZoneId.systemDefault()).toInstant()));
pieceAuditEvent6.setActionDate(Date.from(localDateTime6.atZone(ZoneId.systemDefault()).toInstant()));
pieceAuditEvent7.setActionDate(Date.from(localDateTime7.atZone(ZoneId.systemDefault()).toInstant()));
pieceAuditEventWithDifferentPiece1.setActionDate(Date.from(localDateTime8.atZone(ZoneId.systemDefault()).toInstant()));
pieceAuditEventWithDifferentPiece2.setActionDate(Date.from(localDateTime9.atZone(ZoneId.systemDefault()).toInstant()));

pieceEventsDao.save(pieceAuditEvent1, TENANT_ID);
pieceEventsDao.save(pieceAuditEvent2, TENANT_ID);
pieceEventsDao.save(pieceAuditEvent3, TENANT_ID);
pieceEventsDao.save(pieceAuditEvent4, TENANT_ID);
pieceEventsDao.save(pieceAuditEvent5, TENANT_ID);
pieceEventsDao.save(pieceAuditEvent6, TENANT_ID);
pieceEventsDao.save(pieceAuditEvent7, TENANT_ID);

pieceEventsDao.save(pieceAuditEventWithDifferentPiece1, TENANT_ID);
pieceEventsDao.save(pieceAuditEventWithDifferentPiece2, TENANT_ID);

// based on our business logic, it returns pieceAuditEvent1, pieceAuditEvent3, pieceAuditEvent5
given().header(CONTENT_TYPE).header(TENANT).header(PERMS)
.get(ACQ_AUDIT_PIECE_PATH + INVALID_ID + ACQ_AUDIT_PIECE_STATUS_CHANGE_HISTORY_PATH)
Expand All @@ -213,22 +223,25 @@ void shouldReturnPieceEventsStatusChangesHistoryGetByPieceId() {
.get(ACQ_AUDIT_PIECE_PATH + PIECE_ID + ACQ_AUDIT_PIECE_STATUS_CHANGE_HISTORY_PATH)
.then().log().all().statusCode(200)
.body(containsString(PIECE_ID))
.body(containsString(id1))
.body(containsString(id2))
.body(containsString(id3))
.body(containsString(id5));
.body(containsString(id5))
.body(containsString(id6));

given().header(CONTENT_TYPE).header(TENANT).header(PERMS)
.get(ACQ_AUDIT_PIECE_PATH + PIECE_ID + ACQ_AUDIT_PIECE_STATUS_CHANGE_HISTORY_PATH +"?limit=1")
.get(ACQ_AUDIT_PIECE_PATH + PIECE_ID + ACQ_AUDIT_PIECE_STATUS_CHANGE_HISTORY_PATH + "?limit=1")
.then().log().all().statusCode(200)
.body(containsString(PIECE_ID));

given().header(CONTENT_TYPE).header(TENANT).header(PERMS)
.get(ACQ_AUDIT_PIECE_PATH + PIECE_ID + ACQ_AUDIT_PIECE_STATUS_CHANGE_HISTORY_PATH +"?sortBy=action_date")
.get(ACQ_AUDIT_PIECE_PATH + PIECE_ID + ACQ_AUDIT_PIECE_STATUS_CHANGE_HISTORY_PATH + "?sortBy=action_date")
.then().log().all().statusCode(200)
.body(containsString(PIECE_ID))
.body(containsString(id2))
.body(containsString(id3))
.body(containsString(id5));
.body(containsString(id5))
.body(containsString(id6));

given().header(CONTENT_TYPE).header(TENANT).header(PERMS)
.get(ACQ_AUDIT_PIECE_PATH + PIECE_ID + 123 + ACQ_AUDIT_PIECE_STATUS_CHANGE_HISTORY_PATH)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public static OrderLineAuditEvent createOrderLineAuditEvent(String id) {
public static PieceAuditEvent createPieceAuditEvent(String id) {
JsonObject jsonObject = new JsonObject();
jsonObject.put("name", "Test Product");
jsonObject.put("receivingStatus", "Expected");

return new PieceAuditEvent()
.withId(id)
Expand All @@ -69,14 +70,15 @@ public static PieceAuditEvent createPieceAuditEvent(String id) {
.withPieceSnapshot(jsonObject);
}

public static PieceAuditEvent createPieceAuditEvent(String id, int claimingInterval) {
public static PieceAuditEvent createPieceAuditEvent(String id, int claimingInterval, String receivingStatus) {
JsonObject jsonObject = new JsonObject();
jsonObject.put("name", "Test Product");
jsonObject.put("claimingInterval", claimingInterval);
jsonObject.put("receivingStatus", receivingStatus);

return new PieceAuditEvent()
.withId(id)
.withAction(PieceAuditEvent.Action.CREATE)
.withAction(PieceAuditEvent.Action.EDIT)
.withPieceId(PIECE_ID)
.withUserId(UUID.randomUUID().toString())
.withEventDate(new Date())
Expand Down

0 comments on commit b442237

Please sign in to comment.