-
Notifications
You must be signed in to change notification settings - Fork 0
✨ refactor: 알림 리팩터링 및 테스트 #31
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
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
fd97b7d
fix(#10): User, Notification의 테이블명 추가
y3binchoi 43664c6
test(#10): NotificationService 테스트 메소드 작성
y3binchoi 1ea5797
test(#10): 테스트명이 행동을 잘 묘사하도록 테스트 메소드명 변경
y3binchoi b793501
test(#10): Mockito 주입 어노테이션으로 대체
y3binchoi c35f31b
refactor(#20): API 응답 구조 통일
y3binchoi 6cf3515
refactor(#20): NotificationController에서 Notice로 잘못 줄여 쓴 부분 수정
y3binchoi 23166a1
chore(#20): FCM 알림을 위한 비공개 키 숨김
y3binchoi 83db59d
test(#10): Notification 동기 이벤트에 대한 단위 테스트 추가
y3binchoi 704dbea
feat(#20): 예외 처리 어드바이스 생성
y3binchoi 10ebfa5
refactor(#20): OCP 위반하는 NotificationEventHandler 삭제
y3binchoi f9064d8
refactor(#20): Notification 이 중복되는 이름이라 NotificationEntity로 정정
y3binchoi 51dec4b
refactor(#20): 알림 컨트롤러와 서비스 메서드명 간소화
y3binchoi 7c9236f
refactor(#20): 알림 전송 책임 분리
y3binchoi e52a448
refactor(#20): 이벤트 종류에 따라 전략을 선택하도록 책임 분리
y3binchoi 234bfb6
refactor(#20): 각 이벤트별 전략 구현
y3binchoi 8eaeeea
refactor(#20): 알림 저장 애플리케이션 로직 구현
y3binchoi 5cc3dad
refactor(#20): NotificationEntity로 이름 바꿨음
y3binchoi 4b0f6da
test(#20): 알림 생성 도메인 로직 변경됨
y3binchoi f9913ea
refactor(#20): 알림 레포지토리 의존성 역전
y3binchoi 0abd7db
fix(#20): 알림 저장 활성화
y3binchoi 38e09f1
refactore(#20): 알림 서비스 레이어 의존성 역전
y3binchoi a0fb47f
test(#20): 알림 저장 및 전송 테스트
y3binchoi 0cb639f
test(#20): 알림 전송 서비스 테스트 코드
y3binchoi 1e59b0c
chore(#20): Fcm 연동 설정
y3binchoi 080d901
chore(#20): test 디렉토리의 클래스들은 test/resource에 있는 properties를 우선적으로 인식한다
y3binchoi c1e8031
test(#20): 비동기 이벤트 서비스 테스트
y3binchoi ffc2d85
refactor(#20): 알림 생성 서비스 Strategy -> Handler 이름 수정
y3binchoi 173c5a5
test(#20): 알림 서비스단 테스트 메서드명 형식 통일
y3binchoi dca10eb
fix(#20): Firebase 키 파일 지정
y3binchoi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 33 additions & 24 deletions
57
src/main/java/com/example/moim/external/fcm/FcmConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,41 @@ | ||
| package com.example.moim.external.fcm; | ||
|
|
||
| import com.google.auth.oauth2.GoogleCredentials; | ||
| import com.google.firebase.FirebaseApp; | ||
| import com.google.firebase.FirebaseOptions; | ||
| import com.google.firebase.messaging.FirebaseMessaging; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.List; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.core.io.ClassPathResource; | ||
|
|
||
| @Configuration | ||
| public class FcmConfig { | ||
|
|
||
| // @Bean | ||
| // FirebaseMessaging firebaseMessaging() throws IOException { | ||
| // ClassPathResource resource = new ClassPathResource("firebase/meta-gachon-fcm-firebase-adminsdk-fyr7b-30929b486f.json"); | ||
| // InputStream refreshToken = resource.getInputStream(); | ||
| // | ||
| // FirebaseApp firebaseApp = null; | ||
| // List<FirebaseApp> firebaseAppList = FirebaseApp.getApps(); | ||
| // | ||
| // if (firebaseAppList != null && !firebaseAppList.isEmpty()) { | ||
| // for (FirebaseApp app : firebaseAppList) { | ||
| // if (app.getName().equals(FirebaseApp.DEFAULT_APP_NAME)) { | ||
| // firebaseApp = app; | ||
| // } | ||
| // } | ||
| // } else { | ||
| // FirebaseOptions options = FirebaseOptions.builder() | ||
| // .setCredentials(GoogleCredentials.fromStream(refreshToken)) | ||
| // .build(); | ||
| // | ||
| // firebaseApp = FirebaseApp.initializeApp(options); | ||
| // } | ||
| // | ||
| // return FirebaseMessaging.getInstance(firebaseApp); | ||
| // } | ||
| @Bean | ||
| FirebaseMessaging firebaseMessaging() throws IOException { | ||
| ClassPathResource resource = new ClassPathResource("secret/sample-firebase-test-f3c27-firebase-adminsdk-fbsvc-c15b4b66c6.json"); | ||
| InputStream refreshToken = resource.getInputStream(); | ||
|
|
||
| FirebaseApp firebaseApp = null; | ||
| List<FirebaseApp> firebaseAppList = FirebaseApp.getApps(); | ||
|
|
||
| if (firebaseAppList != null && !firebaseAppList.isEmpty()) { | ||
| for (FirebaseApp app : firebaseAppList) { | ||
| if (app.getName().equals(FirebaseApp.DEFAULT_APP_NAME)) { | ||
| firebaseApp = app; | ||
| } | ||
| } | ||
| } else { | ||
| FirebaseOptions options = FirebaseOptions.builder() | ||
| .setCredentials(GoogleCredentials.fromStream(refreshToken)) | ||
| .build(); | ||
|
|
||
| firebaseApp = FirebaseApp.initializeApp(options); | ||
| } | ||
|
|
||
| return FirebaseMessaging.getInstance(firebaseApp); | ||
| } | ||
| } |
25 changes: 12 additions & 13 deletions
25
src/main/java/com/example/moim/notification/controller/NotificationController.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,34 @@ | ||
| package com.example.moim.notification.controller; | ||
|
|
||
| import com.example.moim.notification.controller.port.NotificationService; | ||
| import com.example.moim.notification.dto.NotificationExistOutput; | ||
| import com.example.moim.notification.dto.NotificationOutput; | ||
| import com.example.moim.notification.service.NotificationService; | ||
| import com.example.moim.user.dto.UserDetailsImpl; | ||
| import java.util.List; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
| 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.RestController; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @RestController | ||
| @RequiredArgsConstructor | ||
| public class NotificationController implements NotificationControllerDocs{ | ||
| public class NotificationController implements NotificationControllerDocs { | ||
| private final NotificationService notificationService; | ||
|
|
||
| @GetMapping(value = "/notice") | ||
| public NotificationExistOutput noticeCheck(@AuthenticationPrincipal UserDetailsImpl userDetailsImpl) { | ||
| return notificationService.checkNotice(userDetailsImpl.getUser()); | ||
| @GetMapping(value = "/notification/unread-count") | ||
| public NotificationExistOutput notificationUnreadCount(@AuthenticationPrincipal UserDetailsImpl userDetailsImpl) { | ||
| return notificationService.checkUnread(userDetailsImpl.getUser()); | ||
| } | ||
|
|
||
| @GetMapping("/notices") | ||
| public List<NotificationOutput> noticeFind(@AuthenticationPrincipal UserDetailsImpl userDetailsImpl) { | ||
| return notificationService.findNotice(userDetailsImpl.getUser()); | ||
| @GetMapping("/notification") | ||
| public List<NotificationOutput> notificationFind(@AuthenticationPrincipal UserDetailsImpl userDetailsImpl) { | ||
| return notificationService.findAll(userDetailsImpl.getUser()); | ||
| } | ||
|
|
||
| @DeleteMapping("/notices/{id}") | ||
| public void noticeRemove(@AuthenticationPrincipal UserDetailsImpl userDetailsImpl, @PathVariable Long id) { | ||
| notificationService.removeNotice(id); | ||
| @DeleteMapping("/notification/{id}") | ||
| public void notificationRemove(@AuthenticationPrincipal UserDetailsImpl userDetailsImpl, @PathVariable Long id) { | ||
| notificationService.remove(id); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/java/com/example/moim/notification/controller/port/NotificationService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| package com.example.moim.notification.controller.port; | ||
|
|
||
| import com.example.moim.notification.dto.NotificationExistOutput; | ||
| import com.example.moim.notification.dto.NotificationOutput; | ||
| import com.example.moim.notification.entity.NotificationEntity; | ||
| import com.example.moim.user.entity.User; | ||
| import java.util.List; | ||
|
|
||
| public interface NotificationService { | ||
| NotificationExistOutput checkUnread(User user); | ||
|
|
||
| List<NotificationOutput> findAll(User user); | ||
|
|
||
| void remove(Long id); | ||
|
|
||
| void sendAll(List<NotificationEntity> notificationEntities); | ||
| } |
26 changes: 13 additions & 13 deletions
26
src/main/java/com/example/moim/notification/dto/NotificationOutput.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,26 @@ | ||
| package com.example.moim.notification.dto; | ||
|
|
||
| import com.example.moim.notification.entity.Notifications; | ||
| import lombok.Data; | ||
|
|
||
| import com.example.moim.notification.entity.NotificationEntity; | ||
| import java.time.format.DateTimeFormatter; | ||
| import lombok.Data; | ||
|
|
||
| @Data | ||
| public class NotificationOutput { | ||
| private Long id; | ||
| private String notificationType; | ||
| private String title; | ||
| private String category; | ||
| private String content; | ||
| private String time; | ||
| private String createdAt; | ||
| private Boolean isRead; | ||
| private String actionUrl; | ||
|
|
||
| public NotificationOutput(Notifications notifications) { | ||
| this.id = notifications.getId(); | ||
| this.title = notifications.getTitle(); | ||
| this.category = notifications.getCategory(); | ||
| this.content = notifications.getContents(); | ||
| this.time = notifications.getCreatedDate().format(DateTimeFormatter.ofPattern("MM/dd HH:mm")); | ||
| notifications.setRead(true); | ||
| this.isRead = notifications.getIsRead(); | ||
| public NotificationOutput(NotificationEntity notificationEntity) { | ||
| this.id = notificationEntity.getId(); | ||
| this.title = notificationEntity.getTitle(); | ||
| this.notificationType = notificationEntity.getType().name(); | ||
| this.content = notificationEntity.getContent(); | ||
| this.createdAt = notificationEntity.getCreatedDate().format(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm")); | ||
| this.isRead = notificationEntity.getIsRead(); | ||
| this.actionUrl = notificationEntity.getType().getCategory() + "/" + notificationEntity.getLinkedId(); | ||
| } | ||
| } |
68 changes: 68 additions & 0 deletions
68
src/main/java/com/example/moim/notification/entity/NotificationEntity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package com.example.moim.notification.entity; | ||
|
|
||
| import com.example.moim.global.entity.BaseEntity; | ||
| import com.example.moim.user.entity.User; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.EnumType; | ||
| import jakarta.persistence.Enumerated; | ||
| import jakarta.persistence.FetchType; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.GenerationType; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.ManyToOne; | ||
| import jakarta.persistence.Table; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Entity | ||
| @Getter | ||
| @NoArgsConstructor | ||
| @Table(name = "notifications") | ||
| public class NotificationEntity extends BaseEntity { | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
| @ManyToOne(fetch = FetchType.LAZY) | ||
| private User targetUser; | ||
| @Enumerated(EnumType.STRING) | ||
| private NotificationType type; | ||
| private String title; | ||
| private String content; | ||
| private Long linkedId; // 알림과 연관된 클럽, 일정, 매치 등의 ID | ||
| private Boolean isRead; | ||
| private NotificationStatus status; | ||
|
|
||
| @Builder | ||
| public NotificationEntity(User targetUser, NotificationType type, String title, String content, Long linkedId) { | ||
| this.targetUser = targetUser; | ||
| this.type = type; | ||
| this.title = title; | ||
| this.content = content; | ||
| this.linkedId = linkedId; | ||
| this.isRead = false; | ||
| this.status = NotificationStatus.READY; | ||
| } | ||
|
|
||
| public static NotificationEntity create(User targetUser, NotificationType type, String content, String title, Long linkedId) { | ||
| return NotificationEntity.builder() | ||
| .targetUser(targetUser) | ||
| .type(type) | ||
| .content(content) | ||
| .title(title) | ||
| .linkedId(linkedId) | ||
| .build(); | ||
| } | ||
|
|
||
| public void read() { | ||
| isRead = true; | ||
| } | ||
|
|
||
| public void sent() { | ||
| status = NotificationStatus.SENT; | ||
| } | ||
|
|
||
| public void failed() { | ||
| status = NotificationStatus.FAILED; | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/example/moim/notification/entity/NotificationStatus.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package com.example.moim.notification.entity; | ||
|
|
||
| public enum NotificationStatus { | ||
| READY, | ||
| SENT, | ||
| FAILED, | ||
| ; | ||
| } |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/example/moim/notification/entity/NotificationType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package com.example.moim.notification.entity; | ||
|
|
||
| import java.util.IllegalFormatException; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum NotificationType { | ||
| CLUB_JOIN("클럽 가입", "%s님이 %s에 가입했습니다."), | ||
| SCHEDULE_SAVE("일정 등록", "%s 일정이 등록되었습니다. \n 참가 여부를 투표해주세요!!"), | ||
| SCHEDULE_REMINDER("일정 하루 전", "내일 %s 일정이 있습니다."), | ||
| SCHEDULE_ENCOURAGE("투표 독려", "%s 일정이 참가투표가 곧 마감됩니다.\n 참가 여부를 투표해주세요!!"), | ||
| SCHEDULE_JOIN("일정 참여", "%s 일정에 참여했습니다."), | ||
| MATCH_SCHEDULED("매치 등록", "%s 클럽 %s %s 매치가 등록되었습니다.\n 매치정보를 확인하고 신청해주세요!"), | ||
| MATCH_SUCCESS("매칭 성공", "%s 클럽과의 %s %s 매치가 확정되었습니다.\n 매치정보를 다시 한 번 확인해주세요!"), | ||
| MATCH_REVIEW("매치 리뷰", "%s 클럽과의 매치는 즐거우셨나요?\n %s 님의 득점 기록을 입력해주세요!"), | ||
| MATCH_SUGGESTION("매치 건의", "클럽원이 %s 클럽과의 %s %s 매치를 원합니다.\n 매치 정보를 확인하고 신청해주세요!"), | ||
| MATCH_REQUEST("매치 요청", "%s 클럽이 %s 매치에 신청했습니다.\n 클럽 정보를 확인하고 매치를 확정해주세요!"), | ||
| MATCH_INVITE("매치 초대", "%s 클럽에서 친선 매치를 제안했습니다.\n 클럽 정보를 확인하고 매치를 확정해주세요!"), | ||
| MATCH_FAILED_UNREQUESTED("매치 실패", "신청 클럽이 없어 <%s> 매치가 성사되지않았습니다 \uD83D\uDE2D\n 다음에 다시 등록해주세요!"), | ||
| MATCH_FAILED_UNSELECTED("매치 실패", "<%s> 매치 등록 클럽이 다른 클럽을 선택했어요\uD83E\uDEE3\n 다음에 다시 신청해주세요!"), | ||
| MATCH_CANCEL_USER("매치 취소", "<%s> 매치가 취소되었습니다.\n 다음에 다시 신청해주세요!"), | ||
| MATCH_CANCEL_CLUB("매치 취소", "<%s> 매치가 취소되었습니다.\n 다음에 다시 신청해주세요!"), | ||
| ; | ||
|
|
||
| private final String title; | ||
| private final String messageTemplate; | ||
|
|
||
| public String formatMessage(Object... args) throws IllegalFormatException { | ||
| return String.format(messageTemplate, args); | ||
| } | ||
|
|
||
| public String getCategory() { | ||
| return name().substring(0, name().indexOf("_")).toLowerCase(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
알림 타입을 ENUM 으로 구현한 것이 한눈에 알림 종류를 볼 수 있어서 깔끔하다고 생각합니다 👍