Search in sources :

Example 1 with Group

use of alfio.model.group.Group in project alf.io by alfio-event.

the class EventApiV1Controller method insertEvent.

private Optional<Event> insertEvent(EventCreationRequest request, Principal user, String imageRef) {
    Organization organization = userManager.findUserOrganizations(user.getName()).get(0);
    EventModification em = request.toEventModification(organization, eventNameManager::generateShortName, imageRef);
    eventManager.createEvent(em, user.getName());
    Optional<Event> event = eventManager.getOptionalByName(em.getShortName(), user.getName());
    event.ifPresent(e -> {
        Optional.ofNullable(request.getTickets().getPromoCodes()).ifPresent(promoCodes -> promoCodes.forEach(// TODO add ref to categories
        pc -> eventManager.addPromoCode(pc.getName(), e.getId(), organization.getId(), ZonedDateTime.of(pc.getValidFrom(), e.getZoneId()), ZonedDateTime.of(pc.getValidTo(), e.getZoneId()), pc.getDiscount(), pc.getDiscountType(), Collections.emptyList(), null, null, null, PromoCodeDiscount.CodeType.DISCOUNT, null)));
        // link categories to groups, if any
        request.getTickets().getCategories().stream().filter(cr -> cr.getGroupLink() != null && cr.getGroupLink().getGroupId() != null).map(cr -> Pair.of(cr, groupManager.findById(cr.getGroupLink().getGroupId(), organization.getId()))).forEach(link -> {
            if (link.getRight().isPresent()) {
                Group group = link.getRight().get();
                EventCreationRequest.CategoryRequest categoryRequest = link.getLeft();
                findCategoryByName(e, categoryRequest.getName()).ifPresent(category -> {
                    EventCreationRequest.GroupLinkRequest groupLinkRequest = categoryRequest.getGroupLink();
                    LinkedGroupModification modification = new LinkedGroupModification(null, group.getId(), e.getId(), category.getId(), groupLinkRequest.getType(), groupLinkRequest.getMatchType(), groupLinkRequest.getMaxAllocation());
                    groupManager.createLink(group.getId(), e.getId(), modification);
                });
            }
        });
        if (!CollectionUtils.isEmpty(request.getExtensionSettings())) {
            request.getExtensionSettings().stream().collect(Collectors.groupingBy(EventCreationRequest.ExtensionSetting::getExtensionId)).forEach((id, settings) -> {
                List<ExtensionSupport.ExtensionMetadataIdAndName> metadata = extensionService.getSingle(organization, e, id).map(es -> extensionRepository.findAllParametersForExtension(es.getId())).orElseGet(Collections::emptyList);
                List<ExtensionMetadataValue> values = settings.stream().map(es -> Pair.of(es, metadata.stream().filter(mm -> mm.getName().equals(es.getKey())).findFirst())).filter(pair -> {
                    if (pair.getRight().isEmpty()) {
                        log.warn("ignoring non-existent extension setting key {}", pair.getLeft().getKey());
                    }
                    return pair.getRight().isPresent();
                }).map(pair -> new ExtensionMetadataValue(pair.getRight().get().getId(), pair.getLeft().getValue())).collect(Collectors.toList());
                extensionService.bulkUpdateEventSettings(organization, e, values);
            });
        }
    });
    return event;
}
Also used : ValidationResult(alfio.model.result.ValidationResult) EventApiController.validateEvent(alfio.controller.api.admin.EventApiController.validateEvent) alfio.manager(alfio.manager) Group(alfio.model.group.Group) Errors(org.springframework.validation.Errors) ConfigurationLevel(alfio.manager.system.ConfigurationLevel) ZonedDateTime(java.time.ZonedDateTime) ConfigurationManager(alfio.manager.system.ConfigurationManager) ExtensionRepository(alfio.repository.ExtensionRepository) EventModification(alfio.model.modification.EventModification) AtomicReference(java.util.concurrent.atomic.AtomicReference) Json(alfio.util.Json) ErrorCode(alfio.model.result.ErrorCode) Pair(org.apache.commons.lang3.tuple.Pair) CollectionUtils(org.apache.commons.collections.CollectionUtils) ExtensionMetadataValue(alfio.model.ExtensionSupport.ExtensionMetadataValue) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) Organization(alfio.model.user.Organization) EventCreationRequest(alfio.model.api.v1.admin.EventCreationRequest) Collectors(java.util.stream.Collectors) LinkedGroupModification(alfio.model.modification.LinkedGroupModification) Result(alfio.model.result.Result) List(java.util.List) ExtensionService(alfio.extension.ExtensionService) Principal(java.security.Principal) StringUtils.isNotBlank(org.apache.commons.lang3.StringUtils.isNotBlank) StringUtils.isBlank(org.apache.commons.lang3.StringUtils.isBlank) alfio.model(alfio.model) UserManager(alfio.manager.user.UserManager) Log4j2(lombok.extern.log4j.Log4j2) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) AllArgsConstructor(lombok.AllArgsConstructor) ConfigurationKeys(alfio.model.system.ConfigurationKeys) Collections(java.util.Collections) Transactional(org.springframework.transaction.annotation.Transactional) Group(alfio.model.group.Group) Organization(alfio.model.user.Organization) EventCreationRequest(alfio.model.api.v1.admin.EventCreationRequest) LinkedGroupModification(alfio.model.modification.LinkedGroupModification) EventModification(alfio.model.modification.EventModification) ExtensionMetadataValue(alfio.model.ExtensionSupport.ExtensionMetadataValue) EventApiController.validateEvent(alfio.controller.api.admin.EventApiController.validateEvent) Collections(java.util.Collections)

