Search in sources :

Example 1 with LoanRepresentation

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

the class LoanCollectionResource method get.

@Override
void get(RoutingContext routingContext) {
    final WebContext context = new WebContext(routingContext);
    final Clients clients = Clients.create(context, client);
    final var userRepository = new UserRepository(clients);
    final var loanRepository = new LoanRepository(clients, new ItemRepository(clients), userRepository);
    final ServicePointRepository servicePointRepository = new ServicePointRepository(clients);
    final LoanRepresentation loanRepresentation = new LoanRepresentation();
    final LoanPolicyRepository loanPolicyRepository = new LoanPolicyRepository(clients);
    final OverdueFinePolicyRepository overdueFinePolicyRepository = new OverdueFinePolicyRepository(clients);
    final LostItemPolicyRepository lostItemPolicyRepository = new LostItemPolicyRepository(clients);
    final AccountRepository accountRepository = new AccountRepository(clients);
    final PatronGroupRepository patronGroupRepository = new PatronGroupRepository(clients);
    String id = routingContext.request().getParam("id");
    loanRepository.getById(id).thenComposeAsync(accountRepository::findAccountsAndActionsForLoan).thenComposeAsync(servicePointRepository::findServicePointsForLoan).thenComposeAsync(userRepository::findUserForLoan).thenComposeAsync(loanPolicyRepository::findPolicyForLoan).thenComposeAsync(overdueFinePolicyRepository::findOverdueFinePolicyForLoan).thenComposeAsync(lostItemPolicyRepository::findLostItemPolicyForLoan).thenComposeAsync(patronGroupRepository::findGroupForLoan).thenApply(loanResult -> loanResult.map(loanRepresentation::extendedLoan)).thenApply(r -> r.map(JsonHttpResponse::ok)).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) 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) JsonHttpResponse(org.folio.circulation.support.http.server.JsonHttpResponse) AccountRepository(org.folio.circulation.infrastructure.storage.feesandfines.AccountRepository) Clients(org.folio.circulation.support.Clients) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) ServicePointRepository(org.folio.circulation.infrastructure.storage.ServicePointRepository) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) LostItemPolicyRepository(org.folio.circulation.infrastructure.storage.loans.LostItemPolicyRepository) UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) LoanRepresentation(org.folio.circulation.domain.LoanRepresentation) LoanPolicyRepository(org.folio.circulation.infrastructure.storage.loans.LoanPolicyRepository) OverdueFinePolicyRepository(org.folio.circulation.infrastructure.storage.loans.OverdueFinePolicyRepository) PatronGroupRepository(org.folio.circulation.infrastructure.storage.users.PatronGroupRepository)

Example 2 with LoanRepresentation

use of org.folio.circulation.domain.LoanRepresentation 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 3 with LoanRepresentation

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

the class CheckInByBarcodeResponse method toJson.

private JsonObject toJson() {
    final LoanRepresentation loanRepresentation = new LoanRepresentation();
    final ItemSummaryRepresentation itemRepresentation = new ItemSummaryRepresentation();
    final JsonObject json = new JsonObject();
    write(json, "loan", loanRepresentation.extendedLoan(context.getLoan()));
    write(json, "item", itemRepresentation.createItemSummary(context.getItem()));
    write(json, "staffSlipContext", createCheckInContext(context));
    write(json, "inHouseUse", context.isInHouseUse());
    return json;
}
Also used : LoanRepresentation(org.folio.circulation.domain.LoanRepresentation) JsonObject(io.vertx.core.json.JsonObject)

Example 4 with LoanRepresentation

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

the class LoanCollectionResource method getMany.

