From 5b7ba3ba1c0ab7d16ef2277d8286dc70a61b8c6f Mon Sep 17 00:00:00 2001 From: ArdyZ <45757560+ArdyZ@users.noreply.github.com> Date: Fri, 24 Dec 2021 17:08:49 +0100 Subject: [PATCH] Making a function to allow free tickets (#551) * Making a function to allow free tickets * Set amount of tickets to int instead of float * Use the ref url instead of hardcoding * Missing the "?order=" added --- .../products/model/order/Order.java | 5 +++++ .../products/service/OrderServiceImpl.java | 9 +++++++- .../areafiftylan/unit/OrderServiceTest.java | 21 +++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/ch/wisv/areafiftylan/products/model/order/Order.java b/src/main/java/ch/wisv/areafiftylan/products/model/order/Order.java index da6b3ae38..4b645b160 100644 --- a/src/main/java/ch/wisv/areafiftylan/products/model/order/Order.java +++ b/src/main/java/ch/wisv/areafiftylan/products/model/order/Order.java @@ -93,6 +93,11 @@ public float getAmount() { return price; } + @JsonView(View.OrderOverview.class) + public int getAmountTickets() { + return tickets.size(); + } + public void setUser(User user) { this.user = user; if (user != null) { diff --git a/src/main/java/ch/wisv/areafiftylan/products/service/OrderServiceImpl.java b/src/main/java/ch/wisv/areafiftylan/products/service/OrderServiceImpl.java index 7afb6f52d..0db054565 100644 --- a/src/main/java/ch/wisv/areafiftylan/products/service/OrderServiceImpl.java +++ b/src/main/java/ch/wisv/areafiftylan/products/service/OrderServiceImpl.java @@ -52,6 +52,9 @@ public class OrderServiceImpl implements OrderService { private final PaymentService paymentService; private final MailService mailService; + @Value("${a5l.paymentReturnUrl}") + private String RETURN_URL; + @Value("${a5l.orderLimit}") private int ORDER_LIMIT; @@ -206,12 +209,16 @@ public Order removeTicketFromOrder(Long orderId, Long ticketId) { @Override public String requestPayment(Long orderId) { Order order = getOrderById(orderId); - if (order.getAmount() == 0) { + if (order.getAmountTickets() == 0) { throw new IllegalStateException("Order can not be empty"); } if (order.getStatus() != OrderStatus.ASSIGNED) { throw new UnassignedOrderException(order.getId()); } + if (order.getAmount() == 0) { + order.setStatus(OrderStatus.PAID); + return RETURN_URL + "?order=" + orderId; + } return paymentService.registerOrder(order); } diff --git a/src/test/java/ch/wisv/areafiftylan/unit/OrderServiceTest.java b/src/test/java/ch/wisv/areafiftylan/unit/OrderServiceTest.java index 560acead5..b23f68850 100644 --- a/src/test/java/ch/wisv/areafiftylan/unit/OrderServiceTest.java +++ b/src/test/java/ch/wisv/areafiftylan/unit/OrderServiceTest.java @@ -19,6 +19,7 @@ import ch.wisv.areafiftylan.exception.*; import ch.wisv.areafiftylan.products.model.Ticket; +import ch.wisv.areafiftylan.products.model.TicketType; import ch.wisv.areafiftylan.products.model.order.Order; import ch.wisv.areafiftylan.products.model.order.OrderStatus; import ch.wisv.areafiftylan.users.model.User; @@ -26,6 +27,7 @@ import org.junit.jupiter.api.Test; import org.mockito.Mockito; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; @@ -33,6 +35,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import java.time.LocalDateTime; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.*; @@ -44,6 +47,9 @@ public class OrderServiceTest extends ServiceTest { @Autowired private MailService mailService; + @Value("${a5l.paymentReturnUrl}") + private String RETURN_URL; + @Test public void getOrderById() { Long id = testEntityManager.persistAndGetId(new Order(), Long.class); @@ -493,6 +499,21 @@ public void requestPayment() { reset(paymentService); } + @Test + public void requestPaymentFree() { + User user = persistUser(); + Order order = new Order(user); + TicketType ticketType = new TicketType("testPaymentFree", "testPaymentFree", 0.0f, 15, LocalDateTime.of(3021, 6, 3, 0, 0), true); + Ticket ticket = new Ticket(ticketType); + assertEquals(0.0, ticket.getPrice(), 0.0001); + order.addTicket(ticket); + + Long id = testEntityManager.persistAndGetId(order, Long.class); + assertEquals(RETURN_URL + "?order=" + order.getId(), orderService.requestPayment(id)); + verify(paymentService, never()).registerOrder(Mockito.any(Order.class)); + reset(paymentService); + } + @Test public void requestPaymentEmptyOrder() { User user = persistUser();