use of org.folio.circulation.support.results.Result in project mod-circulation by folio-org.
the class RequestNoticeSender method sendNoticeOnRequestAwaitingPickup.
public Result<CheckInContext> sendNoticeOnRequestAwaitingPickup(CheckInContext context) {
final Item item = context.getItem();
final RequestQueue requestQueue = context.getRequestQueue();
if (item == null || item.isNotFound()) {
log.warn("Request Awaiting Pickup notice processing is aborted: item is missing");
} else if (requestQueue == null) {
log.warn("Request Awaiting Pickup notice processing is aborted: request queue is null");
} else if (item.isAwaitingPickup()) {
requestQueue.getRequests().stream().filter(Request::hasTopPriority).filter(Request::isAwaitingPickup).filter(Request::hasChangedStatus).findFirst().map(request -> request.withItem(item)).ifPresent(this::fetchDataAndSendRequestAwaitingPickupNotice);
}
return succeeded(context);
}
use of org.folio.circulation.support.results.Result in project mod-circulation by folio-org.
the class LoanCollectionResource method replace.
@Override
void replace(RoutingContext routingContext) {
final WebContext context = new WebContext(routingContext);
JsonObject incomingRepresentation = routingContext.getBodyAsJson();
incomingRepresentation.put("id", routingContext.request().getParam("id"));
final Loan loan = Loan.from(incomingRepresentation);
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 ServicePointRepository servicePointRepository = new ServicePointRepository(clients);
final var requestQueueUpdate = UpdateRequestQueue.using(clients, requestRepository, requestQueueRepository);
final UpdateItem updateItem = new UpdateItem(itemRepository);
final ProxyRelationshipValidator proxyRelationshipValidator = new ProxyRelationshipValidator(clients, () -> singleValidationError("proxyUserId is not valid", "proxyUserId", loan.getProxyUserId()));
final ItemNotFoundValidator itemNotFoundValidator = createItemNotFoundValidator(loan);
final ServicePointLoanLocationValidator spLoanLocationValidator = new ServicePointLoanLocationValidator();
final ChangeDueDateValidator changeDueDateValidator = new ChangeDueDateValidator();
final LoanScheduledNoticeService scheduledNoticeService = LoanScheduledNoticeService.using(clients);
final EventPublisher eventPublisher = new EventPublisher(routingContext);
final LoanNoticeSender loanNoticeSender = LoanNoticeSender.using(clients);
getExistingLoan(loanRepository, loan).thenApply(e -> e.map(existingLoan -> new LoanAndRelatedRecords(loan, existingLoan))).thenCompose(larrResult -> getServicePointsForLoanAndRelated(larrResult, servicePointRepository)).thenApply(this::refuseWhenNotOpenOrClosed).thenApply(this::refuseWhenOpenAndNoUserId).thenApply(spLoanLocationValidator::checkServicePointLoanLocation).thenApply(this::refuseWhenClosedAndNoCheckInServicePointId).thenCombineAsync(itemRepository.fetchFor(loan), this::addItem).thenApply(itemNotFoundValidator::refuseWhenItemNotFound).thenCompose(changeDueDateValidator::refuseChangeDueDateForItemInDisallowedStatus).thenCombineAsync(userRepository.getUser(loan.getUserId()), this::addUser).thenComposeAsync(r -> r.after(proxyRelationshipValidator::refuseWhenInvalid)).thenCombineAsync(requestQueueRepository.getByItemId(loan.getItemId()), this::addRequestQueue).thenApply(r -> r.map(this::unsetDueDateChangedByRecallIfNoOpenRecallsInQueue)).thenComposeAsync(result -> result.after(requestQueueUpdate::onCheckIn)).thenComposeAsync(result -> result.after(updateItem::onLoanUpdate)).thenComposeAsync(result -> result.after(loanRepository::updateLoan)).thenComposeAsync(r -> r.after(eventPublisher::publishDueDateChangedEvent)).thenApply(r -> r.next(scheduledNoticeService::rescheduleDueDateNotices)).thenCompose(r -> r.after(loanNoticeSender::sendManualDueDateChangeNotice)).thenApply(r -> r.map(toFixedValue(NoContentResponse::noContent))).thenAccept(context::writeResultToHttpResponse);
}
use of org.folio.circulation.support.results.Result in project mod-circulation by folio-org.
the class RenewalResource method renewThroughOverride.
public CompletableFuture<Result<RenewalContext>> renewThroughOverride(RenewalContext context) {
final JsonObject overrideBlocks = context.getRenewalRequest().getJsonObject(OVERRIDE_BLOCKS);
final String comment = getProperty(overrideBlocks, COMMENT);
if (StringUtils.isBlank(comment)) {
return completedFuture(failedValidation("Override renewal request must have a comment", COMMENT, null));
}
final ZonedDateTime overrideDueDate = getDateTimeProperty(overrideBlocks.getJsonObject(RENEWAL_DUE_DATE_REQUIRED_OVERRIDE_BLOCK), DUE_DATE);
Loan loan = context.getLoan();
boolean hasRecallRequest = context.getRequestQueue().getRequests().stream().findFirst().map(r -> r.getRequestType() == RequestType.RECALL).orElse(false);
return completedFuture(overrideRenewal(loan, ClockUtil.getZonedDateTime(), overrideDueDate, comment, hasRecallRequest)).thenApply(mapResult(context::withLoan));
}
use of org.folio.circulation.support.results.Result in project mod-circulation by folio-org.
the class LoanRelatedFeeFineClosedHandlerResource method handleFeeFineClosedEvent.
private void handleFeeFineClosedEvent(RoutingContext routingContext) {
final WebContext context = new WebContext(routingContext);
final EventPublisher eventPublisher = new EventPublisher(routingContext);
log.info("Event {} received: {}", LOAN_RELATED_FEE_FINE_CLOSED, routingContext.getBodyAsString());
createAndValidateRequest(routingContext).after(request -> processEvent(context, request, eventPublisher)).thenCompose(r -> r.after(eventPublisher::publishClosedLoanEvent)).exceptionally(CommonFailures::failedDueToServerError).thenApply(r -> r.map(toFixedValue(NoContentResponse::noContent))).thenAccept(result -> result.applySideEffect(context::write, failure -> {
log.error("Cannot handle event [{}], error occurred {}", routingContext.getBodyAsString(), failure);
context.write(noContent());
}));
}
use of org.folio.circulation.support.results.Result in project mod-circulation by folio-org.
the class LoanRelatedFeeFineClosedHandlerResource method processEvent.
private CompletableFuture<Result<Loan>> processEvent(WebContext context, LoanRelatedFeeFineClosedEvent event, EventPublisher eventPublisher) {
final 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 accountRepository = new AccountRepository(clients);
final var lostItemPolicyRepository = new LostItemPolicyRepository(clients);
return loanRepository.getById(event.getLoanId()).thenCompose(r -> r.after(loan -> {
if (loan.isItemLost()) {
return closeLoanWithLostItemIfLostFeesResolved(loan, loanRepository, itemRepository, accountRepository, lostItemPolicyRepository, eventPublisher);
}
return completedFuture(succeeded(loan));
}));
}
Aggregations