From ecd4d84cb5cf15969462971685fff7ecbb43d4d8 Mon Sep 17 00:00:00 2001 From: HyuckJuneHong Date: Wed, 3 Jul 2024 20:10:39 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=EB=A6=AC=EB=B7=B0=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../coupon/CouponManageService.java | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 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 62660f67..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; @@ -48,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); } @@ -82,12 +81,18 @@ public void registerQueue(String couponName, Long memberId) { 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)) { throw new ConflictException(ErrorMessage.CONFLICT_COUPON_ISSUE); } - notificationService.sendCouponIssueResult(memberId, couponName, FAIL_ISSUE_BODY); + 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); + } } }