Search in sources :

Example 1 with CheckInContext

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

the class LogCheckInServiceTest method logCheckInOperationPropagatesException.

@Test
void logCheckInOperationPropagatesException() {
    final CheckInContext context = checkInProcessRecords();
    final ServerErrorFailure postError = new ServerErrorFailure("ServerError");
    when(checkInStorageClient.post(any(JsonObject.class))).thenReturn(CompletableFuture.completedFuture(Result.failed(postError)));
    final CompletableFuture<Result<CheckInContext>> logCheckInOperation = logCheckInService.logCheckInOperation(context);
    final Result<CheckInContext> logResult = logCheckInOperation.getNow(Result.failed(new ServerErrorFailure("Uncompleted")));
    assertThat(logResult.failed(), is(true));
    assertThat(logResult.cause(), is(postError));
}
Also used : ServerErrorFailure(org.folio.circulation.support.ServerErrorFailure) CheckInContext(org.folio.circulation.domain.CheckInContext) JsonObject(io.vertx.core.json.JsonObject) Result(org.folio.circulation.support.results.Result) Test(org.junit.jupiter.api.Test)

Example 2 with CheckInContext

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

the class LogCheckInServiceTest method checkInProcessRecords.

private CheckInContext checkInProcessRecords() {
    JsonObject requestRepresentation = new JsonObject().put("servicePointId", UUID.randomUUID().toString()).put("itemBarcode", "barcode").put("checkInDate", ClockUtil.getZonedDateTime().toString());
    JsonObject itemRepresentation = new JsonObject().put("id", UUID.randomUUID().toString()).put("status", new JsonObject().put("name", "Available"));
    return new CheckInContext(CheckInByBarcodeRequest.from(requestRepresentation).value()).withItem(Item.from(itemRepresentation)).withRequestQueue(new RequestQueue(Collections.emptyList())).withLoggedInUserId(UUID.randomUUID().toString());
}
Also used : CheckInContext(org.folio.circulation.domain.CheckInContext) RequestQueue(org.folio.circulation.domain.RequestQueue) JsonObject(io.vertx.core.json.JsonObject)

Example 3 with CheckInContext

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

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

the class EventPublisher method publishItemCheckedInEvents.

