use of org.folio.circulation.domain.validation.ProxyRelationshipValidator in project mod-circulation by folio-org.
the class LoanCollectionResource method replace.
@Override
void replace(RoutingContext routingContext) {
final WebContext context = new WebContext(routingContext);
JsonObject incomingRepresentation = routingContext.getBodyAsJson();
incomingRepresentation.put("id", routingContext.request().getParam("id"));
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 requestRepository = RequestRepository.using(clients, itemRepository, userRepository, loanRepository);
final var requestQueueRepository = new RequestQueueRepository(requestRepository);
final ServicePointRepository servicePointRepository = new ServicePointRepository(clients);
final var requestQueueUpdate = UpdateRequestQueue.using(clients, requestRepository, requestQueueRepository);
final UpdateItem updateItem = new UpdateItem(itemRepository);
final ProxyRelationshipValidator proxyRelationshipValidator = new ProxyRelationshipValidator(clients, () -> singleValidationError("proxyUserId is not valid", "proxyUserId", loan.getProxyUserId()));
final ItemNotFoundValidator itemNotFoundValidator = createItemNotFoundValidator(loan);
final ServicePointLoanLocationValidator spLoanLocationValidator = new ServicePointLoanLocationValidator();
final ChangeDueDateValidator changeDueDateValidator = new ChangeDueDateValidator();
final LoanScheduledNoticeService scheduledNoticeService = LoanScheduledNoticeService.using(clients);
final EventPublisher eventPublisher = new EventPublisher(routingContext);
final LoanNoticeSender loanNoticeSender = LoanNoticeSender.using(clients);
getExistingLoan(loanRepository, loan).thenApply(e -> e.map(existingLoan -> new LoanAndRelatedRecords(loan, existingLoan))).thenCompose(larrResult -> getServicePointsForLoanAndRelated(larrResult, servicePointRepository)).thenApply(this::refuseWhenNotOpenOrClosed).thenApply(this::refuseWhenOpenAndNoUserId).thenApply(spLoanLocationValidator::checkServicePointLoanLocation).thenApply(this::refuseWhenClosedAndNoCheckInServicePointId).thenCombineAsync(itemRepository.fetchFor(loan), this::addItem).thenApply(itemNotFoundValidator::refuseWhenItemNotFound).thenCompose(changeDueDateValidator::refuseChangeDueDateForItemInDisallowedStatus).thenCombineAsync(userRepository.getUser(loan.getUserId()), this::addUser).thenComposeAsync(r -> r.after(proxyRelationshipValidator::refuseWhenInvalid)).thenCombineAsync(requestQueueRepository.getByItemId(loan.getItemId()), this::addRequestQueue).thenApply(r -> r.map(this::unsetDueDateChangedByRecallIfNoOpenRecallsInQueue)).thenComposeAsync(result -> result.after(requestQueueUpdate::onCheckIn)).thenComposeAsync(result -> result.after(updateItem::onLoanUpdate)).thenComposeAsync(result -> result.after(loanRepository::updateLoan)).thenComposeAsync(r -> r.after(eventPublisher::publishDueDateChangedEvent)).thenApply(r -> r.next(scheduledNoticeService::rescheduleDueDateNotices)).thenCompose(r -> r.after(loanNoticeSender::sendManualDueDateChangeNotice)).thenApply(r -> r.map(toFixedValue(NoContentResponse::noContent))).thenAccept(context::writeResultToHttpResponse);
}
use of org.folio.circulation.domain.validation.ProxyRelationshipValidator 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);
}
Aggregations