Skip to content

Commit

Permalink
hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
hongdosan authored Jul 3, 2024
1 parent b233ca8 commit 5a03927
Showing 1 changed file with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -50,20 +49,18 @@ public void issue() {
String couponName = coupon.getName();
int maxCount = coupon.getMaxCount();
int currentCount = couponManageRepository.getCount(couponName);
Set<Long> 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<Long> 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);
}
Expand All @@ -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);
}
}
}

0 comments on commit 5a03927

Please sign in to comment.