use of org.folio.circulation.resources.handlers.error.CirculationErrorType.USER_IS_BLOCKED_MANUALLY 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);
}
Aggregations