Skip to content

Commit

Permalink
Feat: 이미 진행중 or 대기중 인 콜백이 있으면 추가적인 콜백 생성 안되게 함 (#75)
Browse files Browse the repository at this point in the history
* feat: 이미 진행중 or 대기중 인 콜백이 있으면 추가적인 콜백 생성 안되게 함

* refactor: 이상해진 테스트 메서드 이름 정상화

* refactor: 결제 고객에대한 메시지 순화

* fix: withdraw 관리자 페이지에서 출금요청을 취소상태로 전환 요청하는 URL을 이상한것으로 설정해놓은 치명적 버그 FIX
  • Loading branch information
zzoe2346 authored Oct 16, 2024
1 parent 492bd53 commit a9a26bf
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ResponseEntity<Page<CallbackResponse>> getWaitingCallbackList(@MemberId L
public ResponseEntity<Void> pendingCompleteCallback(@MemberId Long memberId,
@PathVariable Long callbackId) {

callbackService.pendingComplete(memberId, callbackId);
callbackService.changeCallbackStatusToPendingCompleteBySinitto(memberId, callbackId);
return ResponseEntity.ok().build();
}

Expand All @@ -55,7 +55,7 @@ public ResponseEntity<Void> completeCallback(@MemberId Long memberId,
public ResponseEntity<Void> acceptCallback(@MemberId Long memberId,
@PathVariable Long callbackId) {

callbackService.accept(memberId, callbackId);
callbackService.acceptCallbackBySinitto(memberId, callbackId);
return ResponseEntity.ok().build();
}

Expand All @@ -64,14 +64,14 @@ public ResponseEntity<Void> acceptCallback(@MemberId Long memberId,
public ResponseEntity<Void> cancelCallback(@MemberId Long memberId,
@PathVariable Long callbackId) {

callbackService.cancel(memberId, callbackId);
callbackService.cancelCallbackAssignmentBySinitto(memberId, callbackId);
return ResponseEntity.ok().build();
}

@PostMapping("/twilio")
public ResponseEntity<String> addCallCheck(@RequestParam("From") String fromNumber) {

return ResponseEntity.ok(callbackService.add(fromNumber));
return ResponseEntity.ok(callbackService.createCallbackByCall(fromNumber));
}

@Operation(summary = "시니또에게 현재 할당된 콜백 조회", description = "현재 시니또 본인에게 할당된 콜백을 조회합니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ public interface CallbackRepository extends JpaRepository<Callback, Long> {
Page<Callback> findAllBySeniorIn(List<Senior> seniors, Pageable pageable);

List<Callback> findAllByStatusAndPendingCompleteTimeBefore(Callback.Status status, LocalDateTime dateTime);

boolean existsBySeniorAndStatusIn(Senior senior, List<Callback.Status> statuses);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class CallbackService {
private static final String SUCCESS_MESSAGE = "감사합니다. 잠시만 기다려주세요.";
private static final String FAIL_MESSAGE_NOT_ENROLLED = "등록된 사용자가 아닙니다. 서비스 이용이 불가합니다.";
private static final String FAIL_MESSAGE_NOT_ENOUGH_POINT = "포인트가 부족합니다. 서비스 이용이 불가합니다.";
private static final String FAIL_MESSAGE_ALREADY_HAS_CALLBACK_IN_PROGRESS_OR_WAITING = "어르신의 요청이 이미 접수되었습니다. 잠시 기다려주시면 연락드리겠습니다.";
private final CallbackRepository callbackRepository;
private final MemberRepository memberRepository;
private final SeniorRepository seniorRepository;
Expand All @@ -56,7 +57,7 @@ public Page<CallbackResponse> getWaitingCallbacks(Long memberId, Pageable pageab
}

@Transactional
public void accept(Long memberId, Long callbackId) {
public void acceptCallbackBySinitto(Long memberId, Long callbackId) {

checkAuthorization(memberId);

Expand All @@ -71,7 +72,7 @@ public void accept(Long memberId, Long callbackId) {
}

@Transactional
public void pendingComplete(Long memberId, Long callbackId) {
public void changeCallbackStatusToPendingCompleteBySinitto(Long memberId, Long callbackId) {

checkAuthorization(memberId);

Expand Down Expand Up @@ -125,7 +126,7 @@ public void changeOldPendingCompleteToCompleteByPolicy() {
}

@Transactional
public void cancel(Long memberId, Long callbackId) {
public void cancelCallbackAssignmentBySinitto(Long memberId, Long callbackId) {

checkAuthorization(memberId);

Expand All @@ -138,7 +139,7 @@ public void cancel(Long memberId, Long callbackId) {
}

@Transactional
public String add(String fromNumber) {
public String createCallbackByCall(String fromNumber) {

String phoneNumber = TwilioHelper.trimPhoneNumber(fromNumber);

Expand All @@ -152,6 +153,10 @@ public String add(String fromNumber) {
return TwilioHelper.convertMessageToTwiML(FAIL_MESSAGE_NOT_ENOUGH_POINT);
}

if (callbackRepository.existsBySeniorAndStatusIn(senior, List.of(Callback.Status.WAITING, Callback.Status.IN_PROGRESS))) {
return TwilioHelper.convertMessageToTwiML(FAIL_MESSAGE_ALREADY_HAS_CALLBACK_IN_PROGRESS_OR_WAITING);
}

point.deduct(CALLBACK_PRICE);

pointLogRepository.save(
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/point/withdraw.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

<!-- 상태가 CHARGE_WAITING일 때 -->
<form method="post"
th:action="@{/admin/point/charge/fail/{pointLogId}(pointLogId=${logWithBankInfo.pointLogId})}"
th:action="@{/admin/point/withdraw/fail/{pointLogId}(pointLogId=${logWithBankInfo.pointLogId})}"
th:if="${logWithBankInfo.status.toString()} == 'WITHDRAW_WAITING'">
<button type="submit">실패 상태로 변경</button>
</form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,27 @@ void findAllByStatusAndPendingCompleteTimeBefore_success_zeroList() {
assertEquals(0, result.size());

}

@Test
@DisplayName("시니어와 상태들을 조건을 활용한 조회 테스트")
void existsBySeniorAndStatusIn() {
// given
Senior testSenior = seniorRepository.save(new Senior("senior", "01012341234", testMember));

callbackRepository.save(new Callback(Callback.Status.WAITING, testSenior));
callbackRepository.save(new Callback(Callback.Status.PENDING_COMPLETE, testSenior));
callbackRepository.save(new Callback(Callback.Status.COMPLETE, testSenior));

// when
boolean result1 = callbackRepository.existsBySeniorAndStatusIn(testSenior, List.of(Callback.Status.IN_PROGRESS, Callback.Status.WAITING));
boolean result2 = callbackRepository.existsBySeniorAndStatusIn(testSenior, List.of(Callback.Status.WAITING));
boolean result3 = callbackRepository.existsBySeniorAndStatusIn(testSenior, List.of(Callback.Status.IN_PROGRESS));
boolean result4 = callbackRepository.existsBySeniorAndStatusIn(testSenior, List.of(Callback.Status.COMPLETE));

// then
assertTrue(result1);
assertTrue(result2);
assertFalse(result3);
assertTrue(result4);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void getCallbacks_Fail_WhenNotSinitto() {

@Test
@DisplayName("콜백 수락 - 성공")
void accept() {
void acceptCallbackBySinitto() {
//given
Long memberId = 1L;
Long callbackId = 1L;
Expand All @@ -120,7 +120,7 @@ void accept() {
when(callbackRepository.findById(callbackId)).thenReturn(Optional.of(callback));

//when
callbackService.accept(memberId, callbackId);
callbackService.acceptCallbackBySinitto(memberId, callbackId);

//then
verify(callback).assignMember(memberId);
Expand All @@ -144,15 +144,15 @@ void changeCallbackStatusToCompleteByGuard() {
when(callback.getAssignedMemberId()).thenReturn(assignedMemberId);

//when
callbackService.pendingComplete(memberId, callbackId);
callbackService.changeCallbackStatusToPendingCompleteBySinitto(memberId, callbackId);

//then
verify(callback).changeStatusToPendingComplete();
}

@Test
@DisplayName("수락한 콜백 취소 - 성공")
void cancel() {
void cancelCallbackAssignmentBySinitto() {
//given
Long memberId = 1L;
Long callbackId = 1L;
Expand All @@ -166,7 +166,7 @@ void cancel() {
when(callback.getAssignedMemberId()).thenReturn(1L);

//when
callbackService.cancel(memberId, callbackId);
callbackService.cancelCallbackAssignmentBySinitto(memberId, callbackId);

//then
verify(callback).cancelAssignment();
Expand All @@ -175,7 +175,7 @@ void cancel() {

@Test
@DisplayName("새로운 콜백 등록")
void addCallback() {
void createCallbackByCall() {
//given
String fromPhoneNumber = "+821012341234";
String trimmedPhoneNumber = TwilioHelper.trimPhoneNumber(fromPhoneNumber);
Expand All @@ -189,7 +189,7 @@ void addCallback() {
when(pointRepository.findByMemberIdWithWriteLock(1L)).thenReturn(Optional.of(point));
when(point.isSufficientForDeduction(anyInt())).thenReturn(true);
//when
String result = callbackService.add(fromPhoneNumber);
String result = callbackService.createCallbackByCall(fromPhoneNumber);

//then
verify(pointLogRepository, times(1)).save(any());
Expand All @@ -199,18 +199,69 @@ void addCallback() {

@Test
@DisplayName("새로운 콜백 등록할 때 전화온 번호가 등록된 번호가 아닐 때")
void addCallback_fail() {
void createCallbackByCallingCallback_fail() {
//given
String fromPhoneNumber = "+821012341234";
String trimmedPhoneNumber = TwilioHelper.trimPhoneNumber(fromPhoneNumber);

when(seniorRepository.findByPhoneNumber(trimmedPhoneNumber)).thenReturn(Optional.empty());

//when
String result = callbackService.add(fromPhoneNumber);
String result = callbackService.createCallbackByCall(fromPhoneNumber);

//then
verify(callbackRepository, times(0)).save(any());
verify(callbackRepository, never()).save(any());
assertNotNull(result);
}

@Test
@DisplayName("새로운 콜백 등록할 때 포인트가 부족할 때")
void createCallbackByCallingCallback_fail1() {
//given
String fromPhoneNumber = "+821012341234";
String trimmedPhoneNumber = TwilioHelper.trimPhoneNumber(fromPhoneNumber);
Member member = mock(Member.class);
Senior senior = mock(Senior.class);
Point point = mock(Point.class);

when(seniorRepository.findByPhoneNumber(trimmedPhoneNumber)).thenReturn(Optional.of(senior));
when(senior.getMember()).thenReturn(member);
when(member.getId()).thenReturn(1L);
when(pointRepository.findByMemberIdWithWriteLock(any(Long.class))).thenReturn(Optional.of(point));
when(point.isSufficientForDeduction(anyInt())).thenReturn(false);

//when
String result = callbackService.createCallbackByCall(fromPhoneNumber);

//then
verify(point, never()).deduct(anyInt());
verify(callbackRepository, never()).save(any());
assertNotNull(result);
}

@Test
@DisplayName("새로운 콜백 등록할 때 이미 시니어가 신청한 진행중 or 대기중인 콜백이 존재할때")
void createCallbackByCallingCallback_fail2() {
//given
String fromPhoneNumber = "+821012341234";
String trimmedPhoneNumber = TwilioHelper.trimPhoneNumber(fromPhoneNumber);
Member member = mock(Member.class);
Senior senior = mock(Senior.class);
Point point = mock(Point.class);

when(seniorRepository.findByPhoneNumber(trimmedPhoneNumber)).thenReturn(Optional.of(senior));
when(senior.getMember()).thenReturn(member);
when(member.getId()).thenReturn(1L);
when(pointRepository.findByMemberIdWithWriteLock(any(Long.class))).thenReturn(Optional.of(point));
when(point.isSufficientForDeduction(anyInt())).thenReturn(true);
when(callbackRepository.existsBySeniorAndStatusIn(any(), any())).thenReturn(true);

//when
String result = callbackService.createCallbackByCall(fromPhoneNumber);

//then
verify(point, never()).deduct(anyInt());
verify(callbackRepository, never()).save(any());
assertNotNull(result);
}

Expand Down Expand Up @@ -327,7 +378,7 @@ void changeOldPendingCompleteToCompleteByPolicy_Success_zeroList() {
callbackService.changeOldPendingCompleteToCompleteByPolicy();

// Then
verify(pointLogRepository, times(0)).save(any(PointLog.class));
verify(pointLogRepository, never()).save(any(PointLog.class));
}

@Test
Expand Down

0 comments on commit a9a26bf

Please sign in to comment.