Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void handle(ChatMessageSentEvent event) {

User sender = userQueryUseCase.getUser(message.getSenderId());
VoteOptionCode voteOptionCode =
voteQueryUseCase.getSelectedOption(message.getVoteId(), message.getSenderId()).getCode();
voteQueryUseCase.findSelectedOptionCode(message.getVoteId(), message.getSenderId()).orElse(null);

MessageResult messageResult = new MessageResult(
message.getId(),
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/ject/vs/chat/port/ChatService.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public MessageResult sendMessage(SendMessageCommand command) {
ChatMessage saved = chatMessageRepository.save(message);
User sender = userQueryUseCase.getUser(command.senderId());
VoteOptionCode voteOptionCode =
voteQueryUseCase.getSelectedOption(command.voteId(), command.senderId()).getCode();
voteQueryUseCase.findSelectedOptionCode(command.voteId(), command.senderId()).orElse(null);

return new MessageResult(
saved.getId(),
Expand Down Expand Up @@ -141,7 +141,7 @@ public MessagePageResult getMessages(Long voteId, Long userId, Long cursor, int
.map(msg -> {
User sender = userQueryUseCase.getUser(msg.getSenderId());
VoteOptionCode voteOptionCode =
voteQueryUseCase.getSelectedOption(voteId, msg.getSenderId()).getCode();
voteQueryUseCase.findSelectedOptionCode(voteId, msg.getSenderId()).orElse(null);

return new MessageResult(
msg.getId(),
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/ject/vs/vote/port/VoteQueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public VoteOption getSelectedOption(Long voteId, Long userId) {
return vote.getOption(selectedOptionId.get());
}

@Override
public Optional<VoteOptionCode> findSelectedOptionCode(Long voteId, Long userId) {
return getSelectedOptionId(voteId, userId).map(optionId -> getSelectedOption(voteId, userId).getCode());
}

@Override
public int getParticipantCount(Long voteId) {
return (int) voteParticipationRepository.countByVoteId(voteId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public interface VoteQueryUseCase {

VoteOption getSelectedOption(Long voteId, Long userId);

/** 투표 취소 등 참여 이력이 없으면 empty — 채팅 senderVoteOption null 처리용 */
Optional<VoteOptionCode> findSelectedOptionCode(Long voteId, Long userId);

int getParticipantCount(Long voteId);

/** 채팅 도메인 getChatList() 호환용 — 실제 Vote.endAt 기준으로 필터링 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import com.ject.vs.user.domain.ImageColor;
import com.ject.vs.user.domain.User;
import com.ject.vs.user.port.in.UserQueryUseCase;
import com.ject.vs.vote.domain.VoteOption;
import com.ject.vs.vote.domain.VoteOptionCode;
import com.ject.vs.vote.port.in.VoteParticipationQueryUseCase;
import com.ject.vs.vote.port.in.VoteQueryUseCase;
Expand Down Expand Up @@ -64,9 +63,8 @@ class ChatMessageEventListenerTest {
void setUpDefaults() {
lenient().when(userQueryUseCase.getUser(any())).thenReturn(mock(User.class));

VoteOption dummyOption = mock(VoteOption.class);
lenient().when(dummyOption.getCode()).thenReturn(VoteOptionCode.A);
lenient().when(voteQueryUseCase.getSelectedOption(anyLong(), anyLong())).thenReturn(dummyOption);
lenient().when(voteQueryUseCase.findSelectedOptionCode(anyLong(), anyLong()))
.thenReturn(Optional.of(VoteOptionCode.A));
}

@Nested
Expand Down
14 changes: 4 additions & 10 deletions src/unitTest/java/com/ject/vs/chat/port/ChatServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.ject.vs.user.domain.ImageColor;
import com.ject.vs.user.domain.User;
import com.ject.vs.user.port.in.UserQueryUseCase;
import com.ject.vs.vote.domain.VoteOption;
import com.ject.vs.vote.domain.VoteOptionCode;
import com.ject.vs.vote.port.in.VoteParticipationQueryUseCase;
import com.ject.vs.vote.port.in.VoteQueryUseCase;
Expand Down Expand Up @@ -61,9 +60,6 @@ class ChatServiceTest {
@Mock
private UserQueryUseCase userQueryUseCase;

@Mock
private VoteOption selectedOption;

@Nested
class sendMessage {

Expand Down Expand Up @@ -98,8 +94,7 @@ class sendMessage {
given(sender.getNickname()).willReturn("테스트유저");
given(sender.getImageColor()).willReturn(ImageColor.GREEN);
given(userQueryUseCase.getUser(2L)).willReturn(sender);
given(voteQueryUseCase.getSelectedOption(1L, 2L)).willReturn(selectedOption);
given(selectedOption.getCode()).willReturn(VoteOptionCode.A);
given(voteQueryUseCase.findSelectedOptionCode(1L, 2L)).willReturn(Optional.of(VoteOptionCode.A));

// when
MessageResult result = chatService.sendMessage(new SendMessageCommand(1L, 2L, "hello"));
Expand Down Expand Up @@ -206,14 +201,14 @@ class getMessages {
given(sender.getNickname()).willReturn("");
given(sender.getImageColor()).willReturn(ImageColor.GREEN);
given(userQueryUseCase.getUser(2L)).willReturn(sender);
given(voteQueryUseCase.getSelectedOption(1L, 2L)).willReturn(selectedOption);
given(selectedOption.getCode()).willReturn(null);
given(voteQueryUseCase.findSelectedOptionCode(1L, 2L)).willReturn(Optional.empty());

// when
MessagePageResult result = chatService.getMessages(1L, 2L, null, 30);

// then
assertThat(result.messages()).hasSize(1);
assertThat(result.messages().getFirst().senderVoteOption()).isNull();
assertThat(result.hasNext()).isFalse();
verify(chatMessageRepository).findAllByVoteIdOrderByIdDesc(eq(1L), any(PageRequest.class));
}
Expand All @@ -228,8 +223,7 @@ class getMessages {
given(sender.getNickname()).willReturn("");
given(sender.getImageColor()).willReturn(ImageColor.GREEN);
given(userQueryUseCase.getUser(2L)).willReturn(sender);
given(voteQueryUseCase.getSelectedOption(1L, 2L)).willReturn(selectedOption);
given(selectedOption.getCode()).willReturn(null);
given(voteQueryUseCase.findSelectedOptionCode(1L, 2L)).willReturn(Optional.of(VoteOptionCode.B));

// when
MessagePageResult result = chatService.getMessages(1L, 2L, 100L, 30);
Expand Down
Loading