Skip to content

Commit

Permalink
[CIRC-2045] - Patron information is stored in circulation log for che…
Browse files Browse the repository at this point in the history
…ck-ins that are not tied to a loan or request (#1437)

* [CIRC-2045] - Code fix

* [CIRC-2045] - Code fix

* [CIRC-2045] - Code fix

* [CIRC-2045] - Fixed failing test

* [CIRC-2045] - refactored the code

* [CIRC-2045] - refactored the code

* [CIRC-2045] - refactored the code

* [CIRC-2045] - refactored the code

* [CIRC-2045] - Code refactored.

* [CIRC-2045] - Test cases.

* [CIRC-2045] - Test cases.

* Update src/main/java/org/folio/circulation/services/EventPublisher.java

Co-authored-by: OleksandrVidinieiev <[email protected]>

* Update src/main/java/org/folio/circulation/services/EventPublisher.java

Co-authored-by: OleksandrVidinieiev <[email protected]>

* [CIRC-2045] - Test cases.

---------

Co-authored-by: OleksandrVidinieiev <[email protected]>
  • Loading branch information
gurleenkaurbp and OleksandrVidinieiev authored Mar 6, 2024
1 parent e213eb7 commit aaf8270
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
53 changes: 35 additions & 18 deletions src/main/java/org/folio/circulation/services/EventPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.folio.circulation.support.AsyncCoordinationUtil.allOf;
import static org.folio.circulation.support.json.JsonPropertyWriter.write;
import static org.folio.circulation.support.results.CommonFailures.failedDueToServerError;
import static org.folio.circulation.support.results.Result.emptyAsync;
import static org.folio.circulation.support.results.Result.ofAsync;
import static org.folio.circulation.support.results.Result.succeeded;
import static org.folio.circulation.support.utils.ClockUtil.getZonedDateTime;
Expand Down Expand Up @@ -124,34 +125,50 @@ public CompletableFuture<Result<LoanAndRelatedRecords>> publishItemCheckedOutEve
}

