|
1 | 1 | package com.irum.come2us.domain.coupon.application.service; |
2 | 2 |
|
3 | 3 | import com.irum.come2us.domain.coupon.domain.entity.Coupon; |
| 4 | +import com.irum.come2us.domain.coupon.domain.repository.AppliedCouponRepository; |
4 | 5 | import com.irum.come2us.domain.coupon.domain.repository.CouponRepository; |
5 | 6 | import com.irum.come2us.domain.coupon.presentation.dto.request.CouponGenerateRequest; |
6 | 7 | import com.irum.come2us.domain.member.domain.entity.Member; |
7 | 8 | import com.irum.come2us.domain.member.domain.repository.MemberRepository; |
8 | 9 | import com.irum.come2us.global.presentation.advice.exception.CommonException; |
9 | 10 | import com.irum.come2us.global.presentation.advice.exception.errorcode.CouponErrorCode; |
10 | 11 | import com.irum.come2us.global.presentation.advice.exception.errorcode.MemberErrorCode; |
| 12 | +import java.time.LocalDateTime; |
11 | 13 | import java.util.List; |
12 | 14 | import java.util.UUID; |
13 | 15 | import lombok.RequiredArgsConstructor; |
|
20 | 22 | public class CouponService { |
21 | 23 | private final CouponRepository couponRepository; |
22 | 24 | private final MemberRepository memberRepository; |
| 25 | + private final AppliedCouponRepository appliedCouponRepository; |
23 | 26 |
|
24 | 27 | public void createCoupon(CouponGenerateRequest request, Long memberId) { |
25 | 28 |
|
@@ -51,4 +54,36 @@ public void deleteCoupon(UUID couponId, Long memberId) { |
51 | 54 | } |
52 | 55 | couponRepository.delete(coupon); |
53 | 56 | } |
| 57 | + |
| 58 | + /** 쿠폰 유효성 검증 및 할인 금액 계산 */ |
| 59 | + public int validAndCalCoupon(List<UUID> couponIdList, int calculatedTotalPrice, Member member) { |
| 60 | + if (couponIdList.isEmpty()) { |
| 61 | + return 0; |
| 62 | + } |
| 63 | + |
| 64 | + int totalDiscount = 0; |
| 65 | + List<Coupon> couponList = couponRepository.findAllById(couponIdList); |
| 66 | + |
| 67 | + for (Coupon coupon : couponList) { |
| 68 | + // 권한 검사 |
| 69 | + if (!coupon.getMember().getMemberId().equals(member.getMemberId())) { |
| 70 | + throw new CommonException(CouponErrorCode.COUPON_NO_PERMISSION); |
| 71 | + } |
| 72 | + // 만료일 검사 |
| 73 | + if (coupon.getExpiration().isBefore(LocalDateTime.now())) { |
| 74 | + throw new CommonException(CouponErrorCode.COUPON_EXPIRATION); |
| 75 | + } |
| 76 | + // 사용 여부 검사 |
| 77 | + if (appliedCouponRepository.existsByCouponId(coupon.getId())) { |
| 78 | + throw new CommonException(CouponErrorCode.COUPON_ALREADY_USED); |
| 79 | + } |
| 80 | + totalDiscount += coupon.getDiscountAmount(); |
| 81 | + } |
| 82 | + |
| 83 | + if (totalDiscount > calculatedTotalPrice) { |
| 84 | + totalDiscount = calculatedTotalPrice; |
| 85 | + } |
| 86 | + |
| 87 | + return totalDiscount; |
| 88 | + } |
54 | 89 | } |
0 commit comments