77import com .jajaja .domain .cart .entity .Cart ;
88import com .jajaja .domain .cart .entity .CartProduct ;
99import com .jajaja .domain .cart .repository .CartProductRepository ;
10- import com .jajaja .domain .coupon .service .CouponCommonService ;
11- import com .jajaja .domain .member .entity .MemberCoupon ;
12- import com .jajaja .domain .member .repository .MemberCouponRepository ;
1310import com .jajaja .domain .product .entity .Product ;
1411import com .jajaja .domain .product .entity .ProductOption ;
1512import com .jajaja .domain .product .repository .ProductOptionRepository ;
@@ -38,8 +35,6 @@ public class CartCommandServiceImpl implements CartCommandService {
3835 private final ProductRepository productRepository ;
3936 private final TeamCommandRepository teamRepository ;
4037 private final ProductOptionRepository productOptionRepository ;
41- private final MemberCouponRepository memberCouponRepository ;
42- private final CouponCommonService couponCommonService ;
4338 private final ProductCommonService productCommonService ;
4439 private final CartCommonService cartCommonService ;
4540
@@ -49,7 +44,7 @@ public CartResponseDto addOrUpdateCartProduct(Long memberId, List<CartProductAdd
4944 List <CartProductResponseDto > items = request .stream ().map (req -> {
5045 log .info ("[CartCommandService] 사용자 {}의 장바구니에 아이템 {} 추가/수정" , memberId , req .productId ());
5146
52- CartOpterationContext context = prepareCartOperationContext (req .productId (), req .optionId ());
47+ CartOperationContext context = prepareCartOperationContext (req .productId (), req .optionId ());
5348
5449 Optional <CartProduct > existingItem = req .optionId () != null ? cartProductRepository .findByCartIdAndProductIdAndProductOptionId (cart .getId (), context .product ().getId (), context .productOption .getId ())
5550 : cartProductRepository .findByCartIdAndProductIdAndProductOptionIsNull (cart .getId (), context .product ().getId ());
@@ -73,11 +68,8 @@ public CartResponseDto addOrUpdateCartProduct(Long memberId, List<CartProductAdd
7368 return CartProductResponseDto .of (cartProduct , productCommonService .calculateDiscountedPrice (cartProduct .getUnitPrice (), cartProduct .getProduct ().getDiscountRate ()), isTeamAvailable );
7469 }).toList ();
7570
76- PriceInfoDto priceInfo = cart .getCoupon () != null ?
77- couponCommonService .calculateDiscount (cart , cart .getCoupon ()) :
78- PriceInfoDto .noDiscount (cart .calculateTotalAmount ());
71+ PriceInfoDto priceInfo = PriceInfoDto .noDiscount (cart .calculateTotalAmount ());
7972
80- revalidateAppliedCouponIfExists (cart );
8173 return CartConverter .toCartResponseDto (cart , items , priceInfo );
8274 }
8375
@@ -91,7 +83,6 @@ public void deleteCartProduct(Long memberId, Long productId, Long optionId) {
9183 } catch (IllegalArgumentException e ) {
9284 throw new CartHandler (ErrorStatus .CART_PRODUCT_NOT_FOUND );
9385 }
94- revalidateAppliedCouponIfExists (cart );
9586 }
9687
9788 @ Override
@@ -110,17 +101,15 @@ public void deleteCartProducts(Long memberId, List<Long> cartProductIds) {
110101
111102 cartProductRepository .deleteAll (cartProductsToDelete );
112103 cart .getCartProducts ().removeAll (cartProductsToDelete );
113-
114- revalidateAppliedCouponIfExists (cart );
115104 }
116105
117106 /**
118107 * 장바구니 내 아이템 명령 실행에 필요한 도메인 객체를 불러오는 과정입니다.
119108 * option null 처리 등 중복되는 로직 때문에 생성하였습니다.
120109 *
121- * @return Cart, Product, ProductOption을 포함한 CartOpterationContext
110+ * @return Cart, Product, ProductOption을 포함한 CartOperationContext
122111 * */
123- private CartOpterationContext prepareCartOperationContext (Long productId , Long optionId ) {
112+ private CartOperationContext prepareCartOperationContext (Long productId , Long optionId ) {
124113Product product = productRepository .findById (productId )
125114 .orElseThrow (() -> new CartHandler (ErrorStatus .PRODUCT_NOT_FOUND ));
126115
@@ -133,39 +122,8 @@ private CartOpterationContext prepareCartOperationContext(Long productId, Long o
133122 option = null ;
134123 }
135124
136- return new CartOpterationContext (product , option );
125+ return new CartOperationContext (product , option );
137126 }
138127
139- private record CartOpterationContext (Product product , ProductOption productOption ) {}
140-
141- /**
142- * 장바구니에 쿠폰이 적용되어 있는 경우 쿠폰 적용 조건을 재검증합니다.
143- * 조건에 맞지 않으면 쿠폰을 자동으로 해제합니다.
144- */
145- private void revalidateAppliedCouponIfExists (Cart cart ) {
146- if (cart .getCoupon () == null ) {
147- return ;
148- }
149-
150- try {
151- MemberCoupon memberCoupon = memberCouponRepository .findByMemberAndCoupon (cart .getMember (), cart .getCoupon ())
152- .orElse (null );
153-
154- if (memberCoupon == null ) {
155- log .warn ("[CartCommandService] 적용된 쿠폰의 MemberCoupon 정보를 찾을 수 없어 쿠폰을 해제합니다. cartId: {}, couponId: {}" ,
156- cart .getId (), cart .getCoupon ().getId ());
157- cart .removeCoupon ();
158- return ;
159- }
160-
161- couponCommonService .validateCouponEligibility (cart , cart .getCoupon ());
162- log .info ("[CartCommandService] 적용된 쿠폰이 여전히 유효합니다. cartId: {}, couponId: {}" ,
163- cart .getId (), cart .getCoupon ().getId ());
164-
165- } catch (Exception e ) {
166- log .warn ("[CartCommandService] 장바구니 변경으로 인해 쿠폰 조건이 맞지 않아 쿠폰을 해제합니다. cartId: {}, couponId: {}, reason: {}" ,
167- cart .getId (), cart .getCoupon ().getId (), e .getMessage ());
168- cart .removeCoupon ();
169- }
170- }
128+ private record CartOperationContext (Product product , ProductOption productOption ) {}
171129}
0 commit comments