Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/main/java/zip/ootd/ootdzip/fcm/controller/FcmController.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package zip.ootd.ootdzip.fcm.controller;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import zip.ootd.ootdzip.common.response.ApiResponse;
import zip.ootd.ootdzip.fcm.data.FcmPostConfigReq;
import zip.ootd.ootdzip.fcm.data.FcmPostReq;
import zip.ootd.ootdzip.fcm.service.FcmService;
import zip.ootd.ootdzip.user.service.UserService;
Expand All @@ -30,6 +34,7 @@ public class FcmController {
* 해당 토큰값은 디바이스 고유값으로 해당 값으로 FCM 이 디바이스에게 푸쉬알람을 보낼 수 있습니다.
* 유저가 앱을실행하고 로그인할 때마다 프론트는 해당 API 를통해 토큰값을 서버로 보냅니다.
*/
@Operation(summary = "FCM 토큰 등록", description = "해당 유저가 최초 호출시 DB 등록 및 세팅, 두번째 호출시는 로그인상태로 변경합니다.")
@PostMapping("")
public ApiResponse<Boolean> onFcmToken(@RequestBody @Valid FcmPostReq fcmPostReq) {

Expand All @@ -41,11 +46,30 @@ public ApiResponse<Boolean> onFcmToken(@RequestBody @Valid FcmPostReq fcmPostReq
/**
* 사용자 토큰 상태를 off 하여 해당 기기는 알림을 받을 수 없습니다.
*/
@Operation(summary = "FCM 토큰 로그아웃", description = "FCM 토큰 상태를 로그아웃 상태로 변경하여 알람을 수신받지 않습니다.")
@DeleteMapping("")
public ApiResponse<Boolean> offFcmToken(@RequestBody @Valid FcmPostReq fcmPostReq) {

fcmService.offFcmToken(fcmPostReq);

return new ApiResponse<>(true);
}

@Operation(summary = "FCM 토큰 설정 변경", description = "전체 알람 허용, 세부 알람 허용을 설정할 수 있습니다.")
@PostMapping("/config")
public ApiResponse<Boolean> changeConfig(@RequestBody @Valid FcmPostConfigReq fcmPostConfigReq) {

fcmService.changeConfig(fcmPostConfigReq);

return new ApiResponse<>(true);
}

@Operation(summary = "FCM 토큰 설정 조회", description = "현재 설정된 전체알람, 세부알람 정보를 가져옵니다.")
@GetMapping("/config/{token}")
public ApiResponse<Boolean> getConfig(@PathVariable String token) {

fcmService.getConfig(token);

return new ApiResponse<>(true);
}
}
39 changes: 39 additions & 0 deletions src/main/java/zip/ootd/ootdzip/fcm/data/FcmGetConfigRes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package zip.ootd.ootdzip.fcm.data;

import java.util.List;
import java.util.stream.Collectors;

import lombok.Data;
import zip.ootd.ootdzip.fcm.domain.FcmInfo;
import zip.ootd.ootdzip.fcm.domain.FcmNotificationType;
import zip.ootd.ootdzip.notification.domain.NotificationType;

@Data
public class FcmGetConfigRes {

Boolean isPermission;

List<DetailNotification> detailNotifications;

@Data
static class DetailNotification {

NotificationType notificationType;

Boolean isAllow;

DetailNotification(FcmNotificationType fcmNotificationType) {
this.notificationType = fcmNotificationType.getNotificationType();
this.isAllow = fcmNotificationType.getIsAllow();
}
}

FcmGetConfigRes(FcmInfo fcmInfo) {

isPermission = fcmInfo.getIsPermission();

this.detailNotifications = fcmInfo.getFcmNotificationTypes().stream()
.map(DetailNotification::new)
.collect(Collectors.toList());
}
}
26 changes: 26 additions & 0 deletions src/main/java/zip/ootd/ootdzip/fcm/data/FcmPostConfigReq.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package zip.ootd.ootdzip.fcm.data;

import java.util.List;

import jakarta.validation.constraints.NotNull;
import lombok.Data;
import zip.ootd.ootdzip.notification.domain.NotificationType;

@Data
public class FcmPostConfigReq {

@NotNull(message = "토큰값은 필수입니다.")
private String fcmToken;

private Boolean isPermission;

List<DetailNotification> detailNotifications;

@Data
public static class DetailNotification {

private NotificationType notificationType;

private Boolean isAllow;
}
}
6 changes: 5 additions & 1 deletion src/main/java/zip/ootd/ootdzip/fcm/domain/FcmInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static FcmInfo createDefaultFcmInfo(User user, String fcmToken) {

List<FcmNotificationType> fcmDefaultNotificationTypes = Stream.of(NotificationType.values())
.map(notificationType -> FcmNotificationType.builder()
.notificationType(notificationType).build()).toList();
.notificationType(notificationType).build()).toList();

FcmInfo fcmInfo = FcmInfo.builder()
.user(user)
Expand All @@ -80,6 +80,10 @@ public boolean isExistAllowNotificationType(Notification notification) {
.anyMatch(fnt -> fnt.getNotificationType() == notification.getNotificationType() && fnt.getIsAllow());
}

public void changePermission(Boolean isPermission) {
this.isPermission = isPermission;
}

// == 연관관계 메서드 == //
public void addFcmNotificationType(FcmNotificationType fcmNotificationType) {
fcmNotificationTypes.add(fcmNotificationType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ public class FcmNotificationType extends BaseEntity {
@Builder.Default
private Boolean isAllow = true;

public void changeAllow(Boolean isAllow) {
this.isAllow = isAllow;
}
}
28 changes: 28 additions & 0 deletions src/main/java/zip/ootd/ootdzip/fcm/service/FcmService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import lombok.RequiredArgsConstructor;
import zip.ootd.ootdzip.fcm.data.FcmMessageRes;
import zip.ootd.ootdzip.fcm.data.FcmPostConfigReq;
import zip.ootd.ootdzip.fcm.data.FcmPostReq;
import zip.ootd.ootdzip.fcm.domain.FcmInfo;
import zip.ootd.ootdzip.fcm.repository.FcmRepository;
Expand Down Expand Up @@ -170,4 +171,31 @@ private String makeMessage(Notification notification, String receiverToken) {
throw new RuntimeException(e);
}
}

@Transactional
public void changeConfig(FcmPostConfigReq fcmPostConfigReq) {

FcmInfo fcmInfo = fcmRepository.findByFcmToken(fcmPostConfigReq.getFcmToken()).orElseThrow();

// 전체 알림 허용 수정
if (fcmPostConfigReq.getIsPermission() != null) {
fcmInfo.changePermission(fcmPostConfigReq.getIsPermission());
}

// 세부 알림 허용 수정
if (fcmPostConfigReq.getDetailNotifications() != null) {
fcmPostConfigReq.getDetailNotifications().forEach(dn ->
fcmInfo.getFcmNotificationTypes().stream()
.filter(fn -> fn.getNotificationType() == dn.getNotificationType())
.findFirst()
.ifPresent(fn -> fn.changeAllow(dn.getIsAllow())));
}
}

@Transactional
public void getConfig(String token) {

FcmInfo fcmInfo = fcmRepository.findByFcmToken(token).orElseThrow();

}
}