public CompletableFuture<Result<CheckInContext>> publishItemCheckedInEvents(
CheckInContext checkInContext, UserRepository userRepository, LoanRepository loanRepository) {

runAsync(() -> userRepository.getUser(checkInContext.getLoggedInUserId())
.thenCombineAsync(loanRepository.findLastLoanForItem(checkInContext.getItem().getItemId()), (userResult, lastLoan) -> {
if (nonNull(lastLoan.value())) {
return userRepository.getUser(lastLoan.value().getUserId())
.thenApply(userFromLastLoan -> Result.succeeded(pubSubPublishingService.publishEvent(LOG_RECORD.name(),
mapToCheckInLogEventContent(checkInContext, userResult.value(),
checkInContext.isInHouseUse() ? null : userFromLastLoan.value()))));
}
return userResult.after(loggedInUser -> CompletableFuture.completedFuture(
Result.succeeded(pubSubPublishingService.publishEvent(LOG_RECORD.name(),
mapToCheckInLogEventContent(checkInContext, loggedInUser, null)))));
}));
CheckInContext context, UserRepository userRepository, LoanRepository loanRepository) {

if (checkInContext.getLoan() != null) {
Loan loan = checkInContext.getLoan();
runAsync(() -> userRepository.getUser(context.getLoggedInUserId())
.thenCompose(r1 -> r1.after(loggedInUser -> getUserForLastLoan(context, userRepository, loanRepository)
.thenCompose(r -> r.after(userFromLastLoan -> pubSubPublishingService.publishEvent(LOG_RECORD.name(),
mapToCheckInLogEventContent(context, loggedInUser, userFromLastLoan)).thenApply(Result::succeeded)))))
);

if (context.getLoan() != null) {
Loan loan = context.getLoan();

JsonObject payloadJsonObject = new JsonObject();
write(payloadJsonObject, USER_ID_FIELD, loan.getUserId());
write(payloadJsonObject, LOAN_ID_FIELD, loan.getId());
write(payloadJsonObject, RETURN_DATE_FIELD, loan.getReturnDate());

return pubSubPublishingService.publishEvent(ITEM_CHECKED_IN.name(), payloadJsonObject.encode())
.handle((result, error) -> handlePublishEventError(error, checkInContext));
.handle((result, error) -> handlePublishEventError(error, context));
}

return completedFuture(succeeded(context));
}

private CompletableFuture<Result<User>> getUserForLastLoan(CheckInContext context,
UserRepository userRepository, LoanRepository loanRepository) {

if (context.isInHouseUse() || context.getRequestQueue().getRequests().isEmpty()) {
return emptyAsync();
}

return loanRepository.findLastLoanForItem(context.getItem().getItemId())
.thenCompose(r -> r.after(lastLoan -> getUserForLastLoan(context, lastLoan, userRepository, loanRepository)));
}

private CompletableFuture<Result<User>> getUserForLastLoan(CheckInContext context, Loan lastLoan,
UserRepository userRepository, LoanRepository loanRepository) {

if (lastLoan == null) {
return emptyAsync();
}

return completedFuture(succeeded(checkInContext));
return userRepository.getUser(lastLoan.getUserId())
.thenCompose(r1 -> r1.after(userFromLastLoan -> loanRepository.findOpenLoanForItem(context.getItem())
.thenApply(r2 -> r2.map(openLoan -> openLoan == null ? null : userFromLastLoan))));
}

public CompletableFuture<Result<Loan>> publishDeclaredLostEvent(Loan loan) {
Expand Down
11 changes: 9 additions & 2 deletions src/test/java/api/loans/CheckInByBarcodeTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ class CheckInByBarcodeTests extends APITests {
private final static String OPEN_NOT_YET_FILLED = "Open - Not yet filled";
private final static String OPEN_AWAITING_PICKUP = "Open - Awaiting pickup";
private final static String OPEN_AWAITING_DELIVERY = "Open - Awaiting delivery";
private final static String REQUEST_POSITION = "position";
private static final String LOAN_INFO_ADDED = "testing patron info";

public CheckInByBarcodeTests() {
Expand Down Expand Up @@ -1840,11 +1839,19 @@ void shouldNotLinkTitleLevelHoldRequestToAnItemUponCheckInWhenItemIsNonRequestab
overdueFinePoliciesFixture.noOverdueFine().getId(),
lostItemFeePoliciesFixture.facultyStandard().getId());

checkInFixture.checkInByBarcode(item);
CheckInByBarcodeResponse checkInResponse = checkInFixture.checkInByBarcode(item);
JsonObject requestAfterCheckIn = requestsFixture.getById(request.getId()).getJson();

assertThat(requestAfterCheckIn.getString("itemId"), nullValue());
assertThat(requestAfterCheckIn, RequestMatchers.isOpenNotYetFilled());

final var publishedEvents = waitAtMost(2, SECONDS)
.until(FakePubSub::getPublishedEvents, hasSize(5));
final var checkedInEvent = publishedEvents.findFirst(byEventType(ITEM_CHECKED_IN.name()));
assertThat(checkedInEvent, isValidItemCheckedInEvent(checkInResponse.getLoan()));
final var checkInLogEvent = publishedEvents.findFirst(byLogEventType(CHECK_IN.value()));
assertThat(checkInLogEvent, isValidCheckInLogEvent(checkInResponse.getLoan()));

}

@Test
Expand Down
5 changes: 3 additions & 2 deletions src/test/java/api/requests/RequestsAPICreationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -1481,8 +1481,9 @@ void canHaveUserBarcodeInCheckInPublishedEventAfterTitleLevelRequest() {
checkInFixture.checkInByBarcode(item);
checkOutFixture.checkOutByBarcode(item, usersFixture.charlotte()).getJson();

FakePubSub.getPublishedEvents().stream().map(event -> new JsonObject(event.getString("eventPayload")))
.filter(event -> event.containsKey("logEventType") && event.getString("logEventType").equals("CHECK_IN_EVENT"))
FakePubSub.getPublishedEvents().stream().map(event -> new JsonObject(event.getString("eventPayload")))
.filter(event -> event.containsKey("logEventType") && event.getString("logEventType").equals("CHECK_IN_EVENT")
&& !event.containsKey("requests"))
.forEach(event -> assertTrue(event.containsKey("userBarcode")));
}

Expand Down
3 changes: 2 additions & 1 deletion src/test/java/api/support/matchers/EventMatchers.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public static Matcher<JsonObject> isValidCheckInLogEvent(JsonObject checkedInLoa
hasJsonPath("itemId", is(checkedInLoan.getString("itemId"))),
hasJsonPath("zoneId", is("Z")),
hasJsonPath("itemBarcode", is(checkedInLoan.getJsonObject("item").getString("barcode"))),
hasJsonPath("itemStatusName", is(checkedInLoan.getJsonObject("item").getJsonObject("status").getString("name")))
hasJsonPath("itemStatusName", is(checkedInLoan.getJsonObject("item").getJsonObject("status").getString("name"))),
hasJsonPath("userBarcode", is(checkedInLoan.getJsonObject("borrower").getString("barcode")))
))),
isLogRecordEventType());
}
Expand Down

0 comments on commit aaf8270

Please sign in to comment.