use of org.folio.circulation.domain.Request in project mod-circulation by folio-org.
the class MoveRequestTests method dueDateChangedEventIsPublished.
@Test
void dueDateChangedEventIsPublished() {
val secondFloorEconomics = locationsFixture.secondFloorEconomics();
val mezzanineDisplayCase = locationsFixture.mezzanineDisplayCase();
val instance = instancesFixture.basedUponDunkirk();
val itemCopyA = itemsFixture.basedUponDunkirk(holdingBuilder -> holdingBuilder.withPermanentLocation(secondFloorEconomics).withNoTemporaryLocation(), instanceBuilder -> instanceBuilder.withId(instance.getId()), itemBuilder -> itemBuilder.withNoPermanentLocation().withNoTemporaryLocation().withBarcode("10203040506"));
val itemCopyB = itemsFixture.basedUponDunkirk(holdingBuilder -> holdingBuilder.withPermanentLocation(mezzanineDisplayCase).withNoTemporaryLocation(), instanceBuilder -> instanceBuilder.withId(instance.getId()), itemBuilder -> itemBuilder.withNoPermanentLocation().withNoTemporaryLocation().withBarcode("90806050402"));
val james = usersFixture.james();
val jessica = usersFixture.jessica();
val steve = usersFixture.steve();
val charlotte = usersFixture.charlotte();
IndividualResource itemCopyALoan = checkOutFixture.checkOutByBarcode(itemCopyA, james, getZonedDateTime());
requestsFixture.placeItemLevelHoldShelfRequest(itemCopyB, jessica, getZonedDateTime().minusHours(3), RequestType.PAGE.getValue());
IndividualResource recallRequestForItemCopyB = requestsFixture.placeItemLevelHoldShelfRequest(itemCopyB, steve, getZonedDateTime().minusHours(2), RequestType.RECALL.getValue());
requestsFixture.placeItemLevelHoldShelfRequest(itemCopyA, charlotte, getZonedDateTime().minusHours(1), RequestType.HOLD.getValue());
requestsFixture.move(new MoveRequestBuilder(recallRequestForItemCopyB.getId(), itemCopyA.getId()));
itemCopyALoan = loansClient.get(itemCopyALoan);
// There should be four events published - for "check out", for "log event", for "hold" and for "move"
final var publishedEvents = Awaitility.await().atMost(1, TimeUnit.SECONDS).until(FakePubSub::getPublishedEvents, hasSize(11));
Map<String, List<JsonObject>> events = publishedEvents.stream().collect(groupingBy(o -> o.getString("eventType")));
Map<String, List<JsonObject>> logEvents = events.get(LOG_RECORD.name()).stream().collect(groupingBy(e -> new JsonObject(e.getString("eventPayload")).getString("logEventType")));
Request originalCreatedFromEventPayload = Request.from(new JsonObject(logEvents.get(REQUEST_MOVED.value()).get(0).getString("eventPayload")).getJsonObject("payload").getJsonObject("requests").getJsonObject("original"));
Request updatedCreatedFromEventPayload = Request.from(new JsonObject(logEvents.get(REQUEST_MOVED.value()).get(0).getString("eventPayload")).getJsonObject("payload").getJsonObject("requests").getJsonObject("updated"));
assertThat(originalCreatedFromEventPayload.asJson(), Matchers.not(equalTo(updatedCreatedFromEventPayload.asJson())));
assertThat(originalCreatedFromEventPayload.getItemId(), not(equalTo(updatedCreatedFromEventPayload.getItemId())));
assertThat(updatedCreatedFromEventPayload.getItemId(), equalTo(itemCopyA.getId().toString()));
assertThat(events.get(LOAN_DUE_DATE_CHANGED.name()).get(1), isValidLoanDueDateChangedEvent(itemCopyALoan.getJson()));
}
use of org.folio.circulation.domain.Request in project mod-circulation by folio-org.
the class ItemsInTransitReport method buildEntry.
private JsonObject buildEntry(Item item) {
if (item == null || item.isNotFound()) {
return new JsonObject();
}
item = ofNullable(item.getHoldingsRecordId()).map(reportContext.getHoldingsRecords()::get).map(Holdings::getInstanceId).map(reportContext.getInstances()::get).map(item::withInstance).orElse(item);
Loan loan = reportContext.getLoans().get(item.getItemId());
Request request = reportContext.getRequests().get(item.getItemId());
Location location = reportContext.getLocations().get(item.getLocationId());
if (location != null) {
ServicePoint primaryServicePoint = reportContext.getServicePoints().get(location.getPrimaryServicePointId().toString());
item = item.withLocation(location.withPrimaryServicePoint(primaryServicePoint));
}
ServicePoint inTransitDestinationServicePoint = reportContext.getServicePoints().get(item.getInTransitDestinationServicePointId());
ServicePoint lastCheckInServicePoint = reportContext.getServicePoints().get(item.getLastCheckInServicePointId().toString());
item = item.updateLastCheckInServicePoint(lastCheckInServicePoint).updateDestinationServicePoint(inTransitDestinationServicePoint);
final JsonObject entry = new JsonObject();
write(entry, "id", item.getItemId());
write(entry, "title", item.getTitle());
write(entry, "barcode", item.getBarcode());
write(entry, "contributors", mapContributorNamesToJson(item));
write(entry, "callNumber", item.getCallNumber());
write(entry, "enumeration", item.getEnumeration());
write(entry, "volume", item.getVolume());
write(entry, "yearCaption", new JsonArray(item.getYearCaption()));
writeNamedObject(entry, "status", ofNullable(item.getStatus()).map(ItemStatus::getValue).orElse(null));
write(entry, "inTransitDestinationServicePointId", item.getInTransitDestinationServicePointId());
write(entry, "copyNumber", item.getCopyNumber());
write(entry, "effectiveCallNumberComponents", createCallNumberComponents(item.getCallNumberComponents()));
if (inTransitDestinationServicePoint != null) {
writeServicePoint(entry, inTransitDestinationServicePoint, "inTransitDestinationServicePoint");
}
if (location != null) {
writeLocation(entry, location);
}
if (request != null) {
User requester = reportContext.getUsers().get(request.getRequesterId());
PatronGroup requesterPatronGroup = requester == null ? null : reportContext.getPatronGroups().get(requester.getPatronGroupId());
if (requesterPatronGroup != null) {
request = request.withRequester(requester.withPatronGroup(requesterPatronGroup));
}
ServicePoint pickupServicePoint = reportContext.getServicePoints().get(request.getPickupServicePointId());
request = request.withPickupServicePoint(pickupServicePoint);
writeRequest(request, entry);
}
if (loan != null) {
ServicePoint checkoutServicePoint = reportContext.getServicePoints().get(loan.getCheckoutServicePointId());
ServicePoint checkInServicePoint = reportContext.getServicePoints().get(loan.getCheckInServicePointId());
loan = loan.withCheckinServicePoint(checkInServicePoint).withCheckoutServicePoint(checkoutServicePoint);
writeLoan(entry, loan);
}
final LastCheckIn lastCheckIn = item.getLastCheckIn();
if (lastCheckIn != null) {
writeLastCheckIn(entry, lastCheckIn);
}
return entry;
}
use of org.folio.circulation.domain.Request in project mod-circulation by folio-org.
the class LoanPolicy method isAlternatePeriod.
private boolean isAlternatePeriod(RequestQueue requestQueue) {
final JsonObject holds = getHolds();
if (Objects.isNull(requestQueue) || !holds.containsKey(ALTERNATE_CHECKOUT_LOAN_PERIOD_KEY)) {
return false;
}
Optional<Request> potentialRequest = requestQueue.getRequests().stream().skip(1).findFirst();
boolean isAlternateDueDateSchedule = false;
if (potentialRequest.isPresent()) {
Request request = potentialRequest.get();
boolean isHold = request.getRequestType() == RequestType.HOLD;
boolean isOpenNotYetFilled = request.getStatus() == RequestStatus.OPEN_NOT_YET_FILLED;
if (isHold && isOpenNotYetFilled) {
isAlternateDueDateSchedule = true;
}
}
return isAlternateDueDateSchedule;
}
use of org.folio.circulation.domain.Request in project mod-circulation by folio-org.
the class RequestByInstanceIdResource method createInstanceLevelRequests.
private void createInstanceLevelRequests(RoutingContext routingContext) {
final WebContext context = new WebContext(routingContext);
final Clients clients = Clients.create(context, client);
final var itemRepository = new ItemRepository(clients);
final var userRepository = new UserRepository(clients);
final var loanRepository = new LoanRepository(clients, itemRepository, userRepository);
final var requestRepository = RequestRepository.using(clients, itemRepository, userRepository, loanRepository);
final var requestQueueRepository = new RequestQueueRepository(requestRepository);
final var itemFinder = new ItemByInstanceIdFinder(clients.holdingsStorage(), itemRepository);
final var eventPublisher = new EventPublisher(routingContext);
final var requestBody = routingContext.getBodyAsJson();
new ConfigurationRepository(clients).lookupTlrSettings().thenCompose(r -> r.after(config -> buildAndPlaceRequests(clients, eventPublisher, itemRepository, loanRepository, requestRepository, requestQueueRepository, userRepository, itemFinder, config, requestBody))).thenApply(r -> r.map(RequestAndRelatedRecords::getRequest)).thenApply(r -> r.map(new RequestRepresentation()::extendedRepresentation)).thenApply(r -> r.map(JsonHttpResponse::created)).thenAccept(context::writeResultToHttpResponse).exceptionally(err -> {
String reason = "Error processing instance-level request";
log.error(reason, err);
ServerErrorResponse.internalError(routingContext.response(), reason);
return null;
});
}
use of org.folio.circulation.domain.Request in project mod-circulation by folio-org.
the class RequestByInstanceIdResource method placeRequest.
private CompletableFuture<Result<RequestAndRelatedRecords>> placeRequest(List<JsonObject> itemRequests, int startIndex, CreateRequestService createRequestService, Clients clients, LoanRepository loanRepository, List<String> errors, ItemRepository itemRepository, UserRepository userRepository, RequestQueueRepository requestQueueRepository) {
log.debug("RequestByInstanceIdResource.placeRequest, startIndex={}, itemRequestSize={}", startIndex, itemRequests.size());
if (startIndex >= itemRequests.size()) {
String aggregateFailures = String.format("%n%s", String.join("%n", errors));
return CompletableFuture.completedFuture(failedDueToServerError("Failed to place a request for the instance. Reasons: " + aggregateFailures));
}
JsonObject currentItemRequest = itemRequests.get(startIndex);
final RequestFromRepresentationService requestFromRepresentationService = new RequestFromRepresentationService(new InstanceRepository(clients), itemRepository, requestQueueRepository, userRepository, loanRepository, new ServicePointRepository(clients), new ConfigurationRepository(clients), createProxyRelationshipValidator(currentItemRequest, clients), new ServicePointPickupLocationValidator(), new FailFastErrorHandler(), new ItemByInstanceIdFinder(clients.holdingsStorage(), itemRepository));
return requestFromRepresentationService.getRequestFrom(Request.Operation.CREATE, currentItemRequest).thenCompose(r -> r.after(createRequestService::createRequest)).thenCompose(r -> {
if (r.succeeded()) {
log.debug("RequestByInstanceIdResource.placeRequest: succeeded creating request for item {}", currentItemRequest.getString(ITEM_ID));
return CompletableFuture.completedFuture(r);
} else {
String reason = getErrorMessage(r.cause());
errors.add(reason);
log.debug("Failed to create request for item {} with reason: {}", currentItemRequest.getString(ITEM_ID), reason);
return placeRequest(itemRequests, startIndex + 1, createRequestService, clients, loanRepository, errors, itemRepository, userRepository, requestQueueRepository);
}
});
}
Aggregations