From 3fb083a8db76417c1a567d2575c5f2bb0edb5a03 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Sun, 2 Jul 2023 23:14:40 +0900 Subject: [PATCH 01/25] Revert "Write PULL_REQUEST_TEMPLATE.md (#3)" This reverts commit 708d265c0b6bc37ae501701906c686a2de5b4404, reversing changes made to 4c1b8d19d7e62f22c622a0f83524e3d769050f7f. --- .github/PULL_REQUEST_TEMPLATE.md | 11 ----------- .gitignore | 2 +- 2 files changed, 1 insertion(+), 12 deletions(-) delete mode 100644 .github/PULL_REQUEST_TEMPLATE.md diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md deleted file mode 100644 index cd41400f..00000000 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ /dev/null @@ -1,11 +0,0 @@ -### PR 타입(하나 이상의 PR 타입을 선택해주세요) --[ ] 기능 추가 --[ ] 기능 삭제 --[ ] 버그 수정 --[ ] 의존성, 환경 변수, 빌드 관련 코드 업데이트 - -### 작업사항 - -### 화면캡처(선택) - -### 의문점(선택) \ No newline at end of file diff --git a/.gitignore b/.gitignore index b2ea5edf..39fb52a3 100644 --- a/.gitignore +++ b/.gitignore @@ -36,4 +36,4 @@ out/ ### VS Code ### .vscode/ -/src/main/resources/application.yml \ No newline at end of file +/src/main/resources/application.properties \ No newline at end of file From 512c0a2abbe637a2bbc62527d9355aec2d0cf0f2 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Sun, 2 Jul 2023 23:17:12 +0900 Subject: [PATCH 02/25] chore: change application.properties to application.yml --- src/main/resources/application.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/main/resources/application.yml diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 00000000..e69de29b From cb83cb9553b8db604cef0965adf5f3bd54ee3534 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Tue, 7 Nov 2023 00:51:43 +0900 Subject: [PATCH 03/25] fix: resolve cors-error --- .../repl/gifthub/giftcard/controller/GiftcardController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/giftcard/controller/GiftcardController.java b/src/main/java/org/swmaestro/repl/gifthub/giftcard/controller/GiftcardController.java index 5958d7c6..5e5eba45 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/giftcard/controller/GiftcardController.java +++ b/src/main/java/org/swmaestro/repl/gifthub/giftcard/controller/GiftcardController.java @@ -31,7 +31,7 @@ public class GiftcardController { @GetMapping("/{id}") @Operation(summary = "공유 정보 요청 메서드", description = "클라이언트에서 요청한 공유 정보를 전달하기 위한 메서드입니다.") - @CrossOrigin(origins = "http://seheon.kr, https://dev.gifthub.kr, https://gifthub.kr") + @CrossOrigin(origins = "http://seheon.kr,https://dev.gifthub.kr,https://gifthub.kr") @ApiResponses({ @ApiResponse(responseCode = "200", description = "공유하기 정보 조회 성공"), @ApiResponse(responseCode = "400", description = "만료된 공유하기 정보 접근"), From 4536093bf4cbed3ba89fba127329aac23c060af9 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Tue, 7 Nov 2023 01:29:49 +0900 Subject: [PATCH 04/25] fix: modify save voucher-image's presigned-url --- .../swmaestro/repl/gifthub/vouchers/service/StorageService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/StorageService.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/StorageService.java index df79a866..fdd7137b 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/StorageService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/StorageService.java @@ -68,6 +68,6 @@ public String getPresignedUrlForSaveVoucher(String dirName, String extension) { GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.PUT) .withExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 5)); - return amazonS3Client.generatePresignedUrl(generatePresignedUrlRequest).toString(); + return amazonS3Client.generatePresignedUrl(generatePresignedUrlRequest).toString().replace("https://", "http://"); } } From d76328cb346e97bfa95fbb2b4e667c7074330611 Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Tue, 7 Nov 2023 03:39:04 +0900 Subject: [PATCH 05/25] fix: rename dto --- .../dto/{OCRDto.java => VoucherAutoSaveRequestDto.java} | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) rename src/main/java/org/swmaestro/repl/gifthub/vouchers/dto/{OCRDto.java => VoucherAutoSaveRequestDto.java} (75%) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/dto/OCRDto.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/dto/VoucherAutoSaveRequestDto.java similarity index 75% rename from src/main/java/org/swmaestro/repl/gifthub/vouchers/dto/OCRDto.java rename to src/main/java/org/swmaestro/repl/gifthub/vouchers/dto/VoucherAutoSaveRequestDto.java index eda5d361..cec12228 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/dto/OCRDto.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/dto/VoucherAutoSaveRequestDto.java @@ -12,12 +12,14 @@ @Getter @NoArgsConstructor @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) -public class OCRDto { +public class VoucherAutoSaveRequestDto { private List texts; + private String filename; @Builder - public OCRDto(List texts) { + public VoucherAutoSaveRequestDto(List texts, String filename) { this.texts = texts; + this.filename = filename; } public String concatenateTexts() { From d10aac1d30d3f4fc9cd23abd75aad9445322528a Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Tue, 7 Nov 2023 03:39:39 +0900 Subject: [PATCH 06/25] fix: apply modified dto --- .../repl/gifthub/vouchers/service/GptService.java | 9 +++------ .../vouchers/controller/VoucherControllerTest.java | 13 +++++++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/GptService.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/GptService.java index 51456035..21b12d4b 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/GptService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/GptService.java @@ -10,7 +10,7 @@ import org.springframework.stereotype.Service; import org.springframework.web.reactive.function.client.WebClient; import org.swmaestro.repl.gifthub.vouchers.dto.GptResponseDto; -import org.swmaestro.repl.gifthub.vouchers.dto.OCRDto; +import org.swmaestro.repl.gifthub.vouchers.dto.VoucherAutoSaveRequestDto; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -25,9 +25,6 @@ public class GptService { private String apiUrl; @Value("${openai.api-key}") private String apiKey; - - // @Value("${openai.question-path}") - // private String promptPath; private String prompt; @Autowired @@ -38,9 +35,9 @@ public GptService(WebClient.Builder webClientBuilder, @Value("/gpt/question.txt" this.prompt = loadQuestionFromFile(promptPath); } - public Mono getGptResponse(OCRDto ocrDto) throws IOException { + public Mono getGptResponse(VoucherAutoSaveRequestDto voucherAutoSaveRequestDto) throws IOException { String question = prompt; - String content = ocrDto.concatenateTexts(); + String content = voucherAutoSaveRequestDto.concatenateTexts(); String prompt = content + question; ObjectNode requestBody = objectMapper.createObjectNode(); diff --git a/src/test/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherControllerTest.java b/src/test/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherControllerTest.java index fbdd951b..3410afa5 100644 --- a/src/test/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherControllerTest.java +++ b/src/test/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherControllerTest.java @@ -19,8 +19,8 @@ import org.swmaestro.repl.gifthub.giftcard.service.GiftcardService; import org.swmaestro.repl.gifthub.util.JwtProvider; import org.swmaestro.repl.gifthub.vouchers.dto.GptResponseDto; -import org.swmaestro.repl.gifthub.vouchers.dto.OCRDto; import org.swmaestro.repl.gifthub.vouchers.dto.SearchResponseDto; +import org.swmaestro.repl.gifthub.vouchers.dto.VoucherAutoSaveRequestDto; import org.swmaestro.repl.gifthub.vouchers.dto.VoucherListResponseDto; import org.swmaestro.repl.gifthub.vouchers.dto.VoucherReadResponseDto; import org.swmaestro.repl.gifthub.vouchers.dto.VoucherSaveRequestDto; @@ -261,8 +261,9 @@ void saveVoucher() throws Exception { texts.add("012345678910"); texts.add("2023-06-15"); - OCRDto ocrDto = OCRDto.builder() + VoucherAutoSaveRequestDto voucherAutoSaveRequestDto = VoucherAutoSaveRequestDto.builder() .texts(texts) + .filename("1623777600000_스타벅스_아이스아메리카노T.png") .build(); GptResponseDto gptResponseDto = GptResponseDto.builder() .choices(new ArrayList<>()) @@ -280,22 +281,22 @@ void saveVoucher() throws Exception { .id(1L) .build(); - OCRDto mockOcrDto = new OCRDto(); // You might want to set some properties if needed + VoucherAutoSaveRequestDto mockVoucherAutoSaveRequestDto = new VoucherAutoSaveRequestDto(); // You might want to set some properties if needed String mockUsername = "testUser"; when(jwtProvider.resolveToken(any())).thenReturn("my_awesome_access_token"); - when(gptService.getGptResponse(any(OCRDto.class))).thenReturn(Mono.just(gptResponseDto)); + 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 - voucherSaveService.execute(mockOcrDto, mockUsername); + voucherSaveService.execute(mockVoucherAutoSaveRequestDto, mockUsername); // Then mockMvc.perform(post("/vouchers") .header("Authorization", "Bearer my_awesome_access_token") .contentType(MediaType.APPLICATION_JSON) - .content(objectMapper.writeValueAsString(ocrDto))) + .content(objectMapper.writeValueAsString(voucherAutoSaveRequestDto))) .andExpect(status().isOk()); } From cd55ae4c7d9f44125038216b4c936a35eda3ad9f Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Tue, 7 Nov 2023 03:40:04 +0900 Subject: [PATCH 07/25] feat: create get presigned url --- .../repl/gifthub/vouchers/service/StorageService.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/StorageService.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/StorageService.java index fdd7137b..d19689c9 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/StorageService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/StorageService.java @@ -70,4 +70,12 @@ public String getPresignedUrlForSaveVoucher(String dirName, String extension) { .withExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 5)); return amazonS3Client.generatePresignedUrl(generatePresignedUrlRequest).toString().replace("https://", "http://"); } + + public String getPresignedUrl(String dirName, String filename) { + String key = dirName + "/" + filename; + GeneratePresignedUrlRequest generatePresignedUrlRequest = + new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.GET) + .withExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 5)); + return amazonS3Client.generatePresignedUrl(generatePresignedUrlRequest).toString(); + } } From 6ce47974e40b804cb91ff9c5c5def8ffa55f47fe Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Tue, 7 Nov 2023 03:41:13 +0900 Subject: [PATCH 08/25] fix: add filename to save in image-url voucher-save-request-dto --- .../vouchers/service/VoucherSaveService.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherSaveService.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherSaveService.java index 0892c62e..b1013676 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherSaveService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherSaveService.java @@ -15,7 +15,7 @@ import org.swmaestro.repl.gifthub.util.QueryTemplateReader; import org.swmaestro.repl.gifthub.util.StatusEnum; import org.swmaestro.repl.gifthub.vouchers.dto.GptResponseDto; -import org.swmaestro.repl.gifthub.vouchers.dto.OCRDto; +import org.swmaestro.repl.gifthub.vouchers.dto.VoucherAutoSaveRequestDto; import org.swmaestro.repl.gifthub.vouchers.dto.VoucherSaveRequestDto; import org.swmaestro.repl.gifthub.vouchers.dto.VoucherSaveResponseDto; @@ -39,10 +39,11 @@ public class VoucherSaveService { private final UserService userService; private final PendingVoucherService pendingVoucherService; - public void execute(OCRDto ocrDto, String username) throws IOException { + public void execute(VoucherAutoSaveRequestDto voucherAutoSaveRequestDto, String username) throws IOException { Long pendingId = pendingVoucherService.create(userService.read(username)); - handleGptResponse(ocrDto, username) - .flatMap(voucherSaveRequestDto -> handleSearchResponse(voucherSaveRequestDto, username)) + String filename = voucherAutoSaveRequestDto.getFilename(); + handleGptResponse(voucherAutoSaveRequestDto, username) + .flatMap(voucherSaveRequestDto -> handleSearchResponse(voucherSaveRequestDto, username, filename)) .flatMap(voucherSaveRequestDto -> handleVoucherSaving(voucherSaveRequestDto, username)) .subscribe( // onSuccess @@ -91,8 +92,11 @@ public void execute(OCRDto ocrDto, String username) throws IOException { }); } - public Mono handleGptResponse(OCRDto ocrDto, String username) throws IOException, GptResponseException, TimeoutException { - return gptService.getGptResponse(ocrDto) + public Mono handleGptResponse(VoucherAutoSaveRequestDto voucherAutoSaveRequestDto, String username) throws + IOException, + GptResponseException, + TimeoutException { + return gptService.getGptResponse(voucherAutoSaveRequestDto) .timeout(Duration.ofSeconds(15)) .onErrorResume(TimeoutException.class, throwable -> Mono.error(new TimeoutException("GPT 요청이 시간초과되었습니다.", StatusEnum.NOT_FOUND))) .flatMap(response -> { @@ -112,16 +116,18 @@ public Mono handleGptResponse(OCRDto ocrDto, String usern }); } - public Mono handleSearchResponse(VoucherSaveRequestDto voucherSaveRequestDto, String username) { + public Mono handleSearchResponse(VoucherSaveRequestDto voucherSaveRequestDto, String username, String filename) { return searchService.search(createQuery(productNameProcessor.preprocessing(voucherSaveRequestDto))).flatMap(searchResponseDto -> { try { String brandName = searchResponseDto.getHits().getHitsList().get(0).getSource().getBrandName(); String productName = searchResponseDto.getHits().getHitsList().get(0).getSource().getProductName(); voucherSaveRequestDto.setBrandName(brandName); voucherSaveRequestDto.setProductName(productName); + voucherSaveRequestDto.setImageUrl(filename); System.out.println("Search response"); System.out.println(brandName); System.out.println(productName); + System.out.println(filename); return Mono.just(voucherSaveRequestDto); } catch (Exception e) { e.printStackTrace(); From 370f882ef79e0b5270b637249929e0295fd0e6eb Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Tue, 7 Nov 2023 03:42:09 +0900 Subject: [PATCH 09/25] feat: get uuid from voucher image-url --- .../repl/gifthub/vouchers/service/VoucherService.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java index 9a4c7f60..15d4e3b1 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java @@ -46,7 +46,7 @@ public class VoucherService { private final GiftcardService giftCardService; /* - 기프티콘 저장 메서드 + 기프티콘 수동 저장 메서드 */ public VoucherSaveResponseDto save(String username, VoucherSaveRequestDto voucherSaveRequestDto) throws IOException { @@ -456,4 +456,13 @@ public Giftcard cancelShare(String username, Long voucherId) { return giftCardService.delete(savedGiftcard.getId()); } + + /** + * 기프티콘 이미지 조회를 위한 presigned url 생성 메서드를 위한 key 생성 메서드 + */ + public String getPresignedUrlForVoucherImage(String username, Long voucherId) { + String imageUrl = read(voucherId, username).getImageUrl(); + String filename = imageUrl.substring(imageUrl.lastIndexOf("/") + 1); + return storageService.getPresignedUrl(voucherDirName, filename); + } } \ No newline at end of file From 75eaab5b4bed9cea81403c548c77dbe7e43aa209 Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Tue, 7 Nov 2023 03:42:53 +0900 Subject: [PATCH 10/25] fix: modify test code to add filename --- .../controller/VoucherController.java | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherController.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherController.java index 77c01963..e04b0e02 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherController.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherController.java @@ -17,8 +17,8 @@ import org.swmaestro.repl.gifthub.util.JwtProvider; import org.swmaestro.repl.gifthub.util.Message; import org.swmaestro.repl.gifthub.util.SuccessMessage; -import org.swmaestro.repl.gifthub.vouchers.dto.OCRDto; import org.swmaestro.repl.gifthub.vouchers.dto.PresignedUrlResponseDto; +import org.swmaestro.repl.gifthub.vouchers.dto.VoucherAutoSaveRequestDto; import org.swmaestro.repl.gifthub.vouchers.dto.VoucherListResponseDto; import org.swmaestro.repl.gifthub.vouchers.dto.VoucherReadResponseDto; import org.swmaestro.repl.gifthub.vouchers.dto.VoucherSaveRequestDto; @@ -51,14 +51,14 @@ public class VoucherController { private final JwtProvider jwtProvider; private final VoucherSaveService voucherSaveService; - @GetMapping("/images") + @GetMapping("/images/{extension}") @Operation(summary = "Voucher 이미지 등록 메서드", description = "클라이언트에서 요청한 기프티콘 이미지를 Amazon S3에 저장하기 위한 메서드입니다. 요청 시 S3 PreSigned URL이 반환됩니다.") @ApiResponses({ @ApiResponse(responseCode = "200", description = "성공적으로 S3 Presigned URL 반환"), }) - public ResponseEntity saveVoucherImage(HttpServletRequest request) throws IOException { + public ResponseEntity saveVoucherImage(HttpServletRequest request, @PathVariable String extension) throws IOException { PresignedUrlResponseDto presignedUrlResponseDto = PresignedUrlResponseDto.builder() - .presignedUrl(storageService.getPresignedUrlForSaveVoucher("voucher", "PNG")) + .presignedUrl(storageService.getPresignedUrlForSaveVoucher("voucher", extension)) .build(); return ResponseEntity.ok( SuccessMessage.builder() @@ -180,9 +180,9 @@ public ResponseEntity deleteVoucher(HttpServletRequest request, @PathVa @ApiResponses({ @ApiResponse(responseCode = "200(202)", description = "기프티콘 등록 요청"), }) - public ResponseEntity test(HttpServletRequest request, @RequestBody OCRDto ocrDto) throws IOException { + public ResponseEntity test(HttpServletRequest request, @RequestBody VoucherAutoSaveRequestDto voucherAutoSaveRequestDto) throws IOException { String username = jwtProvider.getUsername(jwtProvider.resolveToken(request).substring(7)); - voucherSaveService.execute(ocrDto, username); + voucherSaveService.execute(voucherAutoSaveRequestDto, username); return ResponseEntity.ok( SuccessMessage.builder() .path(request.getRequestURI()) @@ -222,4 +222,21 @@ public ResponseEntity cancelShareVoucher(HttpServletRequest request, @P .path(request.getRequestURI()) .build()); } + + @GetMapping("/{voucherId}/image") + @Operation(summary = "Voucher 이미지 조회 메서드", description = "클라이언트에서 요청한 기프티콘 이미지를 Amazon S3에서 조회하기 위한 메서드입니다. 요청 시 S3 PreSigned URL이 반환됩니다.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "성공적으로 S3 Presigned URL 반환"), + }) + public ResponseEntity readVoucherImage(HttpServletRequest request, @PathVariable Long voucherId) throws IOException { + String username = jwtProvider.getUsername(jwtProvider.resolveToken(request).substring(7)); + PresignedUrlResponseDto presignedUrlResponseDto = PresignedUrlResponseDto.builder() + .presignedUrl(voucherService.getPresignedUrlForVoucherImage(username, voucherId)) + .build(); + return ResponseEntity.ok( + SuccessMessage.builder() + .path(request.getRequestURI()) + .data(presignedUrlResponseDto) + .build()); + } } \ No newline at end of file From 6ac66fcbad6d8df3551fd1f0d051df2f3709d45a Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Tue, 7 Nov 2023 03:47:34 +0900 Subject: [PATCH 11/25] chore: modify annotation --- .../swmaestro/repl/gifthub/vouchers/service/VoucherService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java index 15d4e3b1..5691c3d9 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java @@ -458,7 +458,7 @@ public Giftcard cancelShare(String username, Long voucherId) { } /** - * 기프티콘 이미지 조회를 위한 presigned url 생성 메서드를 위한 key 생성 메서드 + * 기프티콘 이미지 조회를 위한 presigned url 생성 메서드를 위한 filename 추출 메서드 */ public String getPresignedUrlForVoucherImage(String username, Long voucherId) { String imageUrl = read(voucherId, username).getImageUrl(); From 6030fa2e1b2ec8ee3514f34494988287a027fdc8 Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Tue, 7 Nov 2023 14:32:31 +0900 Subject: [PATCH 12/25] fix: collect sentry error when exception occurred --- .../repl/gifthub/exception/GptResponseException.java | 8 ++++++++ .../repl/gifthub/exception/TimeoutException.java | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/main/java/org/swmaestro/repl/gifthub/exception/GptResponseException.java b/src/main/java/org/swmaestro/repl/gifthub/exception/GptResponseException.java index 6a31f3c4..14dd8d90 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/exception/GptResponseException.java +++ b/src/main/java/org/swmaestro/repl/gifthub/exception/GptResponseException.java @@ -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); + } } \ No newline at end of file diff --git a/src/main/java/org/swmaestro/repl/gifthub/exception/TimeoutException.java b/src/main/java/org/swmaestro/repl/gifthub/exception/TimeoutException.java index ef959d3e..b76d0230 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/exception/TimeoutException.java +++ b/src/main/java/org/swmaestro/repl/gifthub/exception/TimeoutException.java @@ -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); + } } From b8c42d94aa3bdc1ae98ed9de62cc3ec4f2009849 Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Tue, 7 Nov 2023 14:33:23 +0900 Subject: [PATCH 13/25] fix: remove pending-voucher-create when asynchronous operation --- .../vouchers/service/VoucherSaveService.java | 20 ++++++------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherSaveService.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherSaveService.java index b1013676..86217b24 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherSaveService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherSaveService.java @@ -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; @@ -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)) @@ -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, - "이미 등록된 기프티콘 입니다."); } }); } From 04fb3be95a36163c259b42f9c364fda8a16093df Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Tue, 7 Nov 2023 14:33:58 +0900 Subject: [PATCH 14/25] fix: separate pending-voucher-create --- .../gifthub/vouchers/controller/VoucherController.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherController.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherController.java index e04b0e02..746b748e 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherController.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherController.java @@ -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; @@ -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; @@ -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이 반환됩니다.") @@ -182,7 +186,8 @@ public ResponseEntity deleteVoucher(HttpServletRequest request, @PathVa }) public ResponseEntity test(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()) From 823df1f84cf2982b11e72ef8271c10ad21506b83 Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Tue, 7 Nov 2023 14:34:15 +0900 Subject: [PATCH 15/25] fix: modify test code --- .../repl/gifthub/vouchers/controller/VoucherControllerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherControllerTest.java b/src/test/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherControllerTest.java index 3410afa5..a75e6dc5 100644 --- a/src/test/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherControllerTest.java +++ b/src/test/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherControllerTest.java @@ -290,7 +290,7 @@ void saveVoucher() throws Exception { when(voucherService.save(anyString(), any(VoucherSaveRequestDto.class))).thenReturn(voucherSaveResponseDto); // When - voucherSaveService.execute(mockVoucherAutoSaveRequestDto, mockUsername); + voucherSaveService.execute(mockVoucherAutoSaveRequestDto, mockUsername, 1L); // Then mockMvc.perform(post("/vouchers") From f2ac12e5647d22f916697ac34b1bf50028decd67 Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Tue, 7 Nov 2023 15:11:13 +0900 Subject: [PATCH 16/25] fix: change method name --- .../repl/gifthub/vouchers/controller/VoucherController.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherController.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherController.java index 746b748e..deac3bea 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherController.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherController.java @@ -184,7 +184,8 @@ public ResponseEntity deleteVoucher(HttpServletRequest request, @PathVa @ApiResponses({ @ApiResponse(responseCode = "200(202)", description = "기프티콘 등록 요청"), }) - public ResponseEntity test(HttpServletRequest request, @RequestBody VoucherAutoSaveRequestDto voucherAutoSaveRequestDto) throws IOException { + public ResponseEntity saveVoucher(HttpServletRequest request, @RequestBody VoucherAutoSaveRequestDto voucherAutoSaveRequestDto) throws + IOException { String username = jwtProvider.getUsername(jwtProvider.resolveToken(request).substring(7)); Long pendingId = pendingVoucherService.create(userService.read(username)); voucherSaveService.execute(voucherAutoSaveRequestDto, username, pendingId); From e75c1c8245fff4663c825121e11e7f40c78a2c2e Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Tue, 7 Nov 2023 15:11:24 +0900 Subject: [PATCH 17/25] fix: modify test code --- .../controller/VoucherControllerTest.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherControllerTest.java b/src/test/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherControllerTest.java index a75e6dc5..4d2dfa17 100644 --- a/src/test/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherControllerTest.java +++ b/src/test/java/org/swmaestro/repl/gifthub/vouchers/controller/VoucherControllerTest.java @@ -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; @@ -69,6 +71,9 @@ class VoucherControllerTest { @MockBean private GiftcardService giftcardService; + @MockBean + private UserService userService; + @Test @WithMockUser(username = "이진우", roles = "USER") void saveVoucherManual() throws Exception { @@ -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, 1L); + voucherSaveService.execute(any(VoucherAutoSaveRequestDto.class), anyString(), anyLong()); // Then mockMvc.perform(post("/vouchers") From d96415f6374b726ce88fea43ac2161464326987d Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Tue, 7 Nov 2023 16:46:48 +0900 Subject: [PATCH 18/25] fix: modify exception with constructor initialization for readability --- .../exception/GptResponseException.java | 17 +++------------- .../exception/GptTimeoutException.java | 9 +++++++++ .../gifthub/exception/TimeoutException.java | 20 ------------------- 3 files changed, 12 insertions(+), 34 deletions(-) create mode 100644 src/main/java/org/swmaestro/repl/gifthub/exception/GptTimeoutException.java delete mode 100644 src/main/java/org/swmaestro/repl/gifthub/exception/TimeoutException.java diff --git a/src/main/java/org/swmaestro/repl/gifthub/exception/GptResponseException.java b/src/main/java/org/swmaestro/repl/gifthub/exception/GptResponseException.java index 14dd8d90..3f23182f 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/exception/GptResponseException.java +++ b/src/main/java/org/swmaestro/repl/gifthub/exception/GptResponseException.java @@ -2,19 +2,8 @@ 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); - +public class GptResponseException extends BusinessException { + public GptResponseException() { + super("GPT 응답이 올바르지 않습니다.", StatusEnum.NOT_FOUND); } } \ No newline at end of file diff --git a/src/main/java/org/swmaestro/repl/gifthub/exception/GptTimeoutException.java b/src/main/java/org/swmaestro/repl/gifthub/exception/GptTimeoutException.java new file mode 100644 index 00000000..4ca2632a --- /dev/null +++ b/src/main/java/org/swmaestro/repl/gifthub/exception/GptTimeoutException.java @@ -0,0 +1,9 @@ +package org.swmaestro.repl.gifthub.exception; + +import org.swmaestro.repl.gifthub.util.StatusEnum; + +public class GptTimeoutException extends BusinessException { + public GptTimeoutException() { + super("GPT 요청이 시간초과되었습니다.", StatusEnum.NOT_FOUND); + } +} diff --git a/src/main/java/org/swmaestro/repl/gifthub/exception/TimeoutException.java b/src/main/java/org/swmaestro/repl/gifthub/exception/TimeoutException.java deleted file mode 100644 index b76d0230..00000000 --- a/src/main/java/org/swmaestro/repl/gifthub/exception/TimeoutException.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.swmaestro.repl.gifthub.exception; - -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); - - } -} From 1916661f42a15596e8aef27baaf2820ebf754eb1 Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Tue, 7 Nov 2023 16:47:11 +0900 Subject: [PATCH 19/25] fix: remove unused exception --- .../org/swmaestro/repl/gifthub/vouchers/service/GptService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/GptService.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/GptService.java index 21b12d4b..066ae0f0 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/GptService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/GptService.java @@ -35,7 +35,7 @@ public GptService(WebClient.Builder webClientBuilder, @Value("/gpt/question.txt" this.prompt = loadQuestionFromFile(promptPath); } - public Mono getGptResponse(VoucherAutoSaveRequestDto voucherAutoSaveRequestDto) throws IOException { + public Mono getGptResponse(VoucherAutoSaveRequestDto voucherAutoSaveRequestDto) { String question = prompt; String content = voucherAutoSaveRequestDto.concatenateTexts(); From 9f9854917a52671c512c48089c9a3546b54ab0b9 Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Tue, 7 Nov 2023 16:47:45 +0900 Subject: [PATCH 20/25] fix: collect sentry errors --- .../vouchers/service/VoucherSaveService.java | 52 ++++++++----------- 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherSaveService.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherSaveService.java index 86217b24..47a1e0a2 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherSaveService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherSaveService.java @@ -8,13 +8,12 @@ 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.exception.GptTimeoutException; import org.swmaestro.repl.gifthub.notifications.NotificationType; import org.swmaestro.repl.gifthub.notifications.service.FCMNotificationService; import org.swmaestro.repl.gifthub.notifications.service.NotificationService; import org.swmaestro.repl.gifthub.util.ProductNameProcessor; import org.swmaestro.repl.gifthub.util.QueryTemplateReader; -import org.swmaestro.repl.gifthub.util.StatusEnum; import org.swmaestro.repl.gifthub.vouchers.dto.GptResponseDto; import org.swmaestro.repl.gifthub.vouchers.dto.VoucherAutoSaveRequestDto; import org.swmaestro.repl.gifthub.vouchers.dto.VoucherSaveRequestDto; @@ -23,6 +22,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; +import io.sentry.Sentry; import lombok.RequiredArgsConstructor; import reactor.core.publisher.Mono; @@ -42,8 +42,7 @@ public class VoucherSaveService { public void execute(VoucherAutoSaveRequestDto voucherAutoSaveRequestDto, String username, Long pendingId) throws IOException { String filename = voucherAutoSaveRequestDto.getFilename(); - handleGptResponse(voucherAutoSaveRequestDto, username) - .flatMap(voucherSaveRequestDto -> handleSearchResponse(voucherSaveRequestDto, username, filename)) + handleGptResponse(voucherAutoSaveRequestDto, username).flatMap(voucherSaveRequestDto -> handleSearchResponse(voucherSaveRequestDto, username, filename)) .flatMap(voucherSaveRequestDto -> handleVoucherSaving(voucherSaveRequestDto, username)) .subscribe( // onSuccess @@ -55,56 +54,49 @@ public void execute(VoucherAutoSaveRequestDto voucherAutoSaveRequestDto, String if (voucherService.read(voucherSaveResponseDto.getId()).getExpiresAt().isBefore(LocalDate.now())) { fcmNotificationService.sendNotification("기프티콘 등록 성공", "만료된 기프티콘을 등록했습니다.", username); notificationService.save(userService.read(username), voucherService.read(voucherSaveResponseDto.getId()), - NotificationType.REGISTERED, - "만료된 기프티콘을 등록했습니다."); + NotificationType.REGISTERED, "만료된 기프티콘을 등록했습니다."); } else { fcmNotificationService.sendNotification("기프티콘 등록 성공", "기프티콘 등록에 성공했습니다!", username); notificationService.save(userService.read(username), voucherService.read(voucherSaveResponseDto.getId()), - NotificationType.REGISTERED, - "기프티콘 등록에 성공했습니다."); + NotificationType.REGISTERED, "기프티콘 등록에 성공했습니다."); } }, // onError throwable -> { System.out.println("등록 실패"); + Sentry.captureException(throwable); // 처리 완료 pendingVoucherService.delete(pendingId); throwable.printStackTrace(); if (throwable instanceof BusinessException) { fcmNotificationService.sendNotification("기프티콘 등록 실패", "이미 등록된 기프티콘 입니다.", username); - notificationService.save(userService.read(username), null, - NotificationType.REGISTERED, - "이미 등록된 기프티콘 입니다."); + notificationService.save(userService.read(username), null, NotificationType.REGISTERED, "이미 등록된 기프티콘 입니다."); } else { fcmNotificationService.sendNotification("기프티콘 등록 실패", "자동 등록에 실패했습니다. 수동 등록을 이용해 주세요.", username); - notificationService.save(userService.read(username), null, - NotificationType.REGISTERED, - "자동 등록에 실패했습니다. 수동 등록을 이용해 주세요."); + notificationService.save(userService.read(username), null, NotificationType.REGISTERED, "자동 등록에 실패했습니다. 수동 등록을 이용해 주세요."); } }); } public Mono handleGptResponse(VoucherAutoSaveRequestDto voucherAutoSaveRequestDto, String username) throws - IOException, GptResponseException, - TimeoutException { + GptTimeoutException { + return gptService.getGptResponse(voucherAutoSaveRequestDto) - .timeout(Duration.ofSeconds(15)) - .onErrorResume(TimeoutException.class, throwable -> Mono.error(new TimeoutException("GPT 요청이 시간초과되었습니다.", StatusEnum.NOT_FOUND))) + .timeout(Duration.ofMinutes(15)) + .onErrorResume(GptTimeoutException.class, throwable -> Mono.error(new GptTimeoutException())) .flatMap(response -> { + VoucherSaveRequestDto voucherSaveRequestDto = null; try { - VoucherSaveRequestDto voucherSaveRequestDto = createVoucherSaveRequestDto(response); - if (voucherSaveRequestDto.getBrandName() == "" || - voucherSaveRequestDto.getProductName() == "" || - voucherSaveRequestDto.getBarcode() == "" || - voucherSaveRequestDto.getExpiresAt() == "") { - throw new GptResponseException("GPT 응답이 올바르지 않습니다.", StatusEnum.NOT_FOUND); - } - return Mono.just(voucherSaveRequestDto); + voucherSaveRequestDto = createVoucherSaveRequestDto(response); } catch (JsonProcessingException e) { - e.printStackTrace(); - return Mono.error(new GptResponseException("GPT 응답이 올바르지 않습니다.", StatusEnum.NOT_FOUND)); + return Mono.error(new RuntimeException(e)); + } + if (voucherSaveRequestDto.getBrandName() == "" || voucherSaveRequestDto.getProductName() == "" || voucherSaveRequestDto.getBarcode() == "" + || voucherSaveRequestDto.getExpiresAt() == "") { + return Mono.error(new GptResponseException()); } + return Mono.just(voucherSaveRequestDto); }); } @@ -146,9 +138,7 @@ private VoucherSaveRequestDto createVoucherSaveRequestDto(GptResponseDto gptResp private String createQuery(VoucherSaveRequestDto voucherSaveRequestDto) { String queryTemplate = queryTemplateReader.readQueryTemplate(); - return String.format(queryTemplate, - voucherSaveRequestDto.getBrandName(), - voucherSaveRequestDto.getProductName()); + return String.format(queryTemplate, voucherSaveRequestDto.getBrandName(), voucherSaveRequestDto.getProductName()); } } From 30379f7cc4dea9d255c6e57971f12348d5376b8c Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Tue, 7 Nov 2023 17:21:51 +0900 Subject: [PATCH 21/25] fix: modify voucher's is-shared --- .../swmaestro/repl/gifthub/vouchers/service/VoucherService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java index 5691c3d9..2a255a6a 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java @@ -268,7 +268,7 @@ public VoucherReadResponseDto mapToDto(Voucher voucher) { .expiresAt(voucher.getExpiresAt().toString()) .imageUrl(voucher.getImageUrl()) .accessible(voucher.getDeletedAt() == null) - .shared(giftCardService.isExist(voucher.getId())) + .shared(giftCardService.read(voucher.getId()).isEnable()) .build(); return voucherReadResponseDto; } From b99db8284f5fa79f84ee05e522289e7e90d3c127 Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Tue, 7 Nov 2023 22:11:59 +0900 Subject: [PATCH 22/25] fix: replace https to http --- .../swmaestro/repl/gifthub/vouchers/service/StorageService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/StorageService.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/StorageService.java index d19689c9..e96eddb0 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/StorageService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/StorageService.java @@ -76,6 +76,6 @@ public String getPresignedUrl(String dirName, String filename) { GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key, HttpMethod.GET) .withExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 5)); - return amazonS3Client.generatePresignedUrl(generatePresignedUrlRequest).toString(); + return amazonS3Client.generatePresignedUrl(generatePresignedUrlRequest).toString().replace("https://", "http://"); } } From 9ca67dcae62e9de0478103df2b0b1d928cb2b49e Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Wed, 8 Nov 2023 08:21:20 +0900 Subject: [PATCH 23/25] fix: modify voucher's is-shared --- .../repl/gifthub/giftcard/service/GiftcardService.java | 7 ++++++- .../repl/gifthub/vouchers/service/VoucherService.java | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/giftcard/service/GiftcardService.java b/src/main/java/org/swmaestro/repl/gifthub/giftcard/service/GiftcardService.java index a1adfb2c..cf409253 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/giftcard/service/GiftcardService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/giftcard/service/GiftcardService.java @@ -153,7 +153,12 @@ public boolean isExist(String id) { * @return: 기프트 카드가 존재하는지 여부 */ public boolean isExist(Long voucherId) { - return giftCardRepository.existsByVoucherId(voucherId); + if (giftCardRepository.existsByVoucherId(voucherId)) { + if (giftCardRepository.findAllByVoucherId(voucherId).get().getExpiresAt().isAfter(LocalDateTime.now())) { + return true; + } + } + return false; } /** diff --git a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java index 2a255a6a..5691c3d9 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/vouchers/service/VoucherService.java @@ -268,7 +268,7 @@ public VoucherReadResponseDto mapToDto(Voucher voucher) { .expiresAt(voucher.getExpiresAt().toString()) .imageUrl(voucher.getImageUrl()) .accessible(voucher.getDeletedAt() == null) - .shared(giftCardService.read(voucher.getId()).isEnable()) + .shared(giftCardService.isExist(voucher.getId())) .build(); return voucherReadResponseDto; } From 0e8d354a03c6f51ac618ef3c81c0ebfeb70ef9d7 Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Wed, 8 Nov 2023 20:30:44 +0900 Subject: [PATCH 24/25] fix: handle error non-unique-result-exception --- .../giftcard/repository/GiftcardRepository.java | 4 ++-- .../gifthub/giftcard/service/GiftcardService.java | 14 +++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/giftcard/repository/GiftcardRepository.java b/src/main/java/org/swmaestro/repl/gifthub/giftcard/repository/GiftcardRepository.java index 51cde233..1108c44c 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/giftcard/repository/GiftcardRepository.java +++ b/src/main/java/org/swmaestro/repl/gifthub/giftcard/repository/GiftcardRepository.java @@ -1,6 +1,6 @@ package org.swmaestro.repl.gifthub.giftcard.repository; -import java.util.Optional; +import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import org.swmaestro.repl.gifthub.giftcard.entity.Giftcard; @@ -8,5 +8,5 @@ public interface GiftcardRepository extends JpaRepository { boolean existsByVoucherId(Long id); - Optional findAllByVoucherId(Long id); + List findAllByVoucherId(Long id); } diff --git a/src/main/java/org/swmaestro/repl/gifthub/giftcard/service/GiftcardService.java b/src/main/java/org/swmaestro/repl/gifthub/giftcard/service/GiftcardService.java index cf409253..2627d885 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/giftcard/service/GiftcardService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/giftcard/service/GiftcardService.java @@ -2,6 +2,7 @@ import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; +import java.util.List; import java.util.Random; import java.util.UUID; @@ -76,7 +77,9 @@ public Giftcard read(String id) { public Giftcard read(Long voucherId) { return giftCardRepository.findAllByVoucherId(voucherId) + .stream() .filter(giftcard -> giftcard.getExpiresAt().isAfter(LocalDateTime.now())) + .findFirst() .orElseThrow(() -> new BusinessException("존재하지 않는 기프트 카드입니다.", StatusEnum.NOT_FOUND)); } @@ -154,9 +157,14 @@ public boolean isExist(String id) { */ public boolean isExist(Long voucherId) { if (giftCardRepository.existsByVoucherId(voucherId)) { - if (giftCardRepository.findAllByVoucherId(voucherId).get().getExpiresAt().isAfter(LocalDateTime.now())) { - return true; - } + // if (giftCardRepository.findAllByVoucherId(voucherId).get().getExpiresAt().isAfter(LocalDateTime.now())) { + // return true; + // } + List giftCards = giftCardRepository.findAllByVoucherId(voucherId); + + return giftCards.stream() + .anyMatch(giftcard -> giftcard.getExpiresAt().isAfter(LocalDateTime.now())); + } return false; } From f6307d4aa19c26092f4c389b218b59cca8320f49 Mon Sep 17 00:00:00 2001 From: Jeong-In-Hee Date: Wed, 8 Nov 2023 20:33:11 +0900 Subject: [PATCH 25/25] fix: remove code annotation --- .../repl/gifthub/giftcard/service/GiftcardService.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/giftcard/service/GiftcardService.java b/src/main/java/org/swmaestro/repl/gifthub/giftcard/service/GiftcardService.java index 2627d885..62f2657d 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/giftcard/service/GiftcardService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/giftcard/service/GiftcardService.java @@ -157,9 +157,6 @@ public boolean isExist(String id) { */ public boolean isExist(Long voucherId) { if (giftCardRepository.existsByVoucherId(voucherId)) { - // if (giftCardRepository.findAllByVoucherId(voucherId).get().getExpiresAt().isAfter(LocalDateTime.now())) { - // return true; - // } List giftCards = giftCardRepository.findAllByVoucherId(voucherId); return giftCards.stream()