Search in sources :

Example 1 with LoanScheduledNoticeService

use of org.folio.circulation.domain.notice.schedule.LoanScheduledNoticeService in project mod-circulation by folio-org.

the class CheckOutByBarcodeResource method checkOut.

private void checkOut(RoutingContext routingContext) {
    final WebContext context = new WebContext(routingContext);
    CheckOutByBarcodeRequest request = CheckOutByBarcodeRequest.fromJson(routingContext.getBodyAsJson());
    final Clients clients = Clients.create(context, client);
    final var userRepository = new UserRepository(clients);
    final var itemRepository = new ItemRepository(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 LoanService loanService = new LoanService(clients);
    final LoanPolicyRepository loanPolicyRepository = new LoanPolicyRepository(clients);
    final OverdueFinePolicyRepository overdueFinePolicyRepository = new OverdueFinePolicyRepository(clients);
    final LostItemPolicyRepository lostItemPolicyRepository = new LostItemPolicyRepository(clients);
    final PatronNoticePolicyRepository patronNoticePolicyRepository = new PatronNoticePolicyRepository(clients);
    final PatronGroupRepository patronGroupRepository = new PatronGroupRepository(clients);
    final ConfigurationRepository configurationRepository = new ConfigurationRepository(clients);
    final ScheduledNoticesRepository scheduledNoticesRepository = ScheduledNoticesRepository.using(clients);
    final LoanScheduledNoticeService scheduledNoticeService = new LoanScheduledNoticeService(scheduledNoticesRepository, patronNoticePolicyRepository);
    OkapiPermissions permissions = OkapiPermissions.from(new WebContext(routingContext).getHeaders());
    CirculationErrorHandler errorHandler = new OverridingErrorHandler(permissions);
    CheckOutValidators validators = new CheckOutValidators(request, clients, errorHandler, permissions, loanRepository);
    final var requestQueueUpdate = UpdateRequestQueue.using(clients, requestRepository, requestQueueRepository);
    final LoanRepresentation loanRepresentation = new LoanRepresentation();
    final EventPublisher eventPublisher = new EventPublisher(routingContext);
    final PatronActionSessionService patronActionSessionService = PatronActionSessionService.using(clients, PatronActionSessionRepository.using(clients, loanRepository, userRepository));
    ofAsync(() -> new LoanAndRelatedRecords(request.toLoan())).thenApply(validators::refuseCheckOutWhenServicePointIsNotPresent).thenComposeAsync(r -> lookupUser(request.getUserBarcode(), userRepository, r, errorHandler)).thenComposeAsync(validators::refuseWhenCheckOutActionIsBlockedManuallyForPatron).thenComposeAsync(validators::refuseWhenCheckOutActionIsBlockedAutomaticallyForPatron).thenComposeAsync(r -> lookupProxyUser(request.getProxyUserBarcode(), userRepository, r, errorHandler)).thenApply(validators::refuseWhenUserIsInactive).thenApply(validators::refuseWhenProxyUserIsInactive).thenComposeAsync(validators::refuseWhenInvalidProxyRelationship).thenComposeAsync(r -> lookupItem(request.getItemBarcode(), itemRepository, r)).thenApply(validators::refuseWhenItemNotFound).thenApply(validators::refuseWhenItemIsAlreadyCheckedOut).thenApply(validators::refuseWhenItemIsNotAllowedForCheckOut).thenComposeAsync(validators::refuseWhenItemHasOpenLoans).thenComposeAsync(r -> r.combineAfter(configurationRepository::lookupTlrSettings, LoanAndRelatedRecords::withTlrSettings)).thenComposeAsync(r -> r.after(requestQueueRepository::get)).thenApply(validators::refuseWhenRequestedByAnotherPatron).thenComposeAsync(r -> r.after(l -> lookupLoanPolicy(l, loanPolicyRepository, errorHandler))).thenComposeAsync(validators::refuseWhenItemLimitIsReached).thenCompose(validators::refuseWhenItemIsNotLoanable).thenApply(r -> r.next(errorHandler::failWithValidationErrors)).thenCompose(r -> r.combineAfter(configurationRepository::findTimeZoneConfiguration, LoanAndRelatedRecords::withTimeZone)).thenComposeAsync(r -> r.after(overdueFinePolicyRepository::lookupOverdueFinePolicy)).thenComposeAsync(r -> r.after(lostItemPolicyRepository::lookupLostItemPolicy)).thenApply(r -> r.next(this::setItemLocationIdAtCheckout)).thenComposeAsync(r -> r.after(relatedRecords -> checkOut(relatedRecords, routingContext.getBodyAsJson(), clients))).thenApply(r -> r.map(this::checkOutItem)).thenComposeAsync(r -> r.after(requestQueueUpdate::onCheckOut)).thenComposeAsync(r -> r.after(loanService::truncateLoanWhenItemRecalled)).thenComposeAsync(r -> r.after(patronGroupRepository::findPatronGroupForLoanAndRelatedRecords)).thenComposeAsync(r -> r.after(l -> updateItem(l, itemRepository))).thenComposeAsync(r -> r.after(loanRepository::createLoan)).thenComposeAsync(r -> r.after(l -> saveCheckOutSessionRecord(l, patronActionSessionService, errorHandler))).thenApplyAsync(r -> r.map(records -> records.withLoggedInUserId(context.getUserId()))).thenComposeAsync(r -> r.after(l -> publishItemCheckedOutEvent(l, eventPublisher, userRepository, errorHandler))).thenApply(r -> r.next(scheduledNoticeService::scheduleNoticesForLoanDueDate)).thenApply(r -> r.map(LoanAndRelatedRecords::getLoan)).thenApply(r -> r.map(loanRepresentation::extendedLoan)).thenApply(r -> createdLoanFrom(r, errorHandler)).thenAccept(context::writeResultToHttpResponse);
}
Also used : UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) UpdateRequestQueue(org.folio.circulation.domain.UpdateRequestQueue) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) ConfigurationRepository(org.folio.circulation.infrastructure.storage.ConfigurationRepository) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) ZonedDateTime(java.time.ZonedDateTime) RequestQueueRepository(org.folio.circulation.infrastructure.storage.requests.RequestQueueRepository) LoanPolicyRepository(org.folio.circulation.infrastructure.storage.loans.LoanPolicyRepository) Router(io.vertx.ext.web.Router) RoutingContext(io.vertx.ext.web.RoutingContext) ClosedLibraryStrategyService(org.folio.circulation.domain.policy.library.ClosedLibraryStrategyService) CheckOutByBarcodeRequest(org.folio.circulation.domain.representations.CheckOutByBarcodeRequest) FAILED_TO_FETCH_PROXY_USER(org.folio.circulation.resources.handlers.error.CirculationErrorType.FAILED_TO_FETCH_PROXY_USER) LoanAndRelatedRecords(org.folio.circulation.domain.LoanAndRelatedRecords) LostItemPolicyRepository(org.folio.circulation.infrastructure.storage.loans.LostItemPolicyRepository) PatronNoticePolicyRepository(org.folio.circulation.infrastructure.storage.notices.PatronNoticePolicyRepository) FAILED_TO_FETCH_ITEM(org.folio.circulation.resources.handlers.error.CirculationErrorType.FAILED_TO_FETCH_ITEM) JsonObject(io.vertx.core.json.JsonObject) LoanScheduledNoticeService(org.folio.circulation.domain.notice.schedule.LoanScheduledNoticeService) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) Result.succeeded(org.folio.circulation.support.results.Result.succeeded) Loan(org.folio.circulation.domain.Loan) FAILED_TO_PUBLISH_CHECKOUT_EVENT(org.folio.circulation.resources.handlers.error.CirculationErrorType.FAILED_TO_PUBLISH_CHECKOUT_EVENT) Result(org.folio.circulation.support.results.Result) LoanPolicy(org.folio.circulation.domain.policy.LoanPolicy) EventPublisher(org.folio.circulation.services.EventPublisher) RouteRegistration(org.folio.circulation.support.RouteRegistration) LoanRepresentation(org.folio.circulation.domain.LoanRepresentation) PatronActionSessionService(org.folio.circulation.domain.notice.session.PatronActionSessionService) LoanService(org.folio.circulation.domain.LoanService) CirculationErrorType(org.folio.circulation.resources.handlers.error.CirculationErrorType) CHECKED_OUT_THROUGH_OVERRIDE(org.folio.circulation.domain.LoanAction.CHECKED_OUT_THROUGH_OVERRIDE) CheckOutValidators(org.folio.circulation.domain.validation.CheckOutValidators) RequestRepository(org.folio.circulation.infrastructure.storage.requests.RequestRepository) 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) FAILED_TO_SAVE_SESSION_RECORD(org.folio.circulation.resources.handlers.error.CirculationErrorType.FAILED_TO_SAVE_SESSION_RECORD) HttpResponse(org.folio.circulation.support.http.server.HttpResponse) JsonHttpResponse.ok(org.folio.circulation.support.http.server.JsonHttpResponse.ok) OkapiPermissions(org.folio.circulation.support.http.OkapiPermissions) RequestQueue(org.folio.circulation.domain.RequestQueue) ScheduledNoticesRepository(org.folio.circulation.infrastructure.storage.notices.ScheduledNoticesRepository) OverridingErrorHandler(org.folio.circulation.resources.handlers.error.OverridingErrorHandler) FAILED_TO_FETCH_USER(org.folio.circulation.resources.handlers.error.CirculationErrorType.FAILED_TO_FETCH_USER) PatronActionSessionRepository(org.folio.circulation.infrastructure.storage.sessions.PatronActionSessionRepository) Result.ofAsync(org.folio.circulation.support.results.Result.ofAsync) CirculationErrorHandler(org.folio.circulation.resources.handlers.error.CirculationErrorHandler) JsonHttpResponse.created(org.folio.circulation.support.http.server.JsonHttpResponse.created) CHECKED_OUT(org.folio.circulation.domain.ItemStatus.CHECKED_OUT) PatronGroupRepository(org.folio.circulation.infrastructure.storage.users.PatronGroupRepository) WebContext(org.folio.circulation.support.http.server.WebContext) CheckOutValidators(org.folio.circulation.domain.validation.CheckOutValidators) PatronNoticePolicyRepository(org.folio.circulation.infrastructure.storage.notices.PatronNoticePolicyRepository) ScheduledNoticesRepository(org.folio.circulation.infrastructure.storage.notices.ScheduledNoticesRepository) PatronActionSessionService(org.folio.circulation.domain.notice.session.PatronActionSessionService) UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) LoanPolicyRepository(org.folio.circulation.infrastructure.storage.loans.LoanPolicyRepository) ConfigurationRepository(org.folio.circulation.infrastructure.storage.ConfigurationRepository) OverdueFinePolicyRepository(org.folio.circulation.infrastructure.storage.loans.OverdueFinePolicyRepository) EventPublisher(org.folio.circulation.services.EventPublisher) OkapiPermissions(org.folio.circulation.support.http.OkapiPermissions) CirculationErrorHandler(org.folio.circulation.resources.handlers.error.CirculationErrorHandler) OverridingErrorHandler(org.folio.circulation.resources.handlers.error.OverridingErrorHandler) Clients(org.folio.circulation.support.Clients) LoanService(org.folio.circulation.domain.LoanService) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) LoanScheduledNoticeService(org.folio.circulation.domain.notice.schedule.LoanScheduledNoticeService) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) LostItemPolicyRepository(org.folio.circulation.infrastructure.storage.loans.LostItemPolicyRepository) CheckOutByBarcodeRequest(org.folio.circulation.domain.representations.CheckOutByBarcodeRequest) LoanRepresentation(org.folio.circulation.domain.LoanRepresentation) LoanAndRelatedRecords(org.folio.circulation.domain.LoanAndRelatedRecords) RequestQueueRepository(org.folio.circulation.infrastructure.storage.requests.RequestQueueRepository) PatronGroupRepository(org.folio.circulation.infrastructure.storage.users.PatronGroupRepository)