public CompletableFuture<Result<CheckInContext>> publishItemCheckedInEvents(CheckInContext checkInContext, UserRepository userRepository) {
    runAsync(() -> userRepository.getUser(checkInContext.getLoggedInUserId()).thenApplyAsync(r -> r.after(loggedInUser -> CompletableFuture.completedFuture(Result.succeeded(pubSubPublishingService.publishEvent(LOG_RECORD.name(), mapToCheckInLogEventContent(checkInContext, loggedInUser)))))));
    if (checkInContext.getLoan() != null) {
        Loan loan = checkInContext.getLoan();
        JsonObject payloadJsonObject = new JsonObject();
        write(payloadJsonObject, USER_ID_FIELD, loan.getUserId());
        write(payloadJsonObject, LOAN_ID_FIELD, loan.getId());
        write(payloadJsonObject, RETURN_DATE_FIELD, loan.getReturnDate());
        return pubSubPublishingService.publishEvent(ITEM_CHECKED_IN.name(), payloadJsonObject.encode()).handle((result, error) -> handlePublishEventError(error, checkInContext));
    } else {
        logger.error(FAILED_TO_PUBLISH_LOG_TEMPLATE, ITEM_CHECKED_IN.name());
    }
    return completedFuture(succeeded(checkInContext));
}
Also used : UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) RenewalContext(org.folio.circulation.resources.context.RenewalContext) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) LogContextActionResolver(org.folio.circulation.domain.representations.logs.LogContextActionResolver) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) RoutingContext(io.vertx.ext.web.RoutingContext) JsonPropertyWriter.write(org.folio.circulation.support.json.JsonPropertyWriter.write) CHECKED_IN(org.folio.circulation.domain.LoanAction.CHECKED_IN) DateFormatUtil.formatDateTimeOptional(org.folio.circulation.support.utils.DateFormatUtil.formatDateTimeOptional) LoanLogContext(org.folio.circulation.domain.representations.logs.LoanLogContext) LOAN_CLOSED(org.folio.circulation.domain.EventType.LOAN_CLOSED) LOG_EVENT_TYPE(org.folio.circulation.domain.representations.logs.LogEventPayloadField.LOG_EVENT_TYPE) LoanAndRelatedRecords(org.folio.circulation.domain.LoanAndRelatedRecords) Period(org.folio.circulation.domain.policy.Period) HttpFailure(org.folio.circulation.support.HttpFailure) CommonFailures.failedDueToServerError(org.folio.circulation.support.results.CommonFailures.failedDueToServerError) LoanAnonymizationRecords(org.folio.circulation.domain.anonymization.LoanAnonymizationRecords) JsonObject(io.vertx.core.json.JsonObject) DUE_DATE_CHANGED(org.folio.circulation.domain.LoanAction.DUE_DATE_CHANGED) LOAN(org.folio.circulation.domain.representations.logs.LogEventType.LOAN) CirculationCheckInCheckOutLogEventMapper.mapToCheckOutLogEventContent(org.folio.circulation.domain.representations.logs.CirculationCheckInCheckOutLogEventMapper.mapToCheckOutLogEventContent) Result.succeeded(org.folio.circulation.support.results.Result.succeeded) Loan(org.folio.circulation.domain.Loan) PAYLOAD(org.folio.circulation.domain.representations.logs.LogEventPayloadField.PAYLOAD) Result(org.folio.circulation.support.results.Result) LoanPolicy(org.folio.circulation.domain.policy.LoanPolicy) Logger(org.apache.logging.log4j.Logger) ITEM_CLAIMED_RETURNED(org.folio.circulation.domain.EventType.ITEM_CLAIMED_RETURNED) NoticeLogContext(org.folio.circulation.domain.representations.logs.NoticeLogContext) NOTICE(org.folio.circulation.domain.representations.logs.LogEventType.NOTICE) ClockUtil.getZonedDateTime(org.folio.circulation.support.utils.ClockUtil.getZonedDateTime) EventType(org.folio.circulation.domain.EventType) Request(org.folio.circulation.domain.Request) CompletableFuture(java.util.concurrent.CompletableFuture) ITEM_CHECKED_OUT(org.folio.circulation.domain.EventType.ITEM_CHECKED_OUT) CheckInContext(org.folio.circulation.domain.CheckInContext) AsyncCoordinationUtil.allOf(org.folio.circulation.support.AsyncCoordinationUtil.allOf) RequestUpdateLogEventMapper.mapToRequestLogEventJson(org.folio.circulation.domain.representations.logs.RequestUpdateLogEventMapper.mapToRequestLogEventJson) RequestAndRelatedRecords(org.folio.circulation.domain.RequestAndRelatedRecords) LOAN_DUE_DATE_CHANGED(org.folio.circulation.domain.EventType.LOAN_DUE_DATE_CHANGED) LogEventType(org.folio.circulation.domain.representations.logs.LogEventType) CompletableFuture.runAsync(java.util.concurrent.CompletableFuture.runAsync) Optional.ofNullable(java.util.Optional.ofNullable) NOTICE_ERROR(org.folio.circulation.domain.representations.logs.LogEventType.NOTICE_ERROR) CirculationCheckInCheckOutLogEventMapper.mapToCheckInLogEventContent(org.folio.circulation.domain.representations.logs.CirculationCheckInCheckOutLogEventMapper.mapToCheckInLogEventContent) Result.ofAsync(org.folio.circulation.support.results.Result.ofAsync) ITEM_AGED_TO_LOST(org.folio.circulation.domain.EventType.ITEM_AGED_TO_LOST) User(org.folio.circulation.domain.User) ITEM_CHECKED_IN(org.folio.circulation.domain.EventType.ITEM_CHECKED_IN) ITEM_DECLARED_LOST(org.folio.circulation.domain.EventType.ITEM_DECLARED_LOST) LOG_RECORD(org.folio.circulation.domain.EventType.LOG_RECORD) RECALLREQUESTED(org.folio.circulation.domain.LoanAction.RECALLREQUESTED) LogManager(org.apache.logging.log4j.LogManager) Loan(org.folio.circulation.domain.Loan) JsonObject(io.vertx.core.json.JsonObject)

Example 5 with CheckInContext

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

the class CheckInByBarcodeResource method checkIn.

