use of org.folio.circulation.domain.validation.ItemStatusValidator in project mod-circulation by folio-org.
the class ChangeDueDateResource method processChangeDueDate.
private CompletableFuture<Result<LoanAndRelatedRecords>> processChangeDueDate(final ChangeDueDateRequest request, 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 LoanScheduledNoticeService scheduledNoticeService = LoanScheduledNoticeService.using(clients);
final ItemStatusValidator itemStatusValidator = new ItemStatusValidator(ChangeDueDateResource::errorWhenInIncorrectStatus);
final EventPublisher eventPublisher = new EventPublisher(routingContext);
final LoanNoticeSender loanNoticeSender = LoanNoticeSender.using(clients);
final ConfigurationRepository configurationRepository = new ConfigurationRepository(clients);
log.info("starting change due date process for loan {}", request.getLoanId());
return succeeded(request).after(r -> getExistingLoan(loanRepository, r)).thenApply(LoanValidator::refuseWhenLoanIsClosed).thenApply(this::toLoanAndRelatedRecords).thenComposeAsync(r -> r.combineAfter(configurationRepository::lookupTlrSettings, LoanAndRelatedRecords::withTlrSettings)).thenComposeAsync(r -> r.after(requestQueueRepository::get)).thenApply(itemStatusValidator::refuseWhenItemStatusDoesNotAllowDueDateChange).thenApply(r -> changeDueDate(r, request)).thenApply(r -> r.map(this::unsetDueDateChangedByRecallIfNoOpenRecallsInQueue)).thenComposeAsync(r -> r.after(loanRepository::updateLoan)).thenComposeAsync(r -> r.after(eventPublisher::publishDueDateChangedEvent)).thenApply(r -> r.next(scheduledNoticeService::rescheduleDueDateNotices)).thenCompose(r -> r.after(loanNoticeSender::sendManualDueDateChangeNotice));
}
use of org.folio.circulation.domain.validation.ItemStatusValidator in project mod-circulation by folio-org.
the class LoanCollectionResource method create.
@Override
void create(RoutingContext routingContext) {
final WebContext context = new WebContext(routingContext);
JsonObject incomingRepresentation = routingContext.getBodyAsJson();
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 servicePointRepository = new ServicePointRepository(clients);
final var requestRepository = RequestRepository.using(clients, itemRepository, userRepository, loanRepository);
final var requestQueueRepository = new RequestQueueRepository(requestRepository);
final var requestQueueUpdate = UpdateRequestQueue.using(clients, requestRepository, requestQueueRepository);
final UpdateItem updateItem = new UpdateItem(itemRepository);
final LoanService loanService = new LoanService(clients);
final LoanPolicyRepository loanPolicyRepository = new LoanPolicyRepository(clients);
final EventPublisher eventPublisher = new EventPublisher(routingContext);
final ProxyRelationshipValidator proxyRelationshipValidator = new ProxyRelationshipValidator(clients, () -> singleValidationError("proxyUserId is not valid", "proxyUserId", loan.getProxyUserId()));
final RequestedByAnotherPatronValidator requestedByAnotherPatronValidator = new RequestedByAnotherPatronValidator(message -> singleValidationError(message, "userId", loan.getUserId()));
final AlreadyCheckedOutValidator alreadyCheckedOutValidator = new AlreadyCheckedOutValidator(message -> singleValidationError(message, "itemId", loan.getItemId()));
final ItemStatusValidator itemStatusValidator = new ItemStatusValidator(LoanCollectionResource::errorWhenInIncorrectStatus);
final ItemNotFoundValidator itemNotFoundValidator = createItemNotFoundValidator(loan);
final ServicePointLoanLocationValidator spLoanLocationValidator = new ServicePointLoanLocationValidator();
final LoanRepresentation loanRepresentation = new LoanRepresentation();
completedFuture(succeeded(new LoanAndRelatedRecords(loan))).thenCompose(larrResult -> getServicePointsForLoanAndRelated(larrResult, servicePointRepository)).thenApply(this::refuseWhenNotOpenOrClosed).thenApply(this::refuseWhenOpenAndNoUserId).thenApply(spLoanLocationValidator::checkServicePointLoanLocation).thenCombineAsync(itemRepository.fetchFor(loan), this::addItem).thenApply(itemNotFoundValidator::refuseWhenItemNotFound).thenApply(alreadyCheckedOutValidator::refuseWhenItemIsAlreadyCheckedOut).thenApply(itemStatusValidator::refuseWhenItemIsMissing).thenComposeAsync(r -> r.after(proxyRelationshipValidator::refuseWhenInvalid)).thenCombineAsync(requestQueueRepository.getByItemId(loan.getItemId()), this::addRequestQueue).thenCombineAsync(userRepository.getUserFailOnNotFound(loan.getUserId()), this::addUser).thenApply(requestedByAnotherPatronValidator::refuseWhenRequestedByAnotherPatron).thenComposeAsync(r -> r.after(loanPolicyRepository::lookupLoanPolicy)).thenComposeAsync(r -> r.after(requestQueueUpdate::onCheckOut)).thenComposeAsync(r -> r.after(updateItem::onLoanCreated)).thenComposeAsync(r -> r.after(loanService::truncateLoanWhenItemRecalled)).thenComposeAsync(r -> r.after(loanRepository::createLoan)).thenComposeAsync(r -> r.after(eventPublisher::publishDueDateChangedEvent)).thenApply(r -> r.map(LoanAndRelatedRecords::getLoan)).thenApply(r -> r.map(loanRepresentation::extendedLoan)).thenApply(r -> r.map(JsonHttpResponse::created)).thenAccept(context::writeResultToHttpResponse);
}
Aggregations