Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public UUID getIdByUserId(UUID userId) {
return getByUserId(userId).getId();
}

@Override
public UUID getUserIdById(UUID centerId) {
return centerRepository.findById(centerId)
.orElseThrow(() -> new NoSuchElementException(ExceptionMessage.NOT_EXISTS_CENTER))
.getUserId();
}

@Override
public CenterProfileResponseDto getCenterProfileById(UUID centerId) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public interface NEWCenterQueryUseCase {

UUID getIdByUserId(UUID userId);

UUID getUserIdById(UUID centerId);

CenterProfileResponseDto getCenterProfileById(UUID centerId);

void validateCenterExists(UUID id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,13 @@ public CommentAddedEvent(
this.volunteerId = volunteerId;
this.communityBoardId = communityBoardId;
}

public static CommentAddedEvent of(UUID targetUserId, Long communityBoardId) {
return CommentAddedEvent.builder()
.type(ServerEventType.NOTIFICATION)
.subType(NotificationSubType.COMMENT_ADDED)
.volunteerId(targetUserId)
.communityBoardId(communityBoardId)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
import com.somemore.domains.community.repository.board.CommunityBoardRepository;
import com.somemore.domains.community.repository.comment.CommunityCommentRepository;
import com.somemore.domains.community.usecase.comment.CreateCommunityCommentUseCase;
import com.somemore.domains.notification.domain.NotificationSubType;
import com.somemore.global.common.event.ServerEventPublisher;
import com.somemore.global.common.event.ServerEventType;
import com.somemore.global.exception.BadRequestException;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -39,7 +37,8 @@ public Long createCommunityComment(CommunityCommentCreateRequestDto requestDto,
validateParentCommentExists(communityComment.getParentCommentId());
}

publishCommentAddedEvent(communityComment);
publishCommentAddedEvent(communityComment, writerId);

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

Expand All @@ -55,36 +54,35 @@ private void validateParentCommentExists(Long parentCommentId) {
}
}

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

UUID targetVolunteerId = getTargetVolunteerId(communityComment, parentCommentId);
UUID targetUserId = getTargetUserId(communityComment, parentCommentId);

if (writerId == targetUserId) {
return;
}

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

serverEventPublisher.publish(event);
}

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

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

return targetVolunteerId;
return targetUserId;
}

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

return targetVolunteerId;
return targetUserId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.somemore.domains.notification.domain.NotificationSubType;
import com.somemore.domains.recruitboard.event.CreateRecruitBoardEvent;
import com.somemore.global.common.event.ServerEvent;
import com.somemore.global.common.event.ServerEventType;
import lombok.Getter;
Expand All @@ -29,4 +30,14 @@ public InterestCenterCreateRecruitBoardEvent(
this.centerId = centerId;
this.recruitBoardId = recruitBoardId;
}

