Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 공지 알람 발송 및 조회 API 구현 #324

Merged
merged 4 commits into from
Nov 11, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.listywave.list.application.domain.comment.Comment;
import com.listywave.list.application.domain.list.ListEntity;
import com.listywave.list.application.domain.reply.Reply;
import com.listywave.notice.application.domain.Notice;
import com.listywave.user.application.domain.User;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -44,7 +45,7 @@ public class Alarm {
@JoinColumn(name = "send_user_id", nullable = false)
private User sendUser;

@Column(nullable = false)
@Column(nullable = true)
private Long receiveUserId;

@ManyToOne(fetch = LAZY)
Expand All @@ -59,6 +60,10 @@ public class Alarm {
@JoinColumn(name = "reply_id", nullable = true, foreignKey = @ForeignKey(name = "alarm_reply_fk"))
private Reply reply;

@ManyToOne(fetch = LAZY)
@JoinColumn(name = "notice_id", nullable = true, foreignKey = @ForeignKey(name = "alarm_notice_fk"))
private Notice notice;

@Column(nullable = false)
@Enumerated(value = STRING)
private AlarmType type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import static com.listywave.alarm.application.domain.AlarmType.COLLECT;
import static com.listywave.alarm.application.domain.AlarmType.COMMENT;
import static com.listywave.alarm.application.domain.AlarmType.FOLLOW;
import static com.listywave.alarm.application.domain.AlarmType.NOTICE;
import static com.listywave.alarm.application.domain.AlarmType.REPLY;

import com.listywave.list.application.domain.comment.Comment;
import com.listywave.list.application.domain.list.ListEntity;
import com.listywave.list.application.domain.reply.Reply;
import com.listywave.mention.Mention;
import com.listywave.notice.application.domain.Notice;
import com.listywave.user.application.domain.User;
import java.util.List;
import lombok.Builder;
Expand All @@ -21,6 +23,7 @@ public record AlarmCreateEvent(
Comment comment,
Reply reply,
List<Mention> mentions,
Notice notice,
AlarmType alarmType
) {

Expand All @@ -32,6 +35,7 @@ public Alarm toEntity() {
.comment(comment)
.reply(reply)
.type(alarmType)
.notice(notice)
.isChecked(false)
.build();
}
Expand Down Expand Up @@ -76,6 +80,14 @@ public static AlarmCreateEvent collect(User publisher, ListEntity list) {
.build();
}

public static AlarmCreateEvent notice(User user, Notice notice) {
return AlarmCreateEvent.builder()
.publisher(user)
.notice(notice)
.alarmType(NOTICE)
.build();
}

public boolean isToMyself() {
return this.publisher.isSame(listenerId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public enum AlarmType {
REPLY,
MENTION,
COLLABORATOR,
NOTICE,
;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.listywave.list.application.domain.list.ListEntity;
import com.listywave.list.application.domain.reply.Reply;
import com.listywave.mention.Mention;
import com.listywave.notice.application.domain.Notice;
import com.listywave.user.application.domain.User;
import jakarta.annotation.Nullable;
import java.time.LocalDateTime;
import java.util.List;
import lombok.Builder;
Expand All @@ -18,9 +20,10 @@ public record AlarmFindResponse(
@JsonProperty("checked") boolean isChecked,
String type,
UserDto sendUser,
ListDto list,
CommentDto comment,
ReplyDto reply
@Nullable ListDto list,
@Nullable CommentDto comment,
@Nullable ReplyDto reply,
@Nullable NoticeDto notice
) {

public static List<AlarmFindResponse> toList(List<Alarm> alarms) {
Expand All @@ -34,6 +37,7 @@ public static List<AlarmFindResponse> toList(List<Alarm> alarms) {
.list(ListDto.of(alarm.getList()))
.comment(CommentDto.of(alarm.getComment()))
.reply(ReplyDto.of(alarm.getReply()))
.notice(NoticeDto.of(alarm.getNotice()))
.build()
).toList();
}
Expand All @@ -54,7 +58,7 @@ public record ListDto(
String title
) {

public static ListDto of(ListEntity list) {
public static ListDto of(@Nullable ListEntity list) {
if (list == null) {
return null;
}
Expand All @@ -68,7 +72,7 @@ public record CommentDto(
List<MentionDto> mentions
) {

public static CommentDto of(Comment comment) {
public static CommentDto of(@Nullable Comment comment) {
if (comment == null) {
return null;
}
Expand All @@ -82,7 +86,7 @@ public record ReplyDto(
List<MentionDto> mentions
) {

public static ReplyDto of(Reply reply) {
public static ReplyDto of(@Nullable Reply reply) {
if (reply == null) {
return null;
}
Expand All @@ -105,5 +109,28 @@ public static List<MentionDto> toList(List<Mention> mentions) {
}
}

// TODO: notice
@Builder
public record NoticeDto(
Long id,
String categoryCode,
String categoryViewName,
String title,
String description,
LocalDateTime createdDate
) {

public static NoticeDto of(@Nullable Notice notice) {
if (notice == null) {
return null;
}
return NoticeDto.builder()
.id(notice.getId())
.categoryCode(String.valueOf(notice.getType().getCode()))
.categoryViewName(notice.getType().getViewName())
.title(notice.getTitle().getValue())
.description(notice.getDescription().getValue())
.createdDate(notice.getCreatedDate())
.build();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static com.listywave.alarm.application.domain.AlarmType.COMMENT;
import static com.listywave.alarm.application.domain.AlarmType.MENTION;
import static com.listywave.alarm.application.domain.AlarmType.NOTICE;
import static com.listywave.alarm.application.domain.AlarmType.REPLY;
import static org.springframework.transaction.annotation.Propagation.REQUIRED;
import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW;
Expand Down Expand Up @@ -126,7 +127,7 @@ public void save(AlarmCreateEvent event) {
@Transactional(readOnly = true)
public List<AlarmFindResponse> findAllBy(Long userId) {
userRepository.getById(userId);
List<Alarm> alarms = alarmRepository.findAllBy(userId, LocalDateTime.now().minusDays(30));
List<Alarm> alarms = alarmRepository.findAllBy(userId, LocalDateTime.now().minusDays(30), NOTICE);
return AlarmFindResponse.toList(alarms);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.listywave.alarm.repository;

import com.listywave.alarm.application.domain.Alarm;
import com.listywave.alarm.application.domain.AlarmType;
import com.listywave.alarm.repository.custom.CustomAlarmRepository;
import com.listywave.common.exception.CustomException;
import com.listywave.common.exception.ErrorCode;
Expand Down Expand Up @@ -49,14 +50,13 @@ select case when count(*) > 0 then false else true end
left join ListEntity l on a.list = l
left join Comment c on a.comment = c
left join Reply r on a.reply = r
where a.receiveUserId = :receiveUserId and a.createdDate >= :thirtyDaysAgo
left join Notice n on a.notice = n
where a.createdDate >= :thirtyDaysAgo and (a.receiveUserId = :receiveUserId or a.type = :type)
Copy link
Collaborator

Choose a reason for hiding this comment

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

알람 받는 대상은 지정이 안돼어 있으니 or 로 NOTICE를 고정으로 뺀거네요 그럼 여기서 궁금한점은 기존 알람들은 개개인들이 설정되어 있어서 읽음처리가 되는데 그럼 공지에 대한 알림의 읽음 처리는 어떤식으로 개개인별로 처리하나요??

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

공지 알람의 경우 현지님과 논의가 필요한 상황이긴 합니다.
현지님 바쁨 이슈로 아직 논의를 하지 못해서 일단 구현 마감 기간 맞추려고 혼자 생각해 봤을 때 최적의 방식을 적용해 놓은 상태입니다.

공지 알람에 대한 동작은, 알람은 단 1개만 만들고 받는 유저는 없고 타입으로 구분합니다. 이에 따라 읽음 처리는 별도로 하지 않는 것으로 할 생각입니다.

처음엔 모든 유저에게 알람을 생성하는 방식을 생각해 봤지만, N명의 유저에게 모두 알람을 생성하는 것은 큰 부하가 될 것 같아서 위와 같은 방안을 적용해 봤습니다.

일단 머지 안하고 있을테니, 혹시 더 나은 방안이 있을까요?

Copy link
Collaborator

Choose a reason for hiding this comment

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

흠 일단 공지 알람을 개개인별로 읽는 거에 대해서는 N명의 유저에게 모두 알람을 생성하는것은 말그대로 큰 부하가 올 거 같아서 저도 일단 생각이 나지 않네요 공지는 그냥 30일 지나면 다른 알림과 똑같이 삭제 처리가 되도록 하는 방법으로 납두고 괜찮은 방법이 있는지 아니면 이게 가장 괜찮은 방법인지는 다같이 논의 해봐야할 거 같아요! merge 일단 하셔도 될 거 같아요!

order by a.createdDate desc
""")
List<Alarm> findAllBy(Long receiveUserId, LocalDateTime thirtyDaysAgo);
List<Alarm> findAllBy(Long receiveUserId, LocalDateTime thirtyDaysAgo, AlarmType type);

List<Alarm> findAllByReply(Reply reply);

void deleteByComment(Comment comment);

void deleteAllByCommentAndReceiveUserId(Comment comment, Long receiveUserId);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.listywave.collection.application.domain;

import static com.listywave.common.exception.ErrorCode.INVALID_ACCESS;
import static lombok.AccessLevel.PROTECTED;

import com.listywave.common.BaseEntity;
import com.listywave.common.exception.CustomException;
import jakarta.persistence.Column;
Expand All @@ -10,9 +13,6 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import static com.listywave.common.exception.ErrorCode.INVALID_ACCESS;
import static lombok.AccessLevel.PROTECTED;

@Entity
@Getter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.listywave.collection.application.domain;

import static com.listywave.common.exception.ErrorCode.LENGTH_EXCEEDED_EXCEPTION;

import com.listywave.common.exception.CustomException;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
Expand All @@ -8,8 +10,6 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import static com.listywave.common.exception.ErrorCode.LENGTH_EXCEEDED_EXCEPTION;

@Getter
@Embeddable
@EqualsAndHashCode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public record FindFolderResponse(
List<FolderResponse> folders
) {

public static FindFolderResponse of(List<FolderResponse> list){
public static FindFolderResponse of(List<FolderResponse> list) {
return new FindFolderResponse(list);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.listywave.collection.presentation.dto;

public record FolderCreateRequest(
String folderName
String folderName
) {
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.listywave.collection.presentation.dto;

public record FolderUpdateRequest(
String folderName
String folderName
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import com.listywave.collection.application.domain.Folder;
import com.listywave.collection.repository.custom.CustomCollectionRepository;
import com.listywave.list.application.domain.list.ListEntity;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;

public interface CollectionRepository extends JpaRepository<Collect, Long>, CustomCollectionRepository {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.listywave.collection.repository.custom;

import com.listywave.collection.application.dto.FolderResponse;

import java.util.List;

public interface CustomFolderRepository {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.listywave.collection.repository.custom.impl;

import static com.listywave.collection.application.domain.QCollect.*;
import static com.listywave.collection.application.domain.QFolder.*;
import static com.querydsl.jpa.JPAExpressions.*;
import static com.listywave.collection.application.domain.QCollect.collect;
import static com.listywave.collection.application.domain.QFolder.folder;
import static com.querydsl.jpa.JPAExpressions.select;

import com.listywave.collection.application.dto.FolderResponse;
import com.listywave.collection.repository.custom.CustomFolderRepository;
Expand All @@ -11,8 +11,8 @@
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.NumberPath;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import java.util.List;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class CustomFolderRepositoryImpl implements CustomFolderRepository {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.listywave.common.exception;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class CustomException extends RuntimeException {

private final ErrorCode errorCode;

public CustomException(ErrorCode errorCode) {
super(errorCode.getDetail());
this.errorCode = errorCode;
}

public CustomException(ErrorCode errorCode, String message) {
super(message);
this.errorCode = errorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public enum ErrorCode {
DUPLICATE_FOLDER_NAME_EXCEPTION(BAD_REQUEST, "중복된 폴더명입니다."),
NULL_OR_BLANK_EXCEPTION(BAD_REQUEST, "값이 null이거나 공백일 수 없습니다."),
NOT_EXIST_CODE(BAD_REQUEST, "존재하지 않는 코드입니다."),
ALREADY_SENT_ALARM_NOTICE(BAD_REQUEST, "이미 발송된 공지입니다."),

// S3
S3_DELETE_OBJECTS_EXCEPTION(INTERNAL_SERVER_ERROR, "S3의 이미지를 삭제 요청하는 과정에서 에러가 발생했습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import com.listywave.list.application.domain.item.Item;
import com.listywave.list.application.domain.list.ListEntity;
import lombok.Builder;

import java.util.List;
import lombok.Builder;

@Builder
public record RecommendedListResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.listywave.notice.application.domain;

import static com.listywave.common.exception.ErrorCode.ALREADY_SENT_ALARM_NOTICE;
import static jakarta.persistence.CascadeType.ALL;
import static jakarta.persistence.FetchType.LAZY;
import static lombok.AccessLevel.PROTECTED;

import com.listywave.common.BaseEntity;
import com.listywave.common.exception.CustomException;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -67,4 +69,11 @@ public void update(NoticeType type, NoticeTitle title, NoticeDescription descrip
public void changeExposure() {
this.isExposed = !this.isExposed;
}

public void sendAlarm() {
if (this.didSendAlarm) {
throw new CustomException(ALREADY_SENT_ALARM_NOTICE);
}
this.didSendAlarm = true;
}
}
Loading
Loading