-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BE] feat: 새로운 FCM 등록 기능 추가 (#998) #1002
base: dev
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.festago.fcm.application.command; | ||
|
||
import com.festago.fcm.domain.MemberFCM; | ||
import com.festago.fcm.domain.MemberFCMExpiredAtPolicy; | ||
import com.festago.fcm.domain.MemberFCMRemoveOldTokensPolicy; | ||
import com.festago.fcm.repository.MemberFCMRepository; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class MemberFCMCommandService { | ||
|
||
private final MemberFCMRepository memberFCMRepository; | ||
private final MemberFCMExpiredAtPolicy memberFCMExpiredAtPolicy; | ||
private final MemberFCMRemoveOldTokensPolicy memberFCMDeleteOldTokensPolicy; | ||
|
||
// TODO 별도의 Service 클래스 생성해서 비즈니스 로직 이관 생각해 볼 것 | ||
public void registerFCM(Long memberId, String fcmToken) { | ||
List<MemberFCM> memberFCMs = memberFCMRepository.findAllByMemberId(memberId); | ||
LocalDateTime expiredAt = memberFCMExpiredAtPolicy.provide(); | ||
memberFCMs.stream() | ||
.filter(memberFCM -> memberFCM.isSameToken(fcmToken)) | ||
.findAny() | ||
.ifPresentOrElse(memberFCM -> { | ||
memberFCM.changeExpiredAt(expiredAt); | ||
}, () -> { | ||
memberFCMDeleteOldTokensPolicy.delete(memberFCMs); | ||
memberFCMRepository.save(new MemberFCM(memberId, fcmToken, expiredAt)); | ||
}); | ||
} | ||
|
||
public void deleteAllMemberFCM(Long memberId) { | ||
memberFCMRepository.deleteAllByMemberId(memberId); | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.festago.fcm.domain; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public interface MemberFCMExpiredAtPolicy { | ||
|
||
LocalDateTime provide(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.festago.fcm.domain; | ||
|
||
import java.time.Clock; | ||
import java.time.LocalDateTime; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class MemberFCMExpiredAtPolicyImpl implements MemberFCMExpiredAtPolicy { | ||
|
||
private static final int EXPIRED_DAY_OFFSET = 180; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 자문자답인 것 같긴 한데.. |
||
private final Clock clock; | ||
|
||
@Override | ||
public LocalDateTime provide() { | ||
return LocalDateTime.now(clock).plusDays(EXPIRED_DAY_OFFSET); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.festago.fcm.domain; | ||
|
||
import com.festago.fcm.repository.MemberFCMRepository; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class MemberFCMRemoveOldTokensPolicy { | ||
|
||
private final MemberFCMRepository memberFCMRepository; | ||
|
||
public void delete(List<MemberFCM> memberFCMs) { | ||
memberFCMRepository.deleteByIn(memberFCMs); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. N개의 FCM 토큰을 허용한다면 아마 다음과 같이 로직을 변경할 수 있을 것 같네요. var oldTokens = memberFCMs.stream()
.sorted(comparing(MemberFCM::getExpiredAt).reversed()) // 만료 시간 내림차순
.skip(n - 1) // 최대 소지 개수 - 1 개를 남기고 모두 삭제
.toList();
memberFCMRepository.deleteByIn(oldTokens); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.festago.fcm.dto.v1; | ||
|
||
public record MemberFCMTokenRegisterV1Request( | ||
String fcmToken | ||
) { | ||
|
||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.festago.fcm.presentation.v1; | ||
|
||
import com.festago.auth.annotation.MemberAuth; | ||
import com.festago.auth.domain.authentication.MemberAuthentication; | ||
import com.festago.fcm.application.command.MemberFCMCommandService; | ||
import com.festago.fcm.dto.v1.MemberFCMTokenRegisterV1Request; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
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; | ||
|
||
@RestController | ||
@RequestMapping("/api/v1/fcm") | ||
@RequiredArgsConstructor | ||
public class MemberFCMV1Controller { | ||
|
||
private final MemberFCMCommandService memberFCMCommandService; | ||
|
||
@MemberAuth | ||
@PostMapping | ||
public ResponseEntity<Void> registerFCM( | ||
MemberAuthentication memberAuthentication, | ||
@RequestBody MemberFCMTokenRegisterV1Request request | ||
) { | ||
memberFCMCommandService.registerFCM(memberAuthentication.getId(), request.fcmToken()); | ||
return ResponseEntity.ok().build(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FCM을 등록하는 메서드가 여러 정책에 의해 부피가 조금 큽니다. 😂
따라서 별도의 서비스 클래스로 나눠야 할 것 같기도 하네요.