public static InterestCenterCreateRecruitBoardEvent of(CreateRecruitBoardEvent createRecruitBoardEvent, UUID volunteerId, UUID centerId) {
return InterestCenterCreateRecruitBoardEvent.builder()
.type(ServerEventType.NOTIFICATION)
.subType(NotificationSubType.INTEREST_CENTER_CREATE_RECRUIT_BOARD)
.volunteerId(volunteerId)
.centerId(centerId)
.recruitBoardId(createRecruitBoardEvent.getRecruitBoardId())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import com.somemore.domains.interestcenter.event.domain.InterestCenterCreateRecruitBoardEvent;
import com.somemore.domains.interestcenter.usecase.InterestCenterQueryUseCase;
import com.somemore.domains.notification.domain.NotificationSubType;
import com.somemore.domains.recruitboard.event.CreateRecruitBoardEvent;
import com.somemore.global.common.event.ServerEventPublisher;
import com.somemore.global.common.event.ServerEventType;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -32,14 +30,7 @@ public void handle(CreateRecruitBoardEvent createRecruitBoardEvent) {
}

private void publishInterestCenterCreateRecruitBoardEvent(CreateRecruitBoardEvent createRecruitBoardEvent, UUID volunteerId, UUID centerId) {
InterestCenterCreateRecruitBoardEvent event = InterestCenterCreateRecruitBoardEvent.builder()
.type(ServerEventType.NOTIFICATION)
.subType(NotificationSubType.INTEREST_CENTER_CREATE_RECRUIT_BOARD)
.volunteerId(volunteerId)
.centerId(centerId)
.recruitBoardId(createRecruitBoardEvent.getRecruitBoardId())
.build();

InterestCenterCreateRecruitBoardEvent event = InterestCenterCreateRecruitBoardEvent.of(createRecruitBoardEvent, volunteerId, centerId);
serverEventPublisher.publish(event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.somemore.domains.notification.dto.NotificationResponseDto;
import com.somemore.domains.notification.usecase.NotificationQueryUseCase;
import com.somemore.global.auth.annotation.CurrentUser;
import com.somemore.global.auth.annotation.UserId;
import com.somemore.global.common.response.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand All @@ -27,7 +27,7 @@ public class NotificationQueryController {
@Operation(summary = "읽지 않은 알림 조회", description = "읽지 않은 알림들을 조회합니다.")
@GetMapping("/unread")
public ApiResponse<List<NotificationResponseDto>> getUnreadNotifications(
@CurrentUser UUID userId
@UserId UUID userId
) {
return ApiResponse.ok(200,
notificationQueryUseCase.getUnreadNotifications(userId),
Expand All @@ -39,7 +39,7 @@ public ApiResponse<List<NotificationResponseDto>> getUnreadNotifications(
@Operation(summary = "읽은 알림 조회", description = "읽은 알림들을 조회합니다.")
@GetMapping("/read")
public ApiResponse<List<NotificationResponseDto>> getReadNotifications(
@CurrentUser UUID userId
@UserId UUID userId
) {

return ApiResponse.ok(200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.somemore.center.usecase.NEWCenterQueryUseCase;
import com.somemore.domains.community.event.CommentAddedEvent;
import com.somemore.domains.interestcenter.event.domain.InterestCenterCreateRecruitBoardEvent;
import com.somemore.domains.notification.domain.Notification;
Expand All @@ -11,18 +12,23 @@
import com.somemore.domains.volunteerapply.event.VolunteerApplyEvent;
import com.somemore.domains.volunteerapply.event.VolunteerApplyStatusChangeEvent;
import com.somemore.domains.volunteerapply.event.VolunteerReviewRequestEvent;
import com.somemore.volunteer.usecase.NEWVolunteerQueryUseCase;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Slf4j
@RequiredArgsConstructor
@Component
public class NotificationMessageConverter {

private final ObjectMapper objectMapper;
private final NEWVolunteerQueryUseCase volunteerQueryUseCase;
private final NEWCenterQueryUseCase centerQueryUseCase;

public static final String SUB_TYPE = "subType";
private static final String SUB_TYPE = "subType";

public Notification from(String message) {
try {
Expand Down Expand Up @@ -50,8 +56,10 @@ private Notification buildNewNoteNotification(String message) {
private Notification buildVolunteerReviewRequestNotification(String message) throws JsonProcessingException {
VolunteerReviewRequestEvent event = objectMapper.readValue(message, VolunteerReviewRequestEvent.class);

UUID volunteerUserId = volunteerQueryUseCase.getUserIdById(event.getVolunteerId());

return Notification.builder()
.receiverId(event.getVolunteerId())
.receiverId(volunteerUserId)
.title(createVolunteerReviewRequestNotificationTitle())
.type(NotificationSubType.VOLUNTEER_REVIEW_REQUEST)
.relatedId(event.getRecruitBoardId())
Expand All @@ -61,8 +69,10 @@ private Notification buildVolunteerReviewRequestNotification(String message) thr
private Notification buildVolunteerApplyStatusChangeNotification(String message) throws JsonProcessingException {
VolunteerApplyStatusChangeEvent event = objectMapper.readValue(message, VolunteerApplyStatusChangeEvent.class);

UUID volunteerUserId = volunteerQueryUseCase.getUserIdById(event.getVolunteerId());

return Notification.builder()
.receiverId(event.getVolunteerId())
.receiverId(volunteerUserId)
.title(createVolunteerApplyStatusChangeNotificationTitle(event.getNewStatus()))
.type(NotificationSubType.VOLUNTEER_APPLY_STATUS_CHANGE)
.relatedId(event.getRecruitBoardId())
Expand All @@ -72,8 +82,10 @@ private Notification buildVolunteerApplyStatusChangeNotification(String message)
private Notification buildCommentAddedNotification(String message) throws JsonProcessingException {
CommentAddedEvent event = objectMapper.readValue(message, CommentAddedEvent.class);

UUID volunteerUserId = volunteerQueryUseCase.getUserIdById(event.getVolunteerId());

return Notification.builder()
.receiverId(event.getVolunteerId())
.receiverId(volunteerUserId)
.title(createCommentAddedNotificationTitle())
.type(NotificationSubType.COMMENT_ADDED)
.relatedId(event.getCommunityBoardId())
Expand All @@ -83,8 +95,10 @@ private Notification buildCommentAddedNotification(String message) throws JsonPr
private Notification buildVolunteerApplyNotification(String message) throws JsonProcessingException {
VolunteerApplyEvent event = objectMapper.readValue(message, VolunteerApplyEvent.class);

UUID centerUserId = centerQueryUseCase.getUserIdById(event.getCenterId());

return Notification.builder()
.receiverId(event.getCenterId())
.receiverId(centerUserId)
.title(createVolunteerApplyNotificationTitle())
.type(NotificationSubType.VOLUNTEER_APPLY)
.relatedId(event.getRecruitBoardId())
Expand All @@ -94,8 +108,10 @@ private Notification buildVolunteerApplyNotification(String message) throws Json
private Notification buildInterestCenterCreateRecruitBoardNotification(String message) throws JsonProcessingException {
InterestCenterCreateRecruitBoardEvent event = objectMapper.readValue(message, InterestCenterCreateRecruitBoardEvent.class);

UUID volunteerUserId = volunteerQueryUseCase.getUserIdById(event.getVolunteerId());

return Notification.builder()
.receiverId(event.getVolunteerId())
.receiverId(volunteerUserId)
.title(createInterestCenterCreateRecruitBoardNotificationTitle())
.type(NotificationSubType.INTEREST_CENTER_CREATE_RECRUIT_BOARD)
.relatedId(event.getRecruitBoardId())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.somemore.domains.recruitboard.domain.RecruitBoard;
import com.somemore.global.common.event.DomainEventSubType;
import com.somemore.global.common.event.ServerEvent;
import com.somemore.global.common.event.ServerEventType;
Expand All @@ -26,4 +27,13 @@ public CreateRecruitBoardEvent(
this.centerId = centerId;
this.recruitBoardId = recruitBoardId;
}

public static CreateRecruitBoardEvent of(UUID centerId, RecruitBoard recruitBoard) {
return CreateRecruitBoardEvent.builder()
.type(ServerEventType.DOMAIN_EVENT)
.subType(DomainEventSubType.CREATE_RECRUIT_BOARD)
.centerId(centerId)
.recruitBoardId(recruitBoard.getId())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import com.somemore.domains.recruitboard.repository.RecruitBoardRepository;
import com.somemore.domains.recruitboard.service.validator.RecruitBoardValidator;
import com.somemore.domains.recruitboard.usecase.CreateRecruitBoardUseCase;
import com.somemore.global.common.event.DomainEventSubType;
import com.somemore.global.common.event.ServerEventPublisher;
import com.somemore.global.common.event.ServerEventType;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -41,13 +39,7 @@ public Long createRecruitBoard(RecruitBoardCreateRequestDto dto, UUID centerId)
}

private void publishCreateRecruitBoardEvent(UUID centerId, RecruitBoard recruitBoard) {
CreateRecruitBoardEvent event = CreateRecruitBoardEvent.builder()
.type(ServerEventType.DOMAIN_EVENT)
.subType(DomainEventSubType.CREATE_RECRUIT_BOARD)
.centerId(centerId)
.recruitBoardId(recruitBoard.getId())
.build();

CreateRecruitBoardEvent event = CreateRecruitBoardEvent.of(centerId, recruitBoard);
serverEventPublisher.publish(event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.somemore.domains.notification.domain.NotificationSubType;
import com.somemore.domains.recruitboard.domain.RecruitBoard;
import com.somemore.domains.volunteerapply.domain.VolunteerApply;
import com.somemore.global.common.event.ServerEvent;
import com.somemore.global.common.event.ServerEventType;
import lombok.Getter;
Expand Down Expand Up @@ -32,4 +34,15 @@ public VolunteerApplyEvent(
this.centerId = centerId;
this.recruitBoardId = recruitBoardId;
}

public static VolunteerApplyEvent of(VolunteerApply apply, RecruitBoard board) {
return VolunteerApplyEvent.builder()
.type(ServerEventType.NOTIFICATION)
.subType(NotificationSubType.VOLUNTEER_APPLY)
.volunteerId(apply.getVolunteerId())
.volunteerApplyId(apply.getId())
.centerId(board.getCenterId())
.recruitBoardId(board.getId())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public VolunteerApplyStatusChangeEvent(
this.newStatus = newStatus;
}

public static VolunteerApplyStatusChangeEvent from(VolunteerApply apply, RecruitBoard recruitBoard, ApplyStatus oldStatus) {
public static VolunteerApplyStatusChangeEvent of(VolunteerApply apply, RecruitBoard recruitBoard, ApplyStatus oldStatus) {

return VolunteerApplyStatusChangeEvent.builder()
.type(ServerEventType.NOTIFICATION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.somemore.domains.notification.domain.NotificationSubType;
import com.somemore.domains.recruitboard.domain.RecruitBoard;
import com.somemore.domains.volunteerapply.domain.VolunteerApply;
import com.somemore.global.common.event.ServerEvent;
import com.somemore.global.common.event.ServerEventType;
import lombok.Getter;
Expand Down Expand Up @@ -32,4 +34,15 @@ public VolunteerReviewRequestEvent(
this.centerId = centerId;
this.recruitBoardId = recruitBoardId;
}

public static VolunteerReviewRequestEvent of(VolunteerApply apply, RecruitBoard recruitBoard) {
return VolunteerReviewRequestEvent.builder()
.type(ServerEventType.NOTIFICATION)
.subType(NotificationSubType.VOLUNTEER_REVIEW_REQUEST)
.volunteerId(apply.getVolunteerId())
.volunteerApplyId(apply.getId())
.centerId(recruitBoard.getCenterId())
.recruitBoardId(recruitBoard.getId())
.build();
}
}
Loading