-
Notifications
You must be signed in to change notification settings - Fork 0
feat(item): enforce purchase limits #54
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
Open
popeye0618
wants to merge
1
commit into
feat/product-catalog-bundle
Choose a base branch
from
feat/item-purchase-limits
base: feat/product-catalog-bundle
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
item-service/src/main/java/com/comatching/item/domain/product/dto/PurchaseLimitResponse.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.comatching.item.domain.product.dto; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import com.comatching.common.domain.enums.ItemType; | ||
|
|
||
| public record PurchaseLimitResponse( | ||
| List<ItemPurchaseLimitResponse> limits | ||
| ) { | ||
| public record ItemPurchaseLimitResponse( | ||
| ItemType itemType, | ||
| String itemName, | ||
| long ownedQuantity, | ||
| long pendingQuantity, | ||
| int maxQuantity, | ||
| long remainingQuantity, | ||
| boolean purchasable | ||
| ) { | ||
| public static ItemPurchaseLimitResponse of( | ||
| ItemType itemType, | ||
| long ownedQuantity, | ||
| long pendingQuantity, | ||
| int maxQuantity | ||
| ) { | ||
| long remainingQuantity = Math.max(0, maxQuantity - ownedQuantity - pendingQuantity); | ||
| return new ItemPurchaseLimitResponse( | ||
| itemType, | ||
| itemType.getName(), | ||
| ownedQuantity, | ||
| pendingQuantity, | ||
| maxQuantity, | ||
| remainingQuantity, | ||
| remainingQuantity > 0 | ||
| ); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,28 @@ | ||
| package com.comatching.item.domain.product.service; | ||
|
|
||
| import java.time.LocalDateTime; | ||
| import java.util.EnumMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
| import org.springframework.util.StringUtils; | ||
|
|
||
| import com.comatching.common.annotation.DistributedLock; | ||
| import com.comatching.common.domain.enums.ItemType; | ||
| import com.comatching.common.dto.member.OrdererInfoDto; | ||
| import com.comatching.common.exception.BusinessException; | ||
| import com.comatching.item.domain.item.repository.ItemRepository; | ||
| import com.comatching.item.domain.order.entity.Order; | ||
| import com.comatching.item.domain.order.entity.OrderItem; | ||
| import com.comatching.item.domain.order.repository.OrderRepository; | ||
| import com.comatching.item.domain.order.service.OrderOutboxService; | ||
| import com.comatching.item.domain.product.dto.ProductResponse; | ||
| import com.comatching.item.domain.product.dto.PurchaseLimitResponse; | ||
| import com.comatching.item.domain.product.dto.PurchasePendingStatusResponse; | ||
| import com.comatching.item.domain.product.entity.Product; | ||
| import com.comatching.item.domain.product.entity.ProductReward; | ||
| import com.comatching.item.domain.product.repository.ProductRepository; | ||
| import com.comatching.item.global.exception.ItemErrorCode; | ||
| import com.comatching.item.global.exception.PaymentErrorCode; | ||
|
|
@@ -30,9 +36,18 @@ | |
| public class ShopServiceImpl implements ShopService { | ||
|
|
||
| private static final int ORDER_EXPIRE_MINUTES = 10; | ||
| private static final Map<ItemType, Integer> PURCHASE_LIMITS = Map.of( | ||
| ItemType.MATCHING_TICKET, 30, | ||
| ItemType.OPTION_TICKET, 90 | ||
| ); | ||
| private static final List<ItemType> LIMITED_ITEM_TYPES = List.of( | ||
| ItemType.MATCHING_TICKET, | ||
| ItemType.OPTION_TICKET | ||
| ); | ||
|
|
||
| private final ProductRepository productRepository; | ||
| private final OrderRepository orderRepository; | ||
| private final ItemRepository itemRepository; | ||
| private final UserOrderClient userOrderClient; | ||
| private final OrderOutboxService orderOutboxService; | ||
|
|
||
|
|
@@ -62,6 +77,8 @@ public void requestPurchase(Long memberId, Long productId) { | |
| throw new BusinessException(PaymentErrorCode.PENDING_REQUEST_ALREADY_EXISTS); | ||
| } | ||
|
|
||
| validatePurchaseLimit(memberId, product, now); | ||
|
|
||
| OrdererInfoDto ordererInfo = userOrderClient.getOrdererInfo(memberId); | ||
| String realName = normalizeRequiredText(ordererInfo.realName(), PaymentErrorCode.REAL_NAME_REQUIRED); | ||
| String username = normalizeRequiredText(ordererInfo.nickname(), PaymentErrorCode.USERNAME_REQUIRED); | ||
|
|
@@ -95,6 +112,48 @@ public PurchasePendingStatusResponse getMyPurchaseRequestStatus(Long memberId) { | |
| return hasPendingRequest ? PurchasePendingStatusResponse.pending() : PurchasePendingStatusResponse.none(); | ||
| } | ||
|
|
||
| @Override | ||
| @Transactional(readOnly = true) | ||
| public PurchaseLimitResponse getMyPurchaseLimits(Long memberId) { | ||
| LocalDateTime now = LocalDateTime.now(); | ||
| return new PurchaseLimitResponse( | ||
| LIMITED_ITEM_TYPES.stream() | ||
| .map(itemType -> PurchaseLimitResponse.ItemPurchaseLimitResponse.of( | ||
| itemType, | ||
| itemRepository.sumUsableQuantityByMemberIdAndItemType(memberId, itemType), | ||
| orderRepository.sumActivePendingQuantityByMemberIdAndItemType(memberId, itemType, now), | ||
| PURCHASE_LIMITS.get(itemType) | ||
| )) | ||
| .toList() | ||
|
Comment on lines
+120
to
+127
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| ); | ||
| } | ||
|
|
||
| private void validatePurchaseLimit(Long memberId, Product product, LocalDateTime now) { | ||
| Map<ItemType, Integer> requestedQuantityByType = getRequestedQuantityByType(product); | ||
|
|
||
| for (ItemType itemType : LIMITED_ITEM_TYPES) { | ||
| int requestedQuantity = requestedQuantityByType.getOrDefault(itemType, 0); | ||
| if (requestedQuantity == 0) { | ||
| continue; | ||
| } | ||
|
|
||
| long ownedQuantity = itemRepository.sumUsableQuantityByMemberIdAndItemType(memberId, itemType); | ||
| long pendingQuantity = orderRepository.sumActivePendingQuantityByMemberIdAndItemType(memberId, itemType, now); | ||
| int maxQuantity = PURCHASE_LIMITS.get(itemType); | ||
| if (ownedQuantity + pendingQuantity + requestedQuantity > maxQuantity) { | ||
| throw new BusinessException(PaymentErrorCode.PURCHASE_LIMIT_EXCEEDED); | ||
| } | ||
| } | ||
|
Comment on lines
+134
to
+146
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
|
|
||
| private Map<ItemType, Integer> getRequestedQuantityByType(Product product) { | ||
| Map<ItemType, Integer> requestedQuantityByType = new EnumMap<>(ItemType.class); | ||
| for (ProductReward reward : product.getRewards()) { | ||
| requestedQuantityByType.merge(reward.getItemType(), reward.getQuantity(), Integer::sum); | ||
| } | ||
|
Comment on lines
+151
to
+153
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return requestedQuantityByType; | ||
| } | ||
|
|
||
| private String normalizeRequiredText(String value, PaymentErrorCode errorCode) { | ||
| if (!StringUtils.hasText(value)) { | ||
| throw new BusinessException(errorCode); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
구매 한도 정책(
PURCHASE_LIMITS,LIMITED_ITEM_TYPES)이 서비스 클래스 내부에 하드코딩되어 있습니다. Repository Style Guide 3.1절에 따라 이러한 설정값은@ConfigurationProperties를 사용하여 외부 설정(yml)으로 관리하는 것을 권장합니다. 또한LIMITED_ITEM_TYPES는PURCHASE_LIMITS.keySet()으로 대체하여 중복 관리를 제거할 수 있습니다.References