diff --git a/src/main/java/com/beginvegan/domain/fcm/application/FcmService.java b/src/main/java/com/beginvegan/domain/fcm/application/FcmService.java index 49a612a..5c4efe1 100644 --- a/src/main/java/com/beginvegan/domain/fcm/application/FcmService.java +++ b/src/main/java/com/beginvegan/domain/fcm/application/FcmService.java @@ -50,17 +50,15 @@ public class FcmService { @Transactional public ResponseEntity sendMessageTo(FcmSendDto fcmSendDto) throws IOException { + User user = validateUserById(fcmSendDto.getUserId()); String msg = "메세지 전송에 실패했습니다(FCM 토큰이 존재하지 않음)"; - if (fcmSendDto.getToken() != null) { - Optional findUser = userRepository.findByFcmToken(fcmSendDto.getToken()); - DefaultAssert.isTrue(findUser.isPresent(), "유저 정보가 올바르지 않습니다."); - User user = findUser.get(); - + String fcmToken = user.getFcmToken(); + if (fcmToken != null) { if (user.getAlarmSetting()) { - sendCombinedMessage(fcmSendDto); + sendCombinedMessage(fcmToken, fcmSendDto); } else { - sendDataMessage(fcmSendDto); + sendDataMessage(fcmToken, fcmSendDto); } // alarmType이 존재할 경우에만 알림 내역에 저장 @@ -78,8 +76,8 @@ public ResponseEntity sendMessageTo(FcmSendDto fcmSendDto) throws IOException return ResponseEntity.ok(apiResponse); } - private void sendCombinedMessage(FcmSendDto fcmSendDto) throws IOException { - String message = makeFcmMessage(fcmSendDto); + private void sendCombinedMessage(String token, FcmSendDto fcmSendDto) throws IOException { + String message = makeFcmMessage(token, fcmSendDto); OkHttpClient client = new OkHttpClient(); RequestBody requestBody = RequestBody.create(message, @@ -96,11 +94,11 @@ private void sendCombinedMessage(FcmSendDto fcmSendDto) throws IOException { System.out.println(response.body().string()); } - private String makeFcmMessage(FcmSendDto fcmSendDto) throws JsonProcessingException { + private String makeFcmMessage(String token, FcmSendDto fcmSendDto) throws JsonProcessingException { FcmMessageDto fcmMessage = FcmMessageDto.builder() .validateOnly(false) .message(FcmMessageDto.Message.builder() - .token(fcmSendDto.getToken()) + .token(token) .notification(FcmMessageDto.Notification.builder() .title(fcmSendDto.getTitle()) .body(fcmSendDto.getBody()) @@ -125,9 +123,9 @@ private Map createDataMassage(FcmSendDto fcmSendDto) { return data; } - private void sendDataMessage(FcmSendDto fcmSendDto) throws IOException { + private void sendDataMessage(String token, FcmSendDto fcmSendDto) throws IOException { Map data = createDataMassage(fcmSendDto); - String message = makeDataMessage(fcmSendDto.getToken(), data); + String message = makeDataMessage(token, data); OkHttpClient client = new OkHttpClient(); RequestBody requestBody = RequestBody.create(message, @@ -183,7 +181,7 @@ private String getAccessToken() throws IOException { public FcmSendDto makeFcmSendDto(User user, AlarmType alarmType, Long itemId, String body, MessageType messageType, UserLevel userLevel) { return FcmSendDto.builder() - .token(user.getFcmToken()) + .userId(user.getId()) .alarmType(alarmType) .itemId(itemId) @@ -196,9 +194,7 @@ public FcmSendDto makeFcmSendDto(User user, AlarmType alarmType, Long itemId, St @Transactional public void saveAlarmHistory(FcmSendDto fcmSendDto) { - Optional findUser = userRepository.findByFcmToken(fcmSendDto.getToken()); - DefaultAssert.isTrue(findUser.isPresent(), "유저 정보가 올바르지 않습니다."); - User user = findUser.get(); + User user = validateUserById(fcmSendDto.getUserId()); Alarm alarm = Alarm.builder() .alarmType(fcmSendDto.getAlarmType()) @@ -209,4 +205,10 @@ public void saveAlarmHistory(FcmSendDto fcmSendDto) { alarmRepository.save(alarm); } + + private User validateUserById(Long userId) { + Optional findUser = userRepository.findById(userId); + DefaultAssert.isTrue(findUser.isPresent(), "유저 정보가 올바르지 않습니다."); + return findUser.get(); + } } \ No newline at end of file diff --git a/src/main/java/com/beginvegan/domain/fcm/dto/FcmSendDto.java b/src/main/java/com/beginvegan/domain/fcm/dto/FcmSendDto.java index 0d9a0c6..ab51619 100644 --- a/src/main/java/com/beginvegan/domain/fcm/dto/FcmSendDto.java +++ b/src/main/java/com/beginvegan/domain/fcm/dto/FcmSendDto.java @@ -18,8 +18,8 @@ @NoArgsConstructor(access = AccessLevel.PROTECTED) public class FcmSendDto { - @Schema(type = "String", example = "BJsVBMM0tWkqZY656FX3kDqhkCXfgKBhcPPSPDgaoa0tYTVKYh5Dt7...", description = "유저의 FCM 토큰입니다.") - private String token; + @Schema(type = "Long", example = "1", description = "유저의 고유 id입니다.") + private Long userId; @Schema(type = "String", example = "비긴, 비건", description = "알림의 제목입니다.") private String title; @@ -40,8 +40,8 @@ public class FcmSendDto { private UserLevel userLevel; @Builder - public FcmSendDto(String token, String title, String body, AlarmType alarmType, Long itemId, MessageType messageType, UserLevel userLevel) { - this.token = token; + public FcmSendDto(Long userId, String title, String body, AlarmType alarmType, Long itemId, MessageType messageType, UserLevel userLevel) { + this.userId = userId; this.title = title; this.body = body; this.alarmType = alarmType; diff --git a/src/main/java/com/beginvegan/domain/user/application/UserService.java b/src/main/java/com/beginvegan/domain/user/application/UserService.java index c35a2ad..ef13310 100644 --- a/src/main/java/com/beginvegan/domain/user/application/UserService.java +++ b/src/main/java/com/beginvegan/domain/user/application/UserService.java @@ -92,6 +92,21 @@ public ResponseEntity updateVeganType(UserPrincipal userPrincipal, UpdateVega return ResponseEntity.ok(apiResponse); } + public ResponseEntity getFcmTokenStatus(UserPrincipal userPrincipal) { + User user = validateUserById(userPrincipal.getId()); + boolean isStored = user.getFcmToken() != null; + + StoredFcmTokenRes storedFcmTokenRes = StoredFcmTokenRes.builder() + .storedFcmToken(isStored) + .build(); + + ApiResponse apiResponse = ApiResponse.builder() + .check(true) + .information(storedFcmTokenRes) + .build(); + return ResponseEntity.ok(apiResponse); + } + public ResponseEntity getAlarmSetting(UserPrincipal userPrincipal) { User user = validateUserById(userPrincipal.getId()); diff --git a/src/main/java/com/beginvegan/domain/user/dto/StoredFcmTokenRes.java b/src/main/java/com/beginvegan/domain/user/dto/StoredFcmTokenRes.java new file mode 100644 index 0000000..1e0f75b --- /dev/null +++ b/src/main/java/com/beginvegan/domain/user/dto/StoredFcmTokenRes.java @@ -0,0 +1,15 @@ +package com.beginvegan.domain.user.dto; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Builder; +import lombok.Data; + +@Data +public class StoredFcmTokenRes { + + @Schema(type = "boolean", example = "true", description = "유저의 fcm 토큰 저장 여부입니다.") + private Boolean storedFcmToken; + + @Builder + public StoredFcmTokenRes(Boolean storedFcmToken) { this.storedFcmToken = storedFcmToken; } +} diff --git a/src/main/java/com/beginvegan/domain/user/presentation/UserController.java b/src/main/java/com/beginvegan/domain/user/presentation/UserController.java index e9c47e0..7f5cc52 100644 --- a/src/main/java/com/beginvegan/domain/user/presentation/UserController.java +++ b/src/main/java/com/beginvegan/domain/user/presentation/UserController.java @@ -44,6 +44,18 @@ public ResponseEntity findUserByToken( return userService.findUserByToken(userPrincipal); } + @Operation(summary = "유저의 FCM 토큰 저장 여부 조회", description = "유저의 FCM 토큰 저장 여부를 조회합니다.") + @ApiResponses(value = { + @ApiResponse(responseCode = "200", description = "FCM 토큰 저장 여부 조회 성공", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = StoredFcmTokenRes.class) ) } ), + @ApiResponse(responseCode = "400", description = "FCM 토큰 저장 여부 조회 실패", content = { @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class) ) } ), + }) + @GetMapping("/fcm") + public ResponseEntity getFcmTokenStatus( + @Parameter(description = "Accesstoken을 입력해주세요.", required = true) @CurrentUser UserPrincipal userPrincipal + ) { + return userService.getFcmTokenStatus(userPrincipal); + } + @Operation(summary = "유저의 알림 여부 조회", description = "유저의 알림 여부를 조회합니다.") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "유저 알람 여부 조회 성공", content = {@Content(mediaType = "application/json", schema = @Schema(implementation = AlarmSettingRes.class))}),