Skip to content

Commit

Permalink
Solve situations when pending voucher is not created (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
inh2613 authored Nov 7, 2023
2 parents 0db25a9 + e75c1c8 commit 080040a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

import org.swmaestro.repl.gifthub.util.StatusEnum;

import io.sentry.Sentry;

public class GptResponseException extends RuntimeException {
private StatusEnum status;

public GptResponseException(String message, StatusEnum status) {
super(message);
this.status = status;
captureExceptionWithSentry(this);
}

private void captureExceptionWithSentry(Throwable throwable) {
Sentry.captureException(throwable);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

import org.swmaestro.repl.gifthub.util.StatusEnum;

import io.sentry.Sentry;

public class TimeoutException extends RuntimeException {
private StatusEnum status;

public TimeoutException(String message, StatusEnum status) {
super(message);
this.status = status;
captureExceptionWithSentry(this);
}

private void captureExceptionWithSentry(Throwable throwable) {
Sentry.captureException(throwable);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.swmaestro.repl.gifthub.auth.service.UserService;
import org.swmaestro.repl.gifthub.util.JwtProvider;
import org.swmaestro.repl.gifthub.util.Message;
import org.swmaestro.repl.gifthub.util.SuccessMessage;
Expand All @@ -28,6 +29,7 @@
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUpdateRequestDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUseRequestDto;
import org.swmaestro.repl.gifthub.vouchers.dto.VoucherUseResponseDto;
import org.swmaestro.repl.gifthub.vouchers.service.PendingVoucherService;
import org.swmaestro.repl.gifthub.vouchers.service.StorageService;
import org.swmaestro.repl.gifthub.vouchers.service.VoucherSaveService;
import org.swmaestro.repl.gifthub.vouchers.service.VoucherService;
Expand All @@ -50,6 +52,8 @@ public class VoucherController {
private final StorageService storageService;
private final JwtProvider jwtProvider;
private final VoucherSaveService voucherSaveService;
private final PendingVoucherService pendingVoucherService;
private final UserService userService;

@GetMapping("/images/{extension}")
@Operation(summary = "Voucher 이미지 등록 메서드", description = "클라이언트에서 요청한 기프티콘 이미지를 Amazon S3에 저장하기 위한 메서드입니다. 요청 시 S3 PreSigned URL이 반환됩니다.")
Expand Down Expand Up @@ -180,9 +184,11 @@ public ResponseEntity<Message> deleteVoucher(HttpServletRequest request, @PathVa
@ApiResponses({
@ApiResponse(responseCode = "200(202)", description = "기프티콘 등록 요청"),
})
public ResponseEntity<Message> test(HttpServletRequest request, @RequestBody VoucherAutoSaveRequestDto voucherAutoSaveRequestDto) throws IOException {
public ResponseEntity<Message> saveVoucher(HttpServletRequest request, @RequestBody VoucherAutoSaveRequestDto voucherAutoSaveRequestDto) throws
IOException {
String username = jwtProvider.getUsername(jwtProvider.resolveToken(request).substring(7));
voucherSaveService.execute(voucherAutoSaveRequestDto, username);
Long pendingId = pendingVoucherService.create(userService.read(username));
voucherSaveService.execute(voucherAutoSaveRequestDto, username, pendingId);
return ResponseEntity.ok(
SuccessMessage.builder()
.path(request.getRequestURI())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.springframework.stereotype.Service;
import org.swmaestro.repl.gifthub.auth.service.UserService;
import org.swmaestro.repl.gifthub.exception.BusinessException;
import org.swmaestro.repl.gifthub.exception.GptResponseException;
import org.swmaestro.repl.gifthub.exception.TimeoutException;
import org.swmaestro.repl.gifthub.notifications.NotificationType;
Expand Down Expand Up @@ -39,8 +40,7 @@ public class VoucherSaveService {
private final UserService userService;
private final PendingVoucherService pendingVoucherService;

public void execute(VoucherAutoSaveRequestDto voucherAutoSaveRequestDto, String username) throws IOException {
Long pendingId = pendingVoucherService.create(userService.read(username));
public void execute(VoucherAutoSaveRequestDto voucherAutoSaveRequestDto, String username, Long pendingId) throws IOException {
String filename = voucherAutoSaveRequestDto.getFilename();
handleGptResponse(voucherAutoSaveRequestDto, username)
.flatMap(voucherSaveRequestDto -> handleSearchResponse(voucherSaveRequestDto, username, filename))
Expand Down Expand Up @@ -70,24 +70,16 @@ public void execute(VoucherAutoSaveRequestDto voucherAutoSaveRequestDto, String
// 처리 완료
pendingVoucherService.delete(pendingId);
throwable.printStackTrace();
// 15초 이상 응답이 없을 경우
if (throwable instanceof TimeoutException) {
fcmNotificationService.sendNotification("기프티콘 등록 실패", "자동 등록에 실패했습니다. 다시 시도해 주세요", username);
if (throwable instanceof BusinessException) {
fcmNotificationService.sendNotification("기프티콘 등록 실패", "이미 등록된 기프티콘 입니다.", username);
notificationService.save(userService.read(username), null,
NotificationType.REGISTERED,
"GPT 요청이 시간초과되었습니다.");
}
//Gpt 에러일 경우
if (throwable instanceof GptResponseException) {
"이미 등록된 기프티콘 입니다.");
} else {
fcmNotificationService.sendNotification("기프티콘 등록 실패", "자동 등록에 실패했습니다. 수동 등록을 이용해 주세요.", username);
notificationService.save(userService.read(username), null,
NotificationType.REGISTERED,
"자동 등록에 실패했습니다. 수동 등록을 이용해 주세요.");
} else {
fcmNotificationService.sendNotification("기프티콘 등록 실패", "이미 등록된 기프티콘 입니다.", username);
notificationService.save(userService.read(username), null,
NotificationType.REGISTERED,
"이미 등록된 기프티콘 입니다.");
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.swmaestro.repl.gifthub.auth.entity.User;
import org.swmaestro.repl.gifthub.auth.service.UserService;
import org.swmaestro.repl.gifthub.giftcard.entity.Giftcard;
import org.swmaestro.repl.gifthub.giftcard.service.GiftcardService;
import org.swmaestro.repl.gifthub.util.JwtProvider;
Expand Down Expand Up @@ -69,6 +71,9 @@ class VoucherControllerTest {
@MockBean
private GiftcardService giftcardService;

@MockBean
private UserService userService;

@Test
@WithMockUser(username = "이진우", roles = "USER")
void saveVoucherManual() throws Exception {
Expand Down Expand Up @@ -280,17 +285,19 @@ void saveVoucher() throws Exception {
VoucherSaveResponseDto voucherSaveResponseDto = VoucherSaveResponseDto.builder()
.id(1L)
.build();

VoucherAutoSaveRequestDto mockVoucherAutoSaveRequestDto = new VoucherAutoSaveRequestDto(); // You might want to set some properties if needed
String mockUsername = "testUser";
User user = User.builder()
.id(1L)
.username("이진우")
.build();

when(jwtProvider.resolveToken(any())).thenReturn("my_awesome_access_token");
when(gptService.getGptResponse(any(VoucherAutoSaveRequestDto.class))).thenReturn(Mono.just(gptResponseDto));
when(searchService.search(anyString())).thenReturn(Mono.just(searchResponseDto));
when(voucherService.save(anyString(), any(VoucherSaveRequestDto.class))).thenReturn(voucherSaveResponseDto);

when(userService.read(anyString())).thenReturn(user);
when(pendingVoucherService.create(user)).thenReturn(1L);
// When
voucherSaveService.execute(mockVoucherAutoSaveRequestDto, mockUsername);
voucherSaveService.execute(any(VoucherAutoSaveRequestDto.class), anyString(), anyLong());

// Then
mockMvc.perform(post("/vouchers")
Expand Down

0 comments on commit 080040a

Please sign in to comment.