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 cadf613c..62660f67 100644 --- a/src/main/java/com/moabam/api/application/coupon/CouponManageService.java +++ b/src/main/java/com/moabam/api/application/coupon/CouponManageService.java @@ -13,7 +13,6 @@ 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; @@ -49,18 +48,20 @@ public void issue() { String couponName = coupon.getName(); int maxCount = coupon.getMaxCount(); int currentCount = couponManageRepository.getCount(couponName); - - if (maxCount <= currentCount) { - return; - } - Set membersId = couponManageRepository.rangeQueue(couponName, currentCount, currentCount + ISSUE_SIZE); - if (membersId.isEmpty()) { + if (membersId == null || membersId.isEmpty()) { return; } for (Long memberId : membersId) { + int rank = couponManageRepository.rankQueue(couponName, memberId); + + if (maxCount <= rank) { + notificationService.sendCouponIssueResult(memberId, couponName, FAIL_ISSUE_BODY); + continue; + } + couponWalletRepository.save(CouponWallet.create(memberId, coupon)); notificationService.sendCouponIssueResult(memberId, couponName, SUCCESS_ISSUE_BODY); } @@ -81,18 +82,12 @@ public void registerQueue(String couponName, Long memberId) { private void validateRegisterQueue(String couponName, Long memberId) { LocalDate now = clockHolder.date(); - Coupon coupon = couponCacheService.getByNameAndStartAt(couponName, now); + couponCacheService.getByNameAndStartAt(couponName, now); if (couponManageRepository.hasValue(couponName, memberId)) { 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); - } + notificationService.sendCouponIssueResult(memberId, couponName, FAIL_ISSUE_BODY); } }