Search in sources :

Example 1 with ChangeDueDateValidator

use of org.folio.circulation.domain.validation.ChangeDueDateValidator 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);
}
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) JsonObject(io.vertx.core.json.JsonObject) ChangeDueDateValidator(org.folio.circulation.domain.validation.ChangeDueDateValidator) ServicePointLoanLocationValidator(org.folio.circulation.domain.validation.ServicePointLoanLocationValidator) ServicePointRepository(org.folio.circulation.infrastructure.storage.ServicePointRepository) UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) EventPublisher(org.folio.circulation.services.EventPublisher) Clients(org.folio.circulation.support.Clients) ItemNotFoundValidator(org.folio.circulation.domain.validation.ItemNotFoundValidator) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) LoanScheduledNoticeService(org.folio.circulation.domain.notice.schedule.LoanScheduledNoticeService) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) Loan(org.folio.circulation.domain.Loan) UpdateItem(org.folio.circulation.domain.UpdateItem) 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