Skip to content

Commit

Permalink
Merge pull request #115 from Yanol-Market/feature/58-chat-nego-price
Browse files Browse the repository at this point in the history
Feature/58 chat nego price
  • Loading branch information
dldmldlsy authored Jan 18, 2024
2 parents 64b097d + 767bf00 commit b3e7cf6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static site.goldenticket.common.response.ErrorCode.CHAT_ROOM_NOT_FOUND;
import static site.goldenticket.common.response.ErrorCode.INVALID_SENDER_TYPE;
import static site.goldenticket.common.response.ErrorCode.INVALID_USER_TYPE;
import static site.goldenticket.common.response.ErrorCode.NEGO_NOT_FOUND;
import static site.goldenticket.common.response.ErrorCode.ORDER_NOT_FOUND;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -11,6 +13,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import site.goldenticket.common.constants.OrderStatus;
import site.goldenticket.common.exception.CustomException;
import site.goldenticket.domain.chat.dto.ChatRequest;
import site.goldenticket.domain.chat.dto.ChatResponse;
Expand All @@ -23,6 +26,12 @@
import site.goldenticket.domain.chat.entity.SenderType;
import site.goldenticket.domain.chat.repository.ChatRepository;
import site.goldenticket.domain.chat.repository.ChatRoomRepository;
import site.goldenticket.domain.nego.entity.Nego;
import site.goldenticket.domain.nego.repository.NegoRepository;
import site.goldenticket.domain.nego.status.NegotiationStatus;
import site.goldenticket.domain.payment.model.Order;
import site.goldenticket.domain.payment.repository.OrderRepository;
import site.goldenticket.domain.product.constants.ProductStatus;
import site.goldenticket.domain.product.model.Product;
import site.goldenticket.domain.product.service.ProductService;
import site.goldenticket.domain.user.entity.User;
Expand All @@ -37,6 +46,8 @@ public class ChatService {
private final ChatRepository chatRepository;
private final ProductService productService;
private final UserService userService;
private final NegoRepository negoRepository;
private final OrderRepository orderRepository;

/***
* 채팅 생성
Expand All @@ -49,6 +60,7 @@ public ChatResponse createChat(ChatRequest chatRequest) {
&& !chatRequest.senderType().equals(SenderType.SELLER)) {
throw new CustomException(INVALID_SENDER_TYPE);
}
// *존재하는 채팅방 ID인지 확인하는 로직 추가 예정
Chat chat = Chat.builder()
.chatRoomId(chatRequest.chatRoomId())
.senderType(chatRequest.senderType())
Expand Down Expand Up @@ -105,7 +117,8 @@ public ChatRoomDetailResponse getChatRoomDetail(Long userId, Long chatRoomId) {
.roomName(product.getRoomName())
.receiverProfileImage(receiver.getImageUrl())
.receiverNickname(receiver.getNickname())
.price(product.getGoldenPrice()) // *네고 승인 시, 네고가격으로 수정하는 로직 추가 예정
//.price(getPriceOfChatRoom(buyerId, product.getId()))
.price(product.getGoldenPrice())
.productId(product.getId())
.productStatus(product.getProductStatus())
.build();
Expand Down Expand Up @@ -136,6 +149,38 @@ public ChatRoomDetailResponse getChatRoomDetail(Long userId, Long chatRoomId) {
.chatResponseList(chatResponseList).build();
}

/***
* 채팅방 상단 가격값 조회
* @param buyerId 구매자 ID
* @param productId 상품 ID
* @return 채팅방 상단에 띄울 가격값
*/
private Integer getPriceOfChatRoom(Long buyerId, Long productId) {
Product product = productService.getProduct(productId);
Integer price = product.getGoldenPrice();

Boolean existsNego = negoRepository.existsByUser_IdAndProduct_Id(buyerId, product.getId());
if (product.getProductStatus().equals(ProductStatus.SELLING) && existsNego) {
Nego nego = negoRepository.findFirstByUser_IdAndProduct_IdOrderByCreatedAtDesc(buyerId,
product.getId()).orElseThrow(() -> new CustomException(NEGO_NOT_FOUND));
if (nego.getStatus().equals(NegotiationStatus.NEGOTIATION_TIMEOUT)) {
price = nego.getPrice();
}
} else if (product.getProductStatus().equals(ProductStatus.RESERVED) && existsNego) {
Nego nego = negoRepository.findFirstByUser_IdAndProduct_IdOrderByCreatedAtDesc(buyerId,
product.getId()).orElseThrow(() -> new CustomException(NEGO_NOT_FOUND));
if (nego.getStatus().equals(NegotiationStatus.PAYMENT_PENDING)) {
price = nego.getPrice();
}
} else if (product.getProductStatus().equals(ProductStatus.SOLD_OUT)) {
Order order = orderRepository.findByProductIdAndStatus(product.getId(),
OrderStatus.COMPLETED_TRANSFER)
.orElseThrow(() -> new CustomException(ORDER_NOT_FOUND));
price = order.getPrice();
}
return price;
}

/***
* 채팅방 목록(거래내역) 조회
* @param userId 회원 ID
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package site.goldenticket.domain.payment.repository;

import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import site.goldenticket.common.constants.OrderStatus;
import site.goldenticket.domain.payment.model.Order;

public interface OrderRepository extends JpaRepository<Order, Long> {

Optional<Order> findByProductIdAndStatus(Long productId, OrderStatus orderStatus);
}

0 comments on commit b3e7cf6

Please sign in to comment.