Skip to content

Commit 46abfe5

Browse files
authored
Merge pull request #98 from MODU-TAXI/MS-192-fcm-exception
[MS-192] Feat: fcm exception
2 parents d975dd5 + db2868a commit 46abfe5

File tree

6 files changed

+37
-8
lines changed

6 files changed

+37
-8
lines changed

config

src/main/java/com/modutaxi/api/common/constants/ServerConstants.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,21 @@ public final class ServerConstants {
1111
@Value("${cloud.aws.s3.basic-profile-image}")
1212
private String basicProfileImageUrl;
1313

14+
@Value("${fcm.default_token}")
15+
private String fcmDefaultToken;
16+
1417
public static String BASIC_PROFILE_IMAGE_URL;
1518
public static final int REPORT_STANDARD = 5;
1619
public static final int FULL_MEMBER = 4;
1720

21+
public static String FCM_DEFAULT_TOKEN;
22+
1823
@PostConstruct
1924
private void init() {
2025
BASIC_PROFILE_IMAGE_URL = basicProfileImageUrl;
26+
FCM_DEFAULT_TOKEN = fcmDefaultToken;
2127
}
2228

29+
30+
2331
}

src/main/java/com/modutaxi/api/common/exception/errorcode/ParticipateErrorCode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public enum ParticipateErrorCode implements ErrorCode {
1616
ROOM_IS_FULL("PRT_006", "방의 정원이 꽉 찬 상태입니다.", HttpStatus.CONFLICT),
1717
USER_NOT_IN_ROOM("PRT_007", "사용자가 해당 채팅방에 없습니다.", HttpStatus.BAD_REQUEST),
1818
USER_ALREADY_IN_OTHER_ROOM("PRT_008", "해당 사용자가 다른 방에 참여한 상태입니다.", HttpStatus.BAD_REQUEST),
19+
USER_ALONE_IN_ROOM("PRT_009", "혼자 있는 방은 매칭완료할 수 없습니다.", HttpStatus.BAD_REQUEST),
1920
;
2021

2122
private final String errorCode;

src/main/java/com/modutaxi/api/common/fcm/FcmService.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.modutaxi.api.common.fcm;
22

3+
import static com.modutaxi.api.common.constants.ServerConstants.FCM_DEFAULT_TOKEN;
4+
5+
import com.google.firebase.FirebaseException;
36
import com.google.firebase.messaging.ApnsConfig;
47
import com.google.firebase.messaging.Aps;
58
import com.google.firebase.messaging.FirebaseMessaging;
6-
import com.google.firebase.messaging.FirebaseMessagingException;
79
import com.google.firebase.messaging.Message;
810
import com.google.firebase.messaging.Notification;
911
import com.google.gson.Gson;
@@ -36,12 +38,20 @@ public class FcmService {
3638
@Transactional
3739
public void subscribe(Long memberId, Long roomId) {
3840
String fcmToken = validateAndGetFcmToken(memberId);
41+
if (!(fcmToken == null || Objects.equals(fcmToken, "") || fcmToken.isEmpty()
42+
|| fcmToken.isBlank())){
43+
fcmToken = FCM_DEFAULT_TOKEN;
44+
Member member = memberRepository.findById(memberId).orElseThrow(
45+
() -> new BaseException(MemberErrorCode.EMPTY_MEMBER)
46+
);
47+
member.changeFcmToken(fcmToken);
48+
}
3949
try {
4050
FirebaseMessaging.getInstance()
4151
.subscribeToTopic(
4252
Collections.singletonList(fcmToken), Long.toString(roomId));
4353
log.info("FCM SUBSCRIBE");
44-
} catch (FirebaseMessagingException e) {
54+
} catch (FirebaseException e) {
4555
log.error("FAIL FCM SUBSCRIBE");
4656
throw new BaseException(ChatErrorCode.FAIL_FCM_SUBSCRIBE);
4757
}
@@ -53,7 +63,7 @@ public void unsubscribe(Long memberId, Long roomId) {
5363
FirebaseMessaging.getInstance()
5464
.unsubscribeFromTopic(
5565
Collections.singletonList(fcmToken), Long.toString(roomId));
56-
} catch (FirebaseMessagingException e) {
66+
} catch (FirebaseException e) {
5767
throw new BaseException(ChatErrorCode.FAIL_FCM_UNSUBSCRIBE);
5868
}
5969
}
@@ -64,8 +74,9 @@ public void send(Message message) {
6474
Gson gson = new Gson();
6575
String fcmMessageJson = gson.toJson(message);
6676
log.info("FCM 메시지: " + fcmMessageJson);
67-
} catch (FirebaseMessagingException e) {
68-
throw new BaseException(ChatErrorCode.FAIL_SEND_MESSAGE);
77+
} catch (FirebaseException e) {
78+
log.error(ChatErrorCode.FAIL_SEND_MESSAGE.getMessage());
79+
// throw new BaseException(ChatErrorCode.FAIL_SEND_MESSAGE);
6980
}
7081
}
7182

src/main/java/com/modutaxi/api/domain/member/service/RegisterMemberService.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.modutaxi.api.domain.member.service;
22

3+
import static com.modutaxi.api.common.constants.ServerConstants.FCM_DEFAULT_TOKEN;
4+
35
import com.modutaxi.api.common.auth.jwt.JwtTokenProvider;
46
import com.modutaxi.api.common.auth.oauth.SocialLoginService;
57
import com.modutaxi.api.common.auth.oauth.SocialLoginType;
@@ -38,8 +40,7 @@ public class RegisterMemberService {
3840
* 회원 가입
3941
*/
4042
public TokenAndMemberResponse registerMember(String key, String name, Gender gender,
41-
String phoneNumber,
42-
String fcmToken) {
43+
String phoneNumber, String fcmToken) {
4344
// key를 이용하여 redis 에서 snsId 추출, 삭제
4445
String snsId = checkSnsIdKey(key);
4546
// DB에 가입 이력 있는지 중복 확인
@@ -134,6 +135,9 @@ private TokenAndMemberResponse generateMemberToken(Member member) {
134135
*/
135136
@Transactional
136137
public void saveFcmToken(Member member, String fcmToken) {
138+
if (fcmToken.isBlank() || fcmToken.isEmpty()) {
139+
fcmToken = FCM_DEFAULT_TOKEN;
140+
}
137141
// db 저장
138142
member.changeFcmToken(fcmToken);
139143
// redis 캐싱

src/main/java/com/modutaxi/api/domain/room/service/UpdateRoomService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.modutaxi.api.common.converter.NaverMapConverter;
88
import com.modutaxi.api.common.converter.RoomTagBitMaskConverter;
99
import com.modutaxi.api.common.exception.BaseException;
10+
import com.modutaxi.api.common.exception.errorcode.ParticipateErrorCode;
1011
import com.modutaxi.api.common.exception.errorcode.RoomErrorCode;
1112
import com.modutaxi.api.common.exception.errorcode.SpotError;
1213
import com.modutaxi.api.common.exception.errorcode.TaxiInfoErrorCode;
@@ -269,6 +270,10 @@ public UpdateRoomResponse finishMatching(Member manager, Long roomId) {
269270

270271
checkManager(room.getRoomManager().getId(), manager.getId());
271272

273+
if (participantRepository.findAllByRoom(room).size() == 1) {
274+
throw new BaseException(ParticipateErrorCode.USER_ALONE_IN_ROOM);
275+
}
276+
272277
if (!room.getRoomStatus().equals(RoomStatus.BEFORE_MATCHING)) {
273278
throw new BaseException(RoomErrorCode.ALREADY_MATCHING_COMPLETE);
274279
}

0 commit comments

Comments
 (0)