Skip to content

Commit

Permalink
Merge pull request #1 from pushedrumex/refactor
Browse files Browse the repository at this point in the history
refactor: 주문 스케줄러 리팩토링
  • Loading branch information
pushedrumex authored Apr 17, 2024
2 parents a6a27d2 + 0745a84 commit eecf23f
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 17 deletions.
1 change: 0 additions & 1 deletion .github/CODEOWNERS

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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())
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<OrderStatus> statusList = List.of(OrderStatus.PENDING, OrderStatus.PAYING);

List<Order> expiredOrders = orderRepository.findByStatusInBeforeExpiredTime(
expiredTime, statusList);

for (Order expirdeOrder : expiredOrders) {
updateItemQuantity(expirdeOrder);
expirdeOrder.updateOrderStatus(OrderStatus.CANCELED);
}
expiredOrders.forEach(orderCancelService::cancelOrder);
}

@Transactional
Expand All @@ -106,13 +103,6 @@ public void deleteOrder(final Long orderId, final Long userId) {
orderRepository.delete(order);
}

private static void updateItemQuantity(Order order) {
List<OrderItem> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,6 +52,9 @@ class PaymentServiceTest {
@Mock
OrderService orderService;

@Mock
OrderCancelService orderCancelService;

@Value("${payment.toss.success_url}")
private String successCallBackUrl;

Expand Down Expand Up @@ -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);

}
}
Expand Down

0 comments on commit eecf23f

Please sign in to comment.