2121import java .time .LocalDate ;
2222import java .util .List ;
2323import java .util .Map ;
24+ import java .util .concurrent .atomic .AtomicBoolean ;
2425import java .util .function .Function ;
2526import java .util .stream .Collectors ;
2627
27- import static com .commerce .shared .exception .BusinessError .DUPLICATE_ISSUED_COUPON ;
2828import static com .commerce .shared .exception .BusinessError .INVALID_COUPON ;
2929import static com .commerce .shared .kafka .event .topic .EventTopic .COUPON_ISSUE_TOPIC ;
3030
@@ -68,16 +68,10 @@ public List<CouponView> getMyCoupons(CustomerId customerId) {
6868
6969 @ Override
7070 public void issueCoupon (CouponId couponId , CustomerId customerId ) {
71- // 캐싱데이터 조회
72- if (isIssued (couponId , customerId )) return ;
73-
74- // db에서 발행여부 조회
75- CouponIssueId couponIssueId = new CouponIssueId (couponId , customerId );
76- couponIssueOutPort .findByCouponIssueId (couponIssueId )
77- .ifPresent (exist -> {
78- throw new BusinessException (DUPLICATE_ISSUED_COUPON );
79- });
80-
71+ // todo 큐이기 때문에 중복 요청이 올 수 있다. redis 검증로직이 필요하다.
72+ // todo 발행실패인 경우, 멱등성있게 처리하는 방법이 필요하다.
73+ // todo 테스트코드 수정
74+
8175 // 발행
8276 Coupon coupon = couponOutPort .findById (couponId )
8377 .orElseThrow (() -> new BusinessException (INVALID_COUPON ));
@@ -97,7 +91,22 @@ public void requestIssueCoupon(CouponId couponId, CustomerId customerId) {
9791 }
9892
9993 @ Override
100- public boolean isIssued (CouponId couponId , CustomerId customerId ) {
101- return couponIssueCache .isIssued (couponId , customerId );
94+ public boolean checkCouponIssueStatus (CouponId couponId , CustomerId customerId ) {
95+ // 쿠폰발행 캐싱 확인
96+ AtomicBoolean issued = new AtomicBoolean (couponIssueCache .isIssued (couponId , customerId ));
97+
98+ if (issued .get ()){
99+ return true ;
100+ }
101+
102+ // cache miss or 미발행
103+ CouponIssueId couponIssueId = new CouponIssueId (couponId , customerId );
104+ couponIssueOutPort .findByCouponIssueId (couponIssueId )
105+ .ifPresent (exist -> {
106+ couponIssueCache .save (couponId , customerId );
107+ issued .set (true );
108+ });
109+
110+ return issued .get ();
102111 }
103112}
0 commit comments