Search in sources :

Example 1 with ReservationCreate

use of alfio.controller.form.ReservationCreate in project alf.io by alfio-event.

the class ReservationUtil method validateCreateRequest.

public static Optional<Pair<List<TicketReservationWithOptionalCodeModification>, List<ASReservationWithOptionalCodeModification>>> validateCreateRequest(ReservationCreate request, Errors bindingResult, TicketReservationManager tickReservationManager, EventManager eventManager, String validatedPromoCodeDiscount, Event event) {
    int selectionCount = ticketSelectionCount(request.getTickets());
    if (selectionCount <= 0) {
        bindingResult.reject(ErrorsCode.STEP_1_SELECT_AT_LEAST_ONE);
        return Optional.empty();
    }
    List<Pair<TicketReservationModification, Integer>> maxTicketsByTicketReservation = selected(request.getTickets()).stream().map(r -> Pair.of(r, tickReservationManager.maxAmountOfTicketsForCategory(event, r.getTicketCategoryId(), validatedPromoCodeDiscount))).collect(toList());
    Optional<Pair<TicketReservationModification, Integer>> error = maxTicketsByTicketReservation.stream().filter(p -> p.getKey().getQuantity() > p.getValue()).findAny();
    if (error.isPresent()) {
        bindingResult.reject(ErrorsCode.STEP_1_OVER_MAXIMUM, new Object[] { error.get().getValue() }, null);
        return Optional.empty();
    }
    final List<TicketReservationModification> categories = selected(request.getTickets());
    final List<AdditionalServiceReservationModification> additionalServices = selectedAdditionalServices(request.getAdditionalServices());
    final boolean validCategorySelection = categories.stream().allMatch(c -> {
        TicketCategory tc = eventManager.getTicketCategoryById(c.getTicketCategoryId(), event.getId());
        return eventManager.eventExistsById(tc.getEventId());
    });
    final boolean validAdditionalServiceSelected = additionalServices.stream().allMatch(asm -> {
        AdditionalService as = eventManager.getAdditionalServiceById(asm.getAdditionalServiceId(), event.getId());
        ZonedDateTime now = event.now(ClockProvider.clock());
        return as.getInception(event.getZoneId()).isBefore(now) && as.getExpiration(event.getZoneId()).isAfter(now) && asm.getQuantity() >= 0 && ((as.isFixPrice() && asm.isQuantityValid(as, selectionCount)) || (!as.isFixPrice() && asm.getAmount() != null && asm.getAmount().compareTo(BigDecimal.ZERO) >= 0)) && eventManager.eventExistsById(as.getEventId());
    });
    if (!validCategorySelection || !validAdditionalServiceSelected) {
        bindingResult.reject(ErrorsCode.STEP_1_TICKET_CATEGORY_MUST_BE_SALEABLE);
        return Optional.empty();
    }
    List<TicketReservationWithOptionalCodeModification> res = new ArrayList<>();
    // 
    Optional<SpecialPrice> specialCode = Optional.ofNullable(StringUtils.trimToNull(request.getPromoCode())).flatMap(tickReservationManager::getSpecialPriceByCode);
    // 
    final ZonedDateTime now = event.now(ClockProvider.clock());
    maxTicketsByTicketReservation.forEach(pair -> validateCategory(bindingResult, tickReservationManager, eventManager, event, pair.getRight(), res, specialCode, now, pair.getLeft()));
    return bindingResult.hasErrors() ? Optional.empty() : Optional.of(Pair.of(res, additionalServices.stream().map(as -> new ASReservationWithOptionalCodeModification(as, specialCode)).collect(Collectors.toList())));
}
Also used : Errors(org.springframework.validation.Errors) TicketReservationModification(alfio.model.modification.TicketReservationModification) TicketReservationWithOptionalCodeModification(alfio.model.modification.TicketReservationWithOptionalCodeModification) PromoCodeRequestManager(alfio.manager.PromoCodeRequestManager) Collections.emptyList(java.util.Collections.emptyList) Optional.ofNullable(java.util.Optional.ofNullable) ZonedDateTime(java.time.ZonedDateTime) BindingResult(org.springframework.validation.BindingResult) SaleableTicketCategory(alfio.controller.decorator.SaleableTicketCategory) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) TicketReservationManager(alfio.manager.TicketReservationManager) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) EventManager(alfio.manager.EventManager) Pair(org.apache.commons.lang3.tuple.Pair) alfio.model(alfio.model) ValidatedResponse(alfio.manager.support.response.ValidatedResponse) ASReservationWithOptionalCodeModification(alfio.model.modification.ASReservationWithOptionalCodeModification) Optional(java.util.Optional) AdditionalServiceReservationModification(alfio.model.modification.AdditionalServiceReservationModification) ReservationCreate(alfio.controller.form.ReservationCreate) ArrayList(java.util.ArrayList) SaleableTicketCategory(alfio.controller.decorator.SaleableTicketCategory) ASReservationWithOptionalCodeModification(alfio.model.modification.ASReservationWithOptionalCodeModification) AdditionalServiceReservationModification(alfio.model.modification.AdditionalServiceReservationModification) TicketReservationWithOptionalCodeModification(alfio.model.modification.TicketReservationWithOptionalCodeModification) ZonedDateTime(java.time.ZonedDateTime) TicketReservationModification(alfio.model.modification.TicketReservationModification) Pair(org.apache.commons.lang3.tuple.Pair)

Aggregations

SaleableTicketCategory (alfio.controller.decorator.SaleableTicketCategory)1 ReservationCreate (alfio.controller.form.ReservationCreate)1 EventManager (alfio.manager.EventManager)1 PromoCodeRequestManager (alfio.manager.PromoCodeRequestManager)1 TicketReservationManager (alfio.manager.TicketReservationManager)1 ValidatedResponse (alfio.manager.support.response.ValidatedResponse)1 alfio.model (alfio.model)1 ASReservationWithOptionalCodeModification (alfio.model.modification.ASReservationWithOptionalCodeModification)1 AdditionalServiceReservationModification (alfio.model.modification.AdditionalServiceReservationModification)1 TicketReservationModification (alfio.model.modification.TicketReservationModification)1 TicketReservationWithOptionalCodeModification (alfio.model.modification.TicketReservationWithOptionalCodeModification)1 BigDecimal (java.math.BigDecimal)1 ZonedDateTime (java.time.ZonedDateTime)1 ArrayList (java.util.ArrayList)1 Collections.emptyList (java.util.Collections.emptyList)1 List (java.util.List)1 Optional (java.util.Optional)1 Optional.ofNullable (java.util.Optional.ofNullable)1 Collectors (java.util.stream.Collectors)1 Collectors.toList (java.util.stream.Collectors.toList)1