Search in sources :

Example 11 with Loan

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

the class ItemLevelRequestNoticeSender method sendNoticeOnRequestCreated.

@Override
public Result<RequestAndRelatedRecords> sendNoticeOnRequestCreated(RequestAndRelatedRecords relatedRecords) {
    Request request = relatedRecords.getRequest();
    Item item = request.getItem();
    NoticeEventType eventType = requestTypeToEventMap.getOrDefault(request.getRequestType(), NoticeEventType.UNKNOWN);
    PatronNoticeEvent requestCreatedEvent = createPatronNoticeEvent(request, eventType).withItem(item);
    patronNoticeService.acceptNoticeEvent(requestCreatedEvent);
    Loan loan = request.getLoan();
    if (request.getRequestType() == RequestType.RECALL && loan != null) {
        sendNoticeOnItemRecalledEvent(loan);
        sendLogEvent(loan);
    }
    return Result.succeeded(relatedRecords);
}
Also used : Item(org.folio.circulation.domain.Item) NoticeEventType(org.folio.circulation.domain.notice.NoticeEventType) Loan(org.folio.circulation.domain.Loan) Request(org.folio.circulation.domain.Request) PatronNoticeEvent(org.folio.circulation.domain.notice.PatronNoticeEvent)

Example 12 with Loan

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

the class RenewByIdResource method findLoan.

@Override
protected CompletableFuture<Result<Loan>> findLoan(JsonObject request, LoanRepository loanRepository, ItemRepository itemRepository, UserRepository userRepository, CirculationErrorHandler errorHandler) {
    final Result<RenewByIdRequest> requestResult = RenewByIdRequest.from(request);
    final String itemId = requestResult.map(RenewByIdRequest::getItemId).orElse("unknown item ID");
    final SingleOpenLoanForItemInStorageFinder singleOpenLoanFinder = new SingleOpenLoanForItemInStorageFinder(loanRepository, userRepository, false);
    final ItemByIdInStorageFinder itemFinder = new ItemByIdInStorageFinder(itemRepository, noItemFoundForIdFailure(itemId));
    return completedFuture(requestResult).thenCompose(r -> lookupItem(itemFinder, itemId, errorHandler)).thenCompose(r -> r.after(item -> lookupLoan(singleOpenLoanFinder, item, errorHandler))).thenApply(r -> r.next(loan -> refuseWhenUserNotFound(loan, errorHandler))).thenApply(r -> r.next(loan -> refuseWhenUserDoesNotMatch(loan, requestResult.value(), errorHandler)));
}
Also used : UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) FAILED_TO_FETCH_USER(org.folio.circulation.resources.handlers.error.CirculationErrorType.FAILED_TO_FETCH_USER) Result.succeeded(org.folio.circulation.support.results.Result.succeeded) ItemByIdInStorageFinder(org.folio.circulation.storage.ItemByIdInStorageFinder) Loan(org.folio.circulation.domain.Loan) FAILED_TO_FIND_SINGLE_OPEN_LOAN(org.folio.circulation.resources.handlers.error.CirculationErrorType.FAILED_TO_FIND_SINGLE_OPEN_LOAN) CompletableFuture(java.util.concurrent.CompletableFuture) Result(org.folio.circulation.support.results.Result) StringUtils(org.apache.commons.lang3.StringUtils) Item(org.folio.circulation.domain.Item) ValidationErrorFailure.failedValidation(org.folio.circulation.support.ValidationErrorFailure.failedValidation) UserNotFoundValidator(org.folio.circulation.domain.validation.UserNotFoundValidator) CirculationErrorHandler(org.folio.circulation.resources.handlers.error.CirculationErrorHandler) CommonFailures.noItemFoundForIdFailure(org.folio.circulation.domain.validation.CommonFailures.noItemFoundForIdFailure) JsonObject(io.vertx.core.json.JsonObject) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) SingleOpenLoanForItemInStorageFinder(org.folio.circulation.storage.SingleOpenLoanForItemInStorageFinder) ITEM_DOES_NOT_EXIST(org.folio.circulation.resources.handlers.error.CirculationErrorType.ITEM_DOES_NOT_EXIST) USER_DOES_NOT_MATCH(org.folio.circulation.resources.handlers.error.CirculationErrorType.USER_DOES_NOT_MATCH) HttpClient(io.vertx.core.http.HttpClient) SingleOpenLoanForItemInStorageFinder(org.folio.circulation.storage.SingleOpenLoanForItemInStorageFinder) ItemByIdInStorageFinder(org.folio.circulation.storage.ItemByIdInStorageFinder)

Example 13 with Loan

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

the class RenewalResource method validateIfRenewIsPossible.

private Result<RenewalContext> validateIfRenewIsPossible(RenewalContext context) {
    Loan loan = context.getLoan();
    if (ITEM_STATUSES_NOT_POSSIBLE_TO_RENEW.contains(loan.getItemStatus())) {
        final List<ValidationError> errors = new ArrayList<>();
        errors.add(itemByIdValidationError("item is " + loan.getItemStatusName(), loan.getItemId()));
        return failedValidation(errors);
    }
    return succeeded(context);
}
Also used : Loan(org.folio.circulation.domain.Loan) ArrayList(java.util.ArrayList) RenewalValidator.loanPolicyValidationError(org.folio.circulation.resources.RenewalValidator.loanPolicyValidationError) ValidationError(org.folio.circulation.support.http.server.ValidationError) RenewalValidator.itemByIdValidationError(org.folio.circulation.resources.RenewalValidator.itemByIdValidationError)

Example 14 with Loan

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

the class RenewalResource method unsetDueDateChangedByRecallIfNoOpenRecallsInQueue.

private RenewalContext unsetDueDateChangedByRecallIfNoOpenRecallsInQueue(RenewalContext renewalContext) {
    Loan loan = renewalContext.getLoan();
    RequestQueue queue = renewalContext.getRequestQueue();
    if (loan.wasDueDateChangedByRecall() && !queue.hasOpenRecalls()) {
        return renewalContext.withLoan(loan.unsetDueDateChangedByRecall());
    } else {
        return renewalContext;
    }
}
Also used : Loan(org.folio.circulation.domain.Loan) RequestQueue(org.folio.circulation.domain.RequestQueue)

Example 15 with Loan

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

the class RenewalResource method renew.

private Result<RenewalContext> renew(RenewalContext context, ZonedDateTime renewDate) {
    final var loan = context.getLoan();
    final var requestQueue = context.getRequestQueue();
    final var loanPolicy = loan.getLoanPolicy();
    final Result<ZonedDateTime> proposedDueDateResult = calculateNewDueDate(loan, requestQueue, renewDate);
    final List<ValidationError> errors = new ArrayList<>();
    addErrorsIfDueDateResultFailed(loan, errors, proposedDueDateResult);
    if (errors.isEmpty()) {
        final BlockOverrides blockOverrides = BlockOverrides.from(getObjectProperty(context.getRenewalRequest(), "overrideBlocks"));
        if (!blockOverrides.getPatronBlockOverride().isRequested() && !blockOverrides.getRenewalBlockOverride().isRequested()) {
            return proposedDueDateResult.map(dueDate -> loan.renew(dueDate, loanPolicy.getId())).map(l -> context);
        }
        return proposedDueDateResult.map(dueDate -> loan.overrideRenewal(dueDate, loanPolicy.getId(), blockOverrides.getComment())).map(l -> context);
    }
    return failedValidation(errors);
}
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) ZonedDateTime(java.time.ZonedDateTime) BlockOverrides(org.folio.circulation.domain.override.BlockOverrides) ArrayList(java.util.ArrayList) RenewalValidator.loanPolicyValidationError(org.folio.circulation.resources.RenewalValidator.loanPolicyValidationError) ValidationError(org.folio.circulation.support.http.server.ValidationError) RenewalValidator.itemByIdValidationError(org.folio.circulation.resources.RenewalValidator.itemByIdValidationError)

Aggregations

Loan (org.folio.circulation.domain.Loan)133 ZonedDateTime (java.time.ZonedDateTime)89 Test (org.junit.jupiter.api.Test)77 LoanPolicyBuilder (api.support.builders.LoanPolicyBuilder)70 RequestQueue (org.folio.circulation.domain.RequestQueue)48 FixedDueDateSchedulesBuilder (api.support.builders.FixedDueDateSchedulesBuilder)39 OverridingErrorHandler (org.folio.circulation.resources.handlers.error.OverridingErrorHandler)39 JsonObject (io.vertx.core.json.JsonObject)37 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)32 CirculationErrorHandler (org.folio.circulation.resources.handlers.error.CirculationErrorHandler)30 Result (org.folio.circulation.support.results.Result)26 CompletableFuture (java.util.concurrent.CompletableFuture)23 Result.succeeded (org.folio.circulation.support.results.Result.succeeded)21 LoanRepository (org.folio.circulation.infrastructure.storage.loans.LoanRepository)20 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)19 ClockUtil.getZonedDateTime (org.folio.circulation.support.utils.ClockUtil.getZonedDateTime)19 UserRepository (org.folio.circulation.infrastructure.storage.users.UserRepository)17 RoutingContext (io.vertx.ext.web.RoutingContext)15 Clients (org.folio.circulation.support.Clients)15 HttpClient (io.vertx.core.http.HttpClient)14