@Override
void getMany(RoutingContext routingContext) {
    WebContext context = new WebContext(routingContext);
    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 ServicePointRepository servicePointRepository = new ServicePointRepository(clients);
    final LoanRepresentation loanRepresentation = new LoanRepresentation();
    final LoanPolicyRepository loanPolicyRepository = new LoanPolicyRepository(clients);
    final OverdueFinePolicyRepository overdueFinePolicyRepository = new OverdueFinePolicyRepository(clients);
    final LostItemPolicyRepository lostItemPolicyRepository = new LostItemPolicyRepository(clients);
    final AccountRepository accountRepository = new AccountRepository(clients);
    final PatronGroupRepository patronGroupRepository = new PatronGroupRepository(clients);
    loanRepository.findBy(routingContext.request().query()).thenCompose(multiLoanRecordsResult -> multiLoanRecordsResult.after(accountRepository::findAccountsForLoans)).thenCompose(multiLoanRecordsResult -> multiLoanRecordsResult.after(servicePointRepository::findServicePointsForLoans)).thenCompose(multiLoanRecordsResult -> multiLoanRecordsResult.after(userRepository::findUsersForLoans)).thenCompose(multiLoanRecordsResult -> multiLoanRecordsResult.after(loanPolicyRepository::findLoanPoliciesForLoans)).thenCompose(multiLoanRecordsResult -> multiLoanRecordsResult.after(overdueFinePolicyRepository::findOverdueFinePoliciesForLoans)).thenCompose(multiLoanRecordsResult -> multiLoanRecordsResult.after(lostItemPolicyRepository::findLostItemPoliciesForLoans)).thenCompose(multiLoanRecordsResult -> multiLoanRecordsResult.after(patronGroupRepository::findPatronGroupsByIds)).thenApply(multipleLoanRecordsResult -> multipleLoanRecordsResult.map(loans -> loans.asJson(loanRepresentation::extendedLoan, "loans"))).thenApply(r -> r.map(JsonHttpResponse::ok)).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) 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) JsonHttpResponse(org.folio.circulation.support.http.server.JsonHttpResponse) AccountRepository(org.folio.circulation.infrastructure.storage.feesandfines.AccountRepository) Clients(org.folio.circulation.support.Clients) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) ServicePointRepository(org.folio.circulation.infrastructure.storage.ServicePointRepository) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) LostItemPolicyRepository(org.folio.circulation.infrastructure.storage.loans.LostItemPolicyRepository) UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) LoanRepresentation(org.folio.circulation.domain.LoanRepresentation) LoanPolicyRepository(org.folio.circulation.infrastructure.storage.loans.LoanPolicyRepository) OverdueFinePolicyRepository(org.folio.circulation.infrastructure.storage.loans.OverdueFinePolicyRepository) PatronGroupRepository(org.folio.circulation.infrastructure.storage.users.PatronGroupRepository)

Example 5 with LoanRepresentation

use of org.folio.circulation.domain.LoanRepresentation 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);
}
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) JsonHttpResponse(org.folio.circulation.support.http.server.JsonHttpResponse) AlreadyCheckedOutValidator(org.folio.circulation.domain.validation.AlreadyCheckedOutValidator) RequestedByAnotherPatronValidator(org.folio.circulation.domain.validation.RequestedByAnotherPatronValidator) JsonObject(io.vertx.core.json.JsonObject) ServicePointLoanLocationValidator(org.folio.circulation.domain.validation.ServicePointLoanLocationValidator) ServicePointRepository(org.folio.circulation.infrastructure.storage.ServicePointRepository) UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) LoanPolicyRepository(org.folio.circulation.infrastructure.storage.loans.LoanPolicyRepository) EventPublisher(org.folio.circulation.services.EventPublisher) ItemStatusValidator(org.folio.circulation.domain.validation.ItemStatusValidator) Clients(org.folio.circulation.support.Clients) ItemNotFoundValidator(org.folio.circulation.domain.validation.ItemNotFoundValidator) LoanService(org.folio.circulation.domain.LoanService) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) Loan(org.folio.circulation.domain.Loan) UpdateItem(org.folio.circulation.domain.UpdateItem) LoanRepresentation(org.folio.circulation.domain.LoanRepresentation) LoanAndRelatedRecords(org.folio.circulation.domain.LoanAndRelatedRecords) RequestQueueRepository(org.folio.circulation.infrastructure.storage.requests.RequestQueueRepository)

Aggregations

JsonObject (io.vertx.core.json.JsonObject)6 LoanRepresentation (org.folio.circulation.domain.LoanRepresentation)6 HttpClient (io.vertx.core.http.HttpClient)5 RoutingContext (io.vertx.ext.web.RoutingContext)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)5 Loan (org.folio.circulation.domain.Loan)5 RequestQueue (org.folio.circulation.domain.RequestQueue)5 LoanScheduledNoticeService (org.folio.circulation.domain.notice.schedule.LoanScheduledNoticeService)5 ItemRepository (org.folio.circulation.infrastructure.storage.inventory.ItemRepository)5 LoanPolicyRepository (org.folio.circulation.infrastructure.storage.loans.LoanPolicyRepository)5 LoanRepository (org.folio.circulation.infrastructure.storage.loans.LoanRepository)5 OverdueFinePolicyRepository (org.folio.circulation.infrastructure.storage.loans.OverdueFinePolicyRepository)5 RequestQueueRepository (org.folio.circulation.infrastructure.storage.requests.RequestQueueRepository)5 RequestRepository (org.folio.circulation.infrastructure.storage.requests.RequestRepository)5 UserRepository (org.folio.circulation.infrastructure.storage.users.UserRepository)5 EventPublisher (org.folio.circulation.services.EventPublisher)5 Clients (org.folio.circulation.support.Clients)5 WebContext (org.folio.circulation.support.http.server.WebContext)5 Result (org.folio.circulation.support.results.Result)5