Skip to content

Commit

Permalink
feat: 공지 알람 발송 및 조회 API 구현 (#316)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdkdhoho committed Nov 9, 2024
1 parent ffa7326 commit 35771cc
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 29 deletions.
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)
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
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
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.listywave.notice.application.service;

import com.listywave.alarm.application.domain.AlarmCreateEvent;
import com.listywave.notice.application.domain.Notice;
import com.listywave.notice.application.domain.NoticeContent;
import com.listywave.notice.application.domain.NoticeDescription;
Expand All @@ -11,8 +12,11 @@
import com.listywave.notice.application.service.dto.NoticeFindResponse;
import com.listywave.notice.application.service.dto.NoticeUpdateRequest;
import com.listywave.notice.repository.NoticeRepository;
import com.listywave.user.application.domain.User;
import com.listywave.user.repository.user.UserRepository;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -21,7 +25,11 @@
@RequiredArgsConstructor
public class NoticeService {

private static final String OFFICIAL_USER_NICKNAME = "ListyWave";

private final UserRepository userRepository;
private final NoticeRepository noticeRepository;
private final ApplicationEventPublisher applicationEventPublisher;

public Long create(NoticeCreateRequest request) {
Notice notice = request.toNotice();
Expand Down Expand Up @@ -70,4 +78,13 @@ public void updateExposure(Long id) {
public void delete(Long id) {
noticeRepository.deleteById(id);
}

public void sendAlarm(Long id) {
User officialUser = userRepository.findByNicknameValue(OFFICIAL_USER_NICKNAME);
Notice notice = noticeRepository.getById(id);
notice.sendAlarm();

AlarmCreateEvent event = AlarmCreateEvent.notice(officialUser, notice);
applicationEventPublisher.publishEvent(event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,10 @@ ResponseEntity<Void> delete(@PathVariable Long noticeId) {
noticeService.delete(noticeId);
return ResponseEntity.noContent().build();
}

@PostMapping("/admin/notices/{noticeId}/alarm")
ResponseEntity<Void> sendAlarm(@PathVariable Long noticeId) {
noticeService.sendAlarm(noticeId);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import com.listywave.user.application.domain.User;
import com.listywave.user.application.dto.FollowersResponse;
import com.listywave.user.application.dto.FollowingsResponse;
import com.listywave.user.application.dto.UsersRecommendedResponse;
import com.listywave.user.application.dto.UserInfoResponse;
import com.listywave.user.application.dto.UserProflieUpdateCommand;
import com.listywave.user.application.dto.UsersRecommendedResponse;
import com.listywave.user.application.dto.search.UserElasticSearchResponse;
import com.listywave.user.application.dto.search.UserSearchResponse;
import com.listywave.user.application.dto.search.UserSearchResult;
Expand Down Expand Up @@ -122,7 +122,7 @@ public FollowersResponse getFollowers(Long userId, Pageable pageable, String sea

@Transactional(readOnly = true)
public List<UsersRecommendedResponse> getRecommendedUsers(Long loginUserId) {
if(loginUserId == null){
if (loginUserId == null) {
List<User> recommendUsers = userRepository.getRecommendUsers(List.of(), null);
return toUsersRecommendedResponse(recommendUsers);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ default User getById(Long id) {
where u.isDelete = false
""")
List<User> findAll();

User findByNicknameValue(String nickname);
}
Loading

0 comments on commit 35771cc

Please sign in to comment.