Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 30 additions & 0 deletions src/main/java/com/somemore/community/event/CommentAddedEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.somemore.community.event;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.somemore.global.common.event.ServerEvent;
import com.somemore.global.common.event.ServerEventType;
import com.somemore.notification.domain.NotificationSubType;
import lombok.Getter;
import lombok.experimental.SuperBuilder;

import java.time.LocalDateTime;
import java.util.UUID;

@Getter
@SuperBuilder
public class CommentAddedEvent extends ServerEvent<NotificationSubType> {

private final UUID volunteerId;
private final Long communityBoardId;

@JsonCreator
public CommentAddedEvent(
@JsonProperty(value = "volunteerId", required = true) UUID volunteerId,
@JsonProperty(value = "communityBoardId", required = true) Long communityBoardId
) {
super(ServerEventType.NOTIFICATION, NotificationSubType.COMMENT_ADDED, LocalDateTime.now());
this.volunteerId = volunteerId;
this.communityBoardId = communityBoardId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

import com.somemore.community.domain.CommunityComment;
import com.somemore.community.dto.request.CommunityCommentCreateRequestDto;
import com.somemore.community.event.CommentAddedEvent;
import com.somemore.community.repository.board.CommunityBoardRepository;
import com.somemore.community.repository.comment.CommunityCommentRepository;
import com.somemore.community.usecase.comment.CreateCommunityCommentUseCase;
import com.somemore.global.common.event.ServerEventPublisher;
import com.somemore.global.common.event.ServerEventType;
import com.somemore.global.exception.BadRequestException;
import com.somemore.notification.domain.NotificationSubType;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -22,6 +27,7 @@ public class CreateCommunityCommentService implements CreateCommunityCommentUseC

private final CommunityBoardRepository communityBoardRepository;
private final CommunityCommentRepository communityCommentRepository;
private final ServerEventPublisher serverEventPublisher;

@Override
public Long createCommunityComment(CommunityCommentCreateRequestDto requestDto, UUID writerId, Long communityBoardId) {
Expand All @@ -33,6 +39,7 @@ public Long createCommunityComment(CommunityCommentCreateRequestDto requestDto,
validateParentCommentExists(communityComment.getParentCommentId());
}

publishCommentAddedEvent(communityComment);
return communityCommentRepository.save(communityComment).getId();
}

Expand All @@ -47,4 +54,37 @@ private void validateParentCommentExists(Long parentCommentId) {
throw new BadRequestException(NOT_EXISTS_COMMUNITY_COMMENT.getMessage());
}
}

private void publishCommentAddedEvent(CommunityComment communityComment) {
Long parentCommentId = communityComment.getParentCommentId();

UUID targetVolunteerId = getTargetVolunteerId(communityComment, parentCommentId);

CommentAddedEvent event = CommentAddedEvent.builder()
.type(ServerEventType.NOTIFICATION)
.subType(NotificationSubType.COMMENT_ADDED)
.volunteerId(targetVolunteerId)
.communityBoardId(communityComment.getCommunityBoardId())
.build();

serverEventPublisher.publish(event);
}

private UUID getTargetVolunteerId(CommunityComment communityComment, Long parentCommentId) {
UUID targetVolunteerId;

if (parentCommentId == null) {
targetVolunteerId = communityBoardRepository.findById(communityComment.getCommunityBoardId())
.orElseThrow(EntityNotFoundException::new)
.getWriterId();

return targetVolunteerId;
}

targetVolunteerId = communityCommentRepository.findById(parentCommentId)
.map(CommunityComment::getWriterId)
.orElse(null);

return targetVolunteerId;
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/somemore/global/common/event/ServerEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,19 @@ protected ServerEvent(
@JsonProperty(value = "subType", required = true) T subType,
@JsonProperty(value = "createdAt", required = true) LocalDateTime createdAt
) {
validate(type, subType);
this.type = type;
this.subType = subType;
this.createdAt = (createdAt == null) ? LocalDateTime.now() : createdAt;
}

private void validate(ServerEventType type, T subType) {
if (!type.getSubtype().isInstance(subType)) {
throw new IllegalArgumentException(String.format(
"잘못된 서브타입: %s는 %s와 일치하지 않습니다.",
subType,
type.getSubtype()
));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.somemore.notification.converter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.somemore.community.event.CommentAddedEvent;
import com.somemore.facade.event.VolunteerReviewRequestEvent;
import com.somemore.notification.domain.Notification;
import com.somemore.notification.domain.NotificationSubType;
Expand All @@ -29,6 +29,7 @@ public Notification from(String message) {
case NOTE_BLAH_BLAH -> throw new UnsupportedOperationException("NOTE 알림 타입 처리 로직 미구현");
case VOLUNTEER_REVIEW_REQUEST -> buildVolunteerReviewRequestNotification(message);
case VOLUNTEER_APPLY_STATUS_CHANGE -> buildVolunteerApplyStatusChangeNotification(message);
case COMMENT_ADDED -> buildCommentAddedNotification(message);
};
} catch (Exception e) {
log.error(e.getMessage());
Expand Down Expand Up @@ -58,6 +59,17 @@ private Notification buildVolunteerApplyStatusChangeNotification(String message)
.build();
}

private Notification buildCommentAddedNotification(String message) throws JsonProcessingException {
CommentAddedEvent event = objectMapper.readValue(message, CommentAddedEvent.class);

return Notification.builder()
.receiverId(event.getVolunteerId())
.title(createCommentAddedNotificationTitle())
.type(NotificationSubType.COMMENT_ADDED)
.relatedId(event.getCommunityBoardId())
.build();
}

private String createVolunteerReviewRequestNotificationTitle() {
return "최근 활동하신 활동의 후기를 작성해 주세요!";
}
Expand All @@ -72,4 +84,8 @@ private String createVolunteerApplyStatusChangeNotificationTitle(ApplyStatus new
}
};
}

private String createCommentAddedNotificationTitle() {
return "새로운 댓글이 작성되었습니다.";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
public enum NotificationSubType {
NOTE_BLAH_BLAH("쪽지"),
VOLUNTEER_REVIEW_REQUEST("봉사 후기 요청"),
VOLUNTEER_APPLY_STATUS_CHANGE("신청 상태 변경")
VOLUNTEER_APPLY_STATUS_CHANGE("신청 상태 변경"),
COMMENT_ADDED("댓글 대댓글 추가")
;

private final String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import java.time.LocalDateTime;

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
@Schema(description = "알림 응답 전송 DTO")
@Schema(description = "알림 응답 DTO")
public record NotificationResponseDto(
@Schema(description = "알림 ID", example = "1")
Long id,
@Schema(description = "알림 제목", example = "봉사 활동 신청이 승인되었습니다.")
String title,

Expand All @@ -25,6 +27,7 @@ public record NotificationResponseDto(

public static NotificationResponseDto from(Notification notification) {
return new NotificationResponseDto(
notification.getId(),
notification.getTitle(),
notification.getType(),
notification.getRelatedId(),
Expand Down
Loading