Search in sources :

Example 1 with TicketsInfo

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

the class AdminReservationManagerIntegrationTest method testReserveFromNewCategory.

@Test
public void testReserveFromNewCategory() throws Exception {
    List<TicketCategoryModification> categories = Collections.singletonList(new TicketCategoryModification(null, "default", 1, new DateTimeModification(LocalDate.now(), LocalTime.now()), new DateTimeModification(LocalDate.now(), LocalTime.now()), DESCRIPTION, BigDecimal.TEN, false, "", true, null, null, null, null, null));
    Pair<Event, String> eventWithUsername = initEvent(categories, organizationRepository, userManager, eventManager, eventRepository);
    Event event = eventWithUsername.getKey();
    String username = eventWithUsername.getValue();
    DateTimeModification expiration = DateTimeModification.fromZonedDateTime(ZonedDateTime.now().plusDays(1));
    CustomerData customerData = new CustomerData("Integration", "Test", "integration-test@test.ch", "Billing Address", "en");
    Category category = new Category(null, "name", new BigDecimal("100.00"));
    int attendees = AVAILABLE_SEATS;
    List<TicketsInfo> ticketsInfoList = Collections.singletonList(new TicketsInfo(category, generateAttendees(attendees), true, false));
    AdminReservationModification modification = new AdminReservationModification(expiration, customerData, ticketsInfoList, "en", false, null);
    Result<Pair<TicketReservation, List<Ticket>>> result = adminReservationManager.createReservation(modification, event.getShortName(), username);
    assertTrue(result.isSuccess());
    Pair<TicketReservation, List<Ticket>> data = result.getData();
    List<Ticket> tickets = data.getRight();
    assertTrue(tickets.size() == attendees);
    assertNotNull(data.getLeft());
    int categoryId = tickets.get(0).getCategoryId();
    Event modified = eventManager.getSingleEvent(event.getShortName(), username);
    assertEquals(attendees + 1, eventRepository.countExistingTickets(event.getId()).intValue());
    assertEquals(attendees, ticketRepository.findPendingTicketsInCategories(Collections.singletonList(categoryId)).size());
    TicketCategory categoryModified = ticketCategoryRepository.getByIdAndActive(categoryId, event.getId());
    assertEquals(categoryModified.getMaxTickets(), attendees);
    ticketCategoryRepository.findByEventId(event.getId()).forEach(tc -> assertTrue(specialPriceRepository.findAllByCategoryId(tc.getId()).stream().allMatch(sp -> sp.getStatus() == SpecialPrice.Status.PENDING)));
    adminReservationManager.confirmReservation(event.getShortName(), data.getLeft().getId(), username);
    ticketCategoryRepository.findByEventId(event.getId()).forEach(tc -> assertTrue(specialPriceRepository.findAllByCategoryId(tc.getId()).stream().allMatch(sp -> sp.getStatus() == SpecialPrice.Status.TAKEN)));
    assertFalse(ticketRepository.findAllReservationsConfirmedButNotAssigned(event.getId()).contains(data.getLeft().getId()));
}
Also used : Category(alfio.model.modification.AdminReservationModification.Category) CustomerData(alfio.model.modification.AdminReservationModification.CustomerData) TicketsInfo(alfio.model.modification.AdminReservationModification.TicketsInfo) BigDecimal(java.math.BigDecimal) Collectors.toList(java.util.stream.Collectors.toList) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 2 with TicketsInfo

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

the class AdminReservationManagerIntegrationTest method testReserveMixed.

