Search in sources :

Example 1 with LoanValidator

use of org.folio.circulation.domain.validation.LoanValidator in project mod-circulation by folio-org.

the class DeclareLostResource method declareItemLost.

private CompletableFuture<Result<Loan>> declareItemLost(DeclareItemLostRequest request, Clients clients, WebContext context) {
    final var itemRepository = new ItemRepository(clients);
    final var userRepository = new UserRepository(clients);
    final var loanRepository = new LoanRepository(clients, itemRepository, userRepository);
    final var storeLoanAndItem = new StoreLoanAndItem(loanRepository, itemRepository);
    final var lostItemFeeService = new LostItemFeeChargingService(clients, storeLoanAndItem, new LostItemFeeRefundService(clients, itemRepository, userRepository, loanRepository));
    return loanRepository.getById(request.getLoanId()).thenApply(LoanValidator::refuseWhenLoanIsClosed).thenApply(this::refuseWhenItemIsAlreadyDeclaredLost).thenCompose(declareItemLost(request, clients)).thenCompose(r -> r.after(storeLoanAndItem::updateLoanAndItemInStorage)).thenCompose(r -> r.after(loan -> lostItemFeeService.chargeLostItemFees(loan, request, context.getUserId())));
}
Also used : UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) Clients(org.folio.circulation.support.Clients) DeclareItemLostRequest(org.folio.circulation.domain.representations.DeclareItemLostRequest) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) WebContext(org.folio.circulation.support.http.server.WebContext) MappingFunctions.when(org.folio.circulation.support.results.MappingFunctions.when) Router(io.vertx.ext.web.Router) CompletableFuture(java.util.concurrent.CompletableFuture) ValidationErrorFailure.singleValidationError(org.folio.circulation.support.ValidationErrorFailure.singleValidationError) LostItemFeeChargingService(org.folio.circulation.services.LostItemFeeChargingService) RoutingContext(io.vertx.ext.web.RoutingContext) Function(java.util.function.Function) NotesRepository(org.folio.circulation.infrastructure.storage.notes.NotesRepository) LoanValidator(org.folio.circulation.domain.validation.LoanValidator) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) LostItemFeeRefundService(org.folio.circulation.services.LostItemFeeRefundService) NoteCreator(org.folio.circulation.domain.notes.NoteCreator) Result.succeeded(org.folio.circulation.support.results.Result.succeeded) Loan(org.folio.circulation.domain.Loan) MappingFunctions.toFixedValue(org.folio.circulation.support.results.MappingFunctions.toFixedValue) Result(org.folio.circulation.support.results.Result) Result.ofAsync(org.folio.circulation.support.results.Result.ofAsync) EventPublisher(org.folio.circulation.services.EventPublisher) StringUtils.defaultIfBlank(org.apache.commons.lang.StringUtils.defaultIfBlank) CompletionStage(java.util.concurrent.CompletionStage) StoreLoanAndItem(org.folio.circulation.StoreLoanAndItem) NoContentResponse(org.folio.circulation.support.http.server.NoContentResponse) HttpClient(io.vertx.core.http.HttpClient) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) StoreLoanAndItem(org.folio.circulation.StoreLoanAndItem) UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) LostItemFeeChargingService(org.folio.circulation.services.LostItemFeeChargingService) LostItemFeeRefundService(org.folio.circulation.services.LostItemFeeRefundService) LoanValidator(org.folio.circulation.domain.validation.LoanValidator) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository)

Example 2 with LoanValidator