private void checkIn(RoutingContext routingContext) {
    final WebContext context = new WebContext(routingContext);
    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 Result<CheckInByBarcodeRequest> checkInRequestResult = CheckInByBarcodeRequest.from(routingContext.getBodyAsJson());
    final EventPublisher eventPublisher = new EventPublisher(routingContext);
    final var checkInValidators = new CheckInValidators(this::errorWhenInIncorrectStatus);
    final CheckInProcessAdapter processAdapter = CheckInProcessAdapter.newInstance(clients, itemRepository, userRepository, loanRepository, requestRepository, new RequestQueueRepository(requestRepository));
    final RequestScheduledNoticeService requestScheduledNoticeService = RequestScheduledNoticeService.using(clients);
    final PatronActionSessionService patronActionSessionService = PatronActionSessionService.using(clients, PatronActionSessionRepository.using(clients, loanRepository, userRepository));
    final RequestNoticeSender requestNoticeSender = RequestNoticeSender.using(clients);
    final ConfigurationRepository configurationRepository = new ConfigurationRepository(clients);
    refuseWhenLoggedInUserNotPresent(context).next(notUsed -> checkInRequestResult).map(CheckInContext::new).combineAfter(processAdapter::findItem, (records, item) -> records.withItem(item).withItemStatusBeforeCheckIn(item.getStatus())).thenApply(checkInValidators::refuseWhenItemIsNotAllowedForCheckIn).thenApply(checkInValidators::refuseWhenClaimedReturnedIsNotResolved).thenComposeAsync(r -> r.combineAfter(configurationRepository::lookupTlrSettings, CheckInContext::withTlrSettings)).thenComposeAsync(findItemResult -> findItemResult.combineAfter(processAdapter::getRequestQueue, CheckInContext::withRequestQueue)).thenApply(findRequestQueueResult -> findRequestQueueResult.map(processAdapter::setInHouseUse)).thenApplyAsync(r -> r.map(records -> records.withLoggedInUserId(context.getUserId()))).thenComposeAsync(setUserResult -> setUserResult.after(processAdapter::logCheckInOperation)).thenComposeAsync(logCheckInResult -> logCheckInResult.combineAfter(processAdapter::findSingleOpenLoan, CheckInContext::withLoan)).thenComposeAsync(findLoanResult -> findLoanResult.combineAfter(processAdapter::checkInLoan, CheckInContext::withLoan)).thenComposeAsync(checkInLoan -> checkInLoan.combineAfter(processAdapter::updateRequestQueue, CheckInContext::withRequestQueue)).thenComposeAsync(updateRequestQueueResult -> updateRequestQueueResult.combineAfter(processAdapter::updateItem, CheckInContext::withItem)).thenApply(handleItemStatus -> handleItemStatus.next(requestNoticeSender::sendNoticeOnRequestAwaitingPickup)).thenComposeAsync(updateItemResult -> updateItemResult.combineAfter(processAdapter::getDestinationServicePoint, CheckInContext::withItem)).thenComposeAsync(updateItemResult -> updateItemResult.combineAfter(processAdapter::getCheckInServicePoint, CheckInContext::withCheckInServicePoint)).thenComposeAsync(updateItemResult -> updateItemResult.combineAfter(processAdapter::getPickupServicePoint, CheckInContext::withHighestPriorityFulfillableRequest)).thenComposeAsync(updateItemResult -> updateItemResult.combineAfter(processAdapter::getRequester, CheckInContext::withHighestPriorityFulfillableRequest)).thenComposeAsync(updateItemResult -> updateItemResult.combineAfter(processAdapter::getAddressType, CheckInContext::withHighestPriorityFulfillableRequest)).thenComposeAsync(updateItemResult -> updateItemResult.combineAfter(processAdapter::updateLoan, CheckInContext::withLoan)).thenComposeAsync(updateItemResult -> updateItemResult.after(patronActionSessionService::saveCheckInSessionRecord)).thenComposeAsync(r -> r.after(processAdapter::refundLostItemFees)).thenComposeAsync(r -> r.after(records -> processAdapter.createOverdueFineIfNecessary(records, context))).thenComposeAsync(r -> r.after(v -> eventPublisher.publishItemCheckedInEvents(v, userRepository))).thenApply(r -> r.next(requestScheduledNoticeService::rescheduleRequestNotices)).thenApply(r -> r.map(CheckInByBarcodeResponse::fromRecords)).thenApply(r -> r.map(CheckInByBarcodeResponse::toHttpResponse)).thenAccept(context::writeResultToHttpResponse);
}
Also used : UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) Clients(org.folio.circulation.support.Clients) RequestScheduledNoticeService(org.folio.circulation.domain.notice.schedule.RequestScheduledNoticeService) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) ConfigurationRepository(org.folio.circulation.infrastructure.storage.ConfigurationRepository) WebContext(org.folio.circulation.support.http.server.WebContext) RequestQueueRepository(org.folio.circulation.infrastructure.storage.requests.RequestQueueRepository) CheckInByBarcodeResponse(org.folio.circulation.domain.representations.CheckInByBarcodeResponse) Router(io.vertx.ext.web.Router) ValidationErrorFailure.singleValidationError(org.folio.circulation.support.ValidationErrorFailure.singleValidationError) RoutingContext(io.vertx.ext.web.RoutingContext) CheckInContext(org.folio.circulation.domain.CheckInContext) Item(org.folio.circulation.domain.Item) UserNotFoundValidator.refuseWhenLoggedInUserNotPresent(org.folio.circulation.domain.validation.UserNotFoundValidator.refuseWhenLoggedInUserNotPresent) CheckInValidators(org.folio.circulation.domain.validation.CheckInValidators) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) ITEM_BARCODE(org.folio.circulation.domain.representations.CheckOutByBarcodeRequest.ITEM_BARCODE) ValidationErrorFailure(org.folio.circulation.support.ValidationErrorFailure) PatronActionSessionRepository(org.folio.circulation.infrastructure.storage.sessions.PatronActionSessionRepository) Result(org.folio.circulation.support.results.Result) EventPublisher(org.folio.circulation.services.EventPublisher) RouteRegistration(org.folio.circulation.support.RouteRegistration) CheckInByBarcodeRequest(org.folio.circulation.domain.representations.CheckInByBarcodeRequest) PatronActionSessionService(org.folio.circulation.domain.notice.session.PatronActionSessionService) RequestRepository(org.folio.circulation.infrastructure.storage.requests.RequestRepository) HttpClient(io.vertx.core.http.HttpClient) WebContext(org.folio.circulation.support.http.server.WebContext) PatronActionSessionService(org.folio.circulation.domain.notice.session.PatronActionSessionService) UserRepository(org.folio.circulation.infrastructure.storage.users.UserRepository) CheckInValidators(org.folio.circulation.domain.validation.CheckInValidators) ConfigurationRepository(org.folio.circulation.infrastructure.storage.ConfigurationRepository) CheckInByBarcodeRequest(org.folio.circulation.domain.representations.CheckInByBarcodeRequest) EventPublisher(org.folio.circulation.services.EventPublisher) RequestScheduledNoticeService(org.folio.circulation.domain.notice.schedule.RequestScheduledNoticeService) Clients(org.folio.circulation.support.Clients) CheckInByBarcodeResponse(org.folio.circulation.domain.representations.CheckInByBarcodeResponse) LoanRepository(org.folio.circulation.infrastructure.storage.loans.LoanRepository) ItemRepository(org.folio.circulation.infrastructure.storage.inventory.ItemRepository) CheckInContext(org.folio.circulation.domain.CheckInContext) RequestQueueRepository(org.folio.circulation.infrastructure.storage.requests.RequestQueueRepository)

