From 4c4752b8cc2756c22711f1b9df2decdf91abc2ed Mon Sep 17 00:00:00 2001 From: pushedrumex Date: Wed, 17 Apr 2024 16:50:39 +0900 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20=EC=A3=BC=EB=AC=B8=20=EC=8A=A4?= =?UTF-8?q?=EC=BC=80=EC=A4=84=EB=9F=AC=20=ED=8A=B8=EB=9E=9C=EC=9E=AD?= =?UTF-8?q?=EC=85=98=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../order/service/OrderCancelService.java | 26 ++++++++++ .../domain/order/service/OrderService.java | 18 ++----- .../payment/service/PaymentService.java | 4 +- .../order/service/OrderCancelServiceTest.java | 51 +++++++++++++++++++ .../payment/service/PaymentServiceTest.java | 6 ++- 5 files changed, 89 insertions(+), 16 deletions(-) create mode 100644 src/main/java/com/prgrms/nabmart/domain/order/service/OrderCancelService.java create mode 100644 src/test/java/com/prgrms/nabmart/domain/order/service/OrderCancelServiceTest.java diff --git a/src/main/java/com/prgrms/nabmart/domain/order/service/OrderCancelService.java b/src/main/java/com/prgrms/nabmart/domain/order/service/OrderCancelService.java new file mode 100644 index 00000000..9c27f879 --- /dev/null +++ b/src/main/java/com/prgrms/nabmart/domain/order/service/OrderCancelService.java @@ -0,0 +1,26 @@ +package com.prgrms.nabmart.domain.order.service; + +import com.prgrms.nabmart.domain.item.repository.ItemRepository; +import com.prgrms.nabmart.domain.order.Order; +import com.prgrms.nabmart.domain.order.OrderStatus; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +public class OrderCancelService { + + private final ItemRepository itemRepository; + + @Transactional + public void cancelOrder(Order order) { + order.updateOrderStatus(OrderStatus.CANCELED); + order.unUseCoupon(); + order.getOrderItems().forEach( + orderItem -> itemRepository.increaseQuantity(orderItem.getItem().getItemId(), + orderItem.getQuantity()) + ); + } + +} diff --git a/src/main/java/com/prgrms/nabmart/domain/order/service/OrderService.java b/src/main/java/com/prgrms/nabmart/domain/order/service/OrderService.java index 3847f4ea..5a4a1ab5 100644 --- a/src/main/java/com/prgrms/nabmart/domain/order/service/OrderService.java +++ b/src/main/java/com/prgrms/nabmart/domain/order/service/OrderService.java @@ -43,12 +43,13 @@ public class OrderService { private static final Integer PAGE_SIZE = 10; + public static final int MINUTES_OF_CANCEL_ORDER = 30; private final OrderRepository orderRepository; private final UserRepository userRepository; private final ItemRepository itemRepository; private final UserCouponRepository userCouponRepository; - + private final OrderCancelService orderCancelService; @Transactional public CreateOrderResponse createOrder(final CreateOrdersCommand createOrdersCommand) { @@ -77,17 +78,13 @@ public UpdateOrderByCouponResponse updateOrderByCoupon( @Transactional public void updateOrderStatus() { - //30분 - LocalDateTime expiredTime = LocalDateTime.now().minusMinutes(30); + LocalDateTime expiredTime = LocalDateTime.now().minusMinutes(MINUTES_OF_CANCEL_ORDER); List statusList = List.of(OrderStatus.PENDING, OrderStatus.PAYING); List expiredOrders = orderRepository.findByStatusInBeforeExpiredTime( expiredTime, statusList); - for (Order expirdeOrder : expiredOrders) { - updateItemQuantity(expirdeOrder); - expirdeOrder.updateOrderStatus(OrderStatus.CANCELED); - } + expiredOrders.forEach(orderCancelService::cancelOrder); } @Transactional @@ -106,13 +103,6 @@ public void deleteOrder(final Long orderId, final Long userId) { orderRepository.delete(order); } - private static void updateItemQuantity(Order order) { - List orderItems = order.getOrderItems(); - for (OrderItem orderItem : orderItems) { - orderItem.getItem().increaseQuantity(orderItem.getQuantity()); - } - } - @Transactional(readOnly = true) public FindOrderDetailResponse findOrderByIdAndUserId(final Long userId, final Long orderId) { final Order order = getOrderByOrderIdAndUserId(orderId, userId); diff --git a/src/main/java/com/prgrms/nabmart/domain/payment/service/PaymentService.java b/src/main/java/com/prgrms/nabmart/domain/payment/service/PaymentService.java index 916da618..d8eeb1d6 100644 --- a/src/main/java/com/prgrms/nabmart/domain/payment/service/PaymentService.java +++ b/src/main/java/com/prgrms/nabmart/domain/payment/service/PaymentService.java @@ -3,6 +3,7 @@ import com.prgrms.nabmart.domain.order.Order; import com.prgrms.nabmart.domain.order.OrderStatus; import com.prgrms.nabmart.domain.order.exception.NotPayingOrderException; +import com.prgrms.nabmart.domain.order.service.OrderCancelService; import com.prgrms.nabmart.domain.order.service.OrderService; import com.prgrms.nabmart.domain.payment.Payment; import com.prgrms.nabmart.domain.payment.PaymentStatus; @@ -23,6 +24,7 @@ public class PaymentService { private final PaymentRepository paymentRepository; private final OrderService orderService; + private final OrderCancelService orderCancelService; @Value("${payment.toss.success-url}") private String successCallBackUrl; @@ -134,7 +136,7 @@ public PaymentResponse processFailPayment(Long userId, String uuid, String error Order order = getOrderByUuidAndUserId(uuid, userId); validateOrderStatusWithPaying(order); - orderService.cancelOrder(order); + orderCancelService.cancelOrder(order); return new PaymentResponse(payment.getPaymentStatus().toString(), errorMessage); } diff --git a/src/test/java/com/prgrms/nabmart/domain/order/service/OrderCancelServiceTest.java b/src/test/java/com/prgrms/nabmart/domain/order/service/OrderCancelServiceTest.java new file mode 100644 index 00000000..3a40bc36 --- /dev/null +++ b/src/test/java/com/prgrms/nabmart/domain/order/service/OrderCancelServiceTest.java @@ -0,0 +1,51 @@ +package com.prgrms.nabmart.domain.order.service; + +import static com.prgrms.nabmart.domain.order.support.OrderFixture.pendingOrder; +import static com.prgrms.nabmart.domain.user.support.UserFixture.user; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import com.prgrms.nabmart.domain.item.repository.ItemRepository; +import com.prgrms.nabmart.domain.order.Order; +import com.prgrms.nabmart.domain.order.OrderItem; +import com.prgrms.nabmart.domain.order.OrderStatus; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class OrderCancelServiceTest { + + @InjectMocks + OrderCancelService orderCancelService; + + @Mock + ItemRepository itemRepository; + + @Nested + @DisplayName("cancelOrder 메서드 실행 시") + class CancelOrderTest { + + @Test + @DisplayName("성공") + void success() { + // given + Order order = pendingOrder(1L, user()); + OrderItem orderItem = order.getOrderItems().get(0); + + // when + orderCancelService.cancelOrder(order); + + // then + assertThat(order.getStatus()).isEqualTo(OrderStatus.CANCELED); + verify(itemRepository, times(1)).increaseQuantity(orderItem.getItem().getItemId(), + orderItem.getQuantity()); + } + } + +} diff --git a/src/test/java/com/prgrms/nabmart/domain/payment/service/PaymentServiceTest.java b/src/test/java/com/prgrms/nabmart/domain/payment/service/PaymentServiceTest.java index 17ab4fda..2b262c8c 100644 --- a/src/test/java/com/prgrms/nabmart/domain/payment/service/PaymentServiceTest.java +++ b/src/test/java/com/prgrms/nabmart/domain/payment/service/PaymentServiceTest.java @@ -19,6 +19,7 @@ import com.prgrms.nabmart.domain.coupon.exception.InvalidUsedCouponException; import com.prgrms.nabmart.domain.order.Order; import com.prgrms.nabmart.domain.order.exception.NotPayingOrderException; +import com.prgrms.nabmart.domain.order.service.OrderCancelService; import com.prgrms.nabmart.domain.order.service.OrderService; import com.prgrms.nabmart.domain.payment.Payment; import com.prgrms.nabmart.domain.payment.PaymentStatus; @@ -51,6 +52,9 @@ class PaymentServiceTest { @Mock OrderService orderService; + @Mock + OrderCancelService orderCancelService; + @Value("${payment.toss.success_url}") private String successCallBackUrl; @@ -288,7 +292,7 @@ void success() { assertThat(payment.getPaymentStatus()).isEqualTo(PaymentStatus.FAILED); assertThat(result).usingRecursiveComparison().isEqualTo(expected); - verify(orderService, times(1)).cancelOrder(order); + verify(orderCancelService, times(1)).cancelOrder(order); } } From 0745a849d07178362c9f19680877f319fcb08b2e Mon Sep 17 00:00:00 2001 From: pushedrumex Date: Wed, 17 Apr 2024 16:53:38 +0900 Subject: [PATCH 2/2] =?UTF-8?q?chore:=20CODEOWNERS=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/CODEOWNERS | 1 - 1 file changed, 1 deletion(-) delete mode 100644 .github/CODEOWNERS diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS deleted file mode 100644 index d2e786fe..00000000 --- a/.github/CODEOWNERS +++ /dev/null @@ -1 +0,0 @@ -*.java @pushedrumex @funnysunny08 @seongHyun-Min @Seongju-Lee @bjo6300 @hseong3243