use of org.folio.circulation.domain.validation.LoanValidator 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));
}
Also used : UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) Clients(org.folio.circulation.support.Clients) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) JsonPropertyFetcher.getDateTimeProperty(org.folio.circulation.support.json.JsonPropertyFetcher.getDateTimeProperty) ConfigurationRepository(org.folio.circulation.infrastructure.storage.ConfigurationRepository) ZonedDateTime(java.time.ZonedDateTime) WebContext(org.folio.circulation.support.http.server.WebContext) RequestQueueRepository(org.folio.circulation.infrastructure.storage.requests.RequestQueueRepository) Router(io.vertx.ext.web.Router) CompletableFuture(java.util.concurrent.CompletableFuture) ValidationErrorFailure.singleValidationError(org.folio.circulation.support.ValidationErrorFailure.singleValidationError) RoutingContext(io.vertx.ext.web.RoutingContext) Item(org.folio.circulation.domain.Item) LoanValidator(org.folio.circulation.domain.validation.LoanValidator) LoanAndRelatedRecords(org.folio.circulation.domain.LoanAndRelatedRecords) JsonObject(io.vertx.core.json.JsonObject) RequestQueue(org.folio.circulation.domain.RequestQueue) LoanScheduledNoticeService(org.folio.circulation.domain.notice.schedule.LoanScheduledNoticeService) Result.failed(org.folio.circulation.support.results.Result.failed) ChangeDueDateRequest(org.folio.circulation.domain.representations.ChangeDueDateRequest) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) ValidationErrorFailure(org.folio.circulation.support.ValidationErrorFailure) Result.succeeded(org.folio.circulation.support.results.Result.succeeded) MethodHandles(java.lang.invoke.MethodHandles) Loan(org.folio.circulation.domain.Loan) MappingFunctions.toFixedValue(org.folio.circulation.support.results.MappingFunctions.toFixedValue) ITEM_ID(org.folio.circulation.domain.representations.LoanProperties.ITEM_ID) Result(org.folio.circulation.support.results.Result) EventPublisher(org.folio.circulation.services.EventPublisher) DUE_DATE(org.folio.circulation.domain.representations.ChangeDueDateRequest.DUE_DATE) RouteRegistration(org.folio.circulation.support.RouteRegistration) Logger(org.apache.logging.log4j.Logger) ItemStatusValidator(org.folio.circulation.domain.validation.ItemStatusValidator) LogManager(org.apache.logging.log4j.LogManager) RequestRepository(org.folio.circulation.infrastructure.storage.requests.RequestRepository) NoContentResponse(org.folio.circulation.support.http.server.NoContentResponse) HttpClient(io.vertx.core.http.HttpClient) WebContext(org.folio.circulation.support.http.server.WebContext) EventPublisher(org.folio.circulation.services.EventPublisher) ItemStatusValidator(org.folio.circulation.domain.validation.ItemStatusValidator) Clients(org.folio.circulation.support.Clients) LoanValidator(org.folio.circulation.domain.validation.LoanValidator) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) LoanScheduledNoticeService(org.folio.circulation.domain.notice.schedule.LoanScheduledNoticeService) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) ConfigurationRepository(org.folio.circulation.infrastructure.storage.ConfigurationRepository) RequestQueueRepository(org.folio.circulation.infrastructure.storage.requests.RequestQueueRepository)

Aggregations

HttpClient (io.vertx.core.http.HttpClient)2 Router (io.vertx.ext.web.Router)2 RoutingContext (io.vertx.ext.web.RoutingContext)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Loan (org.folio.circulation.domain.Loan)2 LoanValidator (org.folio.circulation.domain.validation.LoanValidator)2 ItemRepository (org.folio.circulation.infrastructure.storage.inventory.ItemRepository)2 LoanRepository (org.folio.circulation.infrastructure.storage.loans.LoanRepository)2 UserRepository (org.folio.circulation.infrastructure.storage.users.UserRepository)2 EventPublisher (org.folio.circulation.services.EventPublisher)2 Clients (org.folio.circulation.support.Clients)2 ValidationErrorFailure.singleValidationError (org.folio.circulation.support.ValidationErrorFailure.singleValidationError)2 NoContentResponse (org.folio.circulation.support.http.server.NoContentResponse)2 WebContext (org.folio.circulation.support.http.server.WebContext)2 MappingFunctions.toFixedValue (org.folio.circulation.support.results.MappingFunctions.toFixedValue)2 Result (org.folio.circulation.support.results.Result)2 Result.succeeded (org.folio.circulation.support.results.Result.succeeded)2 JsonObject (io.vertx.core.json.JsonObject)1 MethodHandles (java.lang.invoke.MethodHandles)1 ZonedDateTime (java.time.ZonedDateTime)1