Search in sources :

Example 1 with AdminReservationModification

use of alfio.model.modification.AdminReservationModification in project alf.io by alfio-event.

the class AdminReservationManager method reserveForTicketsInfo.

private Result<List<Ticket>> reserveForTicketsInfo(Event event, AdminReservationModification arm, String reservationId, String specialPriceSessionId, Pair<TicketCategory, TicketsInfo> pair) {
    TicketCategory category = pair.getLeft();
    TicketsInfo ticketsInfo = pair.getRight();
    int categoryId = category.getId();
    List<Attendee> attendees = ticketsInfo.getAttendees();
    List<Integer> reservedForUpdate = ticketReservationManager.reserveTickets(event.getId(), categoryId, attendees.size(), singletonList(Ticket.TicketStatus.FREE));
    if (reservedForUpdate.size() != attendees.size()) {
        return Result.error(ErrorCode.CategoryError.NOT_ENOUGH_SEATS);
    }
    ticketRepository.reserveTickets(reservationId, reservedForUpdate, categoryId, arm.getLanguage(), category.getSrcPriceCts());
    Ticket ticket = ticketRepository.findById(reservedForUpdate.get(0), categoryId);
    TicketPriceContainer priceContainer = TicketPriceContainer.from(ticket, null, event, null);
    ticketRepository.updateTicketPrice(reservedForUpdate, categoryId, event.getId(), category.getSrcPriceCts(), MonetaryUtil.unitToCents(priceContainer.getFinalPrice()), MonetaryUtil.unitToCents(priceContainer.getVAT()), MonetaryUtil.unitToCents(priceContainer.getAppliedDiscount()));
    List<SpecialPrice> codes = category.isAccessRestricted() ? bindSpecialPriceTokens(specialPriceSessionId, categoryId, attendees) : Collections.emptyList();
    assignTickets(event, attendees, categoryId, reservedForUpdate, codes, reservationId, arm.getLanguage(), category.getSrcPriceCts());
    List<Ticket> tickets = reservedForUpdate.stream().map(id -> ticketRepository.findById(id, categoryId)).collect(toList());
    return Result.success(tickets);
}
Also used : alfio.repository(alfio.repository) PaymentProxy(alfio.model.transaction.PaymentProxy) ZonedDateTime(java.time.ZonedDateTime) BiFunction(java.util.function.BiFunction) RequiredArgsConstructor(lombok.RequiredArgsConstructor) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) StringUtils(org.apache.commons.lang3.StringUtils) Collections.singletonList(java.util.Collections.singletonList) ResultStatus(alfio.model.result.Result.ResultStatus) BigDecimal(java.math.BigDecimal) Json(alfio.util.Json) ErrorCode(alfio.model.result.ErrorCode) Pair(org.apache.commons.lang3.tuple.Pair) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Triple(org.apache.commons.lang3.tuple.Triple) TemplateResource(alfio.util.TemplateResource) TICKET(alfio.model.Audit.EntityType.TICKET) StringUtils.trimToNull(org.apache.commons.lang3.StringUtils.trimToNull) Organization(alfio.model.user.Organization) TemplateManager(alfio.util.TemplateManager) Collectors(java.util.stream.Collectors) CANCEL_TICKET(alfio.model.Audit.EventType.CANCEL_TICKET) Stream(java.util.stream.Stream) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) alfio.model(alfio.model) UserRepository(alfio.repository.user.UserRepository) OptionalWrapper.optionally(alfio.util.OptionalWrapper.optionally) java.util(java.util) DuplicateReferenceException(alfio.manager.support.DuplicateReferenceException) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) Function(java.util.function.Function) Attendee(alfio.model.modification.AdminReservationModification.Attendee) DateTimeModification(alfio.model.modification.DateTimeModification) TicketPriceContainer(alfio.model.decorator.TicketPriceContainer) TicketReservationStatus(alfio.model.TicketReservation.TicketReservationStatus) MessageSource(org.springframework.context.MessageSource) DateTimeModification.fromZonedDateTime(alfio.model.modification.DateTimeModification.fromZonedDateTime) TicketCategoryModification(alfio.model.modification.TicketCategoryModification) TicketsInfo(alfio.model.modification.AdminReservationModification.TicketsInfo) Result(alfio.model.result.Result) Component(org.springframework.stereotype.Component) MonetaryUtil(alfio.util.MonetaryUtil) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Log4j2(lombok.extern.log4j.Log4j2) Category(alfio.model.modification.AdminReservationModification.Category) EventUtil.generateEmptyTickets(alfio.util.EventUtil.generateEmptyTickets) AdminReservationModification(alfio.model.modification.AdminReservationModification) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) TicketPriceContainer(alfio.model.decorator.TicketPriceContainer) TicketsInfo(alfio.model.modification.AdminReservationModification.TicketsInfo) Attendee(alfio.model.modification.AdminReservationModification.Attendee)

