99import com .knoc .chat .entity .MessageType ;
1010import com .knoc .chat .service .ChatMessageService ;
1111import com .knoc .chat .service .ChatRoomService ;
12- import com .knoc .order .service .OrderService ;
13- import com .knoc .senior .SeniorProfileService ;
1412import io .swagger .v3 .oas .annotations .Operation ;
1513import io .swagger .v3 .oas .annotations .tags .Tag ;
1614import lombok .RequiredArgsConstructor ;
2321import org .springframework .web .bind .annotation .*;
2422
2523import java .security .Principal ;
26- import java .util .List ;
27- import java .util .Map ;
24+ import java .util .*;
2825
2926@ Tag (name = "Chat Controller" , description = "채팅방 목록, 상세 조회 및 메시지 관리 관련 API" )
3027@ Controller
@@ -34,8 +31,6 @@ public class ChatController {
3431
3532 private final ChatMessageService chatMessageService ;
3633 private final ChatRoomService chatRoomService ;
37- private final OrderService orderService ;
38- private final SeniorProfileService seniorProfileService ;
3934
4035 @ Value ("${toss.payments.client-key:}" )
4136 private String tossClientKey ;
@@ -53,36 +48,45 @@ public String getChatRoomsPage(Model model, Principal principal) {
5348
5449 return "chat/chatrooms" ;
5550 }
56-
5751 @ Operation (summary = "채팅방 생성" , description = "대상 시니어와 새로운 채팅방을 생성하고 해당 방으로 이동합니다." )
5852 @ PostMapping ("/rooms" )
5953 public String createChatRoom (Principal principal , @ RequestParam Long seniorId ) {
6054 ChatRoom chatRoom = chatRoomService .createChatRoom (principal .getName (), seniorId );
6155
6256 return "redirect:/chat/" + chatRoom .getId ();
6357 }
64-
6558 @ Operation (summary = "채팅방 상세 페이지 조회" , description = "선택한 채팅방의 정보, 메시지 내역, 결제 요청 상태 등을 포함한 상세 페이지를 조회합니다." )
6659 @ GetMapping ("/{roomId}" )
6760 public String getChatRoomPage (@ PathVariable ("roomId" ) Long roomId , Model model , Principal principal ) {
6861 ChatRoomDetailDto dto = chatRoomService .getRoomDetailInfo (roomId , principal .getName ());
6962 ChatRoom chatRoom = dto .selectedRoom ();
7063 List <ChatMessage > messages = dto .messages ();
7164
65+ // 현재 사용자의 역할 및 상대 주니어 ID 계산
7266 boolean isSenior = chatRoom .getSenior () != null
7367 && chatRoom .getSenior ().getEmail () != null
7468 && chatRoom .getSenior ().getEmail ().equals (principal .getName ());
7569 Long juniorId = chatRoom .getJunior () != null ? chatRoom .getJunior ().getId () : null ;
7670
71+ // 주니어 전용 시스템 메시지(REVIEW_REQUESTED)는 시니어 화면에서는 노출하지 않는다.
7772 if (isSenior && messages != null && !messages .isEmpty ()) {
7873 messages = messages .stream ()
7974 .filter (m -> m .getMessageType () != MessageType .REVIEW_REQUESTED )
8075 .toList ();
8176 }
8277
83- Map <Long , Integer > orderAmounts = orderService .extractOrderAmounts (messages );
84- boolean hasPaymentRequest = orderService .hasActivePaymentRequest (chatRoom );
85- int seniorPricePerReview = seniorProfileService .getPricePerReview (chatRoom .getSenior ().getId ());
78+ // PAYMENT_REQUESTED 메시지들의 금액 맵 구성 (orderId -> amount)
79+ Map <Long , Integer > orderAmounts = chatMessageService .buildOrderAmounts (messages );
80+
81+ // 해당 채팅방에 이미 결제 요청 시스템 메시지가 있었는지 여부
82+ // - 버튼 초기 노출 제어에만 사용
83+ // - DB에 Order가 남아있더라도, "결제 요청 메시지"가 없다면 버튼은 노출할 수 있다.
84+ boolean hasPaymentRequest = messages != null && messages .stream ()
85+ .anyMatch (m -> m .getMessageType () == MessageType .PAYMENT_REQUESTED );
86+
87+ // 이 채팅방 시니어의 등록 리뷰 단가 (결제 요청 모달 placeholder용)
88+ // 프로필 미등록/미설정 시 0 → 프런트에서 기본값으로 처리
89+ int seniorPricePerReview = dto .seniorPricePerReview ();
8690
8791 model .addAttribute ("selectedRoomId" , dto .selectedRoomId ());
8892 model .addAttribute ("messages" , messages );
@@ -102,7 +106,6 @@ public String getChatRoomPage(@PathVariable("roomId") Long roomId, Model model,
102106
103107 return "chat/chatrooms" ;
104108 }
105-
106109 @ Operation (summary = "메시지 전송 (WebSocket)" , description = "채팅방으로 메시지를 전송합니다. (WebSocket/STOMP)" )
107110 @ MessageMapping ("/{roomId}/send" )
108111 public void sendMessage (@ DestinationVariable Long roomId , @ Payload ChatMessageRequest request , Principal principal ) {
@@ -116,4 +119,4 @@ public void sendMessage(@DestinationVariable Long roomId, @Payload ChatMessageRe
116119 public List <ChatMessageResponse > getMessagesBefore (@ PathVariable ("roomId" ) Long roomId , @ RequestParam Long before , Principal principal ) {
117120 return chatMessageService .getPreviousMessages (roomId , before , principal .getName ());
118121 }
119- }
122+ }
0 commit comments