Skip to content

Commit 93c7196

Browse files
committed
feat(ServerEvent): 서브타입 검증 추가
- 서버 이벤트 타입과 그 서브 타입은 관련이 있어야 함.
1 parent 6c7808b commit 93c7196

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/main/java/com/somemore/global/common/event/ServerEvent.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,19 @@ protected ServerEvent(
2424
@JsonProperty(value = "subType", required = true) T subType,
2525
@JsonProperty(value = "createdAt", required = true) LocalDateTime createdAt
2626
) {
27+
validate(type, subType);
2728
this.type = type;
2829
this.subType = subType;
2930
this.createdAt = (createdAt == null) ? LocalDateTime.now() : createdAt;
3031
}
32+
33+
private void validate(ServerEventType type, T subType) {
34+
if (!type.getSubtype().isInstance(subType)) {
35+
throw new IllegalArgumentException(String.format(
36+
"잘못된 서브타입: %s는 %s와 일치하지 않습니다.",
37+
subType,
38+
type.getSubtype()
39+
));
40+
}
41+
}
3142
}

src/main/java/com/somemore/notification/converter/MessageConverter.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package com.somemore.notification.converter;
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
4-
import com.fasterxml.jackson.databind.JsonMappingException;
54
import com.fasterxml.jackson.databind.JsonNode;
65
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.somemore.community.event.CommentAddedEvent;
77
import com.somemore.facade.event.VolunteerReviewRequestEvent;
88
import com.somemore.notification.domain.Notification;
99
import com.somemore.notification.domain.NotificationSubType;
@@ -29,6 +29,7 @@ public Notification from(String message) {
2929
case NOTE_BLAH_BLAH -> throw new UnsupportedOperationException("NOTE 알림 타입 처리 로직 미구현");
3030
case VOLUNTEER_REVIEW_REQUEST -> buildVolunteerReviewRequestNotification(message);
3131
case VOLUNTEER_APPLY_STATUS_CHANGE -> buildVolunteerApplyStatusChangeNotification(message);
32+
case COMMENT_ADDED -> buildCommentAddedNotification(message);
3233
};
3334
} catch (Exception e) {
3435
log.error(e.getMessage());
@@ -58,6 +59,17 @@ private Notification buildVolunteerApplyStatusChangeNotification(String message)
5859
.build();
5960
}
6061

62+
private Notification buildCommentAddedNotification(String message) throws JsonProcessingException {
63+
CommentAddedEvent event = objectMapper.readValue(message, CommentAddedEvent.class);
64+
65+
return Notification.builder()
66+
.receiverId(event.getVolunteerId())
67+
.title(createCommentAddedNotificationTitle())
68+
.type(NotificationSubType.COMMENT_ADDED)
69+
.relatedId(event.getCommunityBoardId())
70+
.build();
71+
}
72+
6173
private String createVolunteerReviewRequestNotificationTitle() {
6274
return "최근 활동하신 활동의 후기를 작성해 주세요!";
6375
}
@@ -72,4 +84,8 @@ private String createVolunteerApplyStatusChangeNotificationTitle(ApplyStatus new
7284
}
7385
};
7486
}
87+
88+
private String createCommentAddedNotificationTitle() {
89+
return "새로운 댓글이 작성되었습니다.";
90+
}
7591
}

0 commit comments

Comments
 (0)