Aggregations

CheckInContext (org.folio.circulation.domain.CheckInContext)7 JsonObject (io.vertx.core.json.JsonObject)4 Loan (org.folio.circulation.domain.Loan)4 User (org.folio.circulation.domain.User)4 Result (org.folio.circulation.support.results.Result)4 CompletableFuture (java.util.concurrent.CompletableFuture)3 LogManager (org.apache.logging.log4j.LogManager)3 Logger (org.apache.logging.log4j.Logger)3 LoanAndRelatedRecords (org.folio.circulation.domain.LoanAndRelatedRecords)3 NoticeLogContext (org.folio.circulation.domain.representations.logs.NoticeLogContext)3 RoutingContext (io.vertx.ext.web.RoutingContext)2 MethodHandles (java.lang.invoke.MethodHandles)2 EnumMap (java.util.EnumMap)2 List (java.util.List)2 Optional.ofNullable (java.util.Optional.ofNullable)2 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)2 CompletableFuture.runAsync (java.util.concurrent.CompletableFuture.runAsync)2 Collectors (java.util.stream.Collectors)2 LoanRepository (org.folio.circulation.infrastructure.storage.loans.LoanRepository)2 PatronActionSessionRepository (org.folio.circulation.infrastructure.storage.sessions.PatronActionSessionRepository)2