diff --git a/src/main/java/alfio/controller/api/admin/PromoCodeDiscountApiController.java b/src/main/java/alfio/controller/api/admin/PromoCodeDiscountApiController.java index 9636fb61d3..decdb794f6 100644 --- a/src/main/java/alfio/controller/api/admin/PromoCodeDiscountApiController.java +++ b/src/main/java/alfio/controller/api/admin/PromoCodeDiscountApiController.java @@ -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; @@ -73,12 +73,12 @@ private ZoneId zoneIdFromEventId(Integer eventId, Integer utcOffset) { } @GetMapping("/events/{eventId}/promo-code") - public List listPromoCodeInEvent(@PathVariable("eventId") int eventId) { + public List listPromoCodeInEvent(@PathVariable("eventId") int eventId) { return eventManager.findPromoCodesInEvent(eventId); } @GetMapping("/organization/{organizationId}/promo-code") - public List listPromoCodeInOrganization(@PathVariable("organizationId") int organizationId) { + public List listPromoCodeInOrganization(@PathVariable("organizationId") int organizationId) { return eventManager.findPromoCodesInOrganization(organizationId); } diff --git a/src/main/java/alfio/manager/EventManager.java b/src/main/java/alfio/manager/EventManager.java index 9d4fc014d2..4afd66b498 100644 --- a/src/main/java/alfio/manager/EventManager.java +++ b/src/main/java/alfio/manager/EventManager.java @@ -909,14 +909,14 @@ public void updatePromoCode(int promoCodeId, ZonedDateTime start, ZonedDateTime promoCodeRepository.updateEventPromoCode(promoCodeId, start, end, maxUsage, categoriesJson, description, emailReference, hiddenCategoryId); } - public List findPromoCodesInEvent(int eventId) { - ZoneId zoneId = eventRepository.findById(eventId).getZoneId(); - return promoCodeRepository.findAllInEvent(eventId).stream().map(p -> new PromoCodeDiscountWithFormattedTime(p, zoneId)).collect(toList()); + public List 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 findPromoCodesInOrganization(int organizationId) { + public List 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) { diff --git a/src/main/java/alfio/model/modification/PromoCodeDiscountWithFormattedTime.java b/src/main/java/alfio/model/modification/PromoCodeDiscountWithFormattedTimeAndAmount.java similarity index 73% rename from src/main/java/alfio/model/modification/PromoCodeDiscountWithFormattedTime.java rename to src/main/java/alfio/model/modification/PromoCodeDiscountWithFormattedTimeAndAmount.java index 1ca85b1c6d..8d638d3714 100644 --- a/src/main/java/alfio/model/modification/PromoCodeDiscountWithFormattedTime.java +++ b/src/main/java/alfio/model/modification/PromoCodeDiscountWithFormattedTimeAndAmount.java @@ -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() { @@ -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; + } }