Example 2 with LoanScheduledNoticeService

use of org.folio.circulation.domain.notice.schedule.LoanScheduledNoticeService 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)

Example 3 with LoanScheduledNoticeService

use of org.folio.circulation.domain.notice.schedule.LoanScheduledNoticeService 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)

Example 4 with LoanScheduledNoticeService

use of org.folio.circulation.domain.notice.schedule.LoanScheduledNoticeService in project mod-circulation by folio-org.

the class RenewalResource method renew.

private void renew(RoutingContext routingContext) {
    final WebContext webContext = new WebContext(routingContext);
    final Clients clients = Clients.create(webContext, client);
    final OkapiPermissions okapiPermissions = OkapiPermissions.from(webContext.getHeaders());
    final CirculationErrorHandler errorHandler = new OverridingErrorHandler(okapiPermissions);
    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 LoanPolicyRepository loanPolicyRepository = new LoanPolicyRepository(clients);
    final StoreLoanAndItem storeLoanAndItem = new StoreLoanAndItem(loanRepository, itemRepository);
    final LoanRepresentation loanRepresentation = new LoanRepresentation();
    final ConfigurationRepository configurationRepository = new ConfigurationRepository(clients);
    final LoanScheduledNoticeService scheduledNoticeService = LoanScheduledNoticeService.using(clients);
    final EventPublisher eventPublisher = new EventPublisher(routingContext);
    final LoanNoticeSender loanNoticeSender = LoanNoticeSender.using(clients);
    final AutomatedPatronBlocksRepository automatedPatronBlocksRepository = new AutomatedPatronBlocksRepository(clients);
    final FeeFineScheduledNoticeService feeFineNoticesService = FeeFineScheduledNoticeService.using(clients);
    // TODO: Validation check for same user should be in the domain service
    JsonObject bodyAsJson = routingContext.getBodyAsJson();
    BlockOverrides overrideBlocks = getOverrideBlocks(bodyAsJson);
    OkapiPermissions permissions = OkapiPermissions.from(new WebContext(routingContext).getHeaders());
    final Validator<RenewalContext> automatedPatronBlocksValidator = createAutomatedPatronBlocksValidator(bodyAsJson, permissions, automatedPatronBlocksRepository);
    final Validator<RenewalContext> manualPatronBlocksValidator = createManualPatronBlocksValidator(bodyAsJson, permissions, clients);
    final Validator<RenewalContext> overrideRenewValidator = new OverridingBlockValidator<>(RENEWAL_BLOCK, overrideBlocks, permissions);
    isRenewalBlockOverrideRequested = overrideBlocks.getRenewalBlockOverride().isRequested() || overrideBlocks.getRenewalDueDateRequiredBlockOverride().isRequested();
    findLoan(bodyAsJson, loanRepository, itemRepository, userRepository, errorHandler).thenApply(r -> r.map(loan -> RenewalContext.create(loan, bodyAsJson, webContext.getUserId()))).thenComposeAsync(r -> refuseWhenPatronIsInactive(r, errorHandler, USER_IS_INACTIVE)).thenComposeAsync(r -> refuseWhenRenewalActionIsBlockedForPatron(manualPatronBlocksValidator, r, errorHandler, USER_IS_BLOCKED_MANUALLY)).thenComposeAsync(r -> refuseWhenRenewalActionIsBlockedForPatron(automatedPatronBlocksValidator, r, errorHandler, USER_IS_BLOCKED_AUTOMATICALLY)).thenComposeAsync(r -> refuseIfNoPermissionsForRenewalOverride(overrideRenewValidator, r, errorHandler)).thenCompose(r -> r.after(ctx -> lookupLoanPolicy(ctx, loanPolicyRepository, errorHandler))).thenComposeAsync(r -> r.after(ctx -> lookupRequestQueue(ctx, requestQueueRepository, errorHandler))).thenCompose(r -> r.combineAfter(configurationRepository::findTimeZoneConfiguration, RenewalContext::withTimeZone)).thenComposeAsync(r -> r.after(context -> renew(context, clients, errorHandler))).thenApply(r -> r.next(errorHandler::failWithValidationErrors)).thenApply(r -> r.map(this::unsetDueDateChangedByRecallIfNoOpenRecallsInQueue)).thenComposeAsync(r -> r.after(storeLoanAndItem::updateLoanAndItemInStorage)).thenComposeAsync(r -> r.after(context -> processFeesFines(context, clients, itemRepository, userRepository, loanRepository))).thenApplyAsync(r -> r.next(feeFineNoticesService::scheduleOverdueFineNotices)).thenComposeAsync(r -> r.after(eventPublisher::publishDueDateChangedEvent)).thenApply(r -> r.next(scheduledNoticeService::rescheduleDueDateNotices)).thenApply(r -> r.next(loanNoticeSender::sendRenewalPatronNotice)).thenApply(r -> r.map(loanRepresentation::extendedLoan)).thenApply(r -> r.map(this::toResponse)).thenAccept(webContext::writeResultToHttpResponse);
}
Also used : RenewalContext(org.folio.circulation.resources.context.RenewalContext) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) JsonPropertyFetcher.getDateTimeProperty(org.folio.circulation.support.json.JsonPropertyFetcher.getDateTimeProperty) ZonedDateTime(java.time.ZonedDateTime) RequestQueueRepository(org.folio.circulation.infrastructure.storage.requests.RequestQueueRepository) StringUtils(org.apache.commons.lang3.StringUtils) RENEWAL_IS_NOT_POSSIBLE(org.folio.circulation.resources.handlers.error.CirculationErrorType.RENEWAL_IS_NOT_POSSIBLE) CommonFailures.failedDueToServerError(org.folio.circulation.support.results.CommonFailures.failedDueToServerError) FeeFineOwnerRepository(org.folio.circulation.infrastructure.storage.feesandfines.FeeFineOwnerRepository) JsonObject(io.vertx.core.json.JsonObject) RenewalValidator.loanPolicyValidationError(org.folio.circulation.resources.RenewalValidator.loanPolicyValidationError) CLAIMED_RETURNED(org.folio.circulation.domain.ItemStatus.CLAIMED_RETURNED) EnumSet(java.util.EnumSet) Loan(org.folio.circulation.domain.Loan) ValidationError(org.folio.circulation.support.http.server.ValidationError) Result(org.folio.circulation.support.results.Result) RenewalValidator.errorForDueDate(org.folio.circulation.resources.RenewalValidator.errorForDueDate) LoanPolicy(org.folio.circulation.domain.policy.LoanPolicy) InactiveUserRenewalValidator(org.folio.circulation.domain.validation.InactiveUserRenewalValidator) FeeFineScheduledNoticeService(org.folio.circulation.domain.notice.schedule.FeeFineScheduledNoticeService) RouteRegistration(org.folio.circulation.support.RouteRegistration) LoanRepresentation(org.folio.circulation.domain.LoanRepresentation) PATRON_BLOCK(org.folio.circulation.domain.override.OverridableBlockType.PATRON_BLOCK) OverduePeriodCalculatorService(org.folio.circulation.domain.OverduePeriodCalculatorService) JsonPropertyFetcher.getObjectProperty(org.folio.circulation.support.json.JsonPropertyFetcher.getObjectProperty) ITEM_DOES_NOT_EXIST(org.folio.circulation.resources.handlers.error.CirculationErrorType.ITEM_DOES_NOT_EXIST) RequestRepository(org.folio.circulation.infrastructure.storage.requests.RequestRepository) INSUFFICIENT_OVERRIDE_PERMISSIONS(org.folio.circulation.resources.handlers.error.CirculationErrorType.INSUFFICIENT_OVERRIDE_PERMISSIONS) FeeFineRepository(org.folio.circulation.infrastructure.storage.feesandfines.FeeFineRepository) BlockValidator(org.folio.circulation.domain.validation.overriding.BlockValidator) OverdueFinePolicyRepository(org.folio.circulation.infrastructure.storage.loans.OverdueFinePolicyRepository) FeeFineFacade(org.folio.circulation.services.FeeFineFacade) WebContext(org.folio.circulation.support.http.server.WebContext) RenewalValidator.errorForNotMatchingOverrideCases(org.folio.circulation.resources.RenewalValidator.errorForNotMatchingOverrideCases) Resource(org.folio.circulation.resources.Resource) FIXED_POLICY_HAS_ALTERNATE_RENEWAL_PERIOD_FOR_HOLDS(org.folio.circulation.resources.RenewalValidator.FIXED_POLICY_HAS_ALTERNATE_RENEWAL_PERIOD_FOR_HOLDS) ArrayList(java.util.ArrayList) Validator(org.folio.circulation.domain.validation.Validator) RENEWAL_IS_BLOCKED(org.folio.circulation.resources.handlers.error.CirculationErrorType.RENEWAL_IS_BLOCKED) RenewalValidator.itemByIdValidationError(org.folio.circulation.resources.RenewalValidator.itemByIdValidationError) DECLARED_LOST(org.folio.circulation.domain.ItemStatus.DECLARED_LOST) USER_IS_BLOCKED_MANUALLY(org.folio.circulation.resources.handlers.error.CirculationErrorType.USER_IS_BLOCKED_MANUALLY) ScheduledNoticesRepository(org.folio.circulation.infrastructure.storage.notices.ScheduledNoticesRepository) RenewalValidator.errorForRecallRequest(org.folio.circulation.resources.RenewalValidator.errorForRecallRequest) RequestType(org.folio.circulation.domain.RequestType) AutomatedPatronBlocksRepository(org.folio.circulation.infrastructure.storage.AutomatedPatronBlocksRepository) OverdueFineService(org.folio.circulation.domain.OverdueFineService) UserManualBlocksValidator(org.folio.circulation.domain.validation.UserManualBlocksValidator) RENEWAL_VALIDATION_ERROR(org.folio.circulation.resources.handlers.error.CirculationErrorType.RENEWAL_VALIDATION_ERROR) CirculationErrorHandler(org.folio.circulation.resources.handlers.error.CirculationErrorHandler) StoreLoanAndItem(org.folio.circulation.StoreLoanAndItem) CHECKED_OUT(org.folio.circulation.domain.ItemStatus.CHECKED_OUT) HOLD(org.folio.circulation.domain.RequestType.HOLD) RENEWAL_BLOCK(org.folio.circulation.domain.override.OverridableBlockType.RENEWAL_BLOCK) UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) ResultBinding.mapResult(org.folio.circulation.support.results.ResultBinding.mapResult) ConfigurationRepository(org.folio.circulation.infrastructure.storage.ConfigurationRepository) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) LoanPolicyRepository(org.folio.circulation.infrastructure.storage.loans.LoanPolicyRepository) Router(io.vertx.ext.web.Router) RoutingContext(io.vertx.ext.web.RoutingContext) ItemStatus(org.folio.circulation.domain.ItemStatus) ClosedLibraryStrategyService(org.folio.circulation.domain.policy.library.ClosedLibraryStrategyService) CalendarRepository(org.folio.circulation.infrastructure.storage.CalendarRepository) LoanScheduledNoticeService(org.folio.circulation.domain.notice.schedule.LoanScheduledNoticeService) DateTimeUtil.isAfterMillis(org.folio.circulation.support.utils.DateTimeUtil.isAfterMillis) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) LostItemFeeRefundService(org.folio.circulation.services.LostItemFeeRefundService) Result.succeeded(org.folio.circulation.support.results.Result.succeeded) LoanNoticeSender(org.folio.circulation.resources.LoanNoticeSender) BlockOverrides(org.folio.circulation.domain.override.BlockOverrides) EventPublisher(org.folio.circulation.services.EventPublisher) FIXED_POLICY_HAS_ALTERNATE_RENEWAL_PERIOD(org.folio.circulation.resources.RenewalValidator.FIXED_POLICY_HAS_ALTERNATE_RENEWAL_PERIOD) List(java.util.List) RECALL(org.folio.circulation.domain.RequestType.RECALL) CAN_NOT_RENEW_ITEM_ERROR(org.folio.circulation.resources.RenewalValidator.CAN_NOT_RENEW_ITEM_ERROR) ClockUtil(org.folio.circulation.support.utils.ClockUtil) CirculationErrorType(org.folio.circulation.resources.handlers.error.CirculationErrorType) HttpClient(io.vertx.core.http.HttpClient) Clients(org.folio.circulation.support.Clients) Request(org.folio.circulation.domain.Request) CompletableFuture(java.util.concurrent.CompletableFuture) RenewalValidator.errorWhenEarlierOrSameDueDate(org.folio.circulation.resources.RenewalValidator.errorWhenEarlierOrSameDueDate) HttpResponse(org.folio.circulation.support.http.server.HttpResponse) Function(java.util.function.Function) JsonHttpResponse(org.folio.circulation.support.http.server.JsonHttpResponse) USER_IS_INACTIVE(org.folio.circulation.resources.handlers.error.CirculationErrorType.USER_IS_INACTIVE) AutomatedPatronBlocksValidator(org.folio.circulation.domain.validation.AutomatedPatronBlocksValidator) OkapiPermissions(org.folio.circulation.support.http.OkapiPermissions) AGED_TO_LOST(org.folio.circulation.domain.ItemStatus.AGED_TO_LOST) RequestQueue(org.folio.circulation.domain.RequestQueue) OverridingErrorHandler(org.folio.circulation.resources.handlers.error.OverridingErrorHandler) ValidationErrorFailure(org.folio.circulation.support.ValidationErrorFailure) FAILED_TO_FETCH_USER(org.folio.circulation.resources.handlers.error.CirculationErrorType.FAILED_TO_FETCH_USER) RENEWAL_DUE_DATE_REQUIRED_IS_BLOCKED(org.folio.circulation.resources.handlers.error.CirculationErrorType.RENEWAL_DUE_DATE_REQUIRED_IS_BLOCKED) FAILED_TO_FIND_SINGLE_OPEN_LOAN(org.folio.circulation.resources.handlers.error.CirculationErrorType.FAILED_TO_FIND_SINGLE_OPEN_LOAN) ValidationErrorFailure.failedValidation(org.folio.circulation.support.ValidationErrorFailure.failedValidation) RenewalValidator.overrideDueDateIsRequiredError(org.folio.circulation.resources.RenewalValidator.overrideDueDateIsRequiredError) OverridingBlockValidator(org.folio.circulation.domain.validation.overriding.OverridingBlockValidator) USER_IS_BLOCKED_AUTOMATICALLY(org.folio.circulation.resources.handlers.error.CirculationErrorType.USER_IS_BLOCKED_AUTOMATICALLY) JsonPropertyFetcher.getProperty(org.folio.circulation.support.json.JsonPropertyFetcher.getProperty) WebContext(org.folio.circulation.support.http.server.WebContext) JsonObject(io.vertx.core.json.JsonObject) UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) LoanPolicyRepository(org.folio.circulation.infrastructure.storage.loans.LoanPolicyRepository) ConfigurationRepository(org.folio.circulation.infrastructure.storage.ConfigurationRepository) EventPublisher(org.folio.circulation.services.EventPublisher) LoanNoticeSender(org.folio.circulation.resources.LoanNoticeSender) BlockOverrides(org.folio.circulation.domain.override.BlockOverrides) OkapiPermissions(org.folio.circulation.support.http.OkapiPermissions) FeeFineScheduledNoticeService(org.folio.circulation.domain.notice.schedule.FeeFineScheduledNoticeService) CirculationErrorHandler(org.folio.circulation.resources.handlers.error.CirculationErrorHandler) OverridingErrorHandler(org.folio.circulation.resources.handlers.error.OverridingErrorHandler) Clients(org.folio.circulation.support.Clients) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) AutomatedPatronBlocksRepository(org.folio.circulation.infrastructure.storage.AutomatedPatronBlocksRepository) LoanScheduledNoticeService(org.folio.circulation.domain.notice.schedule.LoanScheduledNoticeService) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) StoreLoanAndItem(org.folio.circulation.StoreLoanAndItem) LoanRepresentation(org.folio.circulation.domain.LoanRepresentation) RenewalContext(org.folio.circulation.resources.context.RenewalContext) RequestQueueRepository(org.folio.circulation.infrastructure.storage.requests.RequestQueueRepository) OverridingBlockValidator(org.folio.circulation.domain.validation.overriding.OverridingBlockValidator)

