diff --git a/src/main/java/com/example/mate/common/error/ErrorCode.java b/src/main/java/com/example/mate/common/error/ErrorCode.java index 1e7b7d0b..36c65b3e 100644 --- a/src/main/java/com/example/mate/common/error/ErrorCode.java +++ b/src/main/java/com/example/mate/common/error/ErrorCode.java @@ -130,6 +130,7 @@ public enum ErrorCode { ALREADY_JOINED_CHAT_ROOM(HttpStatus.BAD_REQUEST, "CHAT004", "이미 참여 중인 채팅방입니다."), CHAT_ROOM_CLOSED(HttpStatus.BAD_REQUEST, "CHAT005", "종료된 채팅방입니다."), CHAT_ROOM_ACCESS_DENIED(HttpStatus.FORBIDDEN, "CHAT006", "직관 완료된 채팅방에는 새로운 유저가 입장할 수 없습니다."), + CHAT_AUTHOR_JOIN_DENIED(HttpStatus.FORBIDDEN, "CHAT006", "방장은 채팅방 퇴장 시 재입장할 수 없습니다."), AUTHOR_LEAVE_NOT_ALLOWED(HttpStatus.FORBIDDEN, "CHAT007", "방장은 직관완료가 안된 채팅방에서 나갈 수 없습니다."), CHAT_ROOM_NOT_MESSAGEABLE(HttpStatus.FORBIDDEN, "CHAT008", "메세지 전송이 불가능한 채팅방입니다."), diff --git a/src/main/java/com/example/mate/domain/mate/repository/VisitPartRepository.java b/src/main/java/com/example/mate/domain/mate/repository/VisitPartRepository.java index f8421785..18108d5d 100644 --- a/src/main/java/com/example/mate/domain/mate/repository/VisitPartRepository.java +++ b/src/main/java/com/example/mate/domain/mate/repository/VisitPartRepository.java @@ -1,14 +1,14 @@ package com.example.mate.domain.mate.repository; -import com.example.mate.domain.mate.entity.Visit; import com.example.mate.domain.mate.entity.VisitPart; import com.example.mate.domain.mate.entity.VisitPartId; import com.example.mate.domain.member.entity.Member; -import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; +import java.util.List; + public interface VisitPartRepository extends JpaRepository { int countByMember(Member member); @@ -21,5 +21,12 @@ public interface VisitPartRepository extends JpaRepository findMembersByVisitIdExcludeMember(@Param("visitId") Long visitId, @Param("memberId") Long memberId); - boolean existsByVisitAndMember(Visit visit, Member member); + @Query(""" + SELECT COUNT(vp) > 0 + FROM VisitPart vp + WHERE vp.visit.id = :visitId + AND vp.member.id = :memberId + """) + boolean existsByVisitAndMember(@Param("visitId") Long visitId, @Param("memberId") Long memberId); + } diff --git a/src/main/java/com/example/mate/domain/mateChat/service/MateChatRoomService.java b/src/main/java/com/example/mate/domain/mateChat/service/MateChatRoomService.java index b4b088d7..a4fbac69 100644 --- a/src/main/java/com/example/mate/domain/mateChat/service/MateChatRoomService.java +++ b/src/main/java/com/example/mate/domain/mateChat/service/MateChatRoomService.java @@ -103,8 +103,11 @@ private MateChatRoomResponse processChatRoomJoin(MateChatRoom chatRoom, Member m } private void validateChatRoomJoin(MatePost matePost, Member member) { - // 방장인 경우 모든 제한을 건너뜀 + // 방장인 경우, 직관 완료 상태가 아닐 때만 입장 가능 if (matePost.getAuthor().getId().equals(member.getId())) { + if (matePost.getStatus() == Status.VISIT_COMPLETE) { + throw new CustomException(ErrorCode.CHAT_AUTHOR_JOIN_DENIED); + } return; } @@ -120,12 +123,13 @@ private void validateChatRoomJoin(MatePost matePost, Member member) { } // 3. 직관 완료 상태인 경우 접근 권한 검증 - if (matePost.getStatus() == Status.VISIT_COMPLETE) { + if (matePost.getStatus() == Status.VISIT_COMPLETE && matePost.getVisit() != null) { boolean isVisitParticipant = visitPartRepository.existsByVisitAndMember( - matePost.getVisit(), member); - boolean isAuthor = matePost.getAuthor().getId().equals(member.getId()); + matePost.getVisit().getId(), // Visit 엔티티 대신 ID 전달 + member.getId() // Member 엔티티 대신 ID 전달 + ); - if (!isVisitParticipant && !isAuthor) { + if (!isVisitParticipant) { throw new CustomException(ErrorCode.CHAT_ROOM_ACCESS_DENIED); } }