Skip to content

Commit

Permalink
fix: 🐛 Add "Is Null" Filter on the Foreign Entity in the Chat Room En…
Browse files Browse the repository at this point in the history
…tity (#228)

* fix: add restriction to filter deleted_at is null in the chat-room

* test: check that added 'is null' query into the count query
  • Loading branch information
psychology50 authored Jan 22, 2025
1 parent b21f338 commit f1af051
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class ChatRoom extends DateAuditable {
private LocalDateTime deletedAt;

@OneToMany(mappedBy = "chatRoom")
@SQLRestriction("deleted_at IS NULL")
private List<ChatMember> chatMembers = new ArrayList<>();

@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.context.annotation.Import;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

Expand Down Expand Up @@ -117,4 +118,26 @@ void whenAdminLeavesWithOtherMembers_thenThrowsException() {
assertNull(unchangedMember.getDeletedAt());
assertNull(unchangedRoom.getDeletedAt());
}

@Test
@Transactional
@DisplayName("방장 이외의 멤버가 가입하고 나갔을 때, 남아있는 채팅방 멤버는 한 명이이므로 방장이 나갈 수 있다")
void whenNormalMemberJoinsAndLeaves_thenRemainingMemberShouldBeOne() {
// given
var adminUser = userRepository.save(UserFixture.GENERAL_USER.toUser());
var normalUser = userRepository.save(UserFixture.GENERAL_USER.toUser());
var chatRoom = chatRoomRepository.save(ChatRoomFixture.PUBLIC_CHAT_ROOM.toEntityWithId(4L));

var adminMember = chatMemberRepository.save(ChatMember.of(adminUser, chatRoom, ChatMemberRole.ADMIN));
var normalMember = ChatMember.of(normalUser, chatRoom, ChatMemberRole.MEMBER);
normalMember.leave();
normalMember = chatMemberRepository.save(normalMember);

log.info("chatRoom: {}", chatRoom);
log.info("adminMember: {}", adminMember);
log.info("normalMember: {}", normalMember);

// when & then
assertDoesNotThrow(() -> chatRoomLeaveService.execute(adminMember.getId()));
}
}

0 comments on commit f1af051

Please sign in to comment.