Example 2 with Group

use of alfio.model.group.Group in project alf.io by alfio-event.

the class GroupManager method createNew.

public Result<Integer> createNew(GroupModification input) {
    return requiresNewTransactionTemplate.execute(status -> {
        Group wl = createNew(input.getName(), input.getDescription(), input.getOrganizationId());
        Result<Integer> insertMembers = insertMembers(wl.getId(), input.getItems());
        if (!insertMembers.isSuccess()) {
            status.setRollbackOnly();
        }
        return insertMembers;
    });
}
Also used : Group(alfio.model.group.Group) LinkedGroup(alfio.model.group.LinkedGroup)

Example 3 with Group

use of alfio.model.group.Group in project alf.io by alfio-event.

the class GroupManagerIntegrationTest method testDuplicates.

@Test
void testDuplicates() {
    List<TicketCategoryModification> categories = Collections.singletonList(new TicketCategoryModification(null, "default", TicketCategory.TicketAccessType.INHERIT, 10, new DateTimeModification(LocalDate.now(ClockProvider.clock()).plusDays(1), LocalTime.now(ClockProvider.clock())), new DateTimeModification(LocalDate.now(ClockProvider.clock()).plusDays(2), LocalTime.now(ClockProvider.clock())), DESCRIPTION, BigDecimal.TEN, false, "", false, null, null, null, null, null, 0, null, null, AlfioMetadata.empty()));
    Pair<Event, String> pair = initEvent(categories, organizationRepository, userManager, eventManager, eventRepository);
    Event event = pair.getKey();
    Group group = groupManager.createNew("test", "This is a test", event.getOrganizationId());
    assertNotNull(group);
    LinkedGroupModification modification = new LinkedGroupModification(null, group.getId(), event.getId(), null, LinkedGroup.Type.ONCE_PER_VALUE, LinkedGroup.MatchType.FULL, null);
    LinkedGroup configuration = groupManager.createLink(group.getId(), event.getId(), modification);
    assertNotNull(configuration);
    Result<Integer> items = groupManager.insertMembers(group.getId(), Arrays.asList(new GroupMemberModification(null, "test@test.ch", "description"), new GroupMemberModification(null, "test@test.ch", "description")));
    Assertions.assertFalse(items.isSuccess());
    assertEquals("value.duplicate", items.getFirstErrorOrNull().getCode());
    assertEquals("test@test.ch", items.getFirstErrorOrNull().getDescription());
}
Also used : Group(alfio.model.group.Group) LinkedGroup(alfio.model.group.LinkedGroup) LinkedGroup(alfio.model.group.LinkedGroup) Event(alfio.model.Event) BaseIntegrationTest(alfio.util.BaseIntegrationTest) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with Group

use of alfio.model.group.Group in project alf.io by alfio-event.

the class GroupManagerIntegrationTest method testLinkToEvent.

