use of org.folio.circulation.domain.LoanAndRelatedRecords in project mod-circulation by folio-org.
the class PatronActionSessionService method saveCheckOutSessionRecord.
public CompletableFuture<Result<LoanAndRelatedRecords>> saveCheckOutSessionRecord(LoanAndRelatedRecords records) {
UUID patronId = UUID.fromString(records.getUserId());
UUID loanId = UUID.fromString(records.getLoan().getId());
PatronSessionRecord patronSessionRecord = new PatronSessionRecord(UUID.randomUUID(), patronId, loanId, PatronActionType.CHECK_OUT);
return patronActionSessionRepository.create(patronSessionRecord).thenApply(mapResult(v -> records));
}
use of org.folio.circulation.domain.LoanAndRelatedRecords in project mod-circulation by folio-org.
the class InactiveUserValidatorTests method refuseWhenUserIsNeitherActiveNorInactive.
@Test
void refuseWhenUserIsNeitherActiveNorInactive() {
final User steve = new User(basedUponStevenJones().neitherActiveOrInactive().create());
final InactiveUserValidator validator = forUser(steve.getBarcode());
final Result<LoanAndRelatedRecords> result = validator.refuseWhenUserIsInactive(steve, null);
assertThat(result.failed(), is(true));
}
use of org.folio.circulation.domain.LoanAndRelatedRecords in project mod-circulation by folio-org.
the class EventPublisher method publishItemCheckedOutEvent.
public CompletableFuture<Result<LoanAndRelatedRecords>> publishItemCheckedOutEvent(LoanAndRelatedRecords loanAndRelatedRecords, UserRepository userRepository) {
if (loanAndRelatedRecords.getLoan() != null) {
Loan loan = loanAndRelatedRecords.getLoan();
JsonObject payloadJsonObject = new JsonObject();
write(payloadJsonObject, USER_ID_FIELD, loan.getUserId());
write(payloadJsonObject, LOAN_ID_FIELD, loan.getId());
write(payloadJsonObject, DUE_DATE_FIELD, loan.getDueDate());
ofNullable(loan.getLoanPolicy()).map(LoanPolicy::getGracePeriod).filter(Period::isValid).map(Period::asJson).ifPresent(json -> write(payloadJsonObject, GRACE_PERIOD_FIELD, json));
runAsync(() -> userRepository.getUser(loanAndRelatedRecords.getLoggedInUserId()).thenApplyAsync(r -> r.after(loggedInUser -> CompletableFuture.completedFuture(Result.succeeded(pubSubPublishingService.publishEvent(LOG_RECORD.name(), mapToCheckOutLogEventContent(loanAndRelatedRecords, loggedInUser)))))));
return pubSubPublishingService.publishEvent(ITEM_CHECKED_OUT.name(), payloadJsonObject.encode()).handle((result, error) -> handlePublishEventError(error, loanAndRelatedRecords));
} else {
logger.error(FAILED_TO_PUBLISH_LOG_TEMPLATE, ITEM_CHECKED_OUT.name());
}
return completedFuture(succeeded(loanAndRelatedRecords));
}
use of org.folio.circulation.domain.LoanAndRelatedRecords 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);
}
use of org.folio.circulation.domain.LoanAndRelatedRecords in project mod-circulation by folio-org.
the class ChangeDueDateValidator method refuseChangeDueDateForItemInDisallowedStatus.
public CompletableFuture<Result<LoanAndRelatedRecords>> refuseChangeDueDateForItemInDisallowedStatus(Result<LoanAndRelatedRecords> loanAndRelatedRecordsResult) {
return loanAndRelatedRecordsResult.after(relatedRecords -> {
final Loan changedLoan = relatedRecords.getLoan();
final Result<LoanAndRelatedRecords> statusValidation = itemStatusValidator.refuseWhenItemStatusDoesNotAllowDueDateChange(loanAndRelatedRecordsResult);
// further logic
if (statusValidation.succeeded()) {
return ofAsync(() -> relatedRecords);
}
// all other changes are allowed
return completedFuture(loanAndRelatedRecordsResult.map(LoanAndRelatedRecords::getExistingLoan)).thenApply(r -> r.failWhen(existingLoan -> dueDateHasChanged(existingLoan, changedLoan), existingLoan -> statusValidation.cause())).thenApply(r -> r.map(notUsed -> relatedRecords));
});
}
Aggregations