use of org.folio.circulation.domain.validation.ChangeDueDateValidator 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);
}
Aggregations