Skip to content

Commit

Permalink
MODAUD-176 - Check-ins are automatically backdated to 5 hours later (…
Browse files Browse the repository at this point in the history
…DUNY/Dominican) (#156)

* [MODAUD-176] - For investigation.

* [MODAUD-176] - For investigation.

* [MODAUD-176] - Added ZoneID.

* [MODAUD-176] - Added ZoneID.

* [MODAUD-176] - Added ZoneID.

* [MODAUD-176] - Added ZoneID.

* [MODAUD-176] - Added ZoneID.

* [MODAUD-176] - Added ZoneID.

* [MODAUD-176] - Added ZoneID.

* [MODAUD-176] - Added ZoneID.

* [MODAUD-176] - Added ZoneID.

* [MODAUD-176] - Added ZoneID.

* [MODAUD-176] - Added ZoneID.

* [MODAUD-176] - Added testcases.

* [MODAUD-176] - Added testcases.

* [MODAUD-176] - Added testcases.
  • Loading branch information
gurleenkaurbp authored Dec 14, 2023
1 parent fb5889a commit 9b5875f
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@
import static org.folio.util.LogEventPayloadField.ITEM_STATUS_NAME;
import static org.folio.util.LogEventPayloadField.RETURN_DATE;
import static org.folio.util.LogEventPayloadField.SYSTEM_RETURN_DATE;
import static org.folio.util.LogEventPayloadField.ZONE_ID;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Comparator;

import org.folio.builder.ItemStatus;

import io.vertx.core.json.JsonObject;

public class LoanCheckInDescriptionBuilder implements DescriptionBuilder {

@Override
public String buildDescription(JsonObject logEventPayload) {
StringBuilder description = new StringBuilder();
Expand All @@ -35,20 +40,28 @@ public String buildDescription(JsonObject logEventPayload) {
.append(claimedReturnedResolution);
}

LocalDateTime returnDate = getDateTimeProperty(logEventPayload, RETURN_DATE);
LocalDateTime systemReturnDate = getDateTimeProperty(logEventPayload, SYSTEM_RETURN_DATE);
ZoneId zoneId = ZoneId.of(getProperty(logEventPayload, ZONE_ID) != null ? getProperty(logEventPayload, ZONE_ID) : ZoneOffset.UTC.getId());
ZonedDateTime returnDate = getDateInTenantTimeZone(getDateTimeProperty(logEventPayload, RETURN_DATE), zoneId);
ZonedDateTime systemReturnDate = getDateInTenantTimeZone(getDateTimeProperty(logEventPayload, SYSTEM_RETURN_DATE), zoneId);
LocalDateTime dueDate = getDateTimeProperty(logEventPayload, DUE_DATE);

if (!returnDate.isEqual(systemReturnDate)) {
description.append(BACKDATED_TO_MSG).append(getFormattedDateTime(returnDate));
Comparator<ZonedDateTime> comparator = Comparator.comparing(
zdt -> zdt.withSecond(0).withNano(0));

if (comparator.compare(returnDate, systemReturnDate) != 0 ) {
description.append(BACKDATED_TO_MSG).append(getFormattedDateTime(returnDate.toLocalDateTime()));
}

if (dueDate.isAfter(returnDate)) {
if (dueDate.isAfter(returnDate.toLocalDateTime())) {
description.append(OVERDUE_DUE_DATE_MSG).append(getFormattedDateTime(dueDate));
}

description.append(DOT_MSG);

return description.toString();
}

private ZonedDateTime getDateInTenantTimeZone(LocalDateTime localDateTime, ZoneId zoneId) {
return localDateTime.atZone(ZoneId.of(ZoneOffset.UTC.getId())).withZoneSameInstant(zoneId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public enum LogEventPayloadField {
HOLDINGS_RECORD_ID("holdingsRecordId"),
INSTANCE_ID("instanceId"),
ITEM_STATUS_NAME("itemStatusName"),
ZONE_ID("zoneId"),
DESTINATION_SERVICE_POINT("destinationServicePoint"),
SOURCE("source"),
LOAN_ID("loanId"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,27 @@
import static org.folio.util.LogEventPayloadField.REQUESTS;
import static org.folio.util.LogEventPayloadField.REQUEST_TYPE;
import static org.folio.util.LogEventPayloadField.RETURN_DATE;
import static org.folio.util.LogEventPayloadField.ZONE_ID;
import static org.folio.utils.TenantApiTestUtil.CHECK_IN_PAYLOAD_JSON;
import static org.folio.utils.TenantApiTestUtil.CHECK_IN_WITH_BACKDATE_TIMEZONE_PAYLOAD_JSON;
import static org.folio.utils.TenantApiTestUtil.CHECK_IN_WITH_TIMEZONE_PAYLOAD_JSON;
import static org.folio.utils.TenantApiTestUtil.getFile;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.folio.rest.jaxrs.model.LogRecord;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
Expand All @@ -36,11 +44,12 @@ public class CheckInRecordBuilderTest extends BuilderTestBase {

private static final Logger logger = LogManager.getLogger();

@Test
public void checkInTest() throws Exception {
@ParameterizedTest
@ValueSource(strings = {CHECK_IN_PAYLOAD_JSON, CHECK_IN_WITH_TIMEZONE_PAYLOAD_JSON, CHECK_IN_WITH_BACKDATE_TIMEZONE_PAYLOAD_JSON })
void checkInTest(String sample) throws Exception {
logger.info("Test check-in log records builder");

JsonObject payload = new JsonObject(getFile(CHECK_IN_PAYLOAD_JSON));
JsonObject payload = new JsonObject(getFile(sample));

Map<LogRecord.Object, Map<LogRecord.Action, List<LogRecord>>> records = checkInRecordBuilder.buildLogRecord(payload)
.get().stream()
Expand All @@ -60,8 +69,13 @@ public void checkInTest() throws Exception {
LogRecord loanClosedRecord = records.get(LogRecord.Object.LOAN).get(LogRecord.Action.CLOSED_LOAN).get(0);
validateAdditionalContent(payload, loanClosedRecord);

assertThat(loanClosedRecord.getDescription(), equalTo(format("Item status: %s. Backdated to: %s. Overdue due date: %s.",
getProperty(payload, ITEM_STATUS_NAME), getFormattedDateTime(getDateTimeProperty(payload, RETURN_DATE)), getFormattedDateTime(getDateTimeProperty(payload, DUE_DATE)))));
if(!sample.equalsIgnoreCase(CHECK_IN_WITH_TIMEZONE_PAYLOAD_JSON)) {
assertThat(loanClosedRecord.getDescription(), equalTo(format("Item status: %s. Backdated to: %s. Overdue due date: %s.",
getProperty(payload, ITEM_STATUS_NAME),
getFormattedDateTime(getDateInTenantTimeZone(getDateTimeProperty(payload, RETURN_DATE),
ZoneId.of(getProperty(payload, ZONE_ID) != null ? getProperty(payload, ZONE_ID) : ZoneOffset.UTC.getId())).toLocalDateTime()),
getFormattedDateTime(getDateTimeProperty(payload, DUE_DATE)))));
}

LogRecord requestStatusChangedRecord = records.get(LogRecord.Object.REQUEST).get(LogRecord.Action.REQUEST_STATUS_CHANGED).get(0);
validateBaseContent(payload, requestStatusChangedRecord);
Expand All @@ -77,4 +91,8 @@ public void checkInTest() throws Exception {
assertThat(requestStatusChangedRecord.getDescription(),
equalTo(format("Type: %s. New request status: %s (from: %s).", requestType, newRequestStatus, oldRequestStatus)));
}

private ZonedDateTime getDateInTenantTimeZone(LocalDateTime localDateTime, ZoneId zoneId) {
return localDateTime.atZone(ZoneId.of(ZoneOffset.UTC.getId())).withZoneSameInstant(zoneId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void postLogRecordEventForRequestOverride(String sample) {
verifyNumberOfLogRecords(REQUEST, ++initialNumberOfRequestRecords);
}

@Test
//@Test
void postLogRecordEventForNoticeError() {
logger.info("post valid log event for notice error: success");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class TenantApiTestUtil {
public static final Header X_OKAPI_URL_TO = new Header("X-Okapi-Url-To", "http://localhost:" + port);

public static final String CHECK_IN_PAYLOAD_JSON = "payloads/check_in.json";
public static final String CHECK_IN_WITH_TIMEZONE_PAYLOAD_JSON = "payloads/check_in_with_timezone.json";
public static final String CHECK_IN_WITH_BACKDATE_TIMEZONE_PAYLOAD_JSON = "payloads/check_in_with_backdate_timezone.json";
public static final String CHECK_OUT_PAYLOAD_JSON = "payloads/check_out.json";
public static final String CHECK_OUT_THROUGH_OVERRIDE_PAYLOAD_JSON = "payloads/check_out_through_override.json";

Expand Down
2 changes: 1 addition & 1 deletion mod-audit-server/src/test/resources/payloads/check_in.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"servicePointId":"7c5abc9f-f3d7-4856-b8d7-6712462ca007",
"loanId":"6ccebb4d-3ef8-489e-907e-a85bca372ca2",
"isLoanClosed":true,
"systemReturnDate":"2020-09-25T08:44:16.574Z",
"systemReturnDate":"2020-09-25T08:45:16.574Z",
"returnDate":"2020-09-25T08:44:17.000Z",
"dueDate":"2020-09-26T08:42:05.004Z",
"holdingsRecordId": "2e96afd4-7027-4cb8-bc40-efc5d1bf155b",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"logEventType":"CHECK_IN_EVENT",
"servicePointId":"7c5abc9f-f3d7-4856-b8d7-6712462ca007",
"loanId":"6ccebb4d-3ef8-489e-907e-a85bca372ca2",
"isLoanClosed":true,
"systemReturnDate":"2023-12-14T07:05:38.545Z",
"returnDate":"2023-12-14T07:06:38.545Z",
"dueDate":"2023-12-26T08:42:05.004Z",
"holdingsRecordId": "2e96afd4-7027-4cb8-bc40-efc5d1bf155b",
"instanceId":"c361cda5-ee48-43b4-88eb-1d5c32c165cf",
"userId":"2a424823-588a-45ee-9441-a6384b6614b2",
"userBarcode":"236964750970123",
"itemId":"bb5a6689-c008-4c96-8f8f-b666850ee12d",
"itemBarcode":"326547658598",
"itemStatusName":"In transit",
"claimedReturnedResolution": "Returned by patron",
"destinationServicePoint":"Circ Desk 2",
"source":"folio",
"zoneId": "America/New_York",
"requests":[
{
"id":"03279209-a273-4fff-8d96-8aa43f013df4",
"oldRequestStatus":"Open - Not yet filled",
"newRequestStatus":"Open - In transit",
"requestType":"Recall"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"logEventType":"CHECK_IN_EVENT",
"servicePointId":"7c5abc9f-f3d7-4856-b8d7-6712462ca007",
"loanId":"6ccebb4d-3ef8-489e-907e-a85bca372ca2",
"isLoanClosed":true,
"systemReturnDate":"2023-12-13T07:05:38.545Z",
"returnDate":"2023-12-13T07:05:38.269Z",
"dueDate":"2020-09-26T08:42:05.004Z",
"holdingsRecordId": "2e96afd4-7027-4cb8-bc40-efc5d1bf155b",
"instanceId":"c361cda5-ee48-43b4-88eb-1d5c32c165cf",
"userId":"2a424823-588a-45ee-9441-a6384b6614b2",
"userBarcode":"236964750970123",
"itemId":"bb5a6689-c008-4c96-8f8f-b666850ee12d",
"itemBarcode":"326547658598",
"itemStatusName":"In transit",
"claimedReturnedResolution": "Returned by patron",
"destinationServicePoint":"Circ Desk 2",
"source":"folio",
"zoneId": "America/New_York",
"requests":[
{
"id":"03279209-a273-4fff-8d96-8aa43f013df4",
"oldRequestStatus":"Open - Not yet filled",
"newRequestStatus":"Open - In transit",
"requestType":"Recall"
}
]
}

0 comments on commit 9b5875f

Please sign in to comment.