Skip to content

Commit b1dd455

Browse files
committed
Refactor: 서비스와 이벤트 리스너 역할 분리 + 테스트 수정
1 parent b130873 commit b1dd455

File tree

8 files changed

+199
-880
lines changed

8 files changed

+199
-880
lines changed

src/main/java/com/back/domain/notification/event/community/CommunityNotificationEventListener.java

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
import com.back.domain.notification.entity.NotificationSettingType;
44
import com.back.domain.notification.service.NotificationService;
5-
import com.back.domain.user.entity.User;
6-
import com.back.domain.user.repository.UserRepository;
7-
import com.back.global.exception.CustomException;
8-
import com.back.global.exception.ErrorCode;
95
import lombok.RequiredArgsConstructor;
106
import lombok.extern.slf4j.Slf4j;
117
import org.springframework.context.event.EventListener;
@@ -18,7 +14,6 @@
1814
public class CommunityNotificationEventListener {
1915

2016
private final NotificationService notificationService;
21-
private final UserRepository userRepository;
2217

2318
// 댓글 작성 시 - 게시글 작성자에게 알림
2419
@EventListener
@@ -28,15 +23,9 @@ public void handleCommentCreated(CommentCreatedEvent event) {
2823
event.getPostId(), event.getCommentId(), event.getActorId());
2924

3025
try {
31-
User actor = userRepository.findById(event.getActorId())
32-
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
33-
34-
User receiver = userRepository.findById(event.getReceiverId())
35-
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
36-
3726
notificationService.createCommunityNotification(
38-
receiver,
39-
actor,
27+
event.getReceiverId(),
28+
event.getActorId(),
4029
event.getTitle(),
4130
event.getContent(),
4231
"/posts/" + event.getPostId(),
@@ -58,15 +47,9 @@ public void handleReplyCreated(ReplyCreatedEvent event) {
5847
event.getParentCommentId(), event.getReplyId(), event.getActorId());
5948

6049
try {
61-
User actor = userRepository.findById(event.getActorId())
62-
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
63-
64-
User receiver = userRepository.findById(event.getReceiverId())
65-
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
66-
6750
notificationService.createCommunityNotification(
68-
receiver,
69-
actor,
51+
event.getReceiverId(),
52+
event.getActorId(),
7053
event.getTitle(),
7154
event.getContent(),
7255
"/posts/" + event.getPostId() + "#comment-" + event.getParentCommentId(),
@@ -88,15 +71,9 @@ public void handlePostLiked(PostLikedEvent event) {
8871
event.getPostId(), event.getActorId());
8972

9073
try {
91-
User actor = userRepository.findById(event.getActorId())
92-
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
93-
94-
User receiver = userRepository.findById(event.getReceiverId())
95-
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
96-
9774
notificationService.createCommunityNotification(
98-
receiver,
99-
actor,
75+
event.getReceiverId(),
76+
event.getActorId(),
10077
event.getTitle(),
10178
event.getContent(),
10279
"/posts/" + event.getPostId(),
@@ -118,15 +95,9 @@ public void handleCommentLiked(CommentLikedEvent event) {
11895
event.getCommentId(), event.getActorId());
11996

12097
try {
121-
User actor = userRepository.findById(event.getActorId())
122-
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
123-
124-
User receiver = userRepository.findById(event.getReceiverId())
125-
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
126-
12798
notificationService.createCommunityNotification(
128-
receiver,
129-
actor,
99+
event.getReceiverId(),
100+
event.getActorId(),
130101
event.getTitle(),
131102
event.getContent(),
132103
"/posts/" + event.getPostId() + "#comment-" + event.getCommentId(),

src/main/java/com/back/domain/notification/event/study/StudyNotificationEventListener.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
public class StudyNotificationEventListener {
1919

2020
private final NotificationService notificationService;
21-
private final UserRepository userRepository;
2221

2322
// 학습 기록 등록 시 - 본인에게 알림
2423
@EventListener
@@ -28,11 +27,8 @@ public void handleStudyRecordCreated(StudyRecordCreatedEvent event) {
2827
event.getUserId(), event.getDuration());
2928

3029
try {
31-
User user = userRepository.findById(event.getUserId())
32-
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
33-
3430
notificationService.createSelfNotification(
35-
user,
31+
event.getUserId(),
3632
event.getTitle(),
3733
event.getContent(),
3834
"/study/records/" + event.getStudyRecordId(),
@@ -55,11 +51,8 @@ public void handleDailyGoalAchieved(DailyGoalAchievedEvent event) {
5551
event.getCompletedPlans(), event.getTotalPlans());
5652

5753
try {
58-
User user = userRepository.findById(event.getUserId())
59-
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
60-
6154
notificationService.createSelfNotification(
62-
user,
55+
event.getUserId(),
6356
event.getTitle(),
6457
event.getContent(),
6558
"/study/plans?date=" + event.getAchievedDate(),

src/main/java/com/back/domain/notification/event/studyroom/StudyRoomNotificationEventListener.java

Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
public class StudyRoomNotificationEventListener {
2222

2323
private final NotificationService notificationService;
24-
private final RoomRepository roomRepository;
25-
private final UserRepository userRepository;
26-
private final RoomMemberRepository roomMemberRepository;
2724

2825
// 스터디룸 공지사항 등록 시 - 전체 멤버에게 알림
2926
@EventListener
@@ -33,18 +30,9 @@ public void handleNoticeCreated(StudyRoomNoticeCreatedEvent event) {
3330
event.getStudyRoomId(), event.getActorId());
3431

3532
try {
36-
// Room 조회
37-
Room room = roomRepository.findById(event.getStudyRoomId())
38-
.orElseThrow(() -> new CustomException(ErrorCode.ROOM_NOT_FOUND));
39-
40-
// Actor (공지 작성자) 조회
41-
User actor = userRepository.findById(event.getActorId())
42-
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
43-
44-
// 스터디룸 알림 생성
4533
notificationService.createRoomNotification(
46-
room,
47-
actor,
34+
event.getStudyRoomId(),
35+
event.getActorId(),
4836
event.getTitle(),
4937
event.getNoticeTitle(),
5038
"/rooms/" + event.getStudyRoomId() + "/notices",
@@ -67,18 +55,9 @@ public void handleMemberRoleChanged(MemberRoleChangedEvent event) {
6755
event.getStudyRoomId(), event.getTargetUserId(), event.getNewRole());
6856

6957
try {
70-
// 수신자 조회
71-
User receiver = userRepository.findById(event.getTargetUserId())
72-
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
73-
74-
// 발신자 조회
75-
User actor = userRepository.findById(event.getActorId())
76-
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
77-
78-
// 개인 알림 생성
7958
notificationService.createPersonalNotification(
80-
receiver,
81-
actor,
59+
event.getTargetUserId(),
60+
event.getActorId(),
8261
event.getTitle(),
8362
event.getContent(),
8463
"/rooms/" + event.getStudyRoomId(),
@@ -100,15 +79,9 @@ public void handleMemberKicked(MemberKickedEvent event) {
10079
event.getStudyRoomId(), event.getTargetUserId());
10180

10281
try {
103-
User receiver = userRepository.findById(event.getTargetUserId())
104-
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
105-
106-
User actor = userRepository.findById(event.getActorId())
107-
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
108-
10982
notificationService.createPersonalNotification(
110-
receiver,
111-
actor,
83+
event.getTargetUserId(),
84+
event.getActorId(),
11285
event.getTitle(),
11386
event.getContent(),
11487
"/rooms",
@@ -130,15 +103,9 @@ public void handleOwnerTransferred(OwnerTransferredEvent event) {
130103
event.getStudyRoomId(), event.getNewOwnerId());
131104

132105
try {
133-
User receiver = userRepository.findById(event.getNewOwnerId())
134-
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
135-
136-
User actor = userRepository.findById(event.getActorId())
137-
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
138-
139106
notificationService.createPersonalNotification(
140-
receiver,
141-
actor,
107+
event.getNewOwnerId(),
108+
event.getActorId(),
142109
event.getTitle(),
143110
event.getContent(),
144111
"/rooms/" + event.getStudyRoomId(),

src/main/java/com/back/domain/notification/service/NotificationService.java

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
import com.back.domain.notification.repository.NotificationReadRepository;
88
import com.back.domain.notification.repository.NotificationRepository;
99
import com.back.domain.studyroom.entity.Room;
10+
import com.back.domain.studyroom.repository.RoomRepository;
1011
import com.back.domain.user.entity.User;
12+
import com.back.domain.user.repository.UserRepository;
1113
import com.back.global.exception.CustomException;
1214
import com.back.global.exception.ErrorCode;
1315
import lombok.RequiredArgsConstructor;
@@ -30,79 +32,86 @@ public class NotificationService {
3032
private final NotificationReadRepository notificationReadRepository;
3133
private final NotificationWebSocketService webSocketService;
3234
private final NotificationSettingService notificationSettingService;
35+
private final UserRepository userRepository;
36+
private final RoomRepository roomRepository;
3337

3438
// ==================== 알림 생성 및 전송 ====================
3539

3640
// 개인 알림 생성 및 전송
3741
@Transactional
3842
public Notification createPersonalNotification(
39-
User receiver,
40-
User actor,
43+
Long receiverId,
44+
Long actorId,
4145
String title,
4246
String content,
4347
String targetUrl,
4448
NotificationSettingType settingType) {
4549

50+
User receiver = userRepository.findById(receiverId).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
51+
User actor = userRepository.findById(actorId).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
52+
4653
// 자기 자신에게 알림 방지
4754
validateActorAndReceiver(receiver, actor);
4855

4956
// DB에 알림 저장
5057
Notification notification = Notification.createPersonalNotification(
5158
receiver, actor, title, content, targetUrl);
52-
notificationRepository.save(notification);
59+
Notification savedNotification = notificationRepository.save(notification);
5360

54-
// 알림 설정 체크 후 전송
5561
if (shouldSendNotification(receiver.getId(), settingType)) {
56-
NotificationWebSocketDto dto = NotificationWebSocketDto.from(notification);
62+
NotificationWebSocketDto dto = NotificationWebSocketDto.from(savedNotification); // ID가 있는 객체 사용
5763
webSocketService.sendNotificationToUser(receiver.getId(), dto);
58-
log.info("개인 알림 전송 - 수신자 ID: {}, 발신자 ID: {}, 알림 ID: {}, 설정 타입: {}",
59-
receiver.getId(), actor.getId(), notification.getId(), settingType);
64+
65+
log.info("개인 알림 전송 - 수신자 ID: {}, ... 알림 ID: {}", receiver.getId(), savedNotification.getId());
6066
} else {
61-
log.info("개인 알림 저장만 완료 (전송 생략) - 수신자 ID: {}, 알림 ID: {}, 설정 타입: {}",
62-
receiver.getId(), notification.getId(), settingType);
67+
log.info("개인 알림 저장만 완료 - ... 알림 ID: {}", receiver.getId(), savedNotification.getId());
6368
}
6469

65-
return notification;
70+
return savedNotification;
6671
}
6772

6873
// 개인 알림 생성 및 전송
6974
@Transactional
7075
public Notification createSelfNotification(
71-
User user,
76+
Long userId,
7277
String title,
7378
String content,
7479
String targetUrl,
7580
NotificationSettingType settingType) {
7681

82+
User user = userRepository.findById(userId).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
83+
7784
// DB에 알림 저장
7885
Notification notification = Notification.createPersonalNotification(
7986
user, user, title, content, targetUrl);
80-
notificationRepository.save(notification);
87+
Notification savedNotification = notificationRepository.save(notification);
8188

8289
// 알림 설정 체크 후 전송
8390
if (shouldSendNotification(user.getId(), settingType)) {
84-
NotificationWebSocketDto dto = NotificationWebSocketDto.from(notification);
91+
NotificationWebSocketDto dto = NotificationWebSocketDto.from(savedNotification);
8592
webSocketService.sendNotificationToUser(user.getId(), dto);
86-
log.info("자기 자신 알림 전송 - 사용자 ID: {}, 알림 ID: {}, 설정 타입: {}",
87-
user.getId(), notification.getId(), settingType);
93+
94+
log.info("자기 자신 알림 전송 - ... 알림 ID: {}", user.getId(), savedNotification.getId());
8895
} else {
89-
log.info("자기 자신 알림 저장만 완료 (전송 생략) - 사용자 ID: {}, 알림 ID: {}, 설정 타입: {}",
90-
user.getId(), notification.getId(), settingType);
96+
log.info("자기 자신 알림 저장만 완료 - ... 알림 ID: {}", user.getId(), savedNotification.getId());
9197
}
9298

93-
return notification;
99+
return savedNotification;
94100
}
95101

96102
// 스터디룸 알림 생성 및 전송
97103
@Transactional
98104
public Notification createRoomNotification(
99-
Room room,
100-
User actor,
105+
Long roomId,
106+
Long actorId,
101107
String title,
102108
String content,
103109
String targetUrl,
104110
NotificationSettingType settingType) {
105111

112+
Room room = roomRepository.findById(roomId).orElseThrow(() -> new CustomException(ErrorCode.ROOM_NOT_FOUND));
113+
User actor = userRepository.findById(actorId).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
114+
106115
Notification notification = Notification.createRoomNotification(
107116
room, actor, title, content, targetUrl);
108117
notificationRepository.save(notification);
@@ -134,13 +143,16 @@ public Notification createSystemNotification(String title, String content, Strin
134143
// 커뮤니티 알림 생성 및 전송
135144
@Transactional
136145
public Notification createCommunityNotification(
137-
User receiver,
138-
User actor,
146+
Long receiverId,
147+
Long actorId,
139148
String title,
140149
String content,
141150
String targetUrl,
142151
NotificationSettingType settingType) {
143152

153+
User receiver = userRepository.findById(receiverId).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
154+
User actor = userRepository.findById(actorId).orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
155+
144156
// 자기 자신에게 알림 방지
145157
validateActorAndReceiver(receiver, actor);
146158

0 commit comments

Comments
 (0)