use of alfio.model.modification.PollOptionModification in project alf.io by alfio-event.
the class PollManager method updateOptions.
private void updateOptions(List<PollOptionModification> existingOptions, EventAndOrganizationId event, Long pollId) {
var parameterSources = existingOptions.stream().map(option -> new MapSqlParameterSource("pollId", pollId).addValue("title", Json.toJson(requireNonNullElse(option.getTitle(), Map.of()))).addValue("description", Json.toJson(requireNonNullElse(option.getDescription(), Map.of()))).addValue("id", option.getId())).toArray(MapSqlParameterSource[]::new);
int[] results = jdbcTemplate.batchUpdate(pollRepository.bulkUpdateOptions(), parameterSources);
Validate.isTrue(IntStream.of(results).sum() == existingOptions.size(), "Unexpected result from update.");
}
use of alfio.model.modification.PollOptionModification in project alf.io by alfio-event.
the class PollManager method insertOptions.
private void insertOptions(List<PollOptionModification> options, EventAndOrganizationId event, Long pollId) {
if (options.size() == 1) {
var option = options.get(0);
pollRepository.insertOption(pollId, option.getTitle(), requireNonNullElse(option.getDescription(), Map.of()), event.getOrganizationId());
} else {
var parameterSources = options.stream().map(option -> new MapSqlParameterSource("pollId", pollId).addValue("title", Json.toJson(requireNonNullElse(option.getTitle(), Map.of()))).addValue("description", Json.toJson(requireNonNullElse(option.getDescription(), Map.of()))).addValue("organizationId", event.getOrganizationId())).toArray(MapSqlParameterSource[]::new);
int[] results = jdbcTemplate.batchUpdate(pollRepository.bulkInsertOptions(), parameterSources);
Validate.isTrue(IntStream.of(results).sum() == options.size(), "Unexpected result from update.");
}
}
use of alfio.model.modification.PollOptionModification in project alf.io by alfio-event.
the class PollAdminApiControllerTest method allowPeopleToVote.
@Test
void allowPeopleToVote() {
var options = List.of(new PollOptionModification(null, Map.of("en", "Homer J. Simpson"), null), new PollOptionModification(null, Map.of("en", "Bender B. Rodriguez"), Map.of()));
// this must not have an impact
var form = new PollModification(null, Map.of("en", "Best Employee of the Year"), null, null, options, true, Poll.PollStatus.OPEN);
var eventName = event.getShortName();
var createResponse = controller.createNewPoll(eventName, form);
assertTrue(createResponse.getStatusCode().is2xxSuccessful());
assertNotNull(createResponse.getBody());
var pollId = createResponse.getBody();
var reservationId = UUID.randomUUID().toString();
ticketReservationRepository.createNewReservation(reservationId, ZonedDateTime.now(event.getZoneId()), DateUtils.addMinutes(new Date(), 1), null, "en", event.getId(), null, null, null, event.getOrganizationId(), null);
var firstCategory = CollectionUtils.get(ticketCategoryRepository.findByEventIdAsMap(event.getId()), 0);
int categoryId = firstCategory.getKey();
var tickets = ticketRepository.findFreeByEventId(event.getId());
var firstTicket = tickets.get(0);
int ticketId = firstTicket.getId();
ticketRepository.reserveTickets(reservationId, List.of(ticketId), firstCategory.getValue(), "en", event.getVatStatus(), i -> null);
ticketReservationRepository.updateReservationStatus(reservationId, TicketReservation.TicketReservationStatus.COMPLETE.name());
ticketRepository.updateTicketOwner(firstTicket.getUuid(), "test@test.ch", "First Last", "First", "Last");
ticketRepository.updateTicketsStatusWithReservationId(reservationId, Ticket.TicketStatus.ACQUIRED.name());
// find compatible tickets
var res = controller.findAdditionalAttendees(event.getShortName(), pollId, "First");
assertTrue(res.getStatusCode().is2xxSuccessful());
assertTrue(CollectionUtils.isNotEmpty(res.getBody()));
assertEquals(1, res.getBody().size());
assertEquals(firstTicket.getId(), res.getBody().get(0).getId());
// allow tickets to vote
var poll = pollRepository.findSingleForEvent(event.getId(), pollId).orElseThrow();
assertNotNull(poll);
var participantForm = new PollAdminApiController.UpdateParticipantsForm(List.of(ticketId));
var allowRes = controller.allowAttendees(event.getShortName(), pollId, participantForm);
assertTrue(allowRes.getStatusCode().is2xxSuccessful());
assertEquals(1, auditingRepository.countAuditsOfTypeForReservation(reservationId, Audit.EventType.TAG_TICKET));
assertEquals(0, auditingRepository.countAuditsOfTypeForReservation(reservationId, Audit.EventType.UNTAG_TICKET));
var participantRes = controller.getAllowedAttendees(eventName, pollId);
assertTrue(participantRes.getStatusCode().is2xxSuccessful());
assertTrue(CollectionUtils.isNotEmpty(participantRes.getBody()));
assertEquals(1, participantRes.getBody().size());
assertEquals(firstTicket.getId(), participantRes.getBody().get(0).getId());
// now ticket should not be returned anymore
res = controller.findAdditionalAttendees(event.getShortName(), pollId, "First");
assertTrue(res.getStatusCode().is2xxSuccessful());
assertTrue(CollectionUtils.isEmpty(res.getBody()));
// get statistics
var statsRes = controller.getStatisticsForEvent(event.getShortName(), pollId);
assertTrue(statsRes.getStatusCode().is2xxSuccessful());
assertNotNull(statsRes.getBody());
assertTrue(CollectionUtils.isEmpty(statsRes.getBody().getOptionStatistics()));
assertEquals("0", statsRes.getBody().getParticipationPercentage());
// forbid access to attendee
var forbidRes = controller.forbidAttendees(event.getShortName(), pollId, participantForm);
assertTrue(forbidRes.getStatusCode().is2xxSuccessful());
assertTrue(CollectionUtils.isEmpty(forbidRes.getBody()));
assertEquals(1, auditingRepository.countAuditsOfTypeForReservation(reservationId, Audit.EventType.TAG_TICKET));
assertEquals(1, auditingRepository.countAuditsOfTypeForReservation(reservationId, Audit.EventType.UNTAG_TICKET));
// remove option
var pollWithOptions = controller.getPollDetail(event.getShortName(), pollId).getBody();
assertNotNull(pollWithOptions);
var firstOptionId = pollWithOptions.getOptions().get(0).getId();
var removeOptionResponse = controller.removeOption(event.getShortName(), pollId, firstOptionId);
assertTrue(removeOptionResponse.getStatusCode().is2xxSuccessful());
assertTrue(Objects.requireNonNull(removeOptionResponse.getBody()).getOptions().stream().noneMatch(po -> firstOptionId.equals(po.getId())));
// delete poll
var deletePollResponse = controller.deletePoll(eventName, pollId);
assertTrue(deletePollResponse.getStatusCode().is2xxSuccessful());
assertNotNull(deletePollResponse.getBody());
assertTrue(deletePollResponse.getBody());
}
use of alfio.model.modification.PollOptionModification in project alf.io by alfio-event.
the class PollAdminApiControllerTest method createAndUpdatePoll.
@Test
void createAndUpdatePoll() {
// create poll
var options = List.of(new PollOptionModification(null, Map.of("en", "Homer J. Simpson"), null), new PollOptionModification(null, Map.of("en", "Bender B. Rodriguez"), Map.of()));
// this must not have an impact
var form = new PollModification(null, Map.of("en", "Best Employee of the Year"), null, null, options, false, Poll.PollStatus.OPEN);
var eventName = event.getShortName();
var createResponse = controller.createNewPoll(eventName, form);
assertTrue(createResponse.getStatusCode().is2xxSuccessful());
assertNotNull(createResponse.getBody());
var pollId = createResponse.getBody();
// retrieve poll and check
var getResponse = controller.getPollDetail(eventName, pollId);
assertTrue(getResponse.getStatusCode().is2xxSuccessful());
assertNotNull(getResponse.getBody());
var poll = getResponse.getBody();
assertEquals(2, poll.getOptions().size());
assertEquals("Homer J. Simpson", poll.getOptions().get(0).getTitle().get("en"));
assertEquals(Poll.PollStatus.DRAFT, poll.getStatus());
// update poll status
var updateStatusResponse = controller.updateStatus(eventName, pollId, new PollAdminApiController.UpdatePollStatusForm(Poll.PollStatus.OPEN));
assertTrue(updateStatusResponse.getStatusCode().is2xxSuccessful());
assertNotNull(updateStatusResponse.getBody());
assertEquals(Poll.PollStatus.OPEN, updateStatusResponse.getBody().getStatus());
// update poll
var newOptionsList = new ArrayList<>(poll.getOptions());
newOptionsList.addAll(List.of(new PollOptionModification(null, Map.of("en", "Lisa M. Simpson"), null), new PollOptionModification(null, Map.of("en", "Turanga Leela"), Map.of())));
var updateForm = new PollModification(poll.getId(), poll.getTitle(), poll.getDescription(), poll.getOrder(), newOptionsList, false, Poll.PollStatus.DRAFT);
var updatePollResponse = controller.updatePoll(eventName, pollId, updateForm);
assertTrue(updatePollResponse.getStatusCode().is2xxSuccessful());
assertNotNull(updatePollResponse.getBody());
assertEquals(Poll.PollStatus.OPEN, updatePollResponse.getBody().getStatus());
assertEquals(4, updatePollResponse.getBody().getOptions().size());
assertEquals("Homer J. Simpson", updatePollResponse.getBody().getOptions().get(0).getTitle().get("en"));
}
Aggregations