Search in sources :

Example 1 with GroupMember

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

the class GroupManager method acquireMemberForTicket.

@Transactional
public boolean acquireMemberForTicket(Ticket ticket) {
    List<LinkedGroup> configurations = findLinks(ticket.getEventId(), ticket.getCategoryId());
    if (CollectionUtils.isEmpty(configurations)) {
        return true;
    }
    LinkedGroup configuration = configurations.get(0);
    Optional<GroupMember> optionalItem = getMatchingMember(configuration, ticket.getEmail());
    if (optionalItem.isEmpty()) {
        return false;
    }
    GroupMember item = optionalItem.get();
    boolean preventDuplication = configuration.getType() == ONCE_PER_VALUE;
    boolean limitAssignments = preventDuplication || configuration.getType() == LIMITED_QUANTITY;
    if (limitAssignments) {
        // reload and lock configuration
        configuration = groupRepository.getConfigurationForUpdate(configuration.getId());
        int existing = groupRepository.countExistingWhitelistedTickets(item.getId(), configuration.getId());
        int expected = preventDuplication ? 1 : Optional.ofNullable(configuration.getMaxAllocation()).orElse(0);
        if (existing >= expected) {
            return false;
        }
    }
    groupRepository.insertWhitelistedTicket(item.getId(), configuration.getId(), ticket.getId(), preventDuplication ? Boolean.TRUE : null);
    Map<String, Object> modifications = new HashMap<>();
    modifications.put("itemId", item.getId());
    modifications.put("configurationId", configuration.getId());
    modifications.put("ticketId", ticket.getId());
    auditingRepository.insert(ticket.getTicketsReservationId(), null, ticket.getEventId(), Audit.EventType.GROUP_MEMBER_ACQUIRED, new Date(), Audit.EntityType.TICKET, String.valueOf(ticket.getId()), singletonList(modifications));
    return true;
}
Also used : GroupMember(alfio.model.group.GroupMember) LinkedGroup(alfio.model.group.LinkedGroup) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

GroupMember (alfio.model.group.GroupMember)1 LinkedGroup (alfio.model.group.LinkedGroup)1 Transactional (org.springframework.transaction.annotation.Transactional)1