Aggregations

HttpClient (io.vertx.core.http.HttpClient)4 JsonObject (io.vertx.core.json.JsonObject)4 RoutingContext (io.vertx.ext.web.RoutingContext)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 Loan (org.folio.circulation.domain.Loan)4 RequestQueue (org.folio.circulation.domain.RequestQueue)4 LoanScheduledNoticeService (org.folio.circulation.domain.notice.schedule.LoanScheduledNoticeService)4 ItemRepository (org.folio.circulation.infrastructure.storage.inventory.ItemRepository)4 LoanRepository (org.folio.circulation.infrastructure.storage.loans.LoanRepository)4 RequestQueueRepository (org.folio.circulation.infrastructure.storage.requests.RequestQueueRepository)4 RequestRepository (org.folio.circulation.infrastructure.storage.requests.RequestRepository)4 UserRepository (org.folio.circulation.infrastructure.storage.users.UserRepository)4 Router (io.vertx.ext.web.Router)3 ZonedDateTime (java.time.ZonedDateTime)3 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)3 LoanAndRelatedRecords (org.folio.circulation.domain.LoanAndRelatedRecords)3 LoanRepresentation (org.folio.circulation.domain.LoanRepresentation)3 ConfigurationRepository (org.folio.circulation.infrastructure.storage.ConfigurationRepository)3 LoanPolicyRepository (org.folio.circulation.infrastructure.storage.loans.LoanPolicyRepository)3 OverdueFinePolicyRepository (org.folio.circulation.infrastructure.storage.loans.OverdueFinePolicyRepository)3