Skip to content

Commit

Permalink
[FEAT] fcm token user 저장 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
ydkim-auto committed Feb 14, 2024
1 parent 92ac02e commit d285415
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 46 deletions.
23 changes: 23 additions & 0 deletions src/main/java/com/wooyeon/yeon/chat/dto/FcmDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.wooyeon.yeon.chat.dto;

import lombok.Builder;
import lombok.Getter;
import org.springframework.http.HttpStatus;

import javax.validation.constraints.NotNull;

public class FcmDto {

@Getter
@Builder
public static class Request {
@NotNull
private String fcmToken;
}

@Getter
@Builder
public static class Response {
private HttpStatus status;
}
}
28 changes: 0 additions & 28 deletions src/main/java/com/wooyeon/yeon/chat/service/RoomService.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,34 +103,6 @@ public List<RoomDto.SearchRoomResponse> searchMatchRoomList(String searchWord) {
User loginUser = userRepository.findOptionalByEmail(securityService.getCurrentUserEmail())
.orElseThrow(() -> new WooyeonException(ExceptionCode.USER_NOT_FOUND));

// 채팅방 내 검색 단어 포함 항목 조회 후 추가
// List<Chat> chatList = chatRepository.findAllByMessageContains(searchWord);

// if (0 < chatList.size() && !chatList.isEmpty()) {
// for (Chat chat : chatList) {
// UserMatch userMatch = chat.getUserMatch();
//
// Long matchUserId = getMatchUserId(userMatch, loginUser);
//
// User user = userRepository.findOptionalByUserId(matchUserId)
// .orElseThrow(() -> new IllegalArgumentException("User does not exist"));
//
// // 상대방 프로필 정보 조회
// Profile profile = profileRepository.findById(user.getUserProfile().getId())
// .orElseThrow(() -> new IllegalArgumentException("User Profile does not exist"));
//
// Optional<ProfilePhoto> profilePhoto = profilePhotoRepository.findByProfileId(profile.getId());
//
// RoomDto.SearchRoomResponse response = RoomDto.SearchRoomResponse.builder()
// .matchId(userMatch.getMatchId())
// .profilePhoto(!profilePhoto.isPresent() ? null : profilePhoto.get().getPhotoUrl())
// .name(profile.getNickname())
// .build();
//
// searchRoomList.add(response);
// }
// }

List<RoomDto.SearchRoomResponse> searchRoomList = new ArrayList<>();

// 이름이 같은 사람 조회 후 추가
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ public String getAccessToken() throws IOException {
public void sendNotificationByToken(@RequestBody FcmDto.Request request) throws IOException {
fcmService.sendMessageTo(request);
}

@PostMapping("/save")
public com.wooyeon.yeon.chat.dto.FcmDto.Response saveFcmToken(com.wooyeon.yeon.chat.dto.FcmDto.Request request) {
return fcmService.saveFcmToken(request);
}
}
32 changes: 26 additions & 6 deletions src/main/java/com/wooyeon/yeon/common/fcm/service/FcmService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.Notification;
import com.wooyeon.yeon.common.fcm.dto.FcmDto;
import com.wooyeon.yeon.common.security.SecurityService;
import com.wooyeon.yeon.exception.ExceptionCode;
import com.wooyeon.yeon.exception.WooyeonException;
import com.wooyeon.yeon.user.domain.User;
import com.wooyeon.yeon.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import org.apache.http.HttpHeaders;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.List;
Expand All @@ -26,6 +29,8 @@
@RequiredArgsConstructor
public class FcmService {
private final ObjectMapper objectMapper;
private final UserRepository userRepository;
private final SecurityService securityService;

public String getAccessToken() throws IOException {
String firebaseConfigPath = "fcm-secret.json";
Expand Down Expand Up @@ -103,4 +108,19 @@ public String makeMessage(

return objectMapper.writeValueAsString(fcmMessage);
}

@Transactional
public com.wooyeon.yeon.chat.dto.FcmDto.Response saveFcmToken(com.wooyeon.yeon.chat.dto.FcmDto.Request request) {

User loginUser = userRepository.findOptionalByEmail(securityService.getCurrentUserEmail())
.orElseThrow(() -> new WooyeonException(ExceptionCode.LOGIN_USER_NOT_FOUND));

loginUser.setTargetToken(request.getFcmToken());

userRepository.save(loginUser);

return com.wooyeon.yeon.chat.dto.FcmDto.Response.builder()
.status(HttpStatus.OK)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public enum ExceptionCode {
INVALID_REFRESH_TOKEN("Refresh Token 값이 유효하지 않습니다", "4007"),
NOT_EXIST_REFRESH_TOKEN("Refresh Token 값이 존재하지 않습니다", "4008"),
NOT_MATCH_REFRESH_TOKEN("DB 내 Refresh Token 값이 일치하지 않습니다", "4009"),
SECURITY_CONTEXT_IS_EMPTY("SECURITY CONTEXT USER 정보가 존재하지 않습니다", "4010");
SECURITY_CONTEXT_IS_EMPTY("Security Context USER 정보가 존재하지 않습니다", "4010");

private final String description;
private final String resultCode;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/wooyeon/yeon/user/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public void setProfile(Profile profile) {
this.profile = profile;
}

public void setTargetToken(String targetToken) {
this.targetToken = targetToken;
}

public void updateEmailAuth(boolean emailAuth) { this.emailAuth = emailAuth; }
public void updatePhoneAuth(boolean phoneAuth) { this.phoneAuth = phoneAuth; }
public Long getUserId() {return this.userId;}
Expand Down
21 changes: 10 additions & 11 deletions src/test/java/com/wooyeon/yeon/user/UserTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.wooyeon.yeon.user;

import com.wooyeon.yeon.user.domain.Profile;
import com.wooyeon.yeon.user.domain.User;
import com.wooyeon.yeon.user.repository.UserRepository;
import com.wooyeon.yeon.user.service.UserService;
Expand All @@ -20,16 +19,16 @@ public class UserTest {
@Autowired
private UserService userService;
//
// @Test
// public void createUser() {
// User user = User.builder()
// .email("match1@naver.com")
// .phone("01012345678")
// .password(passwordEncoder.encode("1234"))
// .build();
//
// userRepository.save(user);
// }
@Test
public void createUser() {
User user = User.builder()
.email("young2@naver.com")
.phone("01012345678")
.password(passwordEncoder.encode("A!12345678"))
.build();

userRepository.save(user);
}
//
// passwordEncoder 사용
@Test
Expand Down

0 comments on commit d285415

Please sign in to comment.