Skip to content

Commit

Permalink
do not allow format update to "ONLINE" if "ON_SITE" method is selected
Browse files Browse the repository at this point in the history
  • Loading branch information
cbellone committed Mar 5, 2021
1 parent f82ff11 commit c47c2d7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/main/java/alfio/manager/EventManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
public class EventManager {

private static final Predicate<TicketCategory> IS_CATEGORY_BOUNDED = TicketCategory::isBounded;
static final String ERROR_ONLINE_ON_SITE_NOT_COMPATIBLE = "Cannot switch to Online. Please remove On-Site payment method first.";
private final UserManager userManager;
private final EventRepository eventRepository;
private final EventDescriptionRepository eventDescriptionRepository;
Expand Down Expand Up @@ -318,6 +319,10 @@ public void updateEventHeader(Event original, EventModification em, String usern
int eventId = original.getId();
Validate.isTrue(sameOrganization || groupRepository.countByEventId(eventId) == 0, "Cannot change organization because there is a group linked to this event.");

if(em.getFormat() == Event.EventFormat.ONLINE && em.getFormat() != original.getFormat()) {
Validate.isTrue(original.getAllowedPaymentProxies().stream().allMatch(p -> p != PaymentProxy.ON_SITE), ERROR_ONLINE_ON_SITE_NOT_COMPATIBLE);
}

String timeZone = ObjectUtils.firstNonNull(em.getZoneId(), em.getGeolocation() != null ? em.getGeolocation().getTimeZone() : null);
String latitude = ObjectUtils.firstNonNull(em.getLatitude(), em.getGeolocation() != null ? em.getGeolocation().getLatitude() : null);
String longitude = ObjectUtils.firstNonNull(em.getLongitude(), em.getGeolocation() != null ? em.getGeolocation().getLongitude(): null);
Expand Down
50 changes: 47 additions & 3 deletions src/test/java/alfio/manager/EventManagerIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import alfio.model.modification.*;
import alfio.model.result.ErrorCode;
import alfio.model.result.Result;
import alfio.model.transaction.PaymentProxy;
import alfio.repository.*;
import alfio.repository.system.ConfigurationRepository;
import alfio.repository.user.OrganizationRepository;
Expand Down Expand Up @@ -809,6 +810,41 @@ public void rearrangeTicketCategories() {

}

@Test
public void updateEventFormat() {
var categories = List.of(
new TicketCategoryModification(null, "first", TicketCategory.TicketAccessType.INHERIT, AVAILABLE_SEATS,
new DateTimeModification(LocalDate.now(), LocalTime.now()),
new DateTimeModification(LocalDate.now(), LocalTime.now()),
DESCRIPTION, BigDecimal.TEN, false, "", false, null, null, null, null, null, 2, null, null, AlfioMetadata.empty()));

var eventAndUsername = initEvent(categories, organizationRepository, userManager, eventManager, eventRepository);
var event = eventAndUsername.getLeft();

// add ON_SITE payment method
var withOnSite = List.of(PaymentProxy.OFFLINE, PaymentProxy.ON_SITE);
var onSitePaymentMethodModification = createEventModification(AVAILABLE_SEATS, event, Event.EventFormat.IN_PERSON, withOnSite);
var username = eventAndUsername.getRight();
eventManager.updateEventPrices(event, onSitePaymentMethodModification, username);

event = eventRepository.findById(event.getId());
assertEquals(withOnSite, event.getAllowedPaymentProxies());

try {
eventManager.updateEventHeader(event, createEventModification(AVAILABLE_SEATS, event, Event.EventFormat.ONLINE, withOnSite), username);
fail();
} catch(IllegalArgumentException ex) {
assertEquals(EventManager.ERROR_ONLINE_ON_SITE_NOT_COMPATIBLE, ex.getMessage());
}

event = eventRepository.findById(event.getId());
// update header and remove ON_SITE
eventManager.updateEventPrices(event, createEventModification(AVAILABLE_SEATS, event, event.getFormat(), List.of(PaymentProxy.OFFLINE)), username);
// retry
event = eventRepository.findById(event.getId());
eventManager.updateEventHeader(event, createEventModification(AVAILABLE_SEATS, event, Event.EventFormat.ONLINE, event.getAllowedPaymentProxies()), username);
}

private Pair<Event, String> generateAndEditEvent(int newEventSize) {
List<TicketCategoryModification> categories = Collections.singletonList(
new TicketCategoryModification(null, "default", TicketCategory.TicketAccessType.INHERIT, 10,
Expand All @@ -819,9 +855,7 @@ private Pair<Event, String> generateAndEditEvent(int newEventSize) {

Event event = pair.getKey();
if(newEventSize != AVAILABLE_SEATS) {
EventModification update = new EventModification(event.getId(), Event.EventFormat.IN_PERSON, null, null, null, null, null, null, null, null, event.getOrganizationId(), null, null,
null, event.getZoneId().toString(), Collections.emptyMap(), DateTimeModification.fromZonedDateTime(event.getBegin()), DateTimeModification.fromZonedDateTime(event.getEnd()),
event.getRegularPrice(), event.getCurrency(), newEventSize, event.getVat(), event.isVatIncluded(), event.getAllowedPaymentProxies(), null, event.isFreeOfCharge(), null, 7, null, null, AlfioMetadata.empty());
EventModification update = createEventModification(newEventSize, event);
eventManager.updateEventPrices(event, update, pair.getValue());
}
List<Ticket> tickets = ticketRepository.findFreeByEventId(event.getId());
Expand All @@ -834,4 +868,14 @@ private Pair<Event, String> generateAndEditEvent(int newEventSize) {
assertEquals(10, tickets.stream().filter(t -> t.getCategoryId() != null).count());
return Pair.of(eventRepository.findById(event.getId()), pair.getRight());
}

private EventModification createEventModification(int availableSeats, Event event) {
return createEventModification(availableSeats, event, Event.EventFormat.IN_PERSON, event.getAllowedPaymentProxies());
}

private EventModification createEventModification(int availableSeats, Event event, Event.EventFormat format, List<PaymentProxy> allowedPaymentProxies) {
return new EventModification(event.getId(), format, "http://website-url", null, "http://website-url/tc", null, null, null, null, null, event.getOrganizationId(), null, null,
null, event.getZoneId().toString(), Collections.emptyMap(), DateTimeModification.fromZonedDateTime(event.getBegin()), DateTimeModification.fromZonedDateTime(event.getEnd()),
event.getRegularPrice(), event.getCurrency(), availableSeats, event.getVat(), event.isVatIncluded(), allowedPaymentProxies, null, event.isFreeOfCharge(), null, 7, null, null, AlfioMetadata.empty());
}
}

0 comments on commit c47c2d7

Please sign in to comment.