use of org.folio.circulation.domain.RequestAndRelatedRecords in project mod-circulation by folio-org.
the class RequestCollectionResource method move.
void move(RoutingContext routingContext) {
final var context = new WebContext(routingContext);
final var clients = Clients.create(context, client);
final var representation = routingContext.getBodyAsJson();
final var id = getRequestId(routingContext);
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 loanPolicyRepository = new LoanPolicyRepository(clients);
final var configurationRepository = new ConfigurationRepository(clients);
final var updateUponRequest = new UpdateUponRequest(new UpdateItem(itemRepository), new UpdateLoan(clients, loanRepository, loanPolicyRepository), UpdateRequestQueue.using(clients, requestRepository, requestQueueRepository));
final var moveRequestProcessAdapter = new MoveRequestProcessAdapter(itemRepository, loanRepository, requestRepository);
final var eventPublisher = new EventPublisher(routingContext);
final var moveRequestService = new MoveRequestService(requestRepository, new RequestPolicyRepository(clients), updateUponRequest, moveRequestProcessAdapter, new RequestLoanValidator(new ItemByInstanceIdFinder(clients.holdingsStorage(), itemRepository), loanRepository), RequestNoticeSender.using(clients), configurationRepository, eventPublisher, requestQueueRepository);
fromFutureResult(requestRepository.getById(id)).map(RequestAndRelatedRecords::new).map(request -> asMove(request, representation)).flatMapFuture(move -> moveRequestService.moveRequest(move, move.getOriginalRequest())).onSuccess(records -> eventPublisher.publishDueDateChangedEvent(records, loanRepository)).map(RequestAndRelatedRecords::getRequest).map(new RequestRepresentation()::extendedRepresentation).map(JsonHttpResponse::ok).onComplete(context::write, context::write);
}
use of org.folio.circulation.domain.RequestAndRelatedRecords in project mod-circulation by folio-org.
the class RequestScheduledNoticeService method rescheduleRequestNotices.
public Result<RequestAndRelatedRecords> rescheduleRequestNotices(RequestAndRelatedRecords relatedRecords) {
Request request = relatedRecords.getRequest();
scheduledNoticesRepository.deleteByRequestId(request.getId()).thenAccept(r -> r.next(resp -> scheduleRequestNotices(request)));
return succeeded(relatedRecords);
}
use of org.folio.circulation.domain.RequestAndRelatedRecords in project mod-circulation by folio-org.
the class RequestLoanValidator method refuseWhenUserHasAlreadyBeenLoanedItem.
public CompletableFuture<Result<RequestAndRelatedRecords>> refuseWhenUserHasAlreadyBeenLoanedItem(RequestAndRelatedRecords requestAndRelatedRecords) {
final Request request = requestAndRelatedRecords.getRequest();
return loanRepository.findOpenLoanForRequest(request).thenApply(loanResult -> loanResult.failWhen(loan -> of(() -> loan != null && loan.getUserId().equals(request.getUserId())), loan -> {
Map<String, String> parameters = new HashMap<>();
parameters.put("itemId", request.getItemId());
parameters.put("userId", request.getUserId());
parameters.put("loanId", loan.getId());
String message = "This requester currently has this item on loan.";
return singleValidationError(new ValidationError(message, parameters));
}).map(loan -> requestAndRelatedRecords));
}
use of org.folio.circulation.domain.RequestAndRelatedRecords 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.RequestAndRelatedRecords 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