Skip to content

Commit

Permalink
Feat: 콜백 단건 조회 로직 개선 (#123)
Browse files Browse the repository at this point in the history
feat: 콜백 단건 조회 로직 개선

- 상태에 따른 검증 로직 추가
- 예외 처리 추가
- 가독성 향상
- 테스트 꼼꼼히
  • Loading branch information
zzoe2346 authored Oct 31, 2024
1 parent 6aa1dc2 commit 53151a3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,11 @@ public CallbackForSinittoResponse getCallbackForSinitto(Long memberId, Long call
Callback callback = callbackRepository.findById(callbackId)
.orElseThrow(() -> new NotFoundException("해당 콜백 id에 해당하는 콜백이 없습니다."));

if (callback.getAssignedMemberId().equals(memberId)) {
return new CallbackForSinittoResponse(callback.getId(), callback.getSeniorName(), callback.getPostTime(), callback.getStatus(), callback.getSeniorId(), true, callback.getSenior().getPhoneNumber());
if (!callback.getStatus().equals(Callback.Status.WAITING.toString())) {
if (callback.getAssignedMemberId() != null && callback.getAssignedMemberId().equals(memberId)) {
return new CallbackForSinittoResponse(callback.getId(), callback.getSeniorName(), callback.getPostTime(), callback.getStatus(), callback.getSeniorId(), true, callback.getSenior().getPhoneNumber());
}
throw new ForbiddenException("대기중인 콜백이 아닌경우 오직 할당받은 시니또만이 콜백을 조회할 수 있습니다.");
}

return new CallbackForSinittoResponse(callback.getId(), callback.getSeniorName(), callback.getPostTime(), callback.getStatus(), callback.getSeniorId(), false, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ void localDateTimeBeforeCalculateTest() {
}

@Test
@DisplayName("시니또용 콜백 단건 조회 - api 호출한 시니또 본인이 할당된 콜백일 경우")
@DisplayName("시니또용 콜백 단건 조회 - 1.대기상태 아님 2.AssignedMemberId 이 null 아님 + 해당 콜백에 할당 된 시니또 맞음 ")
void getCallbackForSinitto() {
//given
Long memberId = 1L;
Expand All @@ -415,11 +415,12 @@ void getCallbackForSinitto() {
Senior senior = mock(Senior.class);

when(callbackRepository.findById(callbackId)).thenReturn(Optional.of(callback));

when(callback.getStatus()).thenReturn(Callback.Status.IN_PROGRESS.toString());
when(callback.getAssignedMemberId()).thenReturn(1L);
when(callback.getId()).thenReturn(1L);
when(callback.getSeniorName()).thenReturn("SeniorName");
when(callback.getPostTime()).thenReturn(LocalDateTime.now());
when(callback.getStatus()).thenReturn(Callback.Status.WAITING.toString());
when(callback.getSeniorId()).thenReturn(1L);
when(callback.getSenior()).thenReturn(senior);
when(callback.getSenior().getPhoneNumber()).thenReturn("01012341234");
Expand All @@ -429,28 +430,57 @@ void getCallbackForSinitto() {

//then
assertTrue(result.isAssignedToSelf());
assertEquals("01012341234", result.seniorPhoneNumber());
}

@Test
@DisplayName("시니또용 콜백 단건 조회 - api 호출한 시니또 본인이 할당된 콜백이 아닌 경우")
@DisplayName("시니또용 콜백 단건 조회 - 1.대기상태 아님 2.AssignedMemberId 이 null 인 상황 ")
void getCallbackForSinitto2() {
//given
Long memberId = 1L;
Long callbackId = 1L;
Callback callback = mock(Callback.class);

when(callbackRepository.findById(callbackId)).thenReturn(Optional.of(callback));
when(callback.getAssignedMemberId()).thenReturn(999L); // 여기서 시니또 본인에게 할당된 콜백이 아닌걸 확인
when(callback.getId()).thenReturn(1L);
when(callback.getSeniorName()).thenReturn("SeniorName");
when(callback.getPostTime()).thenReturn(LocalDateTime.now());
when(callback.getAssignedMemberId()).thenReturn(null);
when(callback.getStatus()).thenReturn(Callback.Status.IN_PROGRESS.toString());

//when then
assertThrows(ForbiddenException.class, () -> callbackService.getCallbackForSinitto(memberId, callbackId));
}

@Test
@DisplayName("시니또용 콜백 단건 조회 - 1.대기상태 아님 2.AssignedMemberId 이 null 은 아닌데 해당 콜백에 할당된 시니또는 아닌 상황")
void getCallbackForSinitto3() {
//given
Long memberId = 1L;
Long callbackId = 1L;
Callback callback = mock(Callback.class);

when(callbackRepository.findById(callbackId)).thenReturn(Optional.of(callback));
when(callback.getAssignedMemberId()).thenReturn(999L);
when(callback.getStatus()).thenReturn(Callback.Status.IN_PROGRESS.toString());

//when then
assertThrows(ForbiddenException.class, () -> callbackService.getCallbackForSinitto(memberId, callbackId));
}

@Test
@DisplayName("시니또용 콜백 단건 조회 - 1.콜백이 '대기상태' 인 경우(모든 시니또가 조회 가능하다)")
void getCallbackForSinitto4() {
//given
Long memberId = 1L;
Long callbackId = 1L;
Callback callback = mock(Callback.class);

when(callbackRepository.findById(callbackId)).thenReturn(Optional.of(callback));
when(callback.getStatus()).thenReturn(Callback.Status.WAITING.toString());
when(callback.getSeniorId()).thenReturn(1L);

//when
CallbackForSinittoResponse result = callbackService.getCallbackForSinitto(memberId, callbackId);

//then
assertFalse(result.isAssignedToSelf());
assertEquals("", result.seniorPhoneNumber());
}
}

0 comments on commit 53151a3

Please sign in to comment.