Skip to content

Commit

Permalink
remove saving promo code in httpservlet request attributes #657
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Jun 14, 2019
1 parent 854d8cb commit 5d1fae4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -244,18 +244,18 @@ public ResponseEntity<ValidatedResponse<Boolean>> subscribeToWaitingList(@PathVa
}

@GetMapping("event/{eventName}/ticket-categories")
public ResponseEntity<ItemsByCategory> getTicketCategories(@PathVariable("eventName") String eventName, @RequestParam(value = "code", required = false) String code, HttpServletRequest request) {

var appliedPromoCode = applyPromoCodeInRequest(eventName, code, request);

public ResponseEntity<ItemsByCategory> getTicketCategories(@PathVariable("eventName") String eventName, @RequestParam(value = "code", required = false) String code) {

//
return eventRepository.findOptionalByShortName(eventName).filter(e -> e.getStatus() != Event.Status.DISABLED).map(event -> {
Optional<String> maybeSpecialCode = SessionUtil.retrieveSpecialPriceCode(request);
Optional<SpecialPrice> specialCode = maybeSpecialCode.flatMap(specialPriceRepository::getByCode);

Optional<PromoCodeDiscount> promoCodeDiscount = SessionUtil.retrievePromotionCodeDiscount(request)
.flatMap((retrievedCode) -> promoCodeRepository.findPromoCodeInEventOrOrganization(event.getId(), retrievedCode));

var appliedPromoCode = checkCode(event, code);


Optional<SpecialPrice> specialCode = appliedPromoCode.getValue().getLeft();
Optional<PromoCodeDiscount> promoCodeDiscount = appliedPromoCode.getValue().getRight();

final ZonedDateTime now = ZonedDateTime.now(event.getZoneId());
//hide access restricted ticket categories
var ticketCategories = ticketCategoryRepository.findAllTicketCategories(event.getId());
Expand Down Expand Up @@ -295,7 +295,7 @@ public ResponseEntity<ItemsByCategory> getTicketCategories(@PathVariable("eventN
.collect(Collectors.toList());


var promoCode = appliedPromoCode.filter(ValidatedResponse::isSuccess)
var promoCode = Optional.of(appliedPromoCode).filter(ValidatedResponse::isSuccess)
.map(ValidatedResponse::getValue)
.map(Pair::getRight)
.orElse(Optional.empty());
Expand Down Expand Up @@ -393,17 +393,24 @@ public ResponseEntity<ValidatedResponse<String>> reserveTicket(@PathVariable("ev
BindingResult bindingResult,
ServletWebRequest request) {

if(StringUtils.trimToNull(reservation.getPromoCode()) != null) {
var codeCheck = applyPromoCodeInRequest(eventName, reservation.getPromoCode(), request.getRequest());
codeCheck.ifPresent(res -> {
if(!res.isSuccess()) {


Optional<ResponseEntity<ValidatedResponse<String>>> r = eventRepository.findOptionalByShortName(eventName).map(event -> {


Optional<ValidatedResponse<Pair<Optional<SpecialPrice>, Optional<PromoCodeDiscount>>>> codeCheck = Optional.empty();

if(StringUtils.trimToNull(reservation.getPromoCode()) != null) {
var resCheck = checkCode(event, reservation.getPromoCode());
if(!resCheck.isSuccess()) {
bindingResult.reject(ErrorsCode.STEP_1_CODE_NOT_FOUND, ErrorsCode.STEP_1_CODE_NOT_FOUND);
}
});
}
codeCheck = Optional.of(resCheck);
}
Optional<String> specialPrice = codeCheck.map(ValidatedResponse::getValue).flatMap(Pair::getLeft).map(SpecialPrice::getCode);
Optional<String> promoCodeDiscount = codeCheck.map(ValidatedResponse::getValue).flatMap(Pair::getRight).map(PromoCodeDiscount::getPromoCode);


Optional<ResponseEntity<ValidatedResponse<String>>> r = eventRepository.findOptionalByShortName(eventName).map(event -> {
if (isCaptchaInvalid(reservation.getCaptcha(), request.getRequest(), event)) {
bindingResult.reject(ErrorsCode.STEP_2_CAPTCHA_VALIDATION_FAILED);
}
Expand All @@ -413,8 +420,8 @@ public ResponseEntity<ValidatedResponse<String>> reserveTicket(@PathVariable("ev
try {
String reservationId = ticketReservationManager.createTicketReservation(event,
selected.getLeft(), selected.getRight(), expiration,
SessionUtil.retrieveSpecialPriceSessionId(request.getRequest()),
SessionUtil.retrievePromotionCodeDiscount(request.getRequest()),
specialPrice,
promoCodeDiscount,
Locale.forLanguageTag(lang), false);
return Optional.of(reservationId);
} catch (TicketReservationManager.NotEnoughTicketsException nete) {
Expand Down Expand Up @@ -484,25 +491,6 @@ private static boolean shouldApplyDiscount(PromoCodeDiscount promoCodeDiscount,
return ticketCategory.isAccessRestricted() && ticketCategory.getId() == promoCodeDiscount.getHiddenCategoryId();
}


//TODO: temporary!
private Optional<ValidatedResponse<Pair<Optional<SpecialPrice>, Optional<PromoCodeDiscount>>>> applyPromoCodeInRequest(String eventName, String code, HttpServletRequest request) {
return eventRepository.findOptionalEventAndOrganizationIdByShortName(eventName).map(event -> {
var codeResult = checkCode(event, code);

if (codeResult.isSuccess()) {
codeResult.getValue().getLeft().ifPresent(specialPrice -> {
SessionUtil.saveSpecialPriceCodeOnRequestAttr(specialPrice.getCode(), request);
});
codeResult.getValue().getRight().ifPresent(promoCodeDiscount -> {
SessionUtil.savePromotionCodeDiscountOnRequestAttr(promoCodeDiscount.getPromoCode(), request);
});
}
return codeResult;
});
}


private ValidatedResponse<Pair<Optional<SpecialPrice>, Optional<PromoCodeDiscount>>> checkCode(EventAndOrganizationId event, String promoCode) {
ZoneId eventZoneId = eventRepository.getZoneIdByEventId(event.getId());
ZonedDateTime now = ZonedDateTime.now(eventZoneId);
Expand Down
21 changes: 0 additions & 21 deletions src/main/java/alfio/controller/support/SessionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package alfio.controller.support;

import alfio.manager.PaymentManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

Expand All @@ -34,26 +33,6 @@ public final class SessionUtil {

private SessionUtil() {}

public static void saveSpecialPriceCodeOnRequestAttr(String specialPriceCode, HttpServletRequest request) {
if(StringUtils.isNotEmpty(specialPriceCode)) {
request.setAttribute(SPECIAL_PRICE_CODE, specialPriceCode);
}
}

public static void savePromotionCodeDiscountOnRequestAttr(String promoCodeDiscount, HttpServletRequest request) {
request.setAttribute(PROMOTIONAL_CODE_DISCOUNT, promoCodeDiscount);
}

public static Optional<String> retrieveSpecialPriceCode(HttpServletRequest request) {
return Optional.ofNullable((String)request.getSession().getAttribute(SPECIAL_PRICE_CODE))
.or(() -> Optional.ofNullable((String)request.getAttribute(SPECIAL_PRICE_CODE)));
}

public static Optional<String> retrievePromotionCodeDiscount(HttpServletRequest request) {
return Optional.ofNullable((String) request.getSession().getAttribute(PROMOTIONAL_CODE_DISCOUNT))
.or(() -> Optional.ofNullable((String) request.getAttribute(PROMOTIONAL_CODE_DISCOUNT)));
}

public static Optional<String> retrieveSpecialPriceSessionId(HttpServletRequest request) {
return Optional.ofNullable((String)request.getSession().getAttribute(SPECIAL_PRICE_CODE_SESSION_ID));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,9 @@ public void reservationFlowTest() throws Exception {


// check ticket & all, we have 2 ticket categories, 1 hidden
assertEquals(HttpStatus.NOT_FOUND, eventApiV2Controller.getTicketCategories("NOT_EXISTING", null, new MockHttpServletRequest()).getStatusCode());
assertEquals(HttpStatus.NOT_FOUND, eventApiV2Controller.getTicketCategories("NOT_EXISTING", null).getStatusCode());
{
var itemsRes = eventApiV2Controller.getTicketCategories(event.getShortName(), null, new MockHttpServletRequest());
var itemsRes = eventApiV2Controller.getTicketCategories(event.getShortName(), null);
assertEquals(HttpStatus.OK, itemsRes.getStatusCode());

var items = itemsRes.getBody();
Expand Down Expand Up @@ -378,7 +378,7 @@ public void reservationFlowTest() throws Exception {
var tc = ticketCategoryRepository.getById(visibleCat.getId());
ticketCategoryRepository.fixDates(visibleCat.getId(), tc.getInception(event.getZoneId()).plusDays(2), tc.getExpiration(event.getZoneId()));
//
items = eventApiV2Controller.getTicketCategories(event.getShortName(), null, new MockHttpServletRequest()).getBody();
items = eventApiV2Controller.getTicketCategories(event.getShortName(), null).getBody();
assertTrue(items.isWaitingList());
assertTrue(items.isPreSales());
//
Expand Down Expand Up @@ -418,7 +418,7 @@ public void reservationFlowTest() throws Exception {
var hiddenCode = hiddenCodeRes.getBody();
assertEquals(EventCode.EventCodeType.ACCESS, hiddenCode.getValue().getType());

var itemsRes2 = eventApiV2Controller.getTicketCategories(event.getShortName(), HIDDEN_CODE, new MockHttpServletRequest());
var itemsRes2 = eventApiV2Controller.getTicketCategories(event.getShortName(), HIDDEN_CODE);
var items2 = itemsRes2.getBody();
assertEquals(2, items2.getTicketCategories().size());

Expand All @@ -437,7 +437,7 @@ public void reservationFlowTest() throws Exception {
var discountCodeRes = eventApiV2Controller.validateCode(event.getShortName(), PROMO_CODE);
var discountCode = discountCodeRes.getBody();
assertEquals(EventCode.EventCodeType.DISCOUNT, discountCode.getValue().getType());
var itemsRes3 = eventApiV2Controller.getTicketCategories(event.getShortName(), PROMO_CODE, new MockHttpServletRequest());
var itemsRes3 = eventApiV2Controller.getTicketCategories(event.getShortName(), PROMO_CODE);

var items3 = itemsRes3.getBody();

Expand Down Expand Up @@ -466,7 +466,7 @@ public void reservationFlowTest() throws Exception {
var form = new ReservationForm();
var ticketReservation = new TicketReservationModification();
ticketReservation.setAmount(1);
ticketReservation.setTicketCategoryId(eventApiV2Controller.getTicketCategories(event.getShortName(), null, new MockHttpServletRequest()).getBody().getTicketCategories().get(0).getId());
ticketReservation.setTicketCategoryId(eventApiV2Controller.getTicketCategories(event.getShortName(), null).getBody().getTicketCategories().get(0).getId());
form.setReservation(Collections.singletonList(ticketReservation));
var res = eventApiV2Controller.reserveTicket(event.getShortName(), "en", form, new BeanPropertyBindingResult(form, "reservation"), new ServletWebRequest(new MockHttpServletRequest(), new MockHttpServletResponse()));
assertEquals(HttpStatus.OK, res.getStatusCode());
Expand All @@ -489,7 +489,7 @@ public void reservationFlowTest() throws Exception {
var form = new ReservationForm();
var ticketReservation = new TicketReservationModification();
ticketReservation.setAmount(1);
ticketReservation.setTicketCategoryId(eventApiV2Controller.getTicketCategories(event.getShortName(), null, new MockHttpServletRequest()).getBody().getTicketCategories().get(0).getId());
ticketReservation.setTicketCategoryId(eventApiV2Controller.getTicketCategories(event.getShortName(), null).getBody().getTicketCategories().get(0).getId());
form.setReservation(Collections.singletonList(ticketReservation));
var res = eventApiV2Controller.reserveTicket(event.getShortName(), "en", form, new BeanPropertyBindingResult(form, "reservation"), new ServletWebRequest(new MockHttpServletRequest(), new MockHttpServletResponse()));
assertEquals(HttpStatus.OK, res.getStatusCode());
Expand Down

0 comments on commit 5d1fae4

Please sign in to comment.