Search in sources :

Example 16 with Loan

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

the class FeeFineScheduledNoticeHandler method buildNoticeLogContext.

@Override
protected NoticeLogContext buildNoticeLogContext(ScheduledNoticeContext context) {
    Loan loan = context.getLoan();
    ScheduledNotice notice = context.getNotice();
    NoticeLogContextItem logContextItem = NoticeLogContextItem.from(loan).withTemplateId(notice.getConfiguration().getTemplateId()).withTriggeringEvent(notice.getTriggeringEvent().getRepresentation()).withNoticePolicyId(context.getPatronNoticePolicyId());
    return new NoticeLogContext().withUser(loan.getUser()).withAccountId(context.getAccount().getId()).withItems(singletonList(logContextItem));
}
Also used : NoticeLogContextItem(org.folio.circulation.domain.representations.logs.NoticeLogContextItem) NoticeLogContext(org.folio.circulation.domain.representations.logs.NoticeLogContext) Loan(org.folio.circulation.domain.Loan)

Example 17 with Loan

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

the class GroupedLoanScheduledNoticeHandler method sendGroupedNotice.

private CompletableFuture<Result<List<ScheduledNoticeContext>>> sendGroupedNotice(List<ScheduledNoticeContext> contexts) {
    if (contexts.isEmpty()) {
        log.warn("No notices left in the group to process, skipping the group");
        return completedFuture(succeeded(contexts));
    }
    List<ScheduledNoticeContext> relevantContexts = contexts.stream().filter(not(loanScheduledNoticeHandler::isNoticeIrrelevant)).collect(toList());
    if (relevantContexts.isEmpty()) {
        log.warn("No relevant notices in the group, skipping the group");
        return completedFuture(succeeded(contexts));
    }
    // All the notices have the same properties so we can get any of them
    ScheduledNoticeContext contextSample = relevantContexts.get(0);
    User user = contextSample.getLoan().getUser();
    List<Loan> loans = relevantContexts.stream().map(ScheduledNoticeContext::getLoan).collect(toList());
    log.info("Attempting to send a grouped notice for {} scheduled notices", relevantContexts.size());
    return patronNoticeService.sendNotice(contextSample.getNotice().getConfiguration(), user.getId(), createMultiLoanNoticeContext(user, loans), buildNoticeLogContext(relevantContexts, user)).thenApply(mapResult(v -> contexts));
}
Also used : Clients(org.folio.circulation.support.Clients) ScheduledNoticeContext(org.folio.circulation.domain.notice.schedule.ScheduledNoticeHandler.ScheduledNoticeContext) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) ResultBinding.mapResult(org.folio.circulation.support.results.ResultBinding.mapResult) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncCoordinationUtil.allOf(org.folio.circulation.support.AsyncCoordinationUtil.allOf) AsyncCoordinationUtil.allResultsOf(org.folio.circulation.support.AsyncCoordinationUtil.allResultsOf) HttpFailure(org.folio.circulation.support.HttpFailure) ScheduledPatronNoticeService(org.folio.circulation.domain.notice.ScheduledPatronNoticeService) Logger(org.slf4j.Logger) Result.succeeded(org.folio.circulation.support.results.Result.succeeded) MethodHandles(java.lang.invoke.MethodHandles) Loan(org.folio.circulation.domain.Loan) Result(org.folio.circulation.support.results.Result) Result.ofAsync(org.folio.circulation.support.results.Result.ofAsync) User(org.folio.circulation.domain.User) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) NoticeLogContextItem(org.folio.circulation.domain.representations.logs.NoticeLogContextItem) TemplateContextUtil.createMultiLoanNoticeContext(org.folio.circulation.domain.notice.TemplateContextUtil.createMultiLoanNoticeContext) Predicate.not(java.util.function.Predicate.not) NoticeLogContext(org.folio.circulation.domain.representations.logs.NoticeLogContext) User(org.folio.circulation.domain.User) Loan(org.folio.circulation.domain.Loan) ScheduledNoticeContext(org.folio.circulation.domain.notice.schedule.ScheduledNoticeHandler.ScheduledNoticeContext)

Example 18 with Loan

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

the class PatronActionSessionService method discardInvalidSessions.

private List<PatronSessionRecord> discardInvalidSessions(List<PatronSessionRecord> sessions) {
    List<PatronSessionRecord> validSessions = new ArrayList<>();
    for (PatronSessionRecord session : sessions) {
        Loan loan = session.getLoan();
        String errorMessage = null;
        if (loan == null) {
            errorMessage = "referenced loan was not found";
        } else {
            if (loan.getItem() == null || loan.getItem().isNotFound()) {
                errorMessage = "referenced item was not found";
            }
            if (loan.getUser() == null) {
                errorMessage = "referenced user was not found";
            }
        }
        if (errorMessage != null) {
            errorMessage += ". " + session;
            log.error("Failed to send notice for patron action session: {}", errorMessage);
            publishNoticeErrorEvent(singletonList(session), errorMessage);
        } else {
            validSessions.add(session);
        }
    }
    if (validSessions.size() < sessions.size()) {
        log.warn("{} session(s) are invalid in the group of {} session(s)", sessions.size() - validSessions.size(), sessions.size());
    }
    return validSessions;
}
Also used : Loan(org.folio.circulation.domain.Loan) ArrayList(java.util.ArrayList)

