Skip to content

Commit

Permalink
fix #862
Browse files Browse the repository at this point in the history
(cherry picked from commit bd491dd)
  • Loading branch information
cbellone committed Feb 9, 2020
1 parent 0869a07 commit 0b04923
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import alfio.manager.EventManager;
import alfio.model.PromoCodeDiscount;
import alfio.model.modification.PromoCodeDiscountModification;
import alfio.model.modification.PromoCodeDiscountWithFormattedTime;
import alfio.model.modification.PromoCodeDiscountWithFormattedTimeAndAmount;
import alfio.repository.EventRepository;
import alfio.repository.PromoCodeDiscountRepository;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -73,12 +73,12 @@ private ZoneId zoneIdFromEventId(Integer eventId, Integer utcOffset) {
}

@GetMapping("/events/{eventId}/promo-code")
public List<PromoCodeDiscountWithFormattedTime> listPromoCodeInEvent(@PathVariable("eventId") int eventId) {
public List<PromoCodeDiscountWithFormattedTimeAndAmount> listPromoCodeInEvent(@PathVariable("eventId") int eventId) {
return eventManager.findPromoCodesInEvent(eventId);
}

@GetMapping("/organization/{organizationId}/promo-code")
public List<PromoCodeDiscountWithFormattedTime> listPromoCodeInOrganization(@PathVariable("organizationId") int organizationId) {
public List<PromoCodeDiscountWithFormattedTimeAndAmount> listPromoCodeInOrganization(@PathVariable("organizationId") int organizationId) {
return eventManager.findPromoCodesInOrganization(organizationId);
}

Expand Down
10 changes: 5 additions & 5 deletions src/main/java/alfio/manager/EventManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -909,14 +909,14 @@ public void updatePromoCode(int promoCodeId, ZonedDateTime start, ZonedDateTime
promoCodeRepository.updateEventPromoCode(promoCodeId, start, end, maxUsage, categoriesJson, description, emailReference, hiddenCategoryId);
}

public List<PromoCodeDiscountWithFormattedTime> findPromoCodesInEvent(int eventId) {
ZoneId zoneId = eventRepository.findById(eventId).getZoneId();
return promoCodeRepository.findAllInEvent(eventId).stream().map(p -> new PromoCodeDiscountWithFormattedTime(p, zoneId)).collect(toList());
public List<PromoCodeDiscountWithFormattedTimeAndAmount> findPromoCodesInEvent(int eventId) {
var event = eventRepository.findById(eventId);
return promoCodeRepository.findAllInEvent(eventId).stream().map(p -> new PromoCodeDiscountWithFormattedTimeAndAmount(p, event.getZoneId(), event.getCurrency())).collect(toList());
}

public List<PromoCodeDiscountWithFormattedTime> findPromoCodesInOrganization(int organizationId) {
public List<PromoCodeDiscountWithFormattedTimeAndAmount> findPromoCodesInOrganization(int organizationId) {
ZoneId zoneId = ZoneId.systemDefault();
return promoCodeRepository.findAllInOrganization(organizationId).stream().map(p -> new PromoCodeDiscountWithFormattedTime(p, zoneId)).collect(toList());
return promoCodeRepository.findAllInOrganization(organizationId).stream().map(p -> new PromoCodeDiscountWithFormattedTimeAndAmount(p, zoneId, null)).collect(toList());
}

public String getEventUrl(Event event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,29 @@
*/
package alfio.model.modification;

import java.time.ZoneId;
import java.time.ZonedDateTime;

import alfio.model.EventStatistic;
import alfio.model.PromoCodeDiscount;
import alfio.util.MonetaryUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.experimental.Delegate;
import alfio.model.PromoCodeDiscount;
import org.apache.commons.lang3.StringUtils;

public class PromoCodeDiscountWithFormattedTime {
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class PromoCodeDiscountWithFormattedTimeAndAmount {

@JsonIgnore
@Delegate
private final PromoCodeDiscount promo;

@JsonIgnore
private final ZoneId eventZoneId;
private final String eventCurrency;

public PromoCodeDiscountWithFormattedTime(PromoCodeDiscount promo, ZoneId eventZoneId) {
public PromoCodeDiscountWithFormattedTimeAndAmount(PromoCodeDiscount promo, ZoneId eventZoneId, String eventCurrency) {
this.promo = promo;
this.eventZoneId = eventZoneId;
this.eventCurrency = eventCurrency;
}

public boolean isCurrentlyValid() {
Expand All @@ -54,4 +56,11 @@ public String getFormattedStart() {
public String getFormattedEnd() {
return getUtcEnd().withZoneSameInstant(eventZoneId).format(EventStatistic.JSON_DATE_FORMATTER);
}

public String getFormattedDiscountAmount() {
if(promo.getFixedAmount() && StringUtils.isNotBlank(eventCurrency)) {
return MonetaryUtil.formatCents(promo.getDiscountAmount(), eventCurrency);
}
return null;
}
}

0 comments on commit 0b04923

Please sign in to comment.