Example 2 with AdminReservationModification

use of alfio.model.modification.AdminReservationModification in project alf.io by alfio-event.

the class AdminReservationManager method createReservation.

private Result<Pair<TicketReservation, List<Ticket>>> createReservation(Result<List<TicketsInfo>> input, Event event, AdminReservationModification arm) {
    final TicketsInfo empty = new TicketsInfo(null, null, false, false);
    return input.flatMap(t -> {
        String reservationId = UUID.randomUUID().toString();
        String specialPriceSessionId = UUID.randomUUID().toString();
        Date validity = Date.from(arm.getExpiration().toZonedDateTime(event.getZoneId()).toInstant());
        ticketReservationRepository.createNewReservation(reservationId, validity, null, arm.getLanguage(), event.getId(), event.getVat(), event.isVatIncluded());
        AdminReservationModification.CustomerData customerData = arm.getCustomerData();
        ticketReservationRepository.updateTicketReservation(reservationId, TicketReservationStatus.PENDING.name(), customerData.getEmailAddress(), customerData.getFullName(), customerData.getFirstName(), customerData.getLastName(), arm.getLanguage(), null, null, null);
        Result<List<Ticket>> result = flattenTicketsInfo(event, empty, t).map(pair -> reserveForTicketsInfo(event, arm, reservationId, specialPriceSessionId, pair)).reduce(this::reduceReservationResults).orElseGet(() -> Result.error(ErrorCode.custom("", "unknown error")));
        updateInvoiceReceiptModel(event, arm.getLanguage(), reservationId);
        return result.map(list -> Pair.of(ticketReservationRepository.findReservationById(reservationId), list));
    });
}
Also used : AdminReservationModification(alfio.model.modification.AdminReservationModification) TicketsInfo(alfio.model.modification.AdminReservationModification.TicketsInfo) Collections.singletonList(java.util.Collections.singletonList)

Example 3 with AdminReservationModification

use of alfio.model.modification.AdminReservationModification in project alf.io by alfio-event.

the class AdminReservationManager method createReservation.

public Result<Pair<TicketReservation, List<Ticket>>> createReservation(AdminReservationModification input, String eventName, String username) {
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    TransactionTemplate template = new TransactionTemplate(transactionManager, definition);
    return template.execute(status -> {
        try {
            Result<Pair<TicketReservation, List<Ticket>>> result = eventRepository.findOptionalByShortNameForUpdate(eventName).map(e -> validateTickets(input, e)).map(r -> r.flatMap(p -> transactionalCreateReservation(p.getRight(), p.getLeft(), username))).orElse(Result.error(ErrorCode.EventError.NOT_FOUND));
            if (!result.isSuccess()) {
                log.debug("Error during update of reservation eventName: {}, username: {}, reservation: {}", eventName, username, AdminReservationModification.summary(input));
                status.setRollbackOnly();
            }
            return result;
        } catch (Exception e) {
            log.error("Error during update of reservation eventName: {}, username: {}, reservation: {}", eventName, username, AdminReservationModification.summary(input));
            status.setRollbackOnly();
            return Result.error(singletonList(ErrorCode.custom(e instanceof DuplicateReferenceException ? "duplicate-reference" : "", e.getMessage())));
        }
    });
}
Also used : alfio.repository(alfio.repository) PaymentProxy(alfio.model.transaction.PaymentProxy) ZonedDateTime(java.time.ZonedDateTime) BiFunction(java.util.function.BiFunction) RequiredArgsConstructor(lombok.RequiredArgsConstructor) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) StringUtils(org.apache.commons.lang3.StringUtils) Collections.singletonList(java.util.Collections.singletonList) ResultStatus(alfio.model.result.Result.ResultStatus) BigDecimal(java.math.BigDecimal) Json(alfio.util.Json) ErrorCode(alfio.model.result.ErrorCode) Pair(org.apache.commons.lang3.tuple.Pair) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Triple(org.apache.commons.lang3.tuple.Triple) TemplateResource(alfio.util.TemplateResource) TICKET(alfio.model.Audit.EntityType.TICKET) StringUtils.trimToNull(org.apache.commons.lang3.StringUtils.trimToNull) Organization(alfio.model.user.Organization) TemplateManager(alfio.util.TemplateManager) Collectors(java.util.stream.Collectors) CANCEL_TICKET(alfio.model.Audit.EventType.CANCEL_TICKET) Stream(java.util.stream.Stream) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) alfio.model(alfio.model) UserRepository(alfio.repository.user.UserRepository) OptionalWrapper.optionally(alfio.util.OptionalWrapper.optionally) java.util(java.util) DuplicateReferenceException(alfio.manager.support.DuplicateReferenceException) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) Function(java.util.function.Function) Attendee(alfio.model.modification.AdminReservationModification.Attendee) DateTimeModification(alfio.model.modification.DateTimeModification) TicketPriceContainer(alfio.model.decorator.TicketPriceContainer) TicketReservationStatus(alfio.model.TicketReservation.TicketReservationStatus) MessageSource(org.springframework.context.MessageSource) DateTimeModification.fromZonedDateTime(alfio.model.modification.DateTimeModification.fromZonedDateTime) TicketCategoryModification(alfio.model.modification.TicketCategoryModification) TicketsInfo(alfio.model.modification.AdminReservationModification.TicketsInfo) Result(alfio.model.result.Result) Component(org.springframework.stereotype.Component) MonetaryUtil(alfio.util.MonetaryUtil) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Log4j2(lombok.extern.log4j.Log4j2) Category(alfio.model.modification.AdminReservationModification.Category) EventUtil.generateEmptyTickets(alfio.util.EventUtil.generateEmptyTickets) AdminReservationModification(alfio.model.modification.AdminReservationModification) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) DuplicateReferenceException(alfio.manager.support.DuplicateReferenceException) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) DuplicateReferenceException(alfio.manager.support.DuplicateReferenceException) Pair(org.apache.commons.lang3.tuple.Pair)

