From 312cc29886d964bb490d9bd7707412826e6fcc2c Mon Sep 17 00:00:00 2001 From: Chan Jin Date: Tue, 24 Jan 2023 20:51:04 +0900 Subject: [PATCH] =?UTF-8?q?fix=20:=20=EC=A3=BC=EB=AC=B8=EC=99=84=EB=A3=8C,?= =?UTF-8?q?=EC=B2=A0=ED=9A=8C=20=EC=8B=9C=20=ED=8B=B0=EC=BC=93=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1,=20=EC=9E=AC=EA=B3=A0=20=EA=B4=80=EB=A6=AC=20?= =?UTF-8?q?=EA=B4=80=EB=A0=A8=20=EC=84=B8=EC=85=98=20=EC=98=A4=EB=A5=98=20?= =?UTF-8?q?=ED=95=B4=EA=B2=B0=20(#188)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix : 티켓 생성시 세션 초기화 안된 문제 해결 * fix : WithDrawOrderEvent 리스닝 클래스 정확하게 변경 * refactor : doneorder, withdraworder event itemId 추가 * style : spotless * test : 테스트 돌아가게 수정 , item id 관련 --- .../common/events/order/DoneOrderEvent.java | 3 ++ .../events/order/WithDrawOrderEvent.java | 2 + .../service/IssuedTicketDomainService.java | 48 ++++++++++--------- .../service/handlers/OrderEventHandler.java | 18 ++----- .../handlers/WithDrawOrderEventHandler.java | 14 ++---- .../domain/domains/order/domain/Order.java | 4 ++ ...rderApproveServiceConcurrencyFailTest.java | 4 ++ .../OrderApproveServiceConcurrencyTest.java | 5 +- .../service/WithdrawOrderServiceTest.java | 10 +++- 9 files changed, 61 insertions(+), 47 deletions(-) diff --git a/DuDoong-Domain/src/main/java/band/gosrock/domain/common/events/order/DoneOrderEvent.java b/DuDoong-Domain/src/main/java/band/gosrock/domain/common/events/order/DoneOrderEvent.java index 5dcc80d2..2492d071 100644 --- a/DuDoong-Domain/src/main/java/band/gosrock/domain/common/events/order/DoneOrderEvent.java +++ b/DuDoong-Domain/src/main/java/band/gosrock/domain/common/events/order/DoneOrderEvent.java @@ -20,12 +20,15 @@ public class DoneOrderEvent extends DomainEvent { @Nullable private final String paymentKey; + private final Long itemId; + public static DoneOrderEvent from(Order order) { return DoneOrderEvent.builder() .orderMethod(order.getOrderMethod()) .paymentKey(order.isNeedPaid() ? order.getPaymentKey() : null) .userId(order.getUserId()) .orderUuid(order.getUuid()) + .itemId(order.getItemId()) .build(); } } diff --git a/DuDoong-Domain/src/main/java/band/gosrock/domain/common/events/order/WithDrawOrderEvent.java b/DuDoong-Domain/src/main/java/band/gosrock/domain/common/events/order/WithDrawOrderEvent.java index 21ba5038..5cf7ded2 100644 --- a/DuDoong-Domain/src/main/java/band/gosrock/domain/common/events/order/WithDrawOrderEvent.java +++ b/DuDoong-Domain/src/main/java/band/gosrock/domain/common/events/order/WithDrawOrderEvent.java @@ -19,6 +19,7 @@ public class WithDrawOrderEvent extends DomainEvent { private final OrderStatus orderStatus; @Nullable private final String paymentKey; + private final Long itemId; public static WithDrawOrderEvent from(Order order) { return WithDrawOrderEvent.builder() @@ -27,6 +28,7 @@ public static WithDrawOrderEvent from(Order order) { .userId(order.getUserId()) .orderUuid(order.getUuid()) .orderStatus(order.getOrderStatus()) + .itemId(order.getItemId()) .build(); } } diff --git a/DuDoong-Domain/src/main/java/band/gosrock/domain/domains/issuedTicket/service/IssuedTicketDomainService.java b/DuDoong-Domain/src/main/java/band/gosrock/domain/domains/issuedTicket/service/IssuedTicketDomainService.java index ce2776ce..dfd53091 100644 --- a/DuDoong-Domain/src/main/java/band/gosrock/domain/domains/issuedTicket/service/IssuedTicketDomainService.java +++ b/DuDoong-Domain/src/main/java/band/gosrock/domain/domains/issuedTicket/service/IssuedTicketDomainService.java @@ -4,15 +4,15 @@ import band.gosrock.common.annotation.DomainService; import band.gosrock.domain.common.aop.redissonLock.RedissonLock; import band.gosrock.domain.common.vo.IssuedTicketInfoVo; -import band.gosrock.domain.domains.event.adaptor.EventAdaptor; import band.gosrock.domain.domains.event.exception.HostNotAuthEventException; import band.gosrock.domain.domains.issuedTicket.adaptor.IssuedTicketAdaptor; -import band.gosrock.domain.domains.issuedTicket.adaptor.IssuedTicketOptionAnswerAdaptor; import band.gosrock.domain.domains.issuedTicket.domain.IssuedTicket; import band.gosrock.domain.domains.issuedTicket.dto.request.CreateIssuedTicketDTO; import band.gosrock.domain.domains.issuedTicket.dto.response.CreateIssuedTicketResponse; -import band.gosrock.domain.domains.issuedTicket.repository.IssuedTicketRepository; -import band.gosrock.domain.domains.ticket_item.domain.TicketItem; +import band.gosrock.domain.domains.order.adaptor.OrderAdaptor; +import band.gosrock.domain.domains.order.domain.Order; +import band.gosrock.domain.domains.user.adaptor.UserAdaptor; +import band.gosrock.domain.domains.user.domain.User; import java.util.List; import java.util.Objects; import lombok.RequiredArgsConstructor; @@ -22,27 +22,13 @@ @Transactional(readOnly = true) @RequiredArgsConstructor public class IssuedTicketDomainService { - - private final IssuedTicketRepository issuedTicketRepository; private final IssuedTicketAdaptor issuedTicketAdaptor; - private final IssuedTicketOptionAnswerAdaptor issuedTicketOptionAnswerAdaptor; - private final EventAdaptor eventAdaptor; - - @RedissonLock(LockName = "티켓재고관리", paramClassType = TicketItem.class, identifier = "id") - @Transactional - public void createIssuedTicket( - TicketItem ticketItem, List createIssuedTicketDTOs) { - createIssuedTicketDTOs.forEach( - dto -> { - CreateIssuedTicketResponse responseDTO = - IssuedTicket.orderLineItemToIssuedTickets(dto); - issuedTicketAdaptor.saveAll(responseDTO.getIssuedTickets()); - }); - } + private final UserAdaptor userAdaptor; + private final OrderAdaptor orderAdaptor; - @RedissonLock(LockName = "티켓재고관리", paramClassType = TicketItem.class, identifier = "id") + @RedissonLock(LockName = "티켓재고관리", identifier = "itemId") @Transactional - public void withDrawIssuedTicket(TicketItem ticketItem, List issuedTickets) { + public void withDrawIssuedTicket(Long itemId, List issuedTickets) { issuedTickets.forEach( issuedTicket -> { issuedTicket.getTicketItem().increaseQuantity(1L); @@ -60,4 +46,22 @@ public IssuedTicketInfoVo processingEntranceIssuedTicket( issuedTicket.entrance(); return issuedTicket.toIssuedTicketInfoVo(); } + + @RedissonLock(LockName = "티켓재고관리", identifier = "itemId") + public void createIssuedTicket(Long itemId, String orderUuid, Long userId) { + User user = userAdaptor.queryUser(userId); + Order order = orderAdaptor.findByOrderUuid(orderUuid); + + List createIssuedTicketDTOS = + order.getOrderLineItems().stream() + .map(orderLineItem -> new CreateIssuedTicketDTO(order, orderLineItem, user)) + .toList(); + + createIssuedTicketDTOS.forEach( + dto -> { + CreateIssuedTicketResponse responseDTO = + IssuedTicket.orderLineItemToIssuedTickets(dto); + issuedTicketAdaptor.saveAll(responseDTO.getIssuedTickets()); + }); + } } diff --git a/DuDoong-Domain/src/main/java/band/gosrock/domain/domains/issuedTicket/service/handlers/OrderEventHandler.java b/DuDoong-Domain/src/main/java/band/gosrock/domain/domains/issuedTicket/service/handlers/OrderEventHandler.java index 4cd84c43..4fa19472 100644 --- a/DuDoong-Domain/src/main/java/band/gosrock/domain/domains/issuedTicket/service/handlers/OrderEventHandler.java +++ b/DuDoong-Domain/src/main/java/band/gosrock/domain/domains/issuedTicket/service/handlers/OrderEventHandler.java @@ -2,13 +2,8 @@ import band.gosrock.domain.common.events.order.DoneOrderEvent; -import band.gosrock.domain.domains.issuedTicket.dto.request.CreateIssuedTicketDTO; import band.gosrock.domain.domains.issuedTicket.service.IssuedTicketDomainService; import band.gosrock.domain.domains.order.adaptor.OrderAdaptor; -import band.gosrock.domain.domains.order.domain.Order; -import band.gosrock.domain.domains.user.adaptor.UserAdaptor; -import band.gosrock.domain.domains.user.domain.User; -import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -22,8 +17,6 @@ public class OrderEventHandler { private final IssuedTicketDomainService issuedTicketDomainService; - private final UserAdaptor userAdaptor; - private final OrderAdaptor orderAdaptor; @TransactionalEventListener( @@ -31,13 +24,10 @@ public class OrderEventHandler { phase = TransactionPhase.BEFORE_COMMIT) public void handleDoneOrderEvent(DoneOrderEvent doneOrderEvent) { log.info(doneOrderEvent.getOrderUuid() + "주문 상태 완료, 티켓 생성작업 진행"); - User user = userAdaptor.queryUser(doneOrderEvent.getUserId()); - Order order = orderAdaptor.findByOrderUuid(doneOrderEvent.getOrderUuid()); - List createIssuedTicketDTOS = - order.getOrderLineItems().stream() - .map(orderLineItem -> new CreateIssuedTicketDTO(order, orderLineItem, user)) - .toList(); - issuedTicketDomainService.createIssuedTicket(order.getItem(), createIssuedTicketDTOS); + issuedTicketDomainService.createIssuedTicket( + doneOrderEvent.getItemId(), + doneOrderEvent.getOrderUuid(), + doneOrderEvent.getUserId()); log.info(doneOrderEvent.getOrderUuid() + "주문 상태 완료, 티켓 생성작업 완료"); } } diff --git a/DuDoong-Domain/src/main/java/band/gosrock/domain/domains/issuedTicket/service/handlers/WithDrawOrderEventHandler.java b/DuDoong-Domain/src/main/java/band/gosrock/domain/domains/issuedTicket/service/handlers/WithDrawOrderEventHandler.java index 70837b1c..621fc3a4 100644 --- a/DuDoong-Domain/src/main/java/band/gosrock/domain/domains/issuedTicket/service/handlers/WithDrawOrderEventHandler.java +++ b/DuDoong-Domain/src/main/java/band/gosrock/domain/domains/issuedTicket/service/handlers/WithDrawOrderEventHandler.java @@ -5,8 +5,6 @@ import band.gosrock.domain.domains.issuedTicket.adaptor.IssuedTicketAdaptor; import band.gosrock.domain.domains.issuedTicket.domain.IssuedTicket; import band.gosrock.domain.domains.issuedTicket.service.IssuedTicketDomainService; -import band.gosrock.domain.domains.order.adaptor.OrderAdaptor; -import band.gosrock.domain.domains.order.domain.Order; import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -23,17 +21,15 @@ public class WithDrawOrderEventHandler { private final IssuedTicketAdaptor issuedTicketAdaptor; - private final OrderAdaptor orderAdaptor; - @TransactionalEventListener( - classes = WithDrawOrderEventHandler.class, + classes = WithDrawOrderEvent.class, phase = TransactionPhase.BEFORE_COMMIT) public void handleWithDrawOrderEvent(WithDrawOrderEvent withDrawOrderEvent) { - log.info(withDrawOrderEvent.getOrderUuid() + "주문 상태 철회 , 티켓 제거 필요"); - Order order = orderAdaptor.findByOrderUuid(withDrawOrderEvent.getOrderUuid()); + log.info(withDrawOrderEvent.getOrderUuid() + "주문 상태 철회 , 티켓 철회 필요"); List issuedTickets = issuedTicketAdaptor.findAllByOrderUuid(withDrawOrderEvent.getOrderUuid()); - issuedTicketDomainService.withDrawIssuedTicket(order.getItem(), issuedTickets); - log.info(withDrawOrderEvent.getOrderUuid() + "주문 상태 완료, 티켓 생성작업 완료"); + issuedTicketDomainService.withDrawIssuedTicket( + withDrawOrderEvent.getItemId(), issuedTickets); + log.info(withDrawOrderEvent.getOrderUuid() + "주문 상태 완료, 티켓 철회 완료"); } } diff --git a/DuDoong-Domain/src/main/java/band/gosrock/domain/domains/order/domain/Order.java b/DuDoong-Domain/src/main/java/band/gosrock/domain/domains/order/domain/Order.java index 44750bab..8bac6e0c 100644 --- a/DuDoong-Domain/src/main/java/band/gosrock/domain/domains/order/domain/Order.java +++ b/DuDoong-Domain/src/main/java/band/gosrock/domain/domains/order/domain/Order.java @@ -332,6 +332,10 @@ public TicketItem getItem() { return getOrderLineItem().getTicketItem(); } + public Long getItemId() { + return getOrderLineItem().getTicketItem().getId(); + } + /** 주문에서 티켓 상품의 타입을 반환합니다. */ public TicketType getItemType() { return getItem().getType(); diff --git a/DuDoong-Domain/src/test/java/band/gosrock/domain/domains/order/service/OrderApproveServiceConcurrencyFailTest.java b/DuDoong-Domain/src/test/java/band/gosrock/domain/domains/order/service/OrderApproveServiceConcurrencyFailTest.java index a2c60783..ed323669 100644 --- a/DuDoong-Domain/src/test/java/band/gosrock/domain/domains/order/service/OrderApproveServiceConcurrencyFailTest.java +++ b/DuDoong-Domain/src/test/java/band/gosrock/domain/domains/order/service/OrderApproveServiceConcurrencyFailTest.java @@ -13,6 +13,7 @@ import band.gosrock.domain.domains.order.domain.OrderLineItem; import band.gosrock.domain.domains.order.domain.OrderMethod; import band.gosrock.domain.domains.order.domain.OrderStatus; +import band.gosrock.domain.domains.ticket_item.domain.TicketItem; import java.util.List; import java.util.concurrent.atomic.AtomicLong; import lombok.extern.slf4j.Slf4j; @@ -31,6 +32,7 @@ class OrderApproveServiceConcurrencyFailTest { @Autowired OrderApproveService orderApproveService; @Mock OrderLineItem orderLineItem; + @Mock TicketItem ticketItem; @MockBean OrderAdaptor orderAdaptor; @@ -39,6 +41,8 @@ class OrderApproveServiceConcurrencyFailTest { @BeforeEach void setUp() { given(orderLineItem.isNeedPaid()).willReturn(Boolean.FALSE); + given(orderLineItem.getTicketItem()).willReturn(ticketItem); + given(ticketItem.getId()).willReturn(1L); order = Order.builder() .orderMethod(OrderMethod.APPROVAL) diff --git a/DuDoong-Domain/src/test/java/band/gosrock/domain/domains/order/service/OrderApproveServiceConcurrencyTest.java b/DuDoong-Domain/src/test/java/band/gosrock/domain/domains/order/service/OrderApproveServiceConcurrencyTest.java index 1945e5fc..fd613706 100644 --- a/DuDoong-Domain/src/test/java/band/gosrock/domain/domains/order/service/OrderApproveServiceConcurrencyTest.java +++ b/DuDoong-Domain/src/test/java/band/gosrock/domain/domains/order/service/OrderApproveServiceConcurrencyTest.java @@ -12,6 +12,7 @@ import band.gosrock.domain.domains.order.domain.OrderLineItem; import band.gosrock.domain.domains.order.domain.OrderMethod; import band.gosrock.domain.domains.order.domain.OrderStatus; +import band.gosrock.domain.domains.ticket_item.domain.TicketItem; import java.util.List; import java.util.concurrent.atomic.AtomicLong; import lombok.extern.slf4j.Slf4j; @@ -33,14 +34,16 @@ class OrderApproveServiceConcurrencyTest { @Autowired RedissonClient redissonClient; @Mock OrderLineItem orderLineItem; - @MockBean private OrderAdaptor orderAdaptor; + @Mock TicketItem ticketItem; Order order; @BeforeEach void setUp() { given(orderLineItem.isNeedPaid()).willReturn(Boolean.FALSE); + given(orderLineItem.getTicketItem()).willReturn(ticketItem); + given(ticketItem.getId()).willReturn(1L); order = Order.builder() .orderMethod(OrderMethod.APPROVAL) diff --git a/DuDoong-Domain/src/test/java/band/gosrock/domain/domains/order/service/WithdrawOrderServiceTest.java b/DuDoong-Domain/src/test/java/band/gosrock/domain/domains/order/service/WithdrawOrderServiceTest.java index 2ce37b2c..85a3acef 100644 --- a/DuDoong-Domain/src/test/java/band/gosrock/domain/domains/order/service/WithdrawOrderServiceTest.java +++ b/DuDoong-Domain/src/test/java/band/gosrock/domain/domains/order/service/WithdrawOrderServiceTest.java @@ -10,12 +10,15 @@ import band.gosrock.domain.DomainIntegrateSpringBootTest; import band.gosrock.domain.domains.order.adaptor.OrderAdaptor; import band.gosrock.domain.domains.order.domain.Order; +import band.gosrock.domain.domains.order.domain.OrderLineItem; import band.gosrock.domain.domains.order.domain.OrderStatus; +import band.gosrock.domain.domains.ticket_item.domain.TicketItem; import java.util.List; import java.util.concurrent.atomic.AtomicLong; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.mockito.Mock; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.mock.mockito.MockBean; @@ -27,6 +30,8 @@ class WithdrawOrderServiceTest { @Autowired WithdrawOrderService withdrawOrderService; @MockBean OrderAdaptor orderAdaptor; + @Mock OrderLineItem orderLineItem; + @Mock TicketItem ticketItem; Order order; @@ -38,10 +43,13 @@ void setUp() { Order.builder() .userId(userId) .orderStatus(OrderStatus.CONFIRM) - .orderLineItems(List.of()) + .orderLineItems(List.of(orderLineItem)) .build(); order.addUUID(); given(orderAdaptor.findByOrderUuid(any())).willReturn(order); + given(orderLineItem.getTicketItem()).willReturn(ticketItem); + given(orderLineItem.canRefund()).willReturn(Boolean.TRUE); + given(ticketItem.getId()).willReturn(1L); } @Test