Search in sources :

Example 1 with RequestedByAnotherPatronValidator

use of org.folio.circulation.domain.validation.RequestedByAnotherPatronValidator 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);
}
Also used : ProxyRelationshipValidator(org.folio.circulation.domain.validation.ProxyRelationshipValidator) UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) UpdateRequestQueue(org.folio.circulation.domain.UpdateRequestQueue) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) RequestQueueRepository(org.folio.circulation.infrastructure.storage.requests.RequestQueueRepository) LoanPolicyRepository(org.folio.circulation.infrastructure.storage.loans.LoanPolicyRepository) ValidationErrorFailure.singleValidationError(org.folio.circulation.support.ValidationErrorFailure.singleValidationError) UpdateItem(org.folio.circulation.domain.UpdateItem) RoutingContext(io.vertx.ext.web.RoutingContext) Item(org.folio.circulation.domain.Item) LoanAndRelatedRecords(org.folio.circulation.domain.LoanAndRelatedRecords) ServicePointRepository(org.folio.circulation.infrastructure.storage.ServicePointRepository) AccountRepository(org.folio.circulation.infrastructure.storage.feesandfines.AccountRepository) LostItemPolicyRepository(org.folio.circulation.infrastructure.storage.loans.LostItemPolicyRepository) JsonObject(io.vertx.core.json.JsonObject) LoanScheduledNoticeService(org.folio.circulation.domain.notice.schedule.LoanScheduledNoticeService) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) ChangeDueDateValidator(org.folio.circulation.domain.validation.ChangeDueDateValidator) Result.of(org.folio.circulation.support.results.Result.of) Result.succeeded(org.folio.circulation.support.results.Result.succeeded) MethodHandles(java.lang.invoke.MethodHandles) Loan(org.folio.circulation.domain.Loan) RequestedByAnotherPatronValidator(org.folio.circulation.domain.validation.RequestedByAnotherPatronValidator) ITEM_ID(org.folio.circulation.domain.representations.LoanProperties.ITEM_ID) ServicePointLoanLocationValidator(org.folio.circulation.domain.validation.ServicePointLoanLocationValidator) Result(org.folio.circulation.support.results.Result) EventPublisher(org.folio.circulation.services.EventPublisher) LoanRepresentation(org.folio.circulation.domain.LoanRepresentation) Logger(org.apache.logging.log4j.Logger) ItemStatusValidator(org.folio.circulation.domain.validation.ItemStatusValidator) LoanService(org.folio.circulation.domain.LoanService) RequestRepository(org.folio.circulation.infrastructure.storage.requests.RequestRepository) NoContentResponse(org.folio.circulation.support.http.server.NoContentResponse) HttpClient(io.vertx.core.http.HttpClient) Clients(org.folio.circulation.support.Clients) OverdueFinePolicyRepository(org.folio.circulation.infrastructure.storage.loans.OverdueFinePolicyRepository) WebContext(org.folio.circulation.support.http.server.WebContext) CompletableFuture(java.util.concurrent.CompletableFuture) JsonHttpResponse(org.folio.circulation.support.http.server.JsonHttpResponse) ItemNotFoundValidator(org.folio.circulation.domain.validation.ItemNotFoundValidator) RequestQueue(org.folio.circulation.domain.RequestQueue) DateTimeUtil.isSameMillis(org.folio.circulation.support.utils.DateTimeUtil.isSameMillis) ValidationErrorFailure(org.folio.circulation.support.ValidationErrorFailure) MappingFunctions.toFixedValue(org.folio.circulation.support.results.MappingFunctions.toFixedValue) User(org.folio.circulation.domain.User) AlreadyCheckedOutValidator(org.folio.circulation.domain.validation.AlreadyCheckedOutValidator) ProxyRelationshipValidator(org.folio.circulation.domain.validation.ProxyRelationshipValidator) PatronGroupRepository(org.folio.circulation.infrastructure.storage.users.PatronGroupRepository) LogManager(org.apache.logging.log4j.LogManager) WebContext(org.folio.circulation.support.http.server.WebContext) JsonHttpResponse(org.folio.circulation.support.http.server.JsonHttpResponse) AlreadyCheckedOutValidator(org.folio.circulation.domain.validation.AlreadyCheckedOutValidator) RequestedByAnotherPatronValidator(org.folio.circulation.domain.validation.RequestedByAnotherPatronValidator) JsonObject(io.vertx.core.json.JsonObject) ServicePointLoanLocationValidator(org.folio.circulation.domain.validation.ServicePointLoanLocationValidator) ServicePointRepository(org.folio.circulation.infrastructure.storage.ServicePointRepository) UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) LoanPolicyRepository(org.folio.circulation.infrastructure.storage.loans.LoanPolicyRepository) EventPublisher(org.folio.circulation.services.EventPublisher) ItemStatusValidator(org.folio.circulation.domain.validation.ItemStatusValidator) Clients(org.folio.circulation.support.Clients) ItemNotFoundValidator(org.folio.circulation.domain.validation.ItemNotFoundValidator) LoanService(org.folio.circulation.domain.LoanService) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) Loan(org.folio.circulation.domain.Loan) UpdateItem(org.folio.circulation.domain.UpdateItem) LoanRepresentation(org.folio.circulation.domain.LoanRepresentation) LoanAndRelatedRecords(org.folio.circulation.domain.LoanAndRelatedRecords) RequestQueueRepository(org.folio.circulation.infrastructure.storage.requests.RequestQueueRepository)

Aggregations

HttpClient (io.vertx.core.http.HttpClient)1 JsonObject (io.vertx.core.json.JsonObject)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 MethodHandles (java.lang.invoke.MethodHandles)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)1 LogManager (org.apache.logging.log4j.LogManager)1 Logger (org.apache.logging.log4j.Logger)1 Item (org.folio.circulation.domain.Item)1 Loan (org.folio.circulation.domain.Loan)1 LoanAndRelatedRecords (org.folio.circulation.domain.LoanAndRelatedRecords)1 LoanRepresentation (org.folio.circulation.domain.LoanRepresentation)1 LoanService (org.folio.circulation.domain.LoanService)1 RequestQueue (org.folio.circulation.domain.RequestQueue)1 UpdateItem (org.folio.circulation.domain.UpdateItem)1 UpdateRequestQueue (org.folio.circulation.domain.UpdateRequestQueue)1 User (org.folio.circulation.domain.User)1 LoanScheduledNoticeService (org.folio.circulation.domain.notice.schedule.LoanScheduledNoticeService)1 ITEM_ID (org.folio.circulation.domain.representations.LoanProperties.ITEM_ID)1 AlreadyCheckedOutValidator (org.folio.circulation.domain.validation.AlreadyCheckedOutValidator)1