Example 4 with AdminReservationModification

use of alfio.model.modification.AdminReservationModification in project alf.io by alfio-event.

the class AdminReservationRequestManager method insertRequest.

private Result<String> insertRequest(AdminReservationModification body, Event event, boolean singleReservation, String username) {
    try {
        String requestId = UUID.randomUUID().toString();
        long userId = userRepository.findIdByUserName(username).orElseThrow(IllegalArgumentException::new);
        MapSqlParameterSource[] requests = spread(body, singleReservation).map(res -> new MapSqlParameterSource("userId", userId).addValue("requestId", requestId).addValue("requestType", AdminReservationRequest.RequestType.IMPORT.name()).addValue("status", AdminReservationRequest.Status.PENDING.name()).addValue("eventId", event.getId()).addValue("body", Json.toJson(res))).toArray(MapSqlParameterSource[]::new);
        jdbc.batchUpdate(adminReservationRequestRepository.insertRequest(), requests);
        return Result.success(requestId);
    } catch (Exception e) {
        log.error("can't insert reservation request", e);
        return Result.error(ErrorCode.custom("internal_server_error", e.getMessage()));
    }
}
Also used : TransactionDefinition(org.springframework.transaction.TransactionDefinition) RequiredArgsConstructor(lombok.RequiredArgsConstructor) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) StringUtils(org.apache.commons.lang3.StringUtils) Collections.singletonList(java.util.Collections.singletonList) Json(alfio.util.Json) ErrorCode(alfio.model.result.ErrorCode) Pair(org.apache.commons.lang3.tuple.Pair) CollectionUtils(org.apache.commons.collections.CollectionUtils) Map(java.util.Map) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Triple(org.apache.commons.lang3.tuple.Triple) Optional.ofNullable(java.util.Optional.ofNullable) User(alfio.model.user.User) UUID(java.util.UUID) CustomerData(alfio.model.modification.AdminReservationModification.CustomerData) EventRepository(alfio.repository.EventRepository) Collectors(java.util.stream.Collectors) Result(alfio.model.result.Result) Component(org.springframework.stereotype.Component) List(java.util.List) Stream(java.util.stream.Stream) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) alfio.model(alfio.model) UserRepository(alfio.repository.user.UserRepository) AdminReservationRequestRepository(alfio.repository.AdminReservationRequestRepository) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Log4j2(lombok.extern.log4j.Log4j2) AdminReservationModification(alfio.model.modification.AdminReservationModification) OptionalWrapper.optionally(alfio.util.OptionalWrapper.optionally) Transactional(org.springframework.transaction.annotation.Transactional) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource)

Example 5 with AdminReservationModification

use of alfio.model.modification.AdminReservationModification in project alf.io by alfio-event.

the class AdminReservationManager method updateReservation.

