Skip to content
Merged
Show file tree
Hide file tree
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 Mar 12, 2025
43664c6
test(#10): NotificationService 테스트 메소드 작성
y3binchoi Mar 12, 2025
1ea5797
test(#10): 테스트명이 행동을 잘 묘사하도록 테스트 메소드명 변경
y3binchoi Mar 16, 2025
b793501
test(#10): Mockito 주입 어노테이션으로 대체
y3binchoi Mar 21, 2025
c35f31b
refactor(#20): API 응답 구조 통일
y3binchoi Mar 28, 2025
6cf3515
refactor(#20): NotificationController에서 Notice로 잘못 줄여 쓴 부분 수정
y3binchoi Mar 28, 2025
23166a1
chore(#20): FCM 알림을 위한 비공개 키 숨김
y3binchoi Mar 28, 2025
83db59d
test(#10): Notification 동기 이벤트에 대한 단위 테스트 추가
y3binchoi Mar 30, 2025
704dbea
feat(#20): 예외 처리 어드바이스 생성
y3binchoi Apr 2, 2025
10ebfa5
refactor(#20): OCP 위반하는 NotificationEventHandler 삭제
y3binchoi Apr 9, 2025
f9064d8
refactor(#20): Notification 이 중복되는 이름이라 NotificationEntity로 정정
y3binchoi Apr 9, 2025
51dec4b
refactor(#20): 알림 컨트롤러와 서비스 메서드명 간소화
y3binchoi Apr 9, 2025
7c9236f
refactor(#20): 알림 전송 책임 분리
y3binchoi Apr 9, 2025
e52a448
refactor(#20): 이벤트 종류에 따라 전략을 선택하도록 책임 분리
y3binchoi Apr 9, 2025
234bfb6
refactor(#20): 각 이벤트별 전략 구현
y3binchoi Apr 9, 2025
8eaeeea
refactor(#20): 알림 저장 애플리케이션 로직 구현
y3binchoi Apr 9, 2025
5cc3dad
refactor(#20): NotificationEntity로 이름 바꿨음
y3binchoi Apr 9, 2025
4b0f6da
test(#20): 알림 생성 도메인 로직 변경됨
y3binchoi Apr 9, 2025
f9913ea
refactor(#20): 알림 레포지토리 의존성 역전
y3binchoi Apr 9, 2025
0abd7db
fix(#20): 알림 저장 활성화
y3binchoi Apr 9, 2025
38e09f1
refactore(#20): 알림 서비스 레이어 의존성 역전
y3binchoi Apr 9, 2025
a0fb47f
test(#20): 알림 저장 및 전송 테스트
y3binchoi Apr 9, 2025
0cb639f
test(#20): 알림 전송 서비스 테스트 코드
y3binchoi Apr 9, 2025
1e59b0c
chore(#20): Fcm 연동 설정
y3binchoi Apr 9, 2025
080d901
chore(#20): test 디렉토리의 클래스들은 test/resource에 있는 properties를 우선적으로 인식한다
y3binchoi Apr 9, 2025
c1e8031
test(#20): 비동기 이벤트 서비스 테스트
y3binchoi Apr 9, 2025
ffc2d85
refactor(#20): 알림 생성 서비스 Strategy -> Handler 이름 수정
y3binchoi Apr 13, 2025
173c5a5
test(#20): 알림 서비스단 테스트 메서드명 형식 통일
y3binchoi Apr 13, 2025
dca10eb
fix(#20): Firebase 키 파일 지정
y3binchoi Apr 13, 2025
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ build/generated/
application-local.properties
application-test.properties
application-s3.properties
test_img_dir
test_img_dir

**/src/main/resources/secret
9 changes: 8 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ dependencies {
testCompileOnly 'org.projectlombok:lombok'
testAnnotationProcessor 'org.projectlombok:lombok'
testRuntimeOnly 'com.h2database:h2'
testImplementation 'org.awaitility:awaitility:4.2.0'
}

sourceSets {
Expand All @@ -70,4 +71,10 @@ sourceSets {

tasks.named('test') {
useJUnitPlatform()
}
}

test {
jvmArgs += [
'--add-opens=java.base/java.lang=ALL-UNNAMED'
]
}
57 changes: 33 additions & 24 deletions src/main/java/com/example/moim/external/fcm/FcmConfig.java
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
public interface NotificationControllerDocs {

@Operation(summary = "새로운 알림 있는지 체크")
NotificationExistOutput noticeCheck(@AuthenticationPrincipal UserDetailsImpl userDetailsImpl);
NotificationExistOutput notificationUnreadCount(@AuthenticationPrincipal UserDetailsImpl userDetailsImpl);

@Operation(summary = "알림 조회")
List<NotificationOutput> noticeFind(@AuthenticationPrincipal UserDetailsImpl userDetailsImpl);
List<NotificationOutput> notificationFind(@AuthenticationPrincipal UserDetailsImpl userDetailsImpl);

@Operation(summary = "알림 삭제")
void noticeRemove(@AuthenticationPrincipal UserDetailsImpl userDetailsImpl, @PathVariable Long id);
void notificationRemove(@AuthenticationPrincipal UserDetailsImpl userDetailsImpl, @PathVariable Long id);
}
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);
}
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();
}
}
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;
}
}
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,
;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.example.moim.notification.entity;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

알림 타입을 ENUM 으로 구현한 것이 한눈에 알림 종류를 볼 수 있어서 깔끔하다고 생각합니다 👍


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();
}
}
Loading