@Test
void testLinkToEvent() {
    List<TicketCategoryModification> categories = Collections.singletonList(new TicketCategoryModification(null, "default", TicketCategory.TicketAccessType.INHERIT, 10, new DateTimeModification(LocalDate.now(ClockProvider.clock()).plusDays(1), LocalTime.now(ClockProvider.clock())), new DateTimeModification(LocalDate.now(ClockProvider.clock()).plusDays(2), LocalTime.now(ClockProvider.clock())), DESCRIPTION, BigDecimal.TEN, false, "", false, null, null, null, null, null, 0, null, null, AlfioMetadata.empty()));
    Pair<Event, String> pair = initEvent(categories, organizationRepository, userManager, eventManager, eventRepository);
    Event event = pair.getKey();
    Group group = groupManager.createNew("test", "This is a test", event.getOrganizationId());
    assertNotNull(group);
    LinkedGroupModification modification = new LinkedGroupModification(null, group.getId(), event.getId(), null, LinkedGroup.Type.ONCE_PER_VALUE, LinkedGroup.MatchType.FULL, null);
    LinkedGroup configuration = groupManager.createLink(group.getId(), event.getId(), modification);
    assertNotNull(configuration);
    List<TicketCategory> ticketCategories = eventManager.loadTicketCategories(event);
    int categoryId = ticketCategories.get(0).getId();
    assertTrue(groupManager.isGroupLinked(event.getId(), categoryId));
    List<LinkedGroup> activeConfigurations = groupRepository.findActiveConfigurationsFor(event.getId(), categoryId);
    assertFalse(activeConfigurations.isEmpty(), "ActiveConfigurations should be empty");
    assertEquals(1, activeConfigurations.size());
    assertEquals(configuration.getId(), activeConfigurations.get(0).getId());
    assertFalse(groupManager.isAllowed("test@test.ch", event.getId(), categoryId), "Group is empty, therefore no value is allowed");
    Result<Integer> items = groupManager.insertMembers(group.getId(), Collections.singletonList(new GroupMemberModification(null, "test@test.ch", "description")));
    assertTrue(items.isSuccess());
    assertEquals(Integer.valueOf(1), items.getData());
    assertTrue(groupManager.isAllowed("test@test.ch", event.getId(), categoryId));
    TicketReservationModification ticketReservation = new TicketReservationModification();
    ticketReservation.setQuantity(1);
    ticketReservation.setTicketCategoryId(categoryId);
    String reservationId = ticketReservationManager.createTicketReservation(event, Collections.singletonList(new TicketReservationWithOptionalCodeModification(ticketReservation, Optional.empty())), Collections.emptyList(), DateUtils.addDays(new Date(), 1), Optional.empty(), Locale.ENGLISH, false, null);
    Ticket ticket = ticketRepository.findFirstTicketInReservation(reservationId).orElseThrow(NullPointerException::new);
    ticketRepository.updateTicketOwnerById(ticket.getId(), "test@test.ch", "This is a Test", "This is", "a Test");
    ticket = ticketRepository.findFirstTicketInReservation(reservationId).orElseThrow(NullPointerException::new);
    assertTrue(groupManager.acquireMemberForTicket(ticket));
    reservationId = ticketReservationManager.createTicketReservation(event, Collections.singletonList(new TicketReservationWithOptionalCodeModification(ticketReservation, Optional.empty())), Collections.emptyList(), DateUtils.addDays(new Date(), 1), Optional.empty(), Locale.ENGLISH, false, null);
    ticket = ticketRepository.findFirstTicketInReservation(reservationId).orElseThrow(NullPointerException::new);
    assertFalse(groupManager.acquireMemberForTicket(ticket), "shouldn't be allowed");
}
Also used : Group(alfio.model.group.Group) LinkedGroup(alfio.model.group.LinkedGroup) Ticket(alfio.model.Ticket) LinkedGroup(alfio.model.group.LinkedGroup) TicketCategory(alfio.model.TicketCategory) LocalDate(java.time.LocalDate) Event(alfio.model.Event) BaseIntegrationTest(alfio.util.BaseIntegrationTest) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

Group (alfio.model.group.Group)4 LinkedGroup (alfio.model.group.LinkedGroup)3 Event (alfio.model.Event)2 BaseIntegrationTest (alfio.util.BaseIntegrationTest)2 Test (org.junit.jupiter.api.Test)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 EventApiController.validateEvent (alfio.controller.api.admin.EventApiController.validateEvent)1 ExtensionService (alfio.extension.ExtensionService)1 alfio.manager (alfio.manager)1 ConfigurationLevel (alfio.manager.system.ConfigurationLevel)1 ConfigurationManager (alfio.manager.system.ConfigurationManager)1 UserManager (alfio.manager.user.UserManager)1 alfio.model (alfio.model)1 ExtensionMetadataValue (alfio.model.ExtensionSupport.ExtensionMetadataValue)1 Ticket (alfio.model.Ticket)1 TicketCategory (alfio.model.TicketCategory)1 EventCreationRequest (alfio.model.api.v1.admin.EventCreationRequest)1 EventModification (alfio.model.modification.EventModification)1 LinkedGroupModification (alfio.model.modification.LinkedGroupModification)1 ErrorCode (alfio.model.result.ErrorCode)1