@Test
public void testReserveMixed() throws Exception {
    List<TicketCategoryModification> categories = Collections.singletonList(new TicketCategoryModification(null, "default", 1, new DateTimeModification(LocalDate.now(), LocalTime.now()), new DateTimeModification(LocalDate.now(), LocalTime.now()), DESCRIPTION, BigDecimal.TEN, false, "", false, null, null, null, null, null));
    Pair<Event, String> eventWithUsername = initEvent(categories, organizationRepository, userManager, eventManager, eventRepository);
    Event event = eventWithUsername.getKey();
    String username = eventWithUsername.getValue();
    DateTimeModification expiration = DateTimeModification.fromZonedDateTime(ZonedDateTime.now().plusDays(1));
    CustomerData customerData = new CustomerData("Integration", "Test", "integration-test@test.ch", "Billing Address", "en");
    TicketCategory existingCategory = ticketCategoryRepository.findByEventId(event.getId()).get(0);
    Category resExistingCategory = new Category(existingCategory.getId(), "", existingCategory.getPrice());
    Category resNewCategory = new Category(null, "name", new BigDecimal("100.00"));
    int attendees = 1;
    List<TicketsInfo> ticketsInfoList = Arrays.asList(new TicketsInfo(resExistingCategory, generateAttendees(attendees), false, false), new TicketsInfo(resNewCategory, generateAttendees(attendees), false, false), new TicketsInfo(resExistingCategory, generateAttendees(attendees), false, false));
    AdminReservationModification modification = new AdminReservationModification(expiration, customerData, ticketsInfoList, "en", false, null);
    Result<Pair<TicketReservation, List<Ticket>>> result = adminReservationManager.createReservation(modification, event.getShortName(), username);
    assertTrue(result.isSuccess());
    Pair<TicketReservation, List<Ticket>> data = result.getData();
    List<Ticket> tickets = data.getRight();
    assertTrue(tickets.size() == 3);
    assertNotNull(data.getLeft());
    assertTrue(tickets.stream().allMatch(t -> t.getTicketsReservationId().equals(data.getKey().getId())));
    int resExistingCategoryId = tickets.get(0).getCategoryId();
    int resNewCategoryId = tickets.get(2).getCategoryId();
    Event modified = eventManager.getSingleEvent(event.getShortName(), username);
    assertEquals(AVAILABLE_SEATS, eventRepository.countExistingTickets(event.getId()).intValue());
    assertEquals(3, ticketRepository.findPendingTicketsInCategories(Arrays.asList(resExistingCategoryId, resNewCategoryId)).size());
    assertEquals(3, ticketRepository.findTicketsInReservation(data.getLeft().getId()).size());
    String reservationId = data.getLeft().getId();
    assertEquals(ticketRepository.findTicketsInReservation(reservationId).stream().findFirst().get().getId(), ticketRepository.findFirstTicketInReservation(reservationId).get().getId());
    ticketCategoryRepository.findByEventId(event.getId()).forEach(tc -> assertTrue(specialPriceRepository.findAllByCategoryId(tc.getId()).stream().allMatch(sp -> sp.getStatus() == SpecialPrice.Status.PENDING)));
    adminReservationManager.confirmReservation(event.getShortName(), data.getLeft().getId(), username);
    ticketCategoryRepository.findByEventId(event.getId()).forEach(tc -> assertTrue(specialPriceRepository.findAllByCategoryId(tc.getId()).stream().allMatch(sp -> sp.getStatus() == SpecialPrice.Status.TAKEN)));
    assertFalse(ticketRepository.findAllReservationsConfirmedButNotAssigned(event.getId()).contains(data.getLeft().getId()));
}
Also used : alfio.repository(alfio.repository) IntStream(java.util.stream.IntStream) java.util(java.util) BeforeClass(org.junit.BeforeClass) ZonedDateTime(java.time.ZonedDateTime) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) ActiveProfiles(org.springframework.test.context.ActiveProfiles) Attendee(alfio.model.modification.AdminReservationModification.Attendee) RepositoryConfiguration(alfio.config.RepositoryConfiguration) BigDecimal(java.math.BigDecimal) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) Pair(org.apache.commons.lang3.tuple.Pair) LocalTime(java.time.LocalTime) Triple(org.apache.commons.lang3.tuple.Triple) OrganizationRepository(alfio.repository.user.OrganizationRepository) TicketsInfo(alfio.model.modification.AdminReservationModification.TicketsInfo) Test(org.junit.Test) CustomerData(alfio.model.modification.AdminReservationModification.CustomerData) IntegrationTestUtil(alfio.test.util.IntegrationTestUtil) DateUtils(org.apache.commons.lang3.time.DateUtils) Result(alfio.model.result.Result) Collectors.toList(java.util.stream.Collectors.toList) Initializer(alfio.config.Initializer) alfio.model(alfio.model) UserManager(alfio.manager.user.UserManager) ContextConfiguration(org.springframework.test.context.ContextConfiguration) LocalDate(java.time.LocalDate) DataSourceConfiguration(alfio.config.DataSourceConfiguration) Category(alfio.model.modification.AdminReservationModification.Category) TestConfiguration(alfio.TestConfiguration) Assert(org.junit.Assert) alfio.model.modification(alfio.model.modification) Transactional(org.springframework.transaction.annotation.Transactional) Category(alfio.model.modification.AdminReservationModification.Category) CustomerData(alfio.model.modification.AdminReservationModification.CustomerData) TicketsInfo(alfio.model.modification.AdminReservationModification.TicketsInfo) BigDecimal(java.math.BigDecimal) Collectors.toList(java.util.stream.Collectors.toList) Pair(org.apache.commons.lang3.tuple.Pair) Test(org.junit.Test)

