From 5a03927f26d8b74ec8a6b8c3a66ba21ffb4267d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=ED=99=8D=ED=98=81=EC=A4=80?= Date: Wed, 3 Jul 2024 20:42:30 +0900 Subject: [PATCH] hotfix --- .../coupon/CouponManageService.java | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/moabam/api/application/coupon/CouponManageService.java b/src/main/java/com/moabam/api/application/coupon/CouponManageService.java index c215d6d9..cadf613c 100644 --- a/src/main/java/com/moabam/api/application/coupon/CouponManageService.java +++ b/src/main/java/com/moabam/api/application/coupon/CouponManageService.java @@ -13,6 +13,7 @@ import com.moabam.api.domain.coupon.repository.CouponManageRepository; import com.moabam.api.domain.coupon.repository.CouponWalletRepository; import com.moabam.global.common.util.ClockHolder; +import com.moabam.global.error.exception.BadRequestException; import com.moabam.global.error.exception.ConflictException; import com.moabam.global.error.model.ErrorMessage; @@ -25,8 +26,6 @@ public class CouponManageService { private static final String SUCCESS_ISSUE_BODY = "%s 쿠폰 발행을 성공했습니다. 축하드립니다!"; - private static final String SUCCESS_PRE_ISSUE_BODY = "%s 쿠폰 발급 신청을 성공했습니다."; - private static final String CONFLICT_ISSUE_BODY = "%s 쿠폰 발급 중복 신청은 불가능합니다."; private static final String FAIL_ISSUE_BODY = "%s 쿠폰 발행을 실패했습니다. 다음 기회에!"; private static final long ISSUE_SIZE = 10; @@ -50,20 +49,18 @@ public void issue() { String couponName = coupon.getName(); int maxCount = coupon.getMaxCount(); int currentCount = couponManageRepository.getCount(couponName); - Set membersId = couponManageRepository.rangeQueue(couponName, currentCount, currentCount + ISSUE_SIZE); - if (membersId == null || membersId.isEmpty()) { + if (maxCount <= currentCount) { return; } - for (Long memberId : membersId) { - int rank = couponManageRepository.rankQueue(couponName, memberId); + Set membersId = couponManageRepository.rangeQueue(couponName, currentCount, currentCount + ISSUE_SIZE); - if (maxCount <= rank) { - notificationService.sendCouponIssueResult(memberId, couponName, FAIL_ISSUE_BODY); - continue; - } + if (membersId.isEmpty()) { + return; + } + for (Long memberId : membersId) { couponWalletRepository.save(CouponWallet.create(memberId, coupon)); notificationService.sendCouponIssueResult(memberId, couponName, SUCCESS_ISSUE_BODY); } @@ -80,16 +77,22 @@ public void registerQueue(String couponName, Long memberId) { double registerTime = System.currentTimeMillis(); validateRegisterQueue(couponName, memberId); couponManageRepository.addIfAbsentQueue(couponName, memberId, registerTime); - notificationService.sendCouponIssueResult(memberId, couponName, SUCCESS_PRE_ISSUE_BODY); } private void validateRegisterQueue(String couponName, Long memberId) { LocalDate now = clockHolder.date(); - couponCacheService.getByNameAndStartAt(couponName, now); + Coupon coupon = couponCacheService.getByNameAndStartAt(couponName, now); if (couponManageRepository.hasValue(couponName, memberId)) { - notificationService.sendCouponIssueResult(memberId, couponName, CONFLICT_ISSUE_BODY); throw new ConflictException(ErrorMessage.CONFLICT_COUPON_ISSUE); } + + int maxCount = coupon.getMaxCount(); + int sizeQueue = couponManageRepository.sizeQueue(couponName); + + if (maxCount <= sizeQueue) { + notificationService.sendCouponIssueResult(memberId, couponName, FAIL_ISSUE_BODY); + throw new BadRequestException(ErrorMessage.INVALID_COUPON_STOCK_END); + } } }