Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OrderServiceplaceOrder] 재고 관련 로직 추가 #55

Merged

Conversation

daadaadaah
Copy link
Collaborator

@daadaadaah daadaadaah commented Jun 12, 2023

What

Comment

  • 결제 단계와 주문 데이터 저장 과 관련된 로직이 추가되면, 코드양이 많아질 것 같아서, 일단 재고량 확인 및 재고량 감소 로직만 구현해서 PR 올렸습니다.
  • 테스트 코드는 의존성 있는 PR들 Merge 되면 추가할 예정입니다!

@daadaadaah daadaadaah changed the title [WIP] placeOrder 재고 관련 로직 추가 placeOrder 재고 관련 로직 추가 Jun 16, 2023
@daadaadaah daadaadaah force-pushed the feat/add-placeOrder-in-OrderService branch from c387fc9 to 6af352a Compare June 17, 2023 08:07
@daadaadaah daadaadaah changed the title placeOrder 재고 관련 로직 추가 [WIP] placeOrder 재고 관련 로직 추가 Jun 18, 2023
@daadaadaah daadaadaah force-pushed the feat/add-placeOrder-in-OrderService branch from 6af352a to 6db71a8 Compare June 18, 2023 12:15
@daadaadaah daadaadaah changed the title [WIP] placeOrder 재고 관련 로직 추가 placeOrder 재고 관련 로직 추가 Jun 18, 2023
@daadaadaah daadaadaah changed the title placeOrder 재고 관련 로직 추가 [OrderServiceplaceOrder] 재고 관련 로직 추가 Jun 23, 2023
int currentInventory = inventoryCommandRepository.decreaseByAmount(key, realOrderQuantity); // TODO : 결제가 실패하여 재고량 다시 원상복귀해야할 때도, 분산락 걸어줘야 되나?

if(currentInventory == 0) {
// TODO : [품절 처리] Redis에 저장된 dealproducts 목록에서 딜 상품 상태 오픈 -> 품절로 변경

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dealproducts의 상품 상태를 특정 사용자의 placeOrder에서 진행하는 것이 좋은 workflow일까요?

Copy link
Collaborator Author

@daadaadaah daadaadaah Jun 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

일단 해당 로직은 삭제시켰는데, 제가 생각한 방법는 2가지 입니다. @f-lab-TJ 의견은 어떠신가요?
방법 1. DealService에서 dealproducts의 상품 상태를 변경하는 로직을 구현하고, 여기서 가져다 사용하는 방법
방법 2. 특정 시간(예: 10초 마다, 트리팩 높은 초기 10분은 그보다 짧게?) dealproducts의 상품 상태를 변경하는 AWS CloudWatch Events + AWS Lambda 만드는 방법

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

상품의 상태가 currentInventory에 의해 결정된다면, 상품의 상태를 따로 표현해야 할 이유가 있나요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

상품의 상태가 currentInventory의해서만 결정되는게 아니라 오픈 시간, 마감 시간 등 다른 필드에 의해서도 결정되어서 상품의 상태를 따로 표현했습니다. 재고량, 오픈 시간, 마감 시간 등을 프론트에 내려주고, 그 숫자를 기반으로 프론트에서 상태를 표현하는 방법도 한가지 방법일 수 있겠지만, 백앤드에서 최대한 로직을 계산해서 프론트에는 값만 내려주는 상황을 가정했습니다. @f-lab-TJ 은 어떠신가요?

}

// 2. 재고량 감소
int currentInventory = inventoryCommandRepository.decreaseByAmount(key, realOrderQuantity); // TODO : 결제가 실패하여 재고량 다시 원상복귀해야할 때도, 분산락 걸어줘야 되나?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 함수가 호출 된 이후에 주문이 성공한 경우를 제외하고는 반드시 Amount를 복구시켜주어야 합니다. 그런 경우 어떤식으로 코드를 작성하면 좋을까요?

해당 함수에 의해 품절 처리가 되었으나, 결제가 완료되지 않거나 여러가지 이유로 다시 재고가 발생하는 케이스가 있습니다. 어떻게 처리하면 좋을까요?

Copy link
Collaborator Author

@daadaadaah daadaadaah Jun 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 함수가 호출 된 이후에 주문이 성공한 경우를 제외하고는 반드시 Amount를 복구시켜주어야 합니다. 그런 경우 어떤식으로 코드를 작성하면 좋을까요?

해당 함수에 의해 품절 처리가 되었으나, 결제가 완료되지 않거나 여러가지 이유로 다시 재고가 발생하는 케이스가 있습니다. 어떻게 처리하면 좋을까요?

  • 결제가 완료되지 않거나 여러가지 이유로 다시 재고가 발생하는 상황이 발생하면, 첫번째 질문에 대한 답변처럼 다시 재고량을 올려주는 로직을 추가할 것 같습니다. 추가로, DB에 저장된게 있다면 rollback 처리를 할 것 같고, 이를 위해서 @transactional 을 활용할 것 같습니다.

@daadaadaah daadaadaah force-pushed the feat/add-placeOrder-in-OrderService branch from ab4bb68 to 37a9859 Compare June 24, 2023 18:37

// 2. 재고량 감소
String key = "dealProductInventory:"+dealProductUuid.toString();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

�코드와 코드 사이에 언제 뉴라인을 넣고, 언제 넣지 않나요? 일반적으로 뉴라인은 강제되는 컨벤션은 아니지만, 코드 읽기에 도움을 주기도 방해를 하기도 합니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아래와 같이 함수가 간단한 거나 메서드 바로 밑의 코드를 제외하고는 모두 뉴라인을 넣어주려고 합니다.

    private void rollbackReducedInventory(String key, int amount) {
        inventoryCommandRepository.increaseByAmount(key, amount);
    }

@daadaadaah daadaadaah force-pushed the feat/add-placeOrder-in-OrderService branch from 37a9859 to c9b4cba Compare June 26, 2023 07:03
@daadaadaah daadaadaah force-pushed the feat/add-placeOrder-in-OrderService branch from c9b4cba to 3297982 Compare June 27, 2023 11:04
@daadaadaah daadaadaah merged commit 92eff2f into f-lab-edu:develop Jun 27, 2023
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants