diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS deleted file mode 100644 index d2e786fe9..000000000 --- a/.github/CODEOWNERS +++ /dev/null @@ -1 +0,0 @@ -*.java @pushedrumex @funnysunny08 @seongHyun-Min @Seongju-Lee @bjo6300 @hseong3243 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 000000000..9c27f879e --- /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 3847f4eac..5a4a1ab5f 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 916da6183..d8eeb1d60 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 000000000..3a40bc361 --- /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 17ab4fdad..2b262c8cc 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); } }