Skip to content

Commit

Permalink
feat: 진행중인 주문서의 상태만 변경할 수 있도록 수정, 주문서 이벤트 리스너 클래스 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
hwihwi523 committed Sep 18, 2023
1 parent 0031f45 commit 7d26e1d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public enum ReceiptErrorCode implements ErrorCode {
NOT_EQUALS_MEMBER(BAD_REQUEST.value(), "R116", "다른 사용자의 주문서를 가지고 있습니다."),

NOT_RECEIPT_TYPE_FOUND(NOT_FOUND.value(), "R117", "주문서 상태가 존재하지 않습니다."),
UNMODIFIABLE_STATUS(BAD_REQUEST.value(), "R122", "진행중인 주문서만 변경할 수 있습니다."),

MEMBER_NO_INFORMATION(400, "R118", "회원 정보를 찾을 수 없습니다."),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ public void updateStatus(final ReceiptStatus receiptStatus) {
this.receiptStatus = receiptStatus;
}

public boolean isCompleted() {
return receiptStatus.getType().equals(ReceiptStatusType.COMPLETED);
public boolean isNotInProgress() {
return !receiptStatus.getType().equals(ReceiptStatusType.INPROGRESS);
}

public BigInteger getOriginalTotalPrice() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.woowacamp.soolsool.core.receipt.event.listener;

import com.woowacamp.soolsool.core.receipt.domain.vo.ReceiptStatusType;
import com.woowacamp.soolsool.core.receipt.event.ReceiptExpiredEvent;
import com.woowacamp.soolsool.core.receipt.service.ReceiptService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
@Slf4j
@RequiredArgsConstructor
public class ReceiptEventListener {

private final ReceiptService receiptService;

@Async
@EventListener
public void expireReceipt(final ReceiptExpiredEvent event) {
receiptService.modifyReceiptStatus(
event.getMemberId(),
event.getReceiptId(),
ReceiptStatusType.EXPIRED
);

log.info("Member {}'s Receipt {} Expired", event.getMemberId(), event.getReceiptId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.woowacamp.soolsool.core.receipt.domain.ReceiptStatus;
import com.woowacamp.soolsool.core.receipt.domain.vo.ReceiptStatusType;
import com.woowacamp.soolsool.core.receipt.dto.response.ReceiptDetailResponse;
import com.woowacamp.soolsool.core.receipt.event.ReceiptExpiredEvent;
import com.woowacamp.soolsool.core.receipt.repository.ReceiptRepository;
import com.woowacamp.soolsool.core.receipt.repository.ReceiptStatusCache;
import com.woowacamp.soolsool.core.receipt.repository.redisson.ReceiptRedisRepository;
Expand All @@ -23,16 +22,12 @@
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Slf4j
@RequiredArgsConstructor
public class ReceiptService {

Expand Down Expand Up @@ -82,40 +77,25 @@ public void modifyReceiptStatus(
final Long receiptId,
final ReceiptStatusType receiptStatusType
) {
final Receipt receipt = getReceipt(receiptId);

if (!Objects.equals(receipt.getMemberId(), memberId)) {
throw new SoolSoolException(NOT_EQUALS_MEMBER);
}

receipt.updateStatus(
getReceiptStatus(receiptStatusType)
);
}

@Async
@EventListener
@Transactional
public void expireReceipt(final ReceiptExpiredEvent event) {
final RLock receiptLock = getReceiptLock(event.getReceiptId());
final RLock receiptLock = getReceiptLock(receiptId);

try {
receiptLock.tryLock(LOCK_WAIT_TIME, LOCK_LEASE_TIME, TimeUnit.SECONDS);

final Receipt expiredReceipt = getReceipt(event.getReceiptId());
final Receipt receipt = getReceipt(receiptId);

if (expiredReceipt.isCompleted()) {
return;
if (!Objects.equals(receipt.getMemberId(), memberId)) {
throw new SoolSoolException(NOT_EQUALS_MEMBER);
}

expiredReceipt.updateStatus(
getReceiptStatus(ReceiptStatusType.EXPIRED)
);
if (receipt.isNotInProgress()) {
throw new SoolSoolException(ReceiptErrorCode.UNMODIFIABLE_STATUS);
}

log.info("Member {}'s Receipt {} Expired", event.getMemberId(), event.getReceiptId());
receipt.updateStatus(
getReceiptStatus(receiptStatusType)
);
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();

throw new SoolSoolException(ReceiptErrorCode.INTERRUPTED_THREAD);
} finally {
unlock(receiptLock);
Expand Down

0 comments on commit 7d26e1d

Please sign in to comment.