public Result<Boolean> updateReservation(String eventName, String reservationId, AdminReservationModification adminReservationModification, String username) {
    DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
    TransactionTemplate template = new TransactionTemplate(transactionManager, definition);
    return template.execute(status -> {
        try {
            Result<Boolean> result = eventRepository.findOptionalByShortName(eventName).flatMap(e -> optionally(() -> {
                eventManager.checkOwnership(e, username, e.getOrganizationId());
                return e;
            })).map(event -> ticketReservationRepository.findOptionalReservationById(reservationId).map(r -> performUpdate(reservationId, event, r, adminReservationModification)).orElseGet(() -> Result.error(ErrorCode.ReservationError.UPDATE_FAILED))).orElseGet(() -> Result.error(ErrorCode.ReservationError.NOT_FOUND));
            if (!result.isSuccess()) {
                log.debug("Application error detected eventName: {} reservationId: {}, username: {}, reservation: {}", eventName, reservationId, username, AdminReservationModification.summary(adminReservationModification));
                status.setRollbackOnly();
            }
            return result;
        } catch (Exception e) {
            log.error("Error during update of reservation eventName: {} reservationId: {}, username: {}, reservation: {}", eventName, reservationId, username, AdminReservationModification.summary(adminReservationModification));
            status.setRollbackOnly();
            return Result.error(singletonList(ErrorCode.custom("", e.getMessage())));
        }
    });
}
Also used : alfio.repository(alfio.repository) PaymentProxy(alfio.model.transaction.PaymentProxy) ZonedDateTime(java.time.ZonedDateTime) BiFunction(java.util.function.BiFunction) RequiredArgsConstructor(lombok.RequiredArgsConstructor) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) StringUtils(org.apache.commons.lang3.StringUtils) Collections.singletonList(java.util.Collections.singletonList) ResultStatus(alfio.model.result.Result.ResultStatus) BigDecimal(java.math.BigDecimal) Json(alfio.util.Json) ErrorCode(alfio.model.result.ErrorCode) Pair(org.apache.commons.lang3.tuple.Pair) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Triple(org.apache.commons.lang3.tuple.Triple) TemplateResource(alfio.util.TemplateResource) TICKET(alfio.model.Audit.EntityType.TICKET) StringUtils.trimToNull(org.apache.commons.lang3.StringUtils.trimToNull) Organization(alfio.model.user.Organization) TemplateManager(alfio.util.TemplateManager) Collectors(java.util.stream.Collectors) CANCEL_TICKET(alfio.model.Audit.EventType.CANCEL_TICKET) Stream(java.util.stream.Stream) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) alfio.model(alfio.model) UserRepository(alfio.repository.user.UserRepository) OptionalWrapper.optionally(alfio.util.OptionalWrapper.optionally) java.util(java.util) DuplicateReferenceException(alfio.manager.support.DuplicateReferenceException) MapSqlParameterSource(org.springframework.jdbc.core.namedparam.MapSqlParameterSource) Function(java.util.function.Function) Attendee(alfio.model.modification.AdminReservationModification.Attendee) DateTimeModification(alfio.model.modification.DateTimeModification) TicketPriceContainer(alfio.model.decorator.TicketPriceContainer) TicketReservationStatus(alfio.model.TicketReservation.TicketReservationStatus) MessageSource(org.springframework.context.MessageSource) DateTimeModification.fromZonedDateTime(alfio.model.modification.DateTimeModification.fromZonedDateTime) TicketCategoryModification(alfio.model.modification.TicketCategoryModification) TicketsInfo(alfio.model.modification.AdminReservationModification.TicketsInfo) Result(alfio.model.result.Result) Component(org.springframework.stereotype.Component) MonetaryUtil(alfio.util.MonetaryUtil) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) Log4j2(lombok.extern.log4j.Log4j2) Category(alfio.model.modification.AdminReservationModification.Category) EventUtil.generateEmptyTickets(alfio.util.EventUtil.generateEmptyTickets) AdminReservationModification(alfio.model.modification.AdminReservationModification) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) DuplicateReferenceException(alfio.manager.support.DuplicateReferenceException)

Aggregations

AdminReservationModification (alfio.model.modification.AdminReservationModification)6 Collections.singletonList (java.util.Collections.singletonList)6 alfio.model (alfio.model)5 ErrorCode (alfio.model.result.ErrorCode)5 Result (alfio.model.result.Result)5 UserRepository (alfio.repository.user.UserRepository)5 Json (alfio.util.Json)5 OptionalWrapper.optionally (alfio.util.OptionalWrapper.optionally)5 Collectors (java.util.stream.Collectors)5 Stream (java.util.stream.Stream)5 RequiredArgsConstructor (lombok.RequiredArgsConstructor)5 Log4j2 (lombok.extern.log4j.Log4j2)5 StringUtils (org.apache.commons.lang3.StringUtils)5 Pair (org.apache.commons.lang3.tuple.Pair)5 Triple (org.apache.commons.lang3.tuple.Triple)5 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)5 NamedParameterJdbcTemplate (org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate)5 Component (org.springframework.stereotype.Component)5 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)5 Transactional (org.springframework.transaction.annotation.Transactional)5