use of org.folio.circulation.services.EventPublisher 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);
}
use of org.folio.circulation.services.EventPublisher 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);
}
use of org.folio.circulation.services.EventPublisher in project mod-circulation by folio-org.
the class LoanRelatedFeeFineClosedHandlerResource method handleFeeFineClosedEvent.
private void handleFeeFineClosedEvent(RoutingContext routingContext) {
final WebContext context = new WebContext(routingContext);
final EventPublisher eventPublisher = new EventPublisher(routingContext);
log.info("Event {} received: {}", LOAN_RELATED_FEE_FINE_CLOSED, routingContext.getBodyAsString());
createAndValidateRequest(routingContext).after(request -> processEvent(context, request, eventPublisher)).thenCompose(r -> r.after(eventPublisher::publishClosedLoanEvent)).exceptionally(CommonFailures::failedDueToServerError).thenApply(r -> r.map(toFixedValue(NoContentResponse::noContent))).thenAccept(result -> result.applySideEffect(context::write, failure -> {
log.error("Cannot handle event [{}], error occurred {}", routingContext.getBodyAsString(), failure);
context.write(noContent());
}));
}
use of org.folio.circulation.services.EventPublisher in project mod-circulation by folio-org.
the class LoanRelatedFeeFineClosedHandlerResource method processEvent.
private CompletableFuture<Result<Loan>> processEvent(WebContext context, LoanRelatedFeeFineClosedEvent event, EventPublisher eventPublisher) {
final 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 accountRepository = new AccountRepository(clients);
final var lostItemPolicyRepository = new LostItemPolicyRepository(clients);
return loanRepository.getById(event.getLoanId()).thenCompose(r -> r.after(loan -> {
if (loan.isItemLost()) {
return closeLoanWithLostItemIfLostFeesResolved(loan, loanRepository, itemRepository, accountRepository, lostItemPolicyRepository, eventPublisher);
}
return completedFuture(succeeded(loan));
}));
}
Aggregations