Example 3 with TicketsInfo

use of alfio.model.modification.AdminReservationModification.TicketsInfo 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 4 with TicketsInfo

use of alfio.model.modification.AdminReservationModification.TicketsInfo 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 5 with TicketsInfo

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

the class AdminReservationManagerIntegrationTest method performExistingCategoryTest.

private Triple<Event, String, TicketReservation> performExistingCategoryTest(List<TicketCategoryModification> categories, boolean bounded, List<Integer> attendeesNr, boolean addSeatsIfNotAvailable, boolean expectSuccess, int reservedTickets, int expectedEventSeats) {
    assertEquals("Test error: categories' size must be equal to attendees' size", categories.size(), attendeesNr.size());
    Pair<Event, String> eventWithUsername = initEvent(categories, organizationRepository, userManager, eventManager, eventRepository);
    Event event = eventWithUsername.getKey();
    String username = eventWithUsername.getValue();
    DateTimeModification expiration = DateTimeModification.fromZonedDateTime(ZonedDateTime.now().plusDays(1));
    CustomerData customerData = new CustomerData("Integration", "Test", "integration-test@test.ch", "Billing Address", "en");
    Iterator<Integer> attendeesIterator = attendeesNr.iterator();
    List<TicketCategory> existingCategories = ticketCategoryRepository.findByEventId(event.getId());
    List<Attendee> allAttendees = new ArrayList<>();
    List<TicketsInfo> ticketsInfoList = existingCategories.stream().map(existingCategory -> {
        Category category = new Category(existingCategory.getId(), existingCategory.getName(), existingCategory.getPrice());
        List<Attendee> attendees = generateAttendees(attendeesIterator.next());
        allAttendees.addAll(attendees);
        return new TicketsInfo(category, attendees, addSeatsIfNotAvailable, false);
    }).collect(toList());
    AdminReservationModification modification = new AdminReservationModification(expiration, customerData, ticketsInfoList, "en", false, null);
    if (reservedTickets > 0) {
        TicketReservationModification trm = new TicketReservationModification();
        trm.setAmount(reservedTickets);
        trm.setTicketCategoryId(existingCategories.get(0).getId());
        TicketReservationWithOptionalCodeModification r = new TicketReservationWithOptionalCodeModification(trm, Optional.empty());
        ticketReservationManager.createTicketReservation(event, Collections.singletonList(r), Collections.emptyList(), DateUtils.addDays(new Date(), 1), Optional.empty(), Optional.empty(), Locale.ENGLISH, false);
    }
    Result<Pair<TicketReservation, List<Ticket>>> result = adminReservationManager.createReservation(modification, event.getShortName(), username);
    if (expectSuccess) {
        validateSuccess(bounded, attendeesNr, event, username, existingCategories, result, allAttendees, expectedEventSeats, reservedTickets);
    } else {
        assertFalse(result.isSuccess());
        return null;
    }
    return Triple.of(eventWithUsername.getLeft(), eventWithUsername.getRight(), result.getData().getKey());
}
Also used : alfio.repository(alfio.repository) IntStream(java.util.stream.IntStream) java.util(java.util) BeforeClass(org.junit.BeforeClass) ZonedDateTime(java.time.ZonedDateTime) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) ActiveProfiles(org.springframework.test.context.ActiveProfiles) Attendee(alfio.model.modification.AdminReservationModification.Attendee) RepositoryConfiguration(alfio.config.RepositoryConfiguration) BigDecimal(java.math.BigDecimal) SpringJUnit4ClassRunner(org.springframework.test.context.junit4.SpringJUnit4ClassRunner) Pair(org.apache.commons.lang3.tuple.Pair) LocalTime(java.time.LocalTime) Triple(org.apache.commons.lang3.tuple.Triple) OrganizationRepository(alfio.repository.user.OrganizationRepository) TicketsInfo(alfio.model.modification.AdminReservationModification.TicketsInfo) Test(org.junit.Test) CustomerData(alfio.model.modification.AdminReservationModification.CustomerData) IntegrationTestUtil(alfio.test.util.IntegrationTestUtil) DateUtils(org.apache.commons.lang3.time.DateUtils) Result(alfio.model.result.Result) Collectors.toList(java.util.stream.Collectors.toList) Initializer(alfio.config.Initializer) alfio.model(alfio.model) UserManager(alfio.manager.user.UserManager) ContextConfiguration(org.springframework.test.context.ContextConfiguration) LocalDate(java.time.LocalDate) DataSourceConfiguration(alfio.config.DataSourceConfiguration) Category(alfio.model.modification.AdminReservationModification.Category) TestConfiguration(alfio.TestConfiguration) Assert(org.junit.Assert) alfio.model.modification(alfio.model.modification) Transactional(org.springframework.transaction.annotation.Transactional) Category(alfio.model.modification.AdminReservationModification.Category) CustomerData(alfio.model.modification.AdminReservationModification.CustomerData) TicketsInfo(alfio.model.modification.AdminReservationModification.TicketsInfo) Attendee(alfio.model.modification.AdminReservationModification.Attendee) LocalDate(java.time.LocalDate) Collectors.toList(java.util.stream.Collectors.toList) Pair(org.apache.commons.lang3.tuple.Pair)

Aggregations

TicketsInfo (alfio.model.modification.AdminReservationModification.TicketsInfo)5 Category (alfio.model.modification.AdminReservationModification.Category)4 BigDecimal (java.math.BigDecimal)4 Pair (org.apache.commons.lang3.tuple.Pair)4 alfio.model (alfio.model)3 Attendee (alfio.model.modification.AdminReservationModification.Attendee)3 CustomerData (alfio.model.modification.AdminReservationModification.CustomerData)3 Result (alfio.model.result.Result)3 alfio.repository (alfio.repository)3 ZonedDateTime (java.time.ZonedDateTime)3 java.util (java.util)3 Collectors.toList (java.util.stream.Collectors.toList)3 Triple (org.apache.commons.lang3.tuple.Triple)3 Test (org.junit.Test)3 Transactional (org.springframework.transaction.annotation.Transactional)3 TestConfiguration (alfio.TestConfiguration)2 DataSourceConfiguration (alfio.config.DataSourceConfiguration)2 Initializer (alfio.config.Initializer)2 RepositoryConfiguration (alfio.config.RepositoryConfiguration)2 UserManager (alfio.manager.user.UserManager)2