Example 19 with Loan

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

the class PatronActionSessionService method saveCheckInSessionRecord.

public CompletableFuture<Result<CheckInContext>> saveCheckInSessionRecord(CheckInContext context) {
    Loan loan = context.getLoan();
    if (loan == null) {
        log.info("CheckInSessionRecord is not saved, context doesn't have a valid loan.");
        return completedFuture(of(() -> context));
    }
    UUID patronId = UUID.fromString(loan.getUserId());
    UUID loanId = UUID.fromString(loan.getId());
    PatronSessionRecord patronSessionRecord = new PatronSessionRecord(UUID.randomUUID(), patronId, loanId, PatronActionType.CHECK_IN);
    return patronActionSessionRepository.create(patronSessionRecord).thenApply(mapResult(v -> context));
}
Also used : Clients(org.folio.circulation.support.Clients) ResultBinding.mapResult(org.folio.circulation.support.results.ResultBinding.mapResult) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) CompletableFuture(java.util.concurrent.CompletableFuture) TemplateContextUtil.createLoanNoticeContextWithoutUser(org.folio.circulation.domain.notice.TemplateContextUtil.createLoanNoticeContextWithoutUser) CheckInContext(org.folio.circulation.domain.CheckInContext) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) AsyncCoordinationUtil.allOf(org.folio.circulation.support.AsyncCoordinationUtil.allOf) NoticeEventType(org.folio.circulation.domain.notice.NoticeEventType) LoanAndRelatedRecords(org.folio.circulation.domain.LoanAndRelatedRecords) PageLimit(org.folio.circulation.support.http.client.PageLimit) PatronNoticeEvent(org.folio.circulation.domain.notice.PatronNoticeEvent) EnumMap(java.util.EnumMap) 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) PatronActionSessionRepository(org.folio.circulation.infrastructure.storage.sessions.PatronActionSessionRepository) PageLimit.limit(org.folio.circulation.support.http.client.PageLimit.limit) UUID(java.util.UUID) ImmediatePatronNoticeService(org.folio.circulation.domain.notice.ImmediatePatronNoticeService) Result(org.folio.circulation.support.results.Result) Collectors(java.util.stream.Collectors) Result.ofAsync(org.folio.circulation.support.results.Result.ofAsync) EventPublisher(org.folio.circulation.services.EventPublisher) User(org.folio.circulation.domain.User) PatronNoticeEventBuilder(org.folio.circulation.domain.notice.PatronNoticeEventBuilder) List(java.util.List) Logger(org.apache.logging.log4j.Logger) AsynchronousResultBindings.safelyInitialise(org.folio.circulation.support.results.AsynchronousResultBindings.safelyInitialise) NoticeLogContext(org.folio.circulation.domain.representations.logs.NoticeLogContext) AllArgsConstructor(lombok.AllArgsConstructor) LogManager(org.apache.logging.log4j.LogManager) LoanNoticeContextCombiner(org.folio.circulation.domain.notice.combiner.LoanNoticeContextCombiner) Loan(org.folio.circulation.domain.Loan) UUID(java.util.UUID)

Example 20 with Loan

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

the class FixedLoanPolicyCheckOutDueDateCalculationTests method shouldFailWhenLoanDateIsAfterOnlyScheduleAvailable.

@Test
void shouldFailWhenLoanDateIsAfterOnlyScheduleAvailable() {
    LoanPolicy loanPolicy = LoanPolicy.from(new LoanPolicyBuilder().fixed(UUID.randomUUID()).withName("Example Fixed Schedule Loan Policy").create()).withDueDateSchedules(new FixedDueDateSchedulesBuilder().addSchedule(FixedDueDateSchedule.wholeYear(2018)).create());
    ZonedDateTime loanDate = ZonedDateTime.of(2019, 1, 1, 8, 10, 45, 0, UTC);
    Loan loan = loanFor(loanDate);
    final Result<ZonedDateTime> result = loanPolicy.calculateInitialDueDate(loan, null);
    assertThat(result, hasValidationFailure("loan date falls outside of the date ranges in the loan policy"));
}
Also used : FixedDueDateSchedulesBuilder(api.support.builders.FixedDueDateSchedulesBuilder) Loan(org.folio.circulation.domain.Loan) ZonedDateTime(java.time.ZonedDateTime) LoanPolicyBuilder(api.support.builders.LoanPolicyBuilder) Test(org.